|
2 | 2 | * @file app_set_wifi.cpp |
3 | 3 | * @author Forairaaaaa |
4 | 4 | * @brief |
5 | | - * @version 0.1 |
| 5 | + * @version 0.6 |
6 | 6 | * @date 2023-09-20 |
7 | 7 | * |
8 | 8 | * @copyright Copyright (c) 2023 |
9 | 9 | * |
10 | 10 | */ |
11 | 11 | #include "app_set_wifi.h" |
12 | 12 | #include "spdlog/spdlog.h" |
| 13 | +#include "../utils/wifi_connect_wrap/wifi_connect_wrap.h" |
| 14 | +#include "../utils/sntp_wrap/sntp_wrap.h" |
13 | 15 |
|
14 | 16 |
|
15 | 17 | using namespace MOONCAKE::APPS; |
16 | 18 |
|
17 | 19 |
|
| 20 | +#define _keyboard _data.hal->keyboard() |
| 21 | +#define _canvas _data.hal->canvas() |
| 22 | +#define _canvas_update _data.hal->canvas_update |
| 23 | +#define _canvas_clear() _canvas->fillScreen(THEME_COLOR_BG) |
| 24 | + |
| 25 | + |
| 26 | +void AppSetWiFi::_update_input() |
| 27 | +{ |
| 28 | + // spdlog::info("{} {}", _keyboard->keyList().size(), _data.last_key_num); |
| 29 | + |
| 30 | + // If changed |
| 31 | + if (_keyboard->keyList().size() != _data.last_key_num) |
| 32 | + { |
| 33 | + // If key pressed |
| 34 | + if (_keyboard->keyList().size() != 0) |
| 35 | + { |
| 36 | + // Update states and values |
| 37 | + _keyboard->updateKeysState(); |
| 38 | + |
| 39 | + // If enter |
| 40 | + if (_keyboard->keysState().enter) |
| 41 | + { |
| 42 | + // New line |
| 43 | + _canvas->print(" \n"); |
| 44 | + |
| 45 | + _update_state(); |
| 46 | + |
| 47 | + // Reset buffer |
| 48 | + _data.repl_input_buffer = ""; |
| 49 | + } |
| 50 | + |
| 51 | + // If space |
| 52 | + else if (_keyboard->keysState().space) |
| 53 | + { |
| 54 | + _canvas->print(' '); |
| 55 | + _data.repl_input_buffer += ' '; |
| 56 | + } |
| 57 | + |
| 58 | + // If delete |
| 59 | + else if (_keyboard->keysState().del) |
| 60 | + { |
| 61 | + if (_data.repl_input_buffer.size()) |
| 62 | + { |
| 63 | + // Pop input buffer |
| 64 | + _data.repl_input_buffer.pop_back(); |
| 65 | + |
| 66 | + // Pop canvas display |
| 67 | + int cursor_x = _canvas->getCursorX(); |
| 68 | + int cursor_y = _canvas->getCursorY(); |
| 69 | + |
| 70 | + if (cursor_x - FONT_REPL_WIDTH < 0) |
| 71 | + { |
| 72 | + // Last line |
| 73 | + cursor_y -= FONT_REPL_HEIGHT; |
| 74 | + cursor_x = _canvas->width() - FONT_REPL_WIDTH; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + cursor_x -= FONT_REPL_WIDTH; |
| 79 | + } |
| 80 | + |
| 81 | + spdlog::info("new cursor {} {}", cursor_x, cursor_y); |
| 82 | + |
| 83 | + _canvas->setCursor(cursor_x, cursor_y); |
| 84 | + _canvas->print(" "); |
| 85 | + _canvas->setCursor(cursor_x, cursor_y); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Normal chars |
| 90 | + else |
| 91 | + { |
| 92 | + for (auto& i : _keyboard->keysState().values) |
| 93 | + { |
| 94 | + // spdlog::info("{}", i); |
| 95 | + |
| 96 | + _canvas->print(i); |
| 97 | + _data.repl_input_buffer += i; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + _canvas_update(); |
| 102 | + |
| 103 | + // Update last key num |
| 104 | + _data.last_key_num = _keyboard->keyList().size(); |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + // Reset last key num |
| 109 | + _data.last_key_num = 0; |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +void AppSetWiFi::_update_cursor() |
| 116 | +{ |
| 117 | + if ((millis() - _data.cursor_update_time_count) > _data.cursor_update_period) |
| 118 | + { |
| 119 | + // Get cursor |
| 120 | + int cursor_x = _canvas->getCursorX(); |
| 121 | + int cursor_y = _canvas->getCursorY(); |
| 122 | + |
| 123 | + // spdlog::info("cursor {} {}", cursor_x, cursor_y); |
| 124 | + |
| 125 | + _canvas->print(_data.cursor_state ? '_' : ' '); |
| 126 | + _canvas->setCursor(cursor_x, cursor_y); |
| 127 | + _canvas_update(); |
| 128 | + |
| 129 | + _data.cursor_state = !_data.cursor_state; |
| 130 | + _data.cursor_update_time_count = millis(); |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | + |
| 135 | +void AppSetWiFi::_update_state() |
| 136 | +{ |
| 137 | + if (_data.current_state == state_init) |
| 138 | + { |
| 139 | + _canvas->setTextColor(TFT_ORANGE, THEME_COLOR_BG); |
| 140 | + _canvas->printf("WiFi SSID:\n"); |
| 141 | + _canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG); |
| 142 | + _canvas_update(); |
| 143 | + |
| 144 | + _data.current_state = state_wait_ssid; |
| 145 | + } |
| 146 | + |
| 147 | + else if (_data.current_state == state_wait_ssid) |
| 148 | + { |
| 149 | + _canvas->setTextColor(TFT_ORANGE, THEME_COLOR_BG); |
| 150 | + _canvas->printf("WiFi Password:\n"); |
| 151 | + _canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG); |
| 152 | + _canvas_update(); |
| 153 | + |
| 154 | + _data.wifi_ssid = _data.repl_input_buffer; |
| 155 | + _data.current_state = state_wait_password; |
| 156 | + spdlog::info("wifi ssid set: {}", _data.wifi_ssid); |
| 157 | + } |
| 158 | + |
| 159 | + else if (_data.current_state == state_wait_password) |
| 160 | + { |
| 161 | + _data.wifi_password = _data.repl_input_buffer; |
| 162 | + _data.current_state = state_connect; |
| 163 | + spdlog::info("wifi password set: {}", _data.wifi_password); |
| 164 | + } |
| 165 | + |
| 166 | + if (_data.current_state == state_connect) |
| 167 | + { |
| 168 | + _canvas->setTextColor(TFT_ORANGE, THEME_COLOR_BG); |
| 169 | + _canvas->printf("WiFi config:\n- %s\n- %s\nConnecting...\n", _data.wifi_ssid.c_str(), _data.wifi_password.c_str()); |
| 170 | + _canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG); |
| 171 | + _canvas_update(); |
| 172 | + |
| 173 | + |
| 174 | + wifi_connect_wrap_config(_data.wifi_ssid.c_str(), _data.wifi_password.c_str()); |
| 175 | + wifi_connect_wrap_connect(); |
| 176 | + |
| 177 | + if (wifi_connect_wrap_is_wifi_connect_success() != 0) |
| 178 | + { |
| 179 | + _canvas->setTextColor(TFT_GREENYELLOW, THEME_COLOR_BG); |
| 180 | + _canvas->printf("Connected\nSNTP init..."); |
| 181 | + _canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG); |
| 182 | + _canvas_update(); |
| 183 | + |
| 184 | + sntp_warp_init(); |
| 185 | + _data.hal->setSntpAdjusted(true); |
| 186 | + |
| 187 | + _canvas->setTextColor(TFT_GREENYELLOW, THEME_COLOR_BG); |
| 188 | + _canvas->printf("Done"); |
| 189 | + _canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG); |
| 190 | + _canvas_update(); |
| 191 | + } |
| 192 | + else |
| 193 | + { |
| 194 | + _canvas->setTextColor(TFT_RED, THEME_COLOR_BG); |
| 195 | + _canvas->printf("Failed"); |
| 196 | + _canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG); |
| 197 | + _canvas_update(); |
| 198 | + } |
| 199 | + |
| 200 | + wifi_connect_wrap_disconnect(); |
| 201 | + |
| 202 | + _data.current_state = state_wait_quit; |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | + |
18 | 207 | void AppSetWiFi::onCreate() |
19 | 208 | { |
20 | 209 | spdlog::info("{} onCreate", getAppName()); |
21 | 210 |
|
22 | 211 | // Get hal |
23 | | - _data.hal = mcAppGetDatabase()->Get("HAL")->value<HAL::Hal *>(); |
| 212 | + _data.hal = mcAppGetDatabase()->Get("HAL")->value<HAL::Hal*>(); |
| 213 | +} |
| 214 | + |
| 215 | + |
| 216 | +void AppSetWiFi::onResume() |
| 217 | +{ |
| 218 | + ANIM_APP_OPEN(); |
| 219 | + |
| 220 | + _canvas_clear(); |
| 221 | + _canvas->setTextScroll(true); |
| 222 | + _canvas->setBaseColor(THEME_COLOR_BG); |
| 223 | + _canvas->setTextColor(THEME_COLOR_REPL_TEXT, THEME_COLOR_BG); |
| 224 | + _canvas->setFont(FONT_REPL); |
| 225 | + _canvas->setTextSize(FONT_SIZE_REPL); |
| 226 | + _canvas->setCursor(0, 0); |
| 227 | + |
| 228 | + _data.current_state = state_init; |
| 229 | + _update_state(); |
24 | 230 | } |
25 | 231 |
|
26 | 232 |
|
27 | 233 | void AppSetWiFi::onRunning() |
28 | 234 | { |
29 | | - destroyApp(); |
| 235 | + if (_data.current_state != state_wait_quit) |
| 236 | + _update_input(); |
| 237 | + _update_cursor(); |
| 238 | + |
| 239 | + if (_data.hal->homeButton()->pressed()) |
| 240 | + { |
| 241 | + _data.hal->playNextSound(); |
| 242 | + spdlog::info("quit set wifi"); |
| 243 | + destroyApp(); |
| 244 | + } |
| 245 | +} |
| 246 | + |
| 247 | + |
| 248 | +void AppSetWiFi::onDestroy() |
| 249 | +{ |
| 250 | + _canvas->setTextScroll(false); |
30 | 251 | } |
0 commit comments