Hand-coded Settings + tabbed Gauge Tuning; remembered Wi-Fi; on-screen updates
Firmware / build (push) Successful in 3m58s

Settings (replaces the SquareLine screen, now deleted): sidebar with
Wi-Fi (switch, status card with failure reasons, networks nearby,
full-screen password page), Updates (installed vs latest, Check, Install
behind a confirm dialog) and System (CAN / link / perf overlay switches,
live link, CAN, memory, uptime).

Gauge Tuning: tabs Tachometer / Coolant gauge / Alerts / Simulation,
56 px -/+ rows with hints and real units, accelerating hold-to-repeat,
per-tab Reset with confirm, save indicator, SIMULATION ON chip, swipe
left to go back.

WiFiManager rewritten without LVGL code: one remembered network (NVS
"wifi"), auto-join at boot, scans only on request, no endless reconnect
(keeps the ESP-NOW channel stable), failure reasons from the disconnect
event, password never logged. Ota gains getStatus()/requestCheck()/
requestInstall() for the screen. CAN/UART switches become setEnabled().
Shared ui_kit (header, buttons, switches, confirm dialog); Montserrat
20/24 enabled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-27 07:38:17 +02:00
parent 766019a01e
commit e68fad73b6
30 changed files with 2082 additions and 2276 deletions
+54 -129
View File
@@ -1,160 +1,85 @@
/*
* WiFi Manager Module
*
* Handles WiFi functionality for the Waveshare ESP32-S3-Touch-LCD-5B:
* - WiFi scanning and network discovery
* - Connection management with password entry
* - Dynamic SSID button generation
* - FreeRTOS task on Core 0 for async WiFi operations
* - Thread-safe UI updates via LVGL lock
* - Auto-restore WiFi state from NVS
* Joins one remembered network (for OTA updates) and scans on request. Owns no
* LVGL objects: the Settings screen polls getStatus()/getNetworks() and calls
* the request functions, so screen lifetimes can never leave dangling pointers
* here.
*
* Radio rules (the ESP-NOW link to Controller B shares the radio):
* - the radio stays in STA mode forever; "off" only stops association/scans.
* Never WiFi.disconnect(true) / WiFi.mode(WIFI_OFF) - that kills ESP-NOW.
* - scans only when asked (each one hops every channel);
* - no endless auto-reconnect: one attempt at boot, one after a drop, then
* idle on ESPNOW_CHANNEL until the user asks again.
*
* The network is remembered in NVS (namespace "wifi") after a successful join.
* The password is never printed.
*/
#pragma once
#include <Arduino.h>
#include <WiFi.h>
#include <lvgl.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
namespace WiFiManager {
// ============================================================================
// CONFIGURATION
// ============================================================================
#define WIFI_SCAN_MAX_NETWORKS 20
#define WIFI_SCAN_INTERVAL_MS 10000 // Rescan every 10 seconds when WiFi is ON
#define WIFI_CONNECT_TIMEOUT_MS 15000
#define WIFI_TASK_STACK_SIZE 4096
#define WIFI_TASK_PRIORITY 1
#define WIFI_TASK_CORE 0 // Run WiFi task on Core 0 (LVGL runs on Core 1)
#define WIFI_TASK_CORE 0
// ============================================================================
// PUBLIC TYPES
// ============================================================================
/**
* @brief WiFi state machine states
*/
typedef enum {
WIFI_STATE_OFF = 0,
WIFI_STATE_SCANNING,
WIFI_STATE_SCAN_COMPLETE,
WIFI_STATE_CONNECTED,
WIFI_STATE_CONNECTING,
WIFI_STATE_ERROR
} wifi_state_t;
LINK_OFF = 0, // Wi-Fi switched off
LINK_IDLE, // on, not connected, not trying
LINK_CONNECTING,
LINK_CONNECTED,
LINK_FAILED // last attempt failed; see status_t::error
} link_t;
/**
* @brief WiFi scan result structure
*/
typedef struct {
String ssid;
int32_t rssi;
wifi_auth_mode_t auth;
} wifi_network_t;
link_t link;
char ssid[33]; // network being joined / joined / that failed
char ip[16];
int rssi; // dBm, when connected
char error[48]; // human-readable reason when LINK_FAILED
char saved_ssid[33]; // remembered network ("" = none)
bool scanning;
uint32_t scan_gen; // bumps each time a scan completes
} status_t;
/**
* @brief WiFi connection credentials
*/
typedef struct {
char ssid[32];
char password[64];
} wifi_credentials_t;
char ssid[33];
int8_t rssi;
bool secured;
} network_t;
/**
* @brief WiFi event types for FreeRTOS queue
*/
typedef enum {
WIFI_UI_EVENT_TOGGLE_ON,
WIFI_UI_EVENT_TOGGLE_OFF,
WIFI_UI_EVENT_SCAN_START,
WIFI_UI_EVENT_SCAN_DONE,
WIFI_UI_EVENT_UPDATE_UI,
WIFI_UI_EVENT_CONNECT,
WIFI_UI_EVENT_DISCONNECT
} wifi_ui_event_type_t;
/** Start the Wi-Fi task. If enabled and a network is saved, joins it. */
void init(bool enabled);
/**
* @brief WiFi queue event structure
*/
typedef struct {
wifi_ui_event_type_t type;
void* data;
} wifi_queue_event_t;
/** Wi-Fi switch: persisted. On joins the saved network; off disconnects. */
void setEnabled(bool on);
bool isEnabled();
// ============================================================================
// PUBLIC API
// ============================================================================
/** Scan once (ignored while off or already scanning). */
void requestScan();
/**
* @brief Initialize the WiFi manager subsystem
*
* This function:
* - Creates FreeRTOS mutex and queue
* - Spawns WiFi task on Core 0
* - Optionally auto-starts WiFi based on saved state
*
* @param auto_start If true, automatically start WiFi based on NVS saved state
*/
void init(bool auto_start);
/** Join a network; remembered if it works. pass may be NULL/"" for open networks. */
void connect(const char* ssid, const char* pass);
/**
* @brief Get current WiFi state
* @return Current wifi_state_t value
*/
wifi_state_t getState();
/** Join the remembered network again. */
void connectSaved();
/**
* @brief Get WiFi network count from last scan
* @return Number of networks found
*/
uint8_t getNetworkCount();
/** Drop the remembered network and disconnect. */
void forget();
/**
* @brief Console handler for `wifi status | join <ssid> [pass] | off`.
* @return false if the line isn't a wifi command
*/
status_t getStatus();
/** Copy of the last scan (strongest first, one row per SSID). Returns the count. */
uint8_t getNetworks(network_t* out, uint8_t max);
/** Console: `wifi status | join <ssid> [pass] | forget | on | off`. */
bool handleCommand(const char* line);
// ============================================================================
// EVENT HANDLERS (Called from SquareLine Studio UI)
// ============================================================================
/**
* @brief WiFi toggle event handler
* @param e LVGL event object
* @note This function is registered in SquareLine Studio UI
*/
void onWifiToggle(lv_event_t *e);
/**
* @brief SSID button click handler
* @param e LVGL event object
* @note Shows password entry for secured networks
*/
void onSSIDButtonClick(lv_event_t *e);
/**
* @brief Keyboard event handler for password entry
* @param e LVGL event object
* @note Handles READY (connect) and CANCEL events
*/
void onKeyboardEvent(lv_event_t *e);
/**
* @brief Manual rescan request handler
* @param e LVGL event object
*/
void onRescanRequest(lv_event_t *e);
/**
* @brief Legacy SquareLine Studio compatibility function
* @param e LVGL event object
* @note Kept for compatibility, but dynamic buttons handle clicks now
*/
void onSelectSSID(lv_event_t *e);
} // namespace WiFiManager