From e82cedaf239e4599f819bbaf8f0f5f36107b7c9e Mon Sep 17 00:00:00 2001 From: Jakub <106778416+MarekZegare4@users.noreply.github.com> Date: Mon, 28 Sep 2026 16:24:10 +0200 Subject: [PATCH] feat(ui-lvgl): several saved WiFi networks, joined by signal; Save checks the password - Settings > WiFi keeps up to 8 networks (NVS "s0".."s7" / "p0".."p7"; the single one older builds saved moves over). A tap loads one to change its password, Forget asks with a second tap, a scan marks the known ones. - Map downloads, live tiles and updates join the strongest saved network in range (a short scan first when more than one is saved), else the newest one -- which also covers a hidden network. - Save joins the network once: "password OK", turned away (forgotten again, the form keeps it to fix) or not in range (kept, checked later). Co-Authored-By: Claude Opus 5.5 --- examples/companion_radio/ui-lvgl/LvglPort.h | 141 +++++++++++++-- examples/companion_radio/ui-lvgl/UITask.cpp | 5 +- examples/companion_radio/ui-lvgl/UITask.h | 3 + examples/companion_radio/ui-lvgl/WifiScreen.h | 167 +++++++++++++++--- 4 files changed, 277 insertions(+), 39 deletions(-) diff --git a/examples/companion_radio/ui-lvgl/LvglPort.h b/examples/companion_radio/ui-lvgl/LvglPort.h index 3cac2667..1d35867e 100644 --- a/examples/companion_radio/ui-lvgl/LvglPort.h +++ b/examples/companion_radio/ui-lvgl/LvglPort.h @@ -37,6 +37,50 @@ static void shotCopy(const lv_area_t* a, const uint8_t* px) { for (int32_t y = a->y1; y <= a->y2; y++, src += w) memcpy(s_shot + y * W + a->x1, src, w * 2); } +// Saved WiFi networks (Settings > WiFi), most recently saved first. Kept in RAM +// once read; wifiStoreLoad() / wifiStoreWrite() (per board, below) hold them. +static const int WIFI_SAVED_MAX = 8; +struct WifiNet { char ssid[33]; char pass[65]; }; +static WifiNet s_nets[WIFI_SAVED_MAX]; +static int s_net_count = -1; // -1: not read yet +static void wifiStoreLoad(); +static void wifiStoreWrite(); +static int wifiSavedCount() { + if (s_net_count < 0) wifiStoreLoad(); + return s_net_count; +} +static const WifiNet* wifiSaved(int i) { return i >= 0 && i < wifiSavedCount() ? &s_nets[i] : nullptr; } +static int wifiFind(const char* ssid) { + for (int i = 0; i < wifiSavedCount(); i++) if (!strcmp(s_nets[i].ssid, ssid)) return i; + return -1; +} +// The most recently saved network (false: none) -- what "have WiFi?" asks. +static bool loadWifi(char* ssid, size_t ssid_n, char* pass, size_t pass_n) { + const WifiNet* w = wifiSaved(0); + snprintf(ssid, ssid_n, "%s", w ? w->ssid : ""); + snprintf(pass, pass_n, "%s", w ? w->pass : ""); + return w != nullptr; +} +// Adds a network or changes its password; either way it moves to the top. +// A ninth one pushes out the oldest. +static void saveWifi(const char* ssid, const char* pass) { + int n = wifiSavedCount(); + int at = wifiFind(ssid); + if (at < 0) at = n < WIFI_SAVED_MAX ? n++ : WIFI_SAVED_MAX - 1; + memmove(&s_nets[1], &s_nets[0], at * sizeof(WifiNet)); + snprintf(s_nets[0].ssid, sizeof(s_nets[0].ssid), "%s", ssid); + snprintf(s_nets[0].pass, sizeof(s_nets[0].pass), "%s", pass); + s_net_count = n; + wifiStoreWrite(); +} +static void forgetWifi(int i) { + int n = wifiSavedCount(); + if (i < 0 || i >= n) return; + memmove(&s_nets[i], &s_nets[i + 1], (n - 1 - i) * sizeof(WifiNet)); + s_net_count = n - 1; + wifiStoreWrite(); +} + // Network for map downloads: state of the link, and one HTTP GET at a time. enum NetState { NET_OFF, NET_CONNECTING, NET_UP, NET_FAILED }; static const int WIFI_SCAN_MAX = 20; @@ -174,17 +218,44 @@ static void getStr(const char* ns, const char* key, char* out, size_t n_out) { if (n.ok) n.p.getString(key, out, n_out); } static void putStr(const char* ns, const char* key, const char* v) { Ns n(ns, false); if (n.ok) n.p.putString(key, v); } +static void remove(const char* ns, const char* key) { Ns n(ns, false); if (n.ok && n.p.isKey(key)) n.p.remove(key); } } // namespace nvs // ── WiFi (station, only while a map download runs) ─────────────────────────── -static bool loadWifi(char* ssid, size_t ssid_n, char* pass, size_t pass_n) { - nvs::getStr("mc_wifi", "ssid", ssid, ssid_n); - nvs::getStr("mc_wifi", "pass", pass, pass_n); - return ssid[0] != '\0'; +// The saved networks as "s0".."s7" / "p0".."p7"; the single network older +// builds kept ("ssid" / "pass") becomes the first. +static void wifiStoreLoad() { + char ks[3] = "s0", kp[3] = "p0"; + s_net_count = 0; + for (int i = 0; i < WIFI_SAVED_MAX; i++) { + ks[1] = kp[1] = (char)('0' + i); + nvs::getStr("mc_wifi", ks, s_nets[i].ssid, sizeof(s_nets[i].ssid)); + if (!s_nets[i].ssid[0]) break; + nvs::getStr("mc_wifi", kp, s_nets[i].pass, sizeof(s_nets[i].pass)); + s_net_count++; + } + char old[33]; + nvs::getStr("mc_wifi", "ssid", old, sizeof(old)); + if (old[0]) { + char pass[65]; + nvs::getStr("mc_wifi", "pass", pass, sizeof(pass)); + nvs::remove("mc_wifi", "ssid"); + nvs::remove("mc_wifi", "pass"); + if (wifiFind(old) < 0) saveWifi(old, pass); + } } -static void saveWifi(const char* ssid, const char* pass) { - nvs::putStr("mc_wifi", "ssid", ssid); - nvs::putStr("mc_wifi", "pass", pass); +static void wifiStoreWrite() { + char ks[3] = "s0", kp[3] = "p0"; + for (int i = 0; i < WIFI_SAVED_MAX; i++) { + ks[1] = kp[1] = (char)('0' + i); + if (i < s_net_count) { + nvs::putStr("mc_wifi", ks, s_nets[i].ssid); + nvs::putStr("mc_wifi", kp, s_nets[i].pass); + } else { + nvs::remove("mc_wifi", ks); + nvs::remove("mc_wifi", kp); + } + } } // Settings > WiFi's switch: off keeps the radio off for everything (scan, map // download). Kept with the credentials. @@ -285,11 +356,45 @@ static bool flashInfo(uint64_t& total, uint64_t& used) { return total > 0; } +// Joins the strongest saved network in range: with more than one saved, a +// scan first (a few seconds, inside the callers' connect timeouts). The given +// network is joined when none of them shows up -- a hidden one, say -- and +// straight away when it's the only one. +static bool s_net_scanning = false; +static char s_fb_ssid[33], s_fb_pass[65]; static void netBegin(const char* ssid, const char* pass) { + WiFi.mode(WIFI_STA); + snprintf(s_fb_ssid, sizeof(s_fb_ssid), "%s", ssid); + snprintf(s_fb_pass, sizeof(s_fb_pass), "%s", pass); + if (wifiSavedCount() <= 1) { WiFi.begin(ssid, pass); return; } + s_net_scanning = WiFi.scanNetworks(true /* async */) == WIFI_SCAN_RUNNING; + if (!s_net_scanning) WiFi.begin(ssid, pass); +} +// Just this network, no scan (Settings > WiFi checking a password it saved). +static void netJoin(const char* ssid, const char* pass) { + s_net_scanning = false; WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); } +// After NET_FAILED / a timeout: true when the network wasn't there at all +// (else it answered and turned us away: the password, most likely). +static bool netNotFound() { return WiFi.status() == WL_NO_SSID_AVAIL; } static int netState() { + if (s_net_scanning) { + int n = WiFi.scanComplete(); + if (n == WIFI_SCAN_RUNNING) return NET_CONNECTING; + s_net_scanning = false; + const WifiNet* best = nullptr; + int best_rssi = -1000; + for (int i = 0; i < n; i++) { + int k = wifiFind(WiFi.SSID(i).c_str()); + if (k >= 0 && WiFi.RSSI(i) > best_rssi) { best = &s_nets[k]; best_rssi = WiFi.RSSI(i); } + } + WiFi.scanDelete(); + if (best) WiFi.begin(best->ssid, best->pass); + else WiFi.begin(s_fb_ssid, s_fb_pass); + return NET_CONNECTING; + } switch (WiFi.status()) { case WL_CONNECTED: return NET_UP; case WL_CONNECT_FAILED: @@ -300,9 +405,14 @@ static int netState() { } } static void netEnd() { + if (s_net_scanning) { s_net_scanning = false; WiFi.scanDelete(); } WiFi.disconnect(true); WiFi.mode(WIFI_OFF); } +// The network joined (Settings > WiFi marks it), "" when not connected. +static void netSsid(char* out, size_t n) { + snprintf(out, n, "%s", WiFi.getMode() != WIFI_OFF && WiFi.status() == WL_CONNECTED ? WiFi.SSID().c_str() : ""); +} // For the status bar: NET_UP once connected, else NET_OFF (radio off, a scan, // still connecting). static int netRadio() { return WiFi.getMode() != WIFI_OFF && WiFi.status() == WL_CONNECTED ? NET_UP : NET_OFF; } @@ -497,17 +607,9 @@ static void setBacklightPct(uint8_t pct) { (void)pct; } // the browser canvas // The host page preloads map tiles into the in-memory FS under /sdcard/maps. static bool mountStorage() { return true; } -// The browser is always online; WiFi credentials only live for the session. -static char s_ssid[33] = "", s_pass[65] = ""; -static bool loadWifi(char* ssid, size_t ssid_n, char* pass, size_t pass_n) { - snprintf(ssid, ssid_n, "%s", s_ssid); - snprintf(pass, pass_n, "%s", s_pass); - return ssid[0] != '\0'; -} -static void saveWifi(const char* ssid, const char* pass) { - snprintf(s_ssid, sizeof(s_ssid), "%s", ssid); - snprintf(s_pass, sizeof(s_pass), "%s", pass); -} +// The browser is always online; saved WiFi networks only live for the session. +static void wifiStoreLoad() { s_net_count = 0; } +static void wifiStoreWrite() {} static char s_pin[9] = ""; // the screen PIN, for the session static void loadPin(char* out, size_t n) { snprintf(out, n, "%s", s_pin); } static void savePin(const char* pin) { snprintf(s_pin, sizeof(s_pin), "%s", pin); } @@ -547,9 +649,12 @@ static bool vectorOn() { return s_vector; } static void setVectorOn(bool on) { s_vector = on; } static bool s_net_on = false; static void netBegin(const char*, const char*) { s_net_on = true; } +static void netJoin(const char*, const char*) { s_net_on = true; } +static bool netNotFound() { return false; } static int netState() { return NET_UP; } static void netEnd() { s_net_on = false; } static int netRadio() { return s_net_on ? NET_UP : NET_OFF; } +static void netSsid(char* out, size_t n) { const WifiNet* w = wifiSaved(0); snprintf(out, n, "%s", s_net_on && w ? w->ssid : ""); } static uint32_t s_scan_at = 0; static void scanStart() { s_scan_at = millis(); } static int scanResults(char names[][33], int max) { // a pretend scan, for the UI diff --git a/examples/companion_radio/ui-lvgl/UITask.cpp b/examples/companion_radio/ui-lvgl/UITask.cpp index 046d3a05..36b46a85 100644 --- a/examples/companion_radio/ui-lvgl/UITask.cpp +++ b/examples/companion_radio/ui-lvgl/UITask.cpp @@ -1400,6 +1400,8 @@ static void onBack(lv_event_t* e) { (void)e; s_ui->back(); } // A fresh screen below the status bar, loaded in place of the current one; // returns its content area (flex column). The old screen is deleted // asynchronously -- this usually runs from a click on one of its own widgets. +static uint32_t s_wifi_test_ms = 0; // Settings > WiFi checking a just-saved network (WifiScreen.h) + lv_obj_t* UITask::newScreen(const char* title, bool with_back) { _home_clock = _home_date = _home_unread = nullptr; _thread_list = _compose_ta = _keyboard = nullptr; @@ -1415,8 +1417,9 @@ lv_obj_t* UITask::newScreen(const char* title, bool with_back) { _nav_overlay = _nav_ta = _nav_kb = _nav_del_lbl = _nav_rec = _nav_avg_pill = nullptr; _nav_trail_lbl = _nav_trail_btn = _nav_reset_lbl = _nav_share_lbl = _nav_share_btn = _nav_tb_btn = nullptr; _wifi_ssid = _wifi_pass = _wifi_kb = _wifi_list = _wifi_status = nullptr; - if (_wifi_scanning && !wifiInUse()) lvport::netEnd(); // left mid-scan: the radio goes off + if ((_wifi_scanning || s_wifi_test_ms) && !wifiInUse()) lvport::netEnd(); // left mid-scan / check: the radio goes off _wifi_scanning = false; + s_wifi_test_ms = 0; _ota_status = _ota_bar = _ota_btn = _ota_btn_lbl = nullptr; for (lv_obj_t*& t : _map_tiles) t = nullptr; lv_obj_t* prev = _scr; diff --git a/examples/companion_radio/ui-lvgl/UITask.h b/examples/companion_radio/ui-lvgl/UITask.h index 05c701e8..0f63822e 100644 --- a/examples/companion_radio/ui-lvgl/UITask.h +++ b/examples/companion_radio/ui-lvgl/UITask.h @@ -119,6 +119,9 @@ public: void wifiSetAllowed(bool on); void wifiScan(); void wifiPick(int idx); + void wifiOpenSaved(int idx); // a saved network into the fields, to change its password + void wifiTestShow(const char* msg, const char* ssid, const char* pass); // a password check's outcome + void wifiForget(int idx); // (its button asks first: WifiScreen.h) void wifiSave(); void wifiEdit(lv_obj_t* ta); void wifiKeyboardHide(); diff --git a/examples/companion_radio/ui-lvgl/WifiScreen.h b/examples/companion_radio/ui-lvgl/WifiScreen.h index 72f4bfe6..155294bd 100644 --- a/examples/companion_radio/ui-lvgl/WifiScreen.h +++ b/examples/companion_radio/ui-lvgl/WifiScreen.h @@ -1,9 +1,11 @@ #pragma once -// WiFi settings (Settings > WiFi, or from the map's download popup): the -// network map downloads connect to. Scan lists nearby networks to pick from; -// the password field uses the same keyboard as compose. Saved through -// lvport::saveWifi() (NVS on the board). WiFi itself stays off except while a -// download or scan runs; Settings > CONNECTIVITY's WiFi switch forbids even that. +// WiFi settings (Settings > WiFi, or from the map's download popup): the saved +// networks -- up to lvport::WIFI_SAVED_MAX; map downloads, live tiles and +// updates join the strongest one in range -- and a form to add one or change +// a password. Scan lists nearby networks to pick from; the password field uses +// the same keyboard as compose. Saved through lvport::saveWifi() (NVS on the +// board). WiFi itself stays off except while a download or scan runs; +// Settings > CONNECTIVITY's WiFi switch forbids even that. // // Single-TU fragment: included by ui-lvgl/UITask.cpp after MapScreen.h. @@ -12,6 +14,25 @@ static char s_wifi_names[lvport::WIFI_SCAN_MAX][33]; static void onWifiScan(lv_event_t* e) { (void)e; s_ui->wifiScan(); } static void onWifiSave(lv_event_t* e) { (void)e; s_ui->wifiSave(); } static void onWifiPick(lv_event_t* e) { s_ui->wifiPick((int)(uintptr_t)lv_event_get_user_data(e)); } +static void onWifiSaved(lv_event_t* e) { s_ui->wifiOpenSaved((int)(uintptr_t)lv_event_get_user_data(e)); } +// Forget: the first tap turns the button into a red "Forget?", the second +// forgets (another row's button, or a rebuild, disarms it). +static lv_obj_t* s_wifi_forget_btn = nullptr; +static void wifiForgetLook(lv_obj_t* b, bool armed) { + lv_obj_set_style_bg_color(b, lv_color_hex(armed ? theme::FAIL : theme::SURFACE_2), 0); + lv_label_set_text(lv_obj_get_child(b, 0), armed ? "Forget?" : LV_SYMBOL_TRASH); +} +static void onWifiForget(lv_event_t* e) { + lv_obj_t* b = (lv_obj_t*)lv_event_get_current_target(e); + if (s_wifi_forget_btn != b) { + if (s_wifi_forget_btn) wifiForgetLook(s_wifi_forget_btn, false); + s_wifi_forget_btn = b; + wifiForgetLook(b, true); + return; + } + s_wifi_forget_btn = nullptr; + s_ui->wifiForget((int)(uintptr_t)lv_event_get_user_data(e)); +} static void onWifiAllowed(lv_event_t* e) { s_ui->wifiSetAllowed(lv_obj_has_state((lv_obj_t*)lv_event_get_target(e), LV_STATE_CHECKED)); } @@ -35,10 +56,34 @@ void UITask::showWifi() { void UITask::buildWifi() { lv_obj_t* body = newScreen("WiFi", true); + s_wifi_forget_btn = nullptr; lv_obj_set_style_pad_row(body, 4, 0); - label(body, lvport::wifiAllowed() ? "Used only for map downloads." : "WiFi is off (Settings).", THEME_FONT_SMALL, - theme::TEXT_MUTED); + label(body, lvport::wifiAllowed() ? "For map downloads, live map tiles and updates." : "WiFi is off (Settings).", + THEME_FONT_SMALL, theme::TEXT_MUTED); + // Saved networks: a tap loads one into the form below; Forget takes two taps. + int n = lvport::wifiSavedCount(); + char joined[33]; + lvport::netSsid(joined, sizeof(joined)); + lv_obj_t* card = group(body, "Saved networks"); + for (int i = 0; i < n; i++) { + const lvport::WifiNet* w = lvport::wifiSaved(i); + lv_obj_t* row = groupLine(card, true); + groupText(row, w->ssid, !strcmp(w->ssid, joined) ? "Connected" : w->pass[0] ? nullptr : "Open network"); + lv_obj_add_event_cb(row, onWifiSaved, LV_EVENT_CLICKED, (void*)(uintptr_t)i); + lv_obj_t* fb = lv_button_create(row); + lv_obj_set_size(fb, LV_SIZE_CONTENT, 30); + lv_obj_set_style_pad_hor(fb, 10, 0); + lv_obj_set_style_shadow_width(fb, 0, 0); + lv_obj_set_style_radius(fb, theme::RADIUS, 0); + lv_obj_add_event_cb(fb, onWifiForget, LV_EVENT_CLICKED, (void*)(uintptr_t)i); + lv_obj_center(label(fb, "", THEME_FONT_SMALL, theme::TEXT)); + wifiForgetLook(fb, false); + } + if (!n) groupText(groupLine(card, false), "None yet", "Add one below.", theme::TEXT_MUTED); + if (n > 1) groupNote(body, "The strongest one in range is used."); + + sectionTitle(body, n >= lvport::WIFI_SAVED_MAX ? "Add a network (replaces the oldest)" : "Add a network"); lv_obj_t* row = lv_obj_create(body); styleSurface(row, theme::BG); lv_obj_remove_flag(row, LV_OBJ_FLAG_SCROLLABLE); @@ -77,11 +122,6 @@ void UITask::buildWifi() { stylePrimary(save); _wifi_status = label(body, "", THEME_FONT_SMALL, theme::TEXT_MUTED); - char ssid[33], pass[65]; - lvport::loadWifi(ssid, sizeof(ssid), pass, sizeof(pass)); - lv_textarea_set_text(_wifi_ssid, ssid); - lv_textarea_set_text(_wifi_pass, pass); - // Keyboard over the bottom of the screen; the body shrinks above it while // it is up, so the field being edited stays visible. _wifi_kb = kb::create(screen(), _prefs); @@ -92,6 +132,13 @@ void UITask::buildWifi() { lv_obj_add_flag(_wifi_kb, LV_OBJ_FLAG_HIDDEN); } +// Rebuilt in place (a network saved or forgotten), at the same scroll. +static void wifiRebuild(UITask* ui, lv_obj_t*& body) { + int32_t y = body ? lv_obj_get_scroll_y(body) : 0; + ui->showWifi(); + if (body) { lv_obj_update_layout(body); lv_obj_scroll_to_y(body, y, LV_ANIM_OFF); } +} + void UITask::wifiEdit(lv_obj_t* ta) { if (!_wifi_kb) return; lv_obj_remove_state(_wifi_ssid, LV_STATE_FOCUSED); @@ -117,11 +164,12 @@ void UITask::wifiKeyboardHide() { // Settings > CONNECTIVITY > WiFi, as the Bluetooth row: the switch turns it // on / off, the row opens the network settings. static void wifiRow(lv_obj_t* body) { - char ssid[33], pass[65], sub[48]; - bool have = lvport::loadWifi(ssid, sizeof(ssid), pass, sizeof(pass)); + char sub[48]; + int n = lvport::wifiSavedCount(); bool on = lvport::wifiAllowed(); if (!on) snprintf(sub, sizeof(sub), "Off"); - else if (have) snprintf(sub, sizeof(sub), "%s", ssid); + else if (n == 1) snprintf(sub, sizeof(sub), "%s", lvport::wifiSaved(0)->ssid); + else if (n > 1) snprintf(sub, sizeof(sub), "%d saved networks", n); else snprintf(sub, sizeof(sub), "Tap to pick a network"); lv_obj_t* sw = switchRow(body, LV_SYMBOL_WIFI " WiFi", sub, nullptr); if (on) lv_obj_add_state(sw, LV_STATE_CHECKED); @@ -141,7 +189,7 @@ void UITask::wifiSetAllowed(bool on) { if (mapview::s_dl.active()) mapDownloadStop(); if (_wifi_scanning) { _wifi_scanning = false; lvport::netEnd(); } } - showToast(on ? "WiFi on - used for map downloads" : "WiFi off", 1500); + showToast(on ? "WiFi on" : "WiFi off", 1500); if (_screen == SCR_SETTINGS) { // the row's subtitle lv_obj_t* body = _body; int32_t y = body ? lv_obj_get_scroll_y(body) : 0; @@ -153,12 +201,55 @@ void UITask::wifiSetAllowed(bool on) { void UITask::wifiScan() { if (_wifi_scanning) return; if (!lvport::wifiAllowed()) { lv_label_set_text(_wifi_status, "Turn WiFi on in Settings first."); return; } + if (wifiInUse()) { lv_label_set_text(_wifi_status, "WiFi busy (a download or an update) - try later."); return; } lvport::scanStart(); _wifi_scanning = true; lv_label_set_text(_wifi_status, LV_SYMBOL_REFRESH " Scanning..."); } +// A just-saved network, joined once to check the password: connected, turned +// away (the password, most likely: it's forgotten again, the form keeps it to +// fix) or not found (kept: maybe it's just out of range now). +static char s_wifi_test_ssid[33], s_wifi_test_pass[65]; +static const uint32_t WIFI_TEST_MS = 15000; + +static void wifiTestPoll(UITask* ui, bool in_use) { + if (!s_wifi_test_ms) return; + int ns = lvport::netState(); + bool timed_out = millis() - s_wifi_test_ms > WIFI_TEST_MS; + if (ns != lvport::NET_UP && ns != lvport::NET_FAILED && !timed_out) return; + s_wifi_test_ms = 0; + char msg[80]; + if (ns == lvport::NET_UP) { + snprintf(msg, sizeof(msg), LV_SYMBOL_OK " Connected to %s - password OK.", s_wifi_test_ssid); + } else if (lvport::netNotFound()) { + snprintf(msg, sizeof(msg), "Saved. %s isn't in range now - the password is checked when it is.", s_wifi_test_ssid); + } else { + int k = lvport::wifiFind(s_wifi_test_ssid); + if (k >= 0) lvport::forgetWifi(k); + snprintf(msg, sizeof(msg), LV_SYMBOL_WARNING " %s turned the password down - not saved.", s_wifi_test_ssid); + } + if (!in_use) lvport::netEnd(); + bool failed = ns != lvport::NET_UP && !lvport::netNotFound(); + ui->showWifi(); // the saved list may have changed + ui->wifiTestShow(msg, failed ? s_wifi_test_ssid : nullptr, s_wifi_test_pass); +} + +void UITask::wifiTestShow(const char* msg, const char* ssid, const char* pass) { + if (!_wifi_status) return; + lv_label_set_text(_wifi_status, msg); + lv_label_set_long_mode(_wifi_status, LV_LABEL_LONG_WRAP); + lv_obj_set_width(_wifi_status, LV_PCT(100)); + if (ssid) { // back in the form, to fix + lv_textarea_set_text(_wifi_ssid, ssid); + lv_textarea_set_text(_wifi_pass, pass); + } + lv_obj_update_layout(_body); + lv_obj_scroll_to_view(_wifi_status, LV_ANIM_ON); +} + void UITask::pollWifiScan() { + wifiTestPoll(this, wifiInUse()); if (!_wifi_scanning || !_wifi_list) return; int n = lvport::scanResults(s_wifi_names, lvport::WIFI_SCAN_MAX); if (n < 0) return; @@ -173,7 +264,10 @@ void UITask::pollWifiScan() { lv_obj_set_style_radius(b, LV_RADIUS_CIRCLE, 0); lv_obj_set_style_bg_color(b, lv_color_hex(theme::SURFACE), 0); lv_obj_add_event_cb(b, onWifiPick, LV_EVENT_CLICKED, (void*)(uintptr_t)i); - lv_obj_center(label(b, s_wifi_names[i], THEME_FONT_SMALL, theme::TEXT)); + char text[40]; + bool known = lvport::wifiFind(s_wifi_names[i]) >= 0; + snprintf(text, sizeof(text), known ? LV_SYMBOL_OK " %s" : "%s", s_wifi_names[i]); + lv_obj_center(label(b, text, THEME_FONT_SMALL, known ? theme::ACCENT : theme::TEXT)); } if (n > 0) lv_obj_remove_flag(_wifi_list, LV_OBJ_FLAG_HIDDEN); lv_label_set_text(_wifi_status, n > 0 ? "Tap a network, then enter its password." : "No networks found."); @@ -183,16 +277,49 @@ void UITask::pollWifiScan() { void UITask::wifiPick(int idx) { if (idx < 0 || idx >= lvport::WIFI_SCAN_MAX) return; lv_textarea_set_text(_wifi_ssid, s_wifi_names[idx]); + const lvport::WifiNet* w = lvport::wifiSaved(lvport::wifiFind(s_wifi_names[idx])); + lv_textarea_set_text(_wifi_pass, w ? w->pass : ""); lv_obj_add_flag(_wifi_list, LV_OBJ_FLAG_HIDDEN); wifiEdit(_wifi_pass); } +void UITask::wifiOpenSaved(int idx) { + const lvport::WifiNet* w = lvport::wifiSaved(idx); + if (!w) return; + lv_textarea_set_text(_wifi_ssid, w->ssid); + lv_textarea_set_text(_wifi_pass, w->pass); + lv_label_set_text(_wifi_status, "Change the password, then Save."); + wifiEdit(_wifi_pass); +} + +void UITask::wifiForget(int idx) { + const lvport::WifiNet* w = lvport::wifiSaved(idx); + char msg[48]; + snprintf(msg, sizeof(msg), "Forgot %s", w ? w->ssid : ""); + lvport::forgetWifi(idx); + wifiRebuild(this, _body); + showToast(msg); +} + void UITask::wifiSave() { const char* ssid = lv_textarea_get_text(_wifi_ssid); const char* pass = lv_textarea_get_text(_wifi_pass); if (!ssid[0]) { lv_label_set_text(_wifi_status, "Enter a network name."); return; } - lvport::saveWifi(ssid, pass); + snprintf(s_wifi_test_ssid, sizeof(s_wifi_test_ssid), "%s", ssid); + snprintf(s_wifi_test_pass, sizeof(s_wifi_test_pass), "%s", pass); + lvport::saveWifi(s_wifi_test_ssid, s_wifi_test_pass); wifiKeyboardHide(); - showToast("WiFi saved"); - lv_label_set_text(_wifi_status, "Saved."); + wifiRebuild(this, _body); + // Then joined once, to check the password -- unless WiFi is off or busy. + char msg[80]; + if (!lvport::wifiAllowed() || wifiInUse()) { + snprintf(msg, sizeof(msg), "Saved %s", s_wifi_test_ssid); + showToast(msg); + return; + } + lvport::netEnd(); // a scan might have left the radio on + lvport::netJoin(s_wifi_test_ssid, s_wifi_test_pass); + s_wifi_test_ms = millis() | 1; + snprintf(msg, sizeof(msg), LV_SYMBOL_REFRESH " Connecting to %s...", s_wifi_test_ssid); + wifiTestShow(msg, nullptr, nullptr); }