/* * Settings Screen Module Implementation - see settings_screen.h */ #include "settings_screen.h" #include "ui_kit.h" #include "driveline_screen.h" #include "wifi_manager.h" #include "ota_update.h" #include "link_peer.h" #include "diff_locks.h" #include "uart_manager.h" #include "espnow_link.h" #include "settings_manager.h" #include "ui_state_manager.h" #include "drivers/can_driver.h" #include "display/lvgl_v8_port.h" #include "config/ota_config.h" #include #include namespace SettingsScreen { using namespace UiKit; // ============================================================================ // LAYOUT // ============================================================================ static const lv_coord_t NAV_X = 16, NAV_W = 240, NAV_H = 100; static const lv_coord_t TOP_Y = 88; static const lv_coord_t PAGE_X = 272, PAGE_W = 736, PAGE_H = 600 - TOP_Y - 28; // version label below typedef enum { PAGE_WIFI, PAGE_UPDATES, PAGE_SYSTEM, PAGE_COUNT } page_t; // ============================================================================ // MODULE STATE // ============================================================================ static lv_obj_t* g_screen = NULL; static lv_obj_t* g_nav[PAGE_COUNT]; static lv_obj_t* g_nav_sub[PAGE_COUNT]; static lv_obj_t* g_page[PAGE_COUNT]; static page_t g_cur = PAGE_WIFI; static bool g_refreshing = false; // setting widget states from code: ignore their events // Wi-Fi page static lv_obj_t* g_wifi_sw = NULL; static lv_obj_t* g_wifi_off_note = NULL; static lv_obj_t* g_wifi_body = NULL; static lv_obj_t* g_st_icon = NULL; static lv_obj_t* g_st_spinner = NULL; static lv_obj_t* g_st_title = NULL; static lv_obj_t* g_st_detail = NULL; static lv_obj_t* g_st_connect = NULL; static lv_obj_t* g_st_forget = NULL; static lv_obj_t* g_scan_btn = NULL; static lv_obj_t* g_list = NULL; static lv_obj_t* g_list_note = NULL; static WiFiManager::network_t g_rows[WIFI_SCAN_MAX_NETWORKS]; static uint8_t g_row_count = 0; static uint32_t g_list_gen = 0xFFFFFFFF; static char g_list_connected[33] = ""; static bool g_want_scan = false; // Password entry (full-screen overlay on this screen) static lv_obj_t* g_pw = NULL; static lv_obj_t* g_pw_title = NULL; static lv_obj_t* g_pw_ta = NULL; static lv_obj_t* g_pw_show = NULL; static char g_pw_ssid[33] = ""; // Updates page static lv_obj_t* g_upd_latest = NULL; static lv_obj_t* g_upd_state = NULL; static lv_obj_t* g_upd_check = NULL; static lv_obj_t* g_upd_install = NULL; static lv_obj_t* g_upd_wifi = NULL; static char g_upd_refusal[96] = ""; static Ota::phase_t g_upd_refusal_phase = Ota::PHASE_IDLE; // System page static lv_obj_t* g_sys_can = NULL; static lv_obj_t* g_sys_link = NULL; static lv_obj_t* g_sys_dbg = NULL; static lv_obj_t* g_sys_diff = NULL; static lv_obj_t* g_sys_diff_inv = NULL; // polarity: no current = locked static lv_obj_t* g_sys_diff_sub = NULL; // sub-line of the diff-lock row: live state static lv_obj_t* g_sys_info = NULL; // values column; keys are static static void refresh(bool force); // ============================================================================ // NAVIGATION // ============================================================================ static void selectPage(page_t p) { g_cur = p; for (int i = 0; i < PAGE_COUNT; i++) { bool on = (i == p); setVisible(g_page[i], on); lv_obj_set_style_bg_opa(g_nav[i], on ? LV_OPA_COVER : LV_OPA_TRANSP, 0); lv_obj_set_style_border_width(g_nav[i], on ? 5 : 0, 0); } if (p == PAGE_WIFI) g_want_scan = true; refresh(true); } static void onNav(lv_event_t* e) { selectPage((page_t)(uintptr_t)lv_event_get_user_data(e)); } static void onBack(lv_event_t* e) { (void)e; DrivelineScreen::show(); } static void onGesture(lv_event_t* e) { (void)e; if (g_pw && !lv_obj_has_flag(g_pw, LV_OBJ_FLAG_HIDDEN)) return; // typing a password if (lv_indev_get_gesture_dir(lv_indev_get_act()) == LV_DIR_RIGHT) { lv_indev_wait_release(lv_indev_get_act()); DrivelineScreen::show(); } } static lv_obj_t* makeNav(lv_obj_t* parent, page_t p, const char* title) { lv_obj_t* n = lv_obj_create(parent); lv_obj_set_size(n, NAV_W, NAV_H); lv_obj_set_pos(n, NAV_X, TOP_Y + p * (NAV_H + 12)); lv_obj_set_style_radius(n, 12, 0); lv_obj_set_style_bg_color(n, UI_COL_PANEL, 0); lv_obj_set_style_bg_opa(n, LV_OPA_TRANSP, 0); lv_obj_set_style_bg_color(n, UI_COL_PANEL2, LV_STATE_PRESSED); lv_obj_set_style_bg_opa(n, LV_OPA_COVER, LV_STATE_PRESSED); lv_obj_set_style_border_side(n, LV_BORDER_SIDE_LEFT, 0); lv_obj_set_style_border_color(n, UI_COL_ACCENT, 0); lv_obj_set_style_border_width(n, 0, 0); lv_obj_set_style_shadow_width(n, 0, 0); lv_obj_set_style_pad_left(n, 20, 0); lv_obj_set_style_pad_ver(n, 18, 0); lv_obj_set_flex_flow(n, LV_FLEX_FLOW_COLUMN); lv_obj_set_flex_align(n, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START); lv_obj_set_style_pad_row(n, 6, 0); lv_obj_clear_flag(n, LV_OBJ_FLAG_SCROLLABLE); lv_obj_add_flag(n, LV_OBJ_FLAG_CLICKABLE); lv_obj_add_event_cb(n, onNav, LV_EVENT_CLICKED, (void*)(uintptr_t)p); label(n, title, &lv_font_montserrat_24, UI_COL_TEXT); g_nav_sub[p] = label(n, "", &lv_font_montserrat_20, UI_COL_MUTED); lv_label_set_long_mode(g_nav_sub[p], LV_LABEL_LONG_DOT); lv_obj_set_width(g_nav_sub[p], NAV_W - 40); return n; } static lv_obj_t* makePage(lv_obj_t* parent) { lv_obj_t* p = box(parent, LV_FLEX_FLOW_COLUMN); lv_obj_set_size(p, PAGE_W, PAGE_H); lv_obj_set_pos(p, PAGE_X, TOP_Y); lv_obj_set_style_pad_row(p, 12, 0); return p; } /** "Title" on the left, a widget (switch) on the right, optional sub-line. */ static lv_obj_t* makeSettingRow(lv_obj_t* parent, const char* title, const char* sub, lv_obj_t** right_parent) { lv_obj_t* row = box(parent, LV_FLEX_FLOW_ROW); lv_obj_set_size(row, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_flex_align(row, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); lv_obj_set_style_min_height(row, 64, 0); lv_obj_t* col = box(row, LV_FLEX_FLOW_COLUMN); lv_obj_set_size(col, LV_SIZE_CONTENT, LV_SIZE_CONTENT); lv_obj_set_style_pad_row(col, 4, 0); label(col, title, &lv_font_montserrat_24, UI_COL_TEXT); if (sub) label(col, sub, &lv_font_montserrat_20, UI_COL_MUTED); *right_parent = row; return row; } // ============================================================================ // WI-FI PAGE // ============================================================================ static void onWifiSwitch(lv_event_t* e) { if (g_refreshing) return; bool on = lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED); WiFiManager::setEnabled(on); if (on) g_want_scan = true; refresh(true); } static void onScan(lv_event_t* e) { (void)e; WiFiManager::requestScan(); refresh(true); } static void onConnectSaved(lv_event_t* e) { (void)e; WiFiManager::connectSaved(); refresh(true); } static void onForgetConfirmed(bool ok, void* user) { (void)user; if (!ok) return; WiFiManager::forget(); g_want_scan = true; } static void onForget(lv_event_t* e) { (void)e; WiFiManager::status_t s = WiFiManager::getStatus(); char text[128]; snprintf(text, sizeof(text), "Main CTRL will no longer join \"%s\" by itself.", s.saved_ssid); confirm("Forget this network?", text, "Forget", onForgetConfirmed, NULL); } static void openPassword(const char* ssid) { snprintf(g_pw_ssid, sizeof(g_pw_ssid), "%s", ssid); char t[64]; snprintf(t, sizeof(t), "Join \"%s\"", ssid); lv_label_set_text(g_pw_title, t); lv_textarea_set_text(g_pw_ta, ""); lv_textarea_set_password_mode(g_pw_ta, true); setButtonText(g_pw_show, LV_SYMBOL_EYE_OPEN " Show"); setVisible(g_pw, true); lv_obj_move_foreground(g_pw); } static void closePassword() { lv_textarea_set_text(g_pw_ta, ""); // don't leave the password in a widget setVisible(g_pw, false); } static void submitPassword() { const char* pw = lv_textarea_get_text(g_pw_ta); if (pw == NULL || pw[0] == 0) return; WiFiManager::connect(g_pw_ssid, pw); closePassword(); refresh(true); } static void onPwKeyboard(lv_event_t* e) { lv_event_code_t code = lv_event_get_code(e); if (code == LV_EVENT_READY) submitPassword(); else if (code == LV_EVENT_CANCEL) closePassword(); } static void onPwConnect(lv_event_t* e) { (void)e; submitPassword(); } static void onPwCancel(lv_event_t* e) { (void)e; closePassword(); } static void onPwShow(lv_event_t* e) { (void)e; bool hidden = lv_textarea_get_password_mode(g_pw_ta); lv_textarea_set_password_mode(g_pw_ta, !hidden); setButtonText(g_pw_show, hidden ? LV_SYMBOL_EYE_CLOSE " Hide" : LV_SYMBOL_EYE_OPEN " Show"); } static void onNetworkRow(lv_event_t* e) { uint8_t i = (uint8_t)(uintptr_t)lv_event_get_user_data(e); if (i >= g_row_count) return; const WiFiManager::network_t& n = g_rows[i]; if (strcmp(n.ssid, g_list_connected) == 0) return; // already on it if (n.secured) { openPassword(n.ssid); } else { WiFiManager::connect(n.ssid, ""); refresh(true); } } static const char* strengthWord(int rssi) { if (rssi >= -55) return "Strong"; if (rssi >= -67) return "Good"; if (rssi >= -77) return "Fair"; return "Weak"; } static lv_color_t strengthColour(int rssi) { if (rssi >= -67) return UI_COL_OK; if (rssi >= -77) return UI_COL_AMBER; return UI_COL_WARN; } static void rebuildList(const WiFiManager::status_t& s) { g_row_count = WiFiManager::getNetworks(g_rows, WIFI_SCAN_MAX_NETWORKS); g_list_gen = s.scan_gen; snprintf(g_list_connected, sizeof(g_list_connected), "%s", s.link == WiFiManager::LINK_CONNECTED ? s.ssid : ""); lv_obj_clean(g_list); for (uint8_t i = 0; i < g_row_count; i++) { const WiFiManager::network_t& n = g_rows[i]; bool connected = strcmp(n.ssid, g_list_connected) == 0; lv_obj_t* row = lv_obj_create(g_list); lv_obj_set_size(row, LV_PCT(100), 64); lv_obj_set_style_bg_color(row, UI_COL_PANEL, 0); lv_obj_set_style_bg_color(row, UI_COL_PANEL2, LV_STATE_PRESSED); lv_obj_set_style_border_width(row, 0, 0); lv_obj_set_style_shadow_width(row, 0, 0); lv_obj_set_style_radius(row, 10, 0); lv_obj_set_style_pad_hor(row, 18, 0); lv_obj_set_style_pad_ver(row, 0, 0); lv_obj_set_style_pad_column(row, 16, 0); lv_obj_set_flex_flow(row, LV_FLEX_FLOW_ROW); lv_obj_set_flex_align(row, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); lv_obj_clear_flag(row, LV_OBJ_FLAG_SCROLLABLE); lv_obj_add_flag(row, LV_OBJ_FLAG_CLICKABLE); lv_obj_add_event_cb(row, onNetworkRow, LV_EVENT_CLICKED, (void*)(uintptr_t)i); label(row, LV_SYMBOL_WIFI, &lv_font_montserrat_24, strengthColour(n.rssi)); lv_obj_t* name = label(row, n.ssid, &lv_font_montserrat_24, UI_COL_TEXT); lv_label_set_long_mode(name, LV_LABEL_LONG_DOT); lv_obj_set_flex_grow(name, 1); char right[40]; if (connected) snprintf(right, sizeof(right), LV_SYMBOL_OK " Connected"); else snprintf(right, sizeof(right), "%s %s", strengthWord(n.rssi), n.secured ? "Secured" : "Open"); label(row, right, &lv_font_montserrat_20, connected ? UI_COL_OK : UI_COL_MUTED); } } static void refreshWifi() { WiFiManager::status_t s = WiFiManager::getStatus(); bool on = s.link != WiFiManager::LINK_OFF; setChecked(g_wifi_sw, on); setVisible(g_wifi_off_note, !on); setVisible(g_wifi_body, on); if (!on) return; // Scan when the page opens (not while connected: a scan hops channels // under the ESP-NOW link - the Scan button still works then). if (g_want_scan && (s.link == WiFiManager::LINK_IDLE || s.link == WiFiManager::LINK_FAILED)) { g_want_scan = false; WiFiManager::requestScan(); s = WiFiManager::getStatus(); } // ---- status card char title[80], detail[96]; bool spinner = false, show_connect = false, show_forget = s.saved_ssid[0] != 0; const char* icon = LV_SYMBOL_WIFI; lv_color_t icon_col = UI_COL_MUTED; switch (s.link) { case WiFiManager::LINK_CONNECTED: snprintf(title, sizeof(title), "Connected to %s", s.ssid); snprintf(detail, sizeof(detail), "IP %s \xE2\x80\xA2 signal %s (%d dBm)", s.ip, strengthWord(s.rssi), s.rssi); icon_col = UI_COL_OK; break; case WiFiManager::LINK_CONNECTING: snprintf(title, sizeof(title), "Connecting to %s...", s.ssid); snprintf(detail, sizeof(detail), "This takes up to 15 seconds."); spinner = true; show_forget = false; break; case WiFiManager::LINK_FAILED: snprintf(title, sizeof(title), "Couldn't connect to %s", s.ssid); snprintf(detail, sizeof(detail), "%s", s.error); icon = LV_SYMBOL_WARNING; icon_col = UI_COL_WARN; show_connect = s.saved_ssid[0] != 0; break; default: // IDLE if (s.saved_ssid[0]) { snprintf(title, sizeof(title), "Not connected"); snprintf(detail, sizeof(detail), "Saved network: %s", s.saved_ssid); show_connect = true; } else { snprintf(title, sizeof(title), "Not connected"); snprintf(detail, sizeof(detail), "Pick a network below. It is remembered once it works."); } break; } setText(g_st_title, title); setText(g_st_detail, detail); setText(g_st_icon, icon); setTextColor(g_st_icon, icon_col); setVisible(g_st_icon, !spinner); setVisible(g_st_spinner, spinner); setVisible(g_st_connect, show_connect); setVisible(g_st_forget, show_forget); // ---- scan button + list setButtonText(g_scan_btn, s.scanning ? "Scanning..." : LV_SYMBOL_REFRESH " Scan"); if (s.scanning || s.link == WiFiManager::LINK_CONNECTING) lv_obj_add_state(g_scan_btn, LV_STATE_DISABLED); else lv_obj_clear_state(g_scan_btn, LV_STATE_DISABLED); const char* connected_now = s.link == WiFiManager::LINK_CONNECTED ? s.ssid : ""; if (s.scan_gen != g_list_gen || strcmp(connected_now, g_list_connected) != 0) rebuildList(s); if (g_row_count == 0) { setText(g_list_note, s.scanning ? "Looking for networks..." : s.link == WiFiManager::LINK_CONNECTED ? "Tap Scan to see other networks." : "No networks found. Tap Scan to look again."); setVisible(g_list_note, true); } else { setVisible(g_list_note, false); } } static void buildWifiPage(lv_obj_t* page) { lv_obj_t* right; makeSettingRow(page, "Wi-Fi", "Used for firmware updates", &right); g_wifi_sw = toggle(right, onWifiSwitch, NULL); g_wifi_off_note = label(page, "Wi-Fi is off.\n\nTurn it on to join a network for firmware updates. " "The link to Inst. Cluster works without it.", &lv_font_montserrat_20, UI_COL_MUTED); lv_label_set_long_mode(g_wifi_off_note, LV_LABEL_LONG_WRAP); lv_obj_set_width(g_wifi_off_note, LV_PCT(100)); g_wifi_body = box(page, LV_FLEX_FLOW_COLUMN); lv_obj_set_width(g_wifi_body, LV_PCT(100)); lv_obj_set_flex_grow(g_wifi_body, 1); lv_obj_set_style_pad_row(g_wifi_body, 12, 0); // status card: [icon|spinner] [title / detail] [Connect] [Forget] lv_obj_t* c = card(g_wifi_body); lv_obj_set_size(c, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_flex_flow(c, LV_FLEX_FLOW_ROW); lv_obj_set_flex_align(c, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); lv_obj_set_style_pad_column(c, 16, 0); lv_obj_set_style_min_height(c, 96, 0); g_st_icon = label(c, LV_SYMBOL_WIFI, &lv_font_montserrat_30, UI_COL_MUTED); g_st_spinner = lv_spinner_create(c, 1000, 60); lv_obj_set_size(g_st_spinner, 40, 40); lv_obj_set_style_arc_width(g_st_spinner, 5, LV_PART_MAIN); lv_obj_set_style_arc_width(g_st_spinner, 5, LV_PART_INDICATOR); lv_obj_set_style_arc_color(g_st_spinner, UI_COL_ACCENT, LV_PART_INDICATOR); lv_obj_add_flag(g_st_spinner, LV_OBJ_FLAG_HIDDEN); lv_obj_t* txt = box(c, LV_FLEX_FLOW_COLUMN); lv_obj_set_height(txt, LV_SIZE_CONTENT); lv_obj_set_flex_grow(txt, 1); lv_obj_set_style_pad_row(txt, 6, 0); g_st_title = label(txt, "", &lv_font_montserrat_24, UI_COL_TEXT); lv_label_set_long_mode(g_st_title, LV_LABEL_LONG_DOT); lv_obj_set_width(g_st_title, LV_PCT(100)); g_st_detail = label(txt, "", &lv_font_montserrat_20, UI_COL_MUTED); lv_label_set_long_mode(g_st_detail, LV_LABEL_LONG_WRAP); lv_obj_set_width(g_st_detail, LV_PCT(100)); g_st_connect = button(c, "Connect", 140, 56, BTN_PRIMARY, onConnectSaved, NULL); g_st_forget = button(c, "Forget", 120, 56, BTN_SECONDARY, onForget, NULL); // list header lv_obj_t* hdr = box(g_wifi_body, LV_FLEX_FLOW_ROW); lv_obj_set_size(hdr, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_flex_align(hdr, LV_FLEX_ALIGN_SPACE_BETWEEN, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); label(hdr, "Networks nearby", &lv_font_montserrat_20, UI_COL_MUTED); g_scan_btn = button(hdr, LV_SYMBOL_REFRESH " Scan", 180, 52, BTN_SECONDARY, onScan, NULL); g_list_note = label(g_wifi_body, "", &lv_font_montserrat_20, UI_COL_MUTED); g_list = box(g_wifi_body, LV_FLEX_FLOW_COLUMN); lv_obj_set_width(g_list, LV_PCT(100)); lv_obj_set_flex_grow(g_list, 1); lv_obj_set_style_pad_row(g_list, 8, 0); lv_obj_add_flag(g_list, LV_OBJ_FLAG_SCROLLABLE); lv_obj_set_scroll_dir(g_list, LV_DIR_VER); lv_obj_set_scrollbar_mode(g_list, LV_SCROLLBAR_MODE_ACTIVE); } static void buildPasswordOverlay() { g_pw = lv_obj_create(g_screen); lv_obj_remove_style_all(g_pw); lv_obj_set_size(g_pw, 1024, 600); lv_obj_set_style_bg_color(g_pw, UI_COL_BG, 0); lv_obj_set_style_bg_opa(g_pw, LV_OPA_COVER, 0); lv_obj_clear_flag(g_pw, LV_OBJ_FLAG_SCROLLABLE); lv_obj_add_flag(g_pw, LV_OBJ_FLAG_CLICKABLE); // swallow touches meant for the page below lv_obj_add_flag(g_pw, LV_OBJ_FLAG_HIDDEN); g_pw_title = label(g_pw, "", &lv_font_montserrat_30, UI_COL_TEXT); lv_label_set_long_mode(g_pw_title, LV_LABEL_LONG_DOT); lv_obj_set_width(g_pw_title, 760); lv_obj_set_pos(g_pw_title, 24, 22); lv_obj_t* cancel = button(g_pw, "Cancel", 180, 56, BTN_SECONDARY, onPwCancel, NULL); lv_obj_align(cancel, LV_ALIGN_TOP_RIGHT, -16, 10); g_pw_ta = lv_textarea_create(g_pw); lv_textarea_set_one_line(g_pw_ta, true); lv_textarea_set_password_mode(g_pw_ta, true); lv_textarea_set_placeholder_text(g_pw_ta, "Password"); lv_textarea_set_max_length(g_pw_ta, 63); lv_obj_set_style_text_font(g_pw_ta, &lv_font_montserrat_30, 0); lv_obj_set_style_bg_color(g_pw_ta, UI_COL_PANEL, 0); lv_obj_set_style_text_color(g_pw_ta, UI_COL_TEXT, 0); lv_obj_set_style_border_color(g_pw_ta, UI_COL_ACCENT, 0); lv_obj_set_width(g_pw_ta, 640); lv_obj_set_pos(g_pw_ta, 24, 100); lv_obj_add_state(g_pw_ta, LV_STATE_FOCUSED); // keep the cursor visible g_pw_show = button(g_pw, LV_SYMBOL_EYE_OPEN " Show", 150, 60, BTN_SECONDARY, onPwShow, NULL); lv_obj_set_pos(g_pw_show, 680, 100); lv_obj_t* go = button(g_pw, "Connect", 170, 60, BTN_PRIMARY, onPwConnect, NULL); lv_obj_set_pos(go, 842, 100); lv_obj_t* kb = lv_keyboard_create(g_pw); lv_obj_set_size(kb, 1024, 360); lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 0); lv_obj_set_style_text_font(kb, &lv_font_montserrat_24, 0); lv_obj_set_style_bg_color(kb, UI_COL_PANEL, 0); lv_obj_set_style_bg_color(kb, UI_COL_PANEL2, LV_PART_ITEMS); lv_obj_set_style_text_color(kb, UI_COL_TEXT, LV_PART_ITEMS); lv_obj_set_style_shadow_width(kb, 0, LV_PART_ITEMS); lv_keyboard_set_textarea(kb, g_pw_ta); lv_obj_add_event_cb(kb, onPwKeyboard, LV_EVENT_READY, NULL); lv_obj_add_event_cb(kb, onPwKeyboard, LV_EVENT_CANCEL, NULL); } // ============================================================================ // UPDATES PAGE - Main CTRL (this board) and Inst. Cluster (over the link) // ============================================================================ static lv_obj_t* g_b_installed = NULL; static lv_obj_t* g_b_latest = NULL; static lv_obj_t* g_b_state = NULL; static lv_obj_t* g_b_check = NULL; static lv_obj_t* g_b_install = NULL; static char g_b_refusal[96] = ""; static uint8_t g_b_refusal_phase = LINK_OTA_IDLE; static void onInstallConfirmed(bool ok, void* user) { (void)user; if (!ok) return; Ota::phase_t before = Ota::getStatus().phase; if (!Ota::requestInstall(g_upd_refusal, sizeof(g_upd_refusal))) g_upd_refusal_phase = before; else g_upd_refusal[0] = 0; refresh(true); } static void onInstall(lv_event_t* e) { (void)e; Ota::ui_status_t o = Ota::getStatus(); char title[64]; snprintf(title, sizeof(title), "Install %s?", o.latest); confirm(title, "This Controller will restart after successful update.\n\nInstrument Cluster's gauges will drop out for a few seconds.\n\n" "DO NOT SWITCH OFF THE IGNITION!\n\nOnly while parked with the engine off!", "Install", onInstallConfirmed, NULL); } static void onCheck(lv_event_t* e) { (void)e; Ota::phase_t before = Ota::getStatus().phase; if (!Ota::requestCheck(g_upd_refusal, sizeof(g_upd_refusal))) g_upd_refusal_phase = before; else g_upd_refusal[0] = 0; refresh(true); } static void onGoWifi(lv_event_t* e) { (void)e; selectPage(PAGE_WIFI); } static void onBInstallConfirmed(bool ok, void* user) { (void)user; if (!ok) return; uint8_t before = LinkPeer::getStatus().st.ota_phase; if (!LinkPeer::requestInstall(g_b_refusal, sizeof(g_b_refusal))) g_b_refusal_phase = before; else g_b_refusal[0] = 0; refresh(true); } static void onBInstall(lv_event_t* e) { (void)e; LinkPeer::peer_status_t p = LinkPeer::getStatus(); char title[64]; snprintf(title, sizeof(title), "Install %s on Inst. Cluster?", p.st.latest); confirm(title, "Inst. Cluster joins the Wi-Fi network, downloads its new firmware and restarts; " "its gauges hold still meanwhile and this card shows the progress.\n\n" "Only while parked with the engine off.", "Install", onBInstallConfirmed, NULL); } static void onBCheck(lv_event_t* e) { (void)e; uint8_t before = LinkPeer::getStatus().st.ota_phase; if (!LinkPeer::requestCheck(g_b_refusal, sizeof(g_b_refusal))) g_b_refusal_phase = before; else g_b_refusal[0] = 0; refresh(true); } static void refreshUpdatesA() { Ota::ui_status_t o = Ota::getStatus(); bool wifi_ok = WiFiManager::getStatus().link == WiFiManager::LINK_CONNECTED; if (g_upd_refusal[0] && o.phase != g_upd_refusal_phase) g_upd_refusal[0] = 0; // superseded char msg[140]; lv_color_t col = UI_COL_MUTED; switch (o.phase) { case Ota::PHASE_CHECKING: snprintf(msg, sizeof(msg), "Checking for updates..."); break; case Ota::PHASE_INSTALLING: snprintf(msg, sizeof(msg), "Installing %s...", o.latest); break; case Ota::PHASE_UP_TO_DATE: snprintf(msg, sizeof(msg), LV_SYMBOL_OK " You're up to date."); col = UI_COL_OK; break; case Ota::PHASE_AVAILABLE: snprintf(msg, sizeof(msg), "Version %s is available.", o.latest); col = UI_COL_ACCENT; break; case Ota::PHASE_FAILED: snprintf(msg, sizeof(msg), LV_SYMBOL_WARNING " %s", o.message); col = UI_COL_WARN; break; default: snprintf(msg, sizeof(msg), "%s", wifi_ok ? "Tap Check to look for a new version." : "Connect to Wi-Fi to check for updates."); break; } if (g_upd_refusal[0]) { snprintf(msg, sizeof(msg), LV_SYMBOL_WARNING " %s", g_upd_refusal); col = UI_COL_WARN; } else if (!wifi_ok && o.phase != Ota::PHASE_IDLE && o.phase != Ota::PHASE_CHECKING) { // keep the last result, but say why the buttons are off size_t n = strlen(msg); snprintf(msg + n, sizeof(msg) - n, " Connect to Wi-Fi to check again."); } setText(g_upd_state, msg); setTextColor(g_upd_state, col); setText(g_upd_latest, o.latest[0] ? o.latest : "-"); bool busy = o.phase == Ota::PHASE_CHECKING || o.phase == Ota::PHASE_INSTALLING; setButtonText(g_upd_check, busy ? "Working..." : LV_SYMBOL_REFRESH " Check for updates"); if (busy || !wifi_ok) lv_obj_add_state(g_upd_check, LV_STATE_DISABLED); else lv_obj_clear_state(g_upd_check, LV_STATE_DISABLED); bool can_install = o.phase == Ota::PHASE_AVAILABLE && wifi_ok; char inst[48]; snprintf(inst, sizeof(inst), LV_SYMBOL_DOWNLOAD " Install %s", o.latest); setButtonText(g_upd_install, inst); setVisible(g_upd_install, can_install); setVisible(g_upd_wifi, !wifi_ok); } static void refreshUpdatesB() { LinkPeer::peer_status_t p = LinkPeer::getStatus(); if (g_b_refusal[0] && p.st.ota_phase != g_b_refusal_phase) g_b_refusal[0] = 0; bool has_wifi = (p.st.flags & LINK_STATUS_HAVE_WIFI) != 0; bool ready = p.heard && p.paired && p.key_match && has_wifi; char msg[160]; lv_color_t col = UI_COL_MUTED; if (!p.heard) { snprintf(msg, sizeof(msg), "Inst. Cluster not heard.%s", p.paired ? "" : " Not paired yet: connect the wire and run 'link pair' on the console."); } else if (!p.paired || !p.key_match) { snprintf(msg, sizeof(msg), LV_SYMBOL_WARNING " Not paired with this controller. " "Connect the wire and run 'link pair' on the console."); col = UI_COL_AMBER; } else if (!has_wifi) { snprintf(msg, sizeof(msg), "Inst. Cluster has no Wi-Fi credentials yet. Join a network here; " "they are passed on by themselves."); } else { switch (p.st.ota_phase) { case LINK_OTA_CHECKING: snprintf(msg, sizeof(msg), "Inst. Cluster is checking..."); break; case LINK_OTA_INSTALLING: snprintf(msg, sizeof(msg), "Installing %s on Inst. Cluster... %u %%", p.st.latest, p.st.ota_progress); col = UI_COL_ACCENT; break; case LINK_OTA_UP_TO_DATE: snprintf(msg, sizeof(msg), LV_SYMBOL_OK " Inst. Cluster is up to date."); col = UI_COL_OK; break; case LINK_OTA_AVAILABLE: snprintf(msg, sizeof(msg), "Version %s is available for Inst. Cluster.", p.st.latest); col = UI_COL_ACCENT; break; case LINK_OTA_FAILED: snprintf(msg, sizeof(msg), LV_SYMBOL_WARNING " %s", p.st.message); col = UI_COL_WARN; break; default: snprintf(msg, sizeof(msg), "Tap Check to look for a new version"); break; } if (p.st.flags & LINK_STATUS_PENDING_VERIFY) { size_t n = strlen(msg); snprintf(msg + n, sizeof(msg) - n, " (new firmware, confirming...)"); } } if (g_b_refusal[0]) { snprintf(msg, sizeof(msg), LV_SYMBOL_WARNING " %s", g_b_refusal); col = UI_COL_WARN; } setText(g_b_state, msg); setTextColor(g_b_state, col); setText(g_b_installed, p.heard ? p.st.version : "-"); setText(g_b_latest, (p.heard && p.st.latest[0]) ? p.st.latest : "-"); bool busy = p.heard && (p.st.ota_phase == LINK_OTA_CHECKING || p.st.ota_phase == LINK_OTA_INSTALLING); setButtonText(g_b_check, busy ? "Working..." : LV_SYMBOL_REFRESH " Check for updates"); if (busy || !ready) lv_obj_add_state(g_b_check, LV_STATE_DISABLED); else lv_obj_clear_state(g_b_check, LV_STATE_DISABLED); bool can_install = ready && p.st.ota_phase == LINK_OTA_AVAILABLE; char inst[48]; snprintf(inst, sizeof(inst), LV_SYMBOL_DOWNLOAD " Install %s", p.st.latest); setButtonText(g_b_install, inst); setVisible(g_b_install, can_install); } static void refreshUpdates() { refreshUpdatesA(); refreshUpdatesB(); } /** One controller's card: "Name Installed vX Latest vY", a status line, a button row. */ static lv_obj_t* controllerCard(lv_obj_t* page, const char* name, const char* installed, lv_obj_t** installed_lbl, lv_obj_t** latest_lbl, lv_obj_t** state_lbl) { lv_obj_t* c = card(page); lv_obj_set_size(c, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_flex_flow(c, LV_FLEX_FLOW_COLUMN); lv_obj_set_style_pad_row(c, 8, 0); lv_obj_t* row = box(c, LV_FLEX_FLOW_ROW); lv_obj_set_size(row, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_flex_align(row, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_END, LV_FLEX_ALIGN_END); lv_obj_set_style_pad_column(row, 36, 0); lv_obj_t* title = label(row, name, &lv_font_montserrat_24, UI_COL_TEXT); lv_obj_set_width(title, 190); lv_obj_t* a = box(row, LV_FLEX_FLOW_ROW); lv_obj_set_size(a, LV_SIZE_CONTENT, LV_SIZE_CONTENT); lv_obj_set_flex_align(a, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_END, LV_FLEX_ALIGN_END); lv_obj_set_style_pad_column(a, 10, 0); label(a, "Installed", &lv_font_montserrat_20, UI_COL_MUTED); *installed_lbl = label(a, installed, &lv_font_montserrat_24, UI_COL_TEXT); lv_obj_t* b = box(row, LV_FLEX_FLOW_ROW); lv_obj_set_size(b, LV_SIZE_CONTENT, LV_SIZE_CONTENT); lv_obj_set_flex_align(b, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_END, LV_FLEX_ALIGN_END); lv_obj_set_style_pad_column(b, 10, 0); label(b, "Latest", &lv_font_montserrat_20, UI_COL_MUTED); *latest_lbl = label(b, "-", &lv_font_montserrat_24, UI_COL_TEXT); *state_lbl = label(c, "", &lv_font_montserrat_20, UI_COL_MUTED); lv_label_set_long_mode(*state_lbl, LV_LABEL_LONG_WRAP); lv_obj_set_width(*state_lbl, LV_PCT(100)); return c; } static void buildUpdatesPage(lv_obj_t* page) { label(page, "Firmware updates", &lv_font_montserrat_24, UI_COL_TEXT); lv_obj_t* installed_a = NULL; lv_obj_t* a = controllerCard(page, "Main CTRL", FW_VERSION, &installed_a, &g_upd_latest, &g_upd_state); lv_obj_t* btns = box(a, LV_FLEX_FLOW_ROW); lv_obj_set_size(btns, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_column(btns, 16, 0); g_upd_check = button(btns, LV_SYMBOL_REFRESH " Check for updates", 300, 56, BTN_PRIMARY, onCheck, NULL); g_upd_install = button(btns, "Install", 280, 56, BTN_SUCCESS, onInstall, NULL); g_upd_wifi = button(btns, LV_SYMBOL_WIFI " Wi-Fi settings", 260, 56, BTN_SECONDARY, onGoWifi, NULL); lv_obj_t* b = controllerCard(page, "Inst. Cluster", "-", &g_b_installed, &g_b_latest, &g_b_state); btns = box(b, LV_FLEX_FLOW_ROW); lv_obj_set_size(btns, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_style_pad_column(btns, 16, 0); g_b_check = button(btns, LV_SYMBOL_REFRESH " Check for updates", 300, 56, BTN_PRIMARY, onBCheck, NULL); g_b_install = button(btns, "Install", 280, 56, BTN_SUCCESS, onBInstall, NULL); lv_obj_t* note = label(page, "Each controller restarts after updating. DO NOT TURN OFF THE IGNITION DURING THE UPDATE", &lv_font_montserrat_20, UI_COL_MUTED); lv_label_set_long_mode(note, LV_LABEL_LONG_WRAP); lv_obj_set_width(note, LV_PCT(100)); } // ============================================================================ // SYSTEM PAGE // ============================================================================ static void onCanSwitch(lv_event_t* e) { if (g_refreshing) return; CANDriver::setEnabled(lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED)); } static void onLinkSwitch(lv_event_t* e) { if (g_refreshing) return; UARTManager::setEnabled(lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED)); } static void onDebugSwitch(lv_event_t* e) { if (g_refreshing) return; bool on = lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED); SettingsManager::saveDebugState(on); UIStateManager::setDebugEnabled(on); } static void onDiffSwitch(lv_event_t* e) { if (g_refreshing) return; DiffLocks::setEnabled(lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED)); } static void onDiffInvertSwitch(lv_event_t* e) { if (g_refreshing) return; DiffLocks::setInverted(lv_obj_has_state(lv_event_get_target(e), LV_STATE_CHECKED)); } static void refreshSystem(bool force) { setChecked(g_sys_can, CANDriver::isActive()); setChecked(g_sys_link, UARTManager::getState() != UARTManager::UART_STATE_OFF); setChecked(g_sys_dbg, UIStateManager::isDebugEnabled()); setChecked(g_sys_diff, DiffLocks::isEnabled()); setChecked(g_sys_diff_inv, DiffLocks::isInverted()); // live state in the sub-line, so the wiring can be tested from this page if (!DiffLocks::isEnabled()) setText(g_sys_diff_sub, DiffLocks::isInverted() ? "DI0 rear, DI1 front - 0 V = locked" : "DI0 rear, DI1 front - 12 V = locked"); else if (!DiffLocks::isValid()) setText(g_sys_diff_sub, "Now: reading..."); else setText(g_sys_diff_sub, DiffLocks::frontLocked() ? (DiffLocks::rearLocked() ? "Now: rear LOCKED, front LOCKED" : "Now: FRONT only - check DI0 wiring") : (DiffLocks::rearLocked() ? "Now: rear LOCKED, front open" : "Now: both open")); static uint32_t last = 0; // the figures below change slowly; 1 Hz is plenty if (!force && millis() - last < 1000) return; last = millis(); can_stats_t cs = CANDriver::getStats(); const esp_partition_t* run = esp_ota_get_running_partition(); uint32_t up = millis() / 1000; char buf[400]; snprintf(buf, sizeof(buf), "%s (slot %s), up %lu h %02lu min\n" "ESP-NOW %s, %lu frames sent, %lu failed\n" "%lu frames received\n" "%u KB internal free, %u KB PSRAM free", FW_VERSION, run ? run->label : "?", (unsigned long)(up / 3600), (unsigned long)((up / 60) % 60), EspNowLink::isReady() ? "up" : "DOWN", (unsigned long)EspNowLink::sentCount(), (unsigned long)EspNowLink::failCount(), (unsigned long)cs.messages_received, (unsigned)(heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) / 1024), (unsigned)(heap_caps_get_free_size(MALLOC_CAP_SPIRAM) / 1024)); setText(g_sys_info, buf); } static void buildSystemPage(lv_obj_t* page) { lv_obj_t* c = card(page); lv_obj_set_size(c, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_flex_flow(c, LV_FLEX_FLOW_COLUMN); lv_obj_set_style_pad_row(c, 10, 0); lv_obj_t* right; makeSettingRow(c, "CAN bus", "Read the car's data bus", &right); g_sys_can = toggle(right, onCanSwitch, NULL); makeSettingRow(c, "Send to Inst. Cluster", "Gauge data over the wire and ESP-NOW", &right); g_sys_link = toggle(right, onLinkSwitch, NULL); makeSettingRow(c, "Performance overlay", "Frame rate and CPU load in the corner", &right); g_sys_dbg = toggle(right, onDebugSwitch, NULL); lv_obj_t* diff_row = makeSettingRow(c, "Diff lock inputs", "DI0 rear, DI1 front - 12 V = locked", &right); g_sys_diff_sub = lv_obj_get_child(lv_obj_get_child(diff_row, 0), 1); // two switches on the right: polarity ("Inverted") and on/off; the page has no room for a fifth row lv_obj_t* diff_right = box(right, LV_FLEX_FLOW_ROW); lv_obj_set_size(diff_right, LV_SIZE_CONTENT, LV_SIZE_CONTENT); // START, not END: with END, LVGL 8's content-size pass measures only the last child // (the earlier ones sit at negative x) and clips "Inverted" away - seen on the board. // The row's SPACE_BETWEEN already pushes this box to the right edge. lv_obj_set_flex_align(diff_right, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); lv_obj_set_style_pad_column(diff_right, 12, 0); label(diff_right, "Inverted", &lv_font_montserrat_20, UI_COL_MUTED); g_sys_diff_inv = toggle(diff_right, onDiffInvertSwitch, NULL); lv_obj_t* gap = box(diff_right, LV_FLEX_FLOW_ROW); lv_obj_set_size(gap, 12, 1); g_sys_diff = toggle(diff_right, onDiffSwitch, NULL); // two labels side by side, same line spacing, so keys and values line up lv_obj_t* info = card(page); lv_obj_set_size(info, LV_PCT(100), LV_SIZE_CONTENT); lv_obj_set_flex_flow(info, LV_FLEX_FLOW_ROW); lv_obj_set_style_pad_column(info, 24, 0); // Uptime shares the firmware line: a fourth switch row leaves no room for a fifth line (484 px page) lv_obj_t* keys = label(info, "Firmware\nLink to B\nCAN bus\nMemory", &lv_font_montserrat_20, UI_COL_MUTED); lv_obj_set_style_text_line_space(keys, 6, 0); g_sys_info = label(info, "", &lv_font_montserrat_20, UI_COL_TEXT); lv_obj_set_style_text_line_space(g_sys_info, 6, 0); } // ============================================================================ // BUILD / REFRESH // ============================================================================ static void build() { if (g_screen != NULL) return; g_screen = screen(); lv_obj_add_event_cb(g_screen, onGesture, LV_EVENT_GESTURE, NULL); header(g_screen, "SETTINGS", onBack); g_nav[PAGE_WIFI] = makeNav(g_screen, PAGE_WIFI, "Wi-Fi"); g_nav[PAGE_UPDATES] = makeNav(g_screen, PAGE_UPDATES, "Updates"); g_nav[PAGE_SYSTEM] = makeNav(g_screen, PAGE_SYSTEM, "System"); for (int i = 0; i < PAGE_COUNT; i++) g_page[i] = makePage(g_screen); buildWifiPage(g_page[PAGE_WIFI]); buildUpdatesPage(g_page[PAGE_UPDATES]); buildSystemPage(g_page[PAGE_SYSTEM]); buildPasswordOverlay(); ESP_LOGI("Settings", "Settings screen built"); } static void refreshNav() { WiFiManager::status_t s = WiFiManager::getStatus(); const char* w = "Not connected"; if (s.link == WiFiManager::LINK_OFF) w = "Off"; else if (s.link == WiFiManager::LINK_CONNECTED) w = s.ssid; else if (s.link == WiFiManager::LINK_CONNECTING) w = "Connecting..."; setText(g_nav_sub[PAGE_WIFI], w); setTextColor(g_nav_sub[PAGE_WIFI], s.link == WiFiManager::LINK_CONNECTED ? UI_COL_OK : UI_COL_MUTED); Ota::ui_status_t o = Ota::getStatus(); bool avail = o.phase == Ota::PHASE_AVAILABLE; setText(g_nav_sub[PAGE_UPDATES], avail ? "Update available" : FW_VERSION); setTextColor(g_nav_sub[PAGE_UPDATES], avail ? UI_COL_ACCENT : UI_COL_MUTED); char sys[40]; snprintf(sys, sizeof(sys), "CAN %s \xE2\x80\xA2 Link %s", CANDriver::isActive() ? "on" : "off", UARTManager::getState() != UARTManager::UART_STATE_OFF ? "on" : "off"); setText(g_nav_sub[PAGE_SYSTEM], sys); } /** Caller holds the LVGL lock. */ static void refresh(bool force) { g_refreshing = true; refreshNav(); switch (g_cur) { case PAGE_WIFI: refreshWifi(); break; case PAGE_UPDATES: refreshUpdates(); break; case PAGE_SYSTEM: refreshSystem(force); break; default: break; } g_refreshing = false; } // ============================================================================ // PUBLIC API // ============================================================================ void show() { bool first = (g_screen == NULL); build(); if (first) selectPage(PAGE_WIFI); else if (g_cur == PAGE_WIFI) g_want_scan = true; refresh(true); lv_scr_load_anim(g_screen, LV_SCR_LOAD_ANIM_NONE, 0, 0, false); // instant: fades stutter at 1024x600 } void tick() { if (g_screen == NULL || lv_scr_act() != g_screen) return; static uint32_t last = 0; if (millis() - last < 250) return; last = millis(); lvgl_port_lock(-1); refresh(false); lvgl_port_unlock(); } } // namespace SettingsScreen