Init new era, with wiki integrated.
This commit is contained in:
@@ -0,0 +1,759 @@
|
||||
/*
|
||||
* WiFi Manager Module Implementation
|
||||
*/
|
||||
|
||||
#include "wifi_manager.h"
|
||||
#include "display/lvgl_v8_port.h"
|
||||
|
||||
// External UI element references (from ui_Settings_Screen.c)
|
||||
// MUST be declared outside the namespace to match the global C symbols
|
||||
extern "C" {
|
||||
extern lv_obj_t *uic_Select_SSID_BTN;
|
||||
extern lv_obj_t *uic_Settings_Loading_Container;
|
||||
extern lv_obj_t *uic_Wifi_Container;
|
||||
extern lv_obj_t *uic_Toggle_Wifi_Switch;
|
||||
extern lv_obj_t *uic_Wifi_Passkey_TB;
|
||||
extern lv_obj_t *uic_Keyboard;
|
||||
}
|
||||
|
||||
namespace WiFiManager {
|
||||
|
||||
// ============================================================================
|
||||
// PRIVATE MODULE STATE
|
||||
// ============================================================================
|
||||
|
||||
// Global WiFi state variables
|
||||
static wifi_state_t g_wifi_state = WIFI_STATE_OFF;
|
||||
static TaskHandle_t g_wifi_task_handle = NULL;
|
||||
static QueueHandle_t g_wifi_event_queue = NULL;
|
||||
static SemaphoreHandle_t g_wifi_mutex = NULL;
|
||||
static wifi_network_t g_scanned_networks[WIFI_SCAN_MAX_NETWORKS];
|
||||
static uint8_t g_network_count = 0;
|
||||
static bool g_wifi_scan_requested = false;
|
||||
static unsigned long g_last_scan_time = 0;
|
||||
|
||||
// Currently selected network for connection
|
||||
static int8_t g_selected_network_index = -1;
|
||||
|
||||
// Dynamic SSID buttons
|
||||
static lv_obj_t* g_ssid_buttons[WIFI_SCAN_MAX_NETWORKS] = {NULL};
|
||||
static uint8_t g_button_count = 0;
|
||||
|
||||
// ============================================================================
|
||||
// PRIVATE HELPER FUNCTIONS
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @brief Get WiFi encryption type as string
|
||||
*/
|
||||
static const char* getAuthModeString(wifi_auth_mode_t authmode) {
|
||||
switch (authmode) {
|
||||
case WIFI_AUTH_OPEN: return "Open";
|
||||
case WIFI_AUTH_WEP: return "WEP";
|
||||
case WIFI_AUTH_WPA_PSK: return "WPA";
|
||||
case WIFI_AUTH_WPA2_PSK: return "WPA2";
|
||||
case WIFI_AUTH_WPA_WPA2_PSK: return "WPA/WPA2";
|
||||
case WIFI_AUTH_WPA2_ENTERPRISE: return "WPA2-Ent";
|
||||
case WIFI_AUTH_WPA3_PSK: return "WPA3";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get signal strength indicator
|
||||
*/
|
||||
static const char* getSignalStrength(int32_t rssi) {
|
||||
if (rssi > -50) return "[****]"; // Excellent
|
||||
else if (rssi > -60) return "[*** ]"; // Good
|
||||
else if (rssi > -70) return "[** ]"; // Fair
|
||||
else if (rssi > -80) return "[* ]"; // Weak
|
||||
else return "[ ]"; // Very weak
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Format network name for display in button
|
||||
*/
|
||||
static String formatNetworkName(const wifi_network_t& network) {
|
||||
String formatted = getSignalStrength(network.rssi);
|
||||
formatted += " ";
|
||||
formatted += network.ssid;
|
||||
if (network.ssid.length() == 0) {
|
||||
formatted += "[Hidden]";
|
||||
}
|
||||
if (network.auth != WIFI_AUTH_OPEN) {
|
||||
formatted += " [Secured]"; // Text indicator for secured networks
|
||||
}
|
||||
return formatted;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Show connection result to user
|
||||
* @note Must be called with LVGL lock held OR will acquire lock internally
|
||||
*/
|
||||
static void showConnectionResult(bool success, const char* message) {
|
||||
lvgl_port_lock(0);
|
||||
|
||||
// Hide loading spinner
|
||||
lv_obj_add_flag(uic_Settings_Loading_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
if (success) {
|
||||
// Connection successful
|
||||
Serial.printf("WiFi connected: %s\n", message);
|
||||
|
||||
// Update the roller to show connected status
|
||||
if (g_selected_network_index >= 0 && g_selected_network_index < g_network_count) {
|
||||
String connected_text = "[CONNECTED] ";
|
||||
connected_text += g_scanned_networks[g_selected_network_index].ssid;
|
||||
|
||||
// Update just the selected item in the roller
|
||||
// Note: This is a simplified approach - in production, you'd want to
|
||||
// rebuild the entire roller list with the connected indicator
|
||||
}
|
||||
|
||||
// Optionally show a success message label temporarily
|
||||
// You could create a temporary label here if needed
|
||||
|
||||
} else {
|
||||
// Connection failed - show error and return to SSID buttons
|
||||
Serial.printf("WiFi connection failed: %s\n", message);
|
||||
|
||||
// Show SSID buttons again
|
||||
for (uint8_t i = 0; i < g_button_count; i++) {
|
||||
if (g_ssid_buttons[i] != NULL) {
|
||||
lv_obj_clear_flag(g_ssid_buttons[i], LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
}
|
||||
|
||||
// Optionally show an error message label temporarily
|
||||
// You could create a temporary label here to show the error
|
||||
}
|
||||
|
||||
// Reset selected network index
|
||||
g_selected_network_index = -1;
|
||||
|
||||
lvgl_port_unlock();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reset WiFi UI to initial state (showing SSID list)
|
||||
*/
|
||||
static void resetWiFiUI() {
|
||||
lvgl_port_lock(0);
|
||||
|
||||
// Hide all password entry elements
|
||||
lv_obj_add_flag(uic_Keyboard, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(uic_Wifi_Passkey_TB, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(uic_Settings_Loading_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Clear password field for security
|
||||
lv_textarea_set_text(uic_Wifi_Passkey_TB, "");
|
||||
|
||||
// Show SSID buttons
|
||||
for (uint8_t i = 0; i < g_button_count; i++) {
|
||||
if (g_ssid_buttons[i] != NULL) {
|
||||
lv_obj_clear_flag(g_ssid_buttons[i], LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset selected network
|
||||
g_selected_network_index = -1;
|
||||
|
||||
lvgl_port_unlock();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update WiFi UI state based on state machine
|
||||
* @note This function MUST be called with lvgl_port_lock held
|
||||
*/
|
||||
static void updateWifiUIState(wifi_state_t state) {
|
||||
// This function MUST be called with lvgl_port_lock held
|
||||
|
||||
if (!uic_Select_SSID_BTN || !uic_Settings_Loading_Container || !uic_Wifi_Container) {
|
||||
Serial.println("WARNING: UI elements not initialized");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (state) {
|
||||
case WIFI_STATE_OFF:
|
||||
// Hide WiFi container and spinner
|
||||
lv_obj_add_flag(uic_Wifi_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(uic_Settings_Loading_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(uic_Select_SSID_BTN, LV_OBJ_FLAG_HIDDEN);
|
||||
break;
|
||||
|
||||
case WIFI_STATE_SCANNING:
|
||||
// Show container and spinner, hide button
|
||||
lv_obj_clear_flag(uic_Wifi_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_clear_flag(uic_Settings_Loading_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(uic_Select_SSID_BTN, LV_OBJ_FLAG_HIDDEN);
|
||||
break;
|
||||
|
||||
case WIFI_STATE_SCAN_COMPLETE:
|
||||
// Show button, hide spinner
|
||||
lv_obj_clear_flag(uic_Wifi_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(uic_Settings_Loading_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_clear_flag(uic_Select_SSID_BTN, LV_OBJ_FLAG_HIDDEN);
|
||||
break;
|
||||
|
||||
case WIFI_STATE_ERROR:
|
||||
// Show error state (container visible but no spinner or button)
|
||||
lv_obj_clear_flag(uic_Wifi_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(uic_Settings_Loading_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(uic_Select_SSID_BTN, LV_OBJ_FLAG_HIDDEN);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Update SSID list with dynamic buttons
|
||||
* @note This function MUST be called with lvgl_port_lock held
|
||||
*/
|
||||
static void updateSSIDList() {
|
||||
// This function MUST be called with lvgl_port_lock held
|
||||
|
||||
if (!uic_Wifi_Container) {
|
||||
Serial.println("ERROR: uic_Wifi_Container is NULL!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Clean up old buttons
|
||||
for (uint8_t i = 0; i < g_button_count; i++) {
|
||||
if (g_ssid_buttons[i] != NULL) {
|
||||
lv_obj_del(g_ssid_buttons[i]);
|
||||
g_ssid_buttons[i] = NULL;
|
||||
}
|
||||
}
|
||||
g_button_count = 0;
|
||||
|
||||
if (g_network_count == 0) {
|
||||
Serial.println("No WiFi networks found");
|
||||
return;
|
||||
}
|
||||
|
||||
Serial.printf("Creating %d SSID buttons:\n", g_network_count);
|
||||
|
||||
// Create a button for each scanned network
|
||||
for (uint8_t i = 0; i < g_network_count; i++) {
|
||||
// Create button in the WiFi container
|
||||
lv_obj_t* btn = lv_btn_create(uic_Wifi_Container);
|
||||
lv_obj_set_width(btn, 600); // Adjust width as needed
|
||||
lv_obj_set_height(btn, 60); // Adjust height as needed
|
||||
|
||||
// Create label for the button text
|
||||
lv_obj_t* label = lv_label_create(btn);
|
||||
String btnText = formatNetworkName(g_scanned_networks[i]);
|
||||
lv_label_set_text(label, btnText.c_str());
|
||||
lv_obj_center(label);
|
||||
|
||||
// Store network index as user data
|
||||
lv_obj_set_user_data(btn, (void*)(intptr_t)i);
|
||||
|
||||
// Add click event handler
|
||||
lv_obj_add_event_cb(btn, onSSIDButtonClick, LV_EVENT_CLICKED, NULL);
|
||||
|
||||
// Store button reference
|
||||
g_ssid_buttons[i] = btn;
|
||||
g_button_count++;
|
||||
|
||||
Serial.printf(" [%d] Created button for: %s\n", i, g_scanned_networks[i].ssid.c_str());
|
||||
}
|
||||
|
||||
Serial.printf("Successfully created %d SSID buttons\n", g_button_count);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initiate WiFi connection to selected network
|
||||
*/
|
||||
static void connectToWiFi(const char* ssid, const char* password) {
|
||||
if (ssid == NULL) return;
|
||||
|
||||
// Allocate credentials structure
|
||||
wifi_credentials_t* creds = (wifi_credentials_t*)malloc(sizeof(wifi_credentials_t));
|
||||
if (creds == NULL) {
|
||||
Serial.println("ERROR: Failed to allocate memory for WiFi credentials!");
|
||||
showConnectionResult(false, "Memory allocation failed");
|
||||
return;
|
||||
}
|
||||
|
||||
// Copy credentials
|
||||
strncpy(creds->ssid, ssid, sizeof(creds->ssid) - 1);
|
||||
creds->ssid[sizeof(creds->ssid) - 1] = '\0';
|
||||
|
||||
if (password != NULL) {
|
||||
strncpy(creds->password, password, sizeof(creds->password) - 1);
|
||||
creds->password[sizeof(creds->password) - 1] = '\0';
|
||||
} else {
|
||||
creds->password[0] = '\0'; // Empty password for open networks
|
||||
}
|
||||
|
||||
// Send connect event to WiFi task
|
||||
wifi_queue_event_t evt;
|
||||
evt.type = WIFI_UI_EVENT_CONNECT;
|
||||
evt.data = creds; // WiFi task will free this memory
|
||||
|
||||
if (g_wifi_event_queue != NULL) {
|
||||
if (xQueueSend(g_wifi_event_queue, &evt, pdMS_TO_TICKS(100)) != pdTRUE) {
|
||||
Serial.println("ERROR: Failed to send connect event to WiFi task!");
|
||||
free(creds);
|
||||
showConnectionResult(false, "Failed to initiate connection");
|
||||
}
|
||||
} else {
|
||||
Serial.println("ERROR: WiFi event queue not initialized!");
|
||||
free(creds);
|
||||
showConnectionResult(false, "System not ready");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief WiFi task - Runs on Core 0
|
||||
* @param parameter Task parameter (unused)
|
||||
*/
|
||||
static void wifiTask(void* parameter) {
|
||||
wifi_queue_event_t event;
|
||||
bool scanning = false;
|
||||
|
||||
Serial.println("WiFi task started on Core 0");
|
||||
|
||||
while (true) {
|
||||
// Check for events from the queue (non-blocking with timeout)
|
||||
if (xQueueReceive(g_wifi_event_queue, &event, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
switch (event.type) {
|
||||
case WIFI_UI_EVENT_TOGGLE_ON:
|
||||
Serial.println("WiFi Task: Enabling WiFi...");
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.disconnect(); // Clear any previous connection
|
||||
g_wifi_state = WIFI_STATE_SCANNING;
|
||||
g_wifi_scan_requested = true;
|
||||
break;
|
||||
|
||||
case WIFI_UI_EVENT_TOGGLE_OFF:
|
||||
Serial.println("WiFi Task: Disabling WiFi...");
|
||||
WiFi.disconnect(true);
|
||||
WiFi.mode(WIFI_OFF);
|
||||
g_wifi_state = WIFI_STATE_OFF;
|
||||
g_network_count = 0;
|
||||
scanning = false;
|
||||
break;
|
||||
|
||||
case WIFI_UI_EVENT_SCAN_START:
|
||||
if (g_wifi_state != WIFI_STATE_OFF && !scanning) {
|
||||
g_wifi_scan_requested = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case WIFI_UI_EVENT_CONNECT:
|
||||
if (event.data != NULL) {
|
||||
wifi_credentials_t* creds = (wifi_credentials_t*)event.data;
|
||||
Serial.printf("WiFi Task: Connecting to %s...\n", creds->ssid);
|
||||
|
||||
// Stop any ongoing scan
|
||||
WiFi.scanDelete();
|
||||
scanning = false;
|
||||
|
||||
// Update state
|
||||
g_wifi_state = WIFI_STATE_CONNECTED; // Will be updated based on result
|
||||
|
||||
// Attempt connection
|
||||
WiFi.disconnect(true); // Clear any previous connection
|
||||
delay(100);
|
||||
|
||||
if (strlen(creds->password) > 0) {
|
||||
WiFi.begin(creds->ssid, creds->password);
|
||||
} else {
|
||||
WiFi.begin(creds->ssid); // Open network
|
||||
}
|
||||
|
||||
// Wait for connection (with timeout)
|
||||
int timeout = 20; // 10 seconds timeout
|
||||
while (WiFi.status() != WL_CONNECTED && timeout > 0) {
|
||||
delay(500);
|
||||
Serial.print(".");
|
||||
timeout--;
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// Check connection result
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.printf("Connected! IP: %s\n", WiFi.localIP().toString().c_str());
|
||||
showConnectionResult(true, WiFi.localIP().toString().c_str());
|
||||
g_wifi_state = WIFI_STATE_CONNECTED;
|
||||
} else {
|
||||
Serial.println("Connection failed!");
|
||||
const char* error_msg = "Connection timeout";
|
||||
if (WiFi.status() == WL_CONNECT_FAILED) {
|
||||
error_msg = "Wrong password";
|
||||
} else if (WiFi.status() == WL_NO_SSID_AVAIL) {
|
||||
error_msg = "Network not found";
|
||||
}
|
||||
showConnectionResult(false, error_msg);
|
||||
g_wifi_state = WIFI_STATE_SCANNING;
|
||||
g_wifi_scan_requested = true; // Resume scanning
|
||||
}
|
||||
|
||||
// Free the credentials memory
|
||||
free(creds);
|
||||
}
|
||||
break;
|
||||
|
||||
case WIFI_UI_EVENT_DISCONNECT:
|
||||
Serial.println("WiFi Task: Disconnecting...");
|
||||
WiFi.disconnect(true);
|
||||
g_wifi_state = WIFI_STATE_SCANNING;
|
||||
g_wifi_scan_requested = true;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle WiFi scanning
|
||||
if (g_wifi_state != WIFI_STATE_OFF) {
|
||||
unsigned long current_time = millis();
|
||||
|
||||
// Start scan if requested or periodic scan is due
|
||||
if (g_wifi_scan_requested ||
|
||||
(current_time - g_last_scan_time > WIFI_SCAN_INTERVAL_MS && !scanning)) {
|
||||
|
||||
Serial.println("Starting WiFi scan...");
|
||||
scanning = true;
|
||||
g_wifi_scan_requested = false;
|
||||
g_last_scan_time = current_time;
|
||||
|
||||
// Start async scan
|
||||
WiFi.scanNetworks(true, false); // async=true, show_hidden=false
|
||||
|
||||
// Only show spinner if list is empty (first scan or after error)
|
||||
// For periodic rescans, keep the existing list visible
|
||||
if (g_network_count == 0) {
|
||||
lvgl_port_lock(-1);
|
||||
updateWifiUIState(WIFI_STATE_SCANNING);
|
||||
lvgl_port_unlock();
|
||||
Serial.println("Showing loading spinner (list is empty)");
|
||||
} else {
|
||||
Serial.println("Background rescan (keeping list visible)");
|
||||
}
|
||||
}
|
||||
|
||||
// Check if scan is complete
|
||||
if (scanning) {
|
||||
int16_t result = WiFi.scanComplete();
|
||||
|
||||
if (result >= 0) {
|
||||
// Scan completed successfully
|
||||
scanning = false;
|
||||
Serial.printf("WiFi scan complete: %d networks found\n", result);
|
||||
|
||||
// Lock mutex to update shared data
|
||||
if (xSemaphoreTake(g_wifi_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
g_network_count = min((int)result, WIFI_SCAN_MAX_NETWORKS);
|
||||
|
||||
// Copy scan results to our buffer
|
||||
for (uint8_t i = 0; i < g_network_count; i++) {
|
||||
g_scanned_networks[i].ssid = WiFi.SSID(i);
|
||||
g_scanned_networks[i].rssi = WiFi.RSSI(i);
|
||||
g_scanned_networks[i].auth = WiFi.encryptionType(i);
|
||||
|
||||
Serial.printf(" [%d] %s (RSSI: %d, Auth: %s)\n",
|
||||
i,
|
||||
g_scanned_networks[i].ssid.c_str(),
|
||||
g_scanned_networks[i].rssi,
|
||||
getAuthModeString(g_scanned_networks[i].auth));
|
||||
}
|
||||
|
||||
xSemaphoreGive(g_wifi_mutex);
|
||||
|
||||
// Update UI with scan results
|
||||
g_wifi_state = WIFI_STATE_SCAN_COMPLETE;
|
||||
lvgl_port_lock(-1);
|
||||
updateSSIDList();
|
||||
updateWifiUIState(WIFI_STATE_SCAN_COMPLETE);
|
||||
lvgl_port_unlock();
|
||||
}
|
||||
|
||||
// Delete scan results to free memory
|
||||
WiFi.scanDelete();
|
||||
|
||||
} else if (result == WIFI_SCAN_FAILED) {
|
||||
// Scan failed
|
||||
scanning = false;
|
||||
Serial.println("WiFi scan failed!");
|
||||
g_wifi_state = WIFI_STATE_ERROR;
|
||||
|
||||
lvgl_port_lock(-1);
|
||||
updateWifiUIState(WIFI_STATE_ERROR);
|
||||
lvgl_port_unlock();
|
||||
}
|
||||
// If result == WIFI_SCAN_RUNNING, scan is still in progress
|
||||
}
|
||||
}
|
||||
|
||||
// Small delay to prevent task from hogging CPU
|
||||
vTaskDelay(pdMS_TO_TICKS(50));
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// PUBLIC API IMPLEMENTATION
|
||||
// ============================================================================
|
||||
|
||||
void init(bool auto_start) {
|
||||
Serial.println("[WiFiManager] Initializing WiFi subsystem...");
|
||||
|
||||
// Create mutex for protecting shared WiFi data
|
||||
g_wifi_mutex = xSemaphoreCreateMutex();
|
||||
if (g_wifi_mutex == NULL) {
|
||||
Serial.println("[WiFiManager] ERROR: Failed to create WiFi mutex!");
|
||||
}
|
||||
|
||||
// Create event queue for WiFi task communication
|
||||
g_wifi_event_queue = xQueueCreate(10, sizeof(wifi_queue_event_t));
|
||||
if (g_wifi_event_queue == NULL) {
|
||||
Serial.println("[WiFiManager] ERROR: Failed to create WiFi event queue!");
|
||||
}
|
||||
|
||||
// Create WiFi task on Core 0
|
||||
BaseType_t task_created = xTaskCreatePinnedToCore(
|
||||
wifiTask, // Task function
|
||||
"WiFi_Task", // Task name
|
||||
WIFI_TASK_STACK_SIZE, // Stack size
|
||||
NULL, // Parameters
|
||||
WIFI_TASK_PRIORITY, // Priority (1)
|
||||
&g_wifi_task_handle, // Task handle
|
||||
WIFI_TASK_CORE // Core 0
|
||||
);
|
||||
|
||||
if (task_created != pdPASS) {
|
||||
Serial.println("[WiFiManager] ERROR: Failed to create WiFi task!");
|
||||
} else {
|
||||
Serial.println("[WiFiManager] WiFi task created successfully on Core 0");
|
||||
}
|
||||
|
||||
// Auto-start WiFi if requested
|
||||
if (auto_start) {
|
||||
Serial.println("[WiFiManager] Auto-starting WiFi...");
|
||||
wifi_queue_event_t evt;
|
||||
evt.type = WIFI_UI_EVENT_TOGGLE_ON;
|
||||
evt.data = NULL;
|
||||
xQueueSend(g_wifi_event_queue, &evt, 0);
|
||||
} else {
|
||||
WiFi.mode(WIFI_OFF);
|
||||
Serial.println("[WiFiManager] WiFi initially disabled");
|
||||
}
|
||||
}
|
||||
|
||||
wifi_state_t getState() {
|
||||
return g_wifi_state;
|
||||
}
|
||||
|
||||
uint8_t getNetworkCount() {
|
||||
return g_network_count;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// EVENT HANDLERS
|
||||
// ============================================================================
|
||||
|
||||
void onWifiToggle(lv_event_t *e)
|
||||
{
|
||||
// Get the actual widget from the event (safer than global variable)
|
||||
lv_obj_t *target = lv_event_get_target(e);
|
||||
|
||||
// Verify the widget is valid
|
||||
if (target == NULL) {
|
||||
Serial.println("ERROR: WiFi toggle target is NULL!");
|
||||
Serial.flush();
|
||||
return;
|
||||
}
|
||||
|
||||
// Check the toggle state
|
||||
bool state = lv_obj_has_state(target, LV_STATE_CHECKED);
|
||||
|
||||
Serial.printf("WiFi Toggle RELEASED - State: %s\n", state ? "ON" : "OFF");
|
||||
Serial.flush();
|
||||
|
||||
// Send event to WiFi task via queue
|
||||
wifi_queue_event_t evt;
|
||||
if (state) {
|
||||
evt.type = WIFI_UI_EVENT_TOGGLE_ON;
|
||||
|
||||
// Show loading state immediately
|
||||
updateWifiUIState(WIFI_STATE_SCANNING);
|
||||
} else {
|
||||
evt.type = WIFI_UI_EVENT_TOGGLE_OFF;
|
||||
|
||||
// Hide WiFi UI immediately
|
||||
updateWifiUIState(WIFI_STATE_OFF);
|
||||
}
|
||||
evt.data = NULL;
|
||||
|
||||
// Send event to WiFi task (non-blocking)
|
||||
if (g_wifi_event_queue != NULL) {
|
||||
xQueueSend(g_wifi_event_queue, &evt, 0);
|
||||
} else {
|
||||
Serial.println("ERROR: WiFi event queue not initialized!");
|
||||
}
|
||||
}
|
||||
|
||||
void onSSIDButtonClick(lv_event_t *e) {
|
||||
lv_obj_t* btn = lv_event_get_target(e);
|
||||
if (btn == NULL) {
|
||||
Serial.println("ERROR: Button target is NULL!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the network index from user data
|
||||
int8_t network_idx = (int8_t)(intptr_t)lv_obj_get_user_data(btn);
|
||||
|
||||
if (network_idx < 0 || network_idx >= g_network_count) {
|
||||
Serial.printf("ERROR: Invalid network index: %d\n", network_idx);
|
||||
return;
|
||||
}
|
||||
|
||||
g_selected_network_index = network_idx;
|
||||
Serial.printf("===== SSID Button Clicked: %s =====\n",
|
||||
g_scanned_networks[network_idx].ssid.c_str());
|
||||
|
||||
// Verify UI widgets exist
|
||||
if (uic_Wifi_Passkey_TB == NULL || uic_Keyboard == NULL) {
|
||||
Serial.println("ERROR: Password UI widgets are NULL!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get LVGL lock to update UI
|
||||
lvgl_port_lock(0);
|
||||
|
||||
// Hide all SSID buttons
|
||||
for (uint8_t i = 0; i < g_button_count; i++) {
|
||||
if (g_ssid_buttons[i] != NULL) {
|
||||
lv_obj_add_flag(g_ssid_buttons[i], LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if network requires password
|
||||
if (g_scanned_networks[network_idx].auth == WIFI_AUTH_OPEN) {
|
||||
// Open network - connect directly without password
|
||||
Serial.println("Open network - connecting without password...");
|
||||
|
||||
// Show loading spinner
|
||||
lv_obj_clear_flag(uic_Settings_Loading_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
lvgl_port_unlock();
|
||||
|
||||
// Connect to open network
|
||||
connectToWiFi(g_scanned_networks[network_idx].ssid.c_str(), NULL);
|
||||
|
||||
} else {
|
||||
// Secured network - show password entry
|
||||
Serial.println("Secured network - showing password entry UI...");
|
||||
|
||||
// Configure and show password textbox
|
||||
lv_textarea_set_text(uic_Wifi_Passkey_TB, "");
|
||||
lv_textarea_set_placeholder_text(uic_Wifi_Passkey_TB,
|
||||
g_scanned_networks[network_idx].ssid.c_str());
|
||||
lv_textarea_set_password_mode(uic_Wifi_Passkey_TB, true);
|
||||
lv_textarea_set_one_line(uic_Wifi_Passkey_TB, true);
|
||||
lv_obj_clear_flag(uic_Wifi_Passkey_TB, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Configure and show keyboard
|
||||
lv_keyboard_set_textarea(uic_Keyboard, uic_Wifi_Passkey_TB);
|
||||
lv_keyboard_set_mode(uic_Keyboard, LV_KEYBOARD_MODE_TEXT_LOWER);
|
||||
lv_obj_clear_flag(uic_Keyboard, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Add event handler for keyboard (if not already added)
|
||||
static bool keyboard_handler_added = false;
|
||||
if (!keyboard_handler_added) {
|
||||
lv_obj_add_event_cb(uic_Keyboard, onKeyboardEvent, LV_EVENT_ALL, NULL);
|
||||
keyboard_handler_added = true;
|
||||
Serial.println("Keyboard event handler registered");
|
||||
}
|
||||
|
||||
lvgl_port_unlock();
|
||||
|
||||
Serial.println("Password entry UI shown successfully");
|
||||
}
|
||||
}
|
||||
|
||||
void onKeyboardEvent(lv_event_t *e) {
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
lv_obj_t *keyboard = lv_event_get_target(e);
|
||||
|
||||
if (code == LV_EVENT_READY || code == LV_EVENT_CANCEL) {
|
||||
// Get LVGL lock for UI updates
|
||||
lvgl_port_lock(0);
|
||||
|
||||
if (code == LV_EVENT_READY) {
|
||||
// User pressed checkmark/enter - attempt connection
|
||||
const char *password = lv_textarea_get_text(uic_Wifi_Passkey_TB);
|
||||
|
||||
if (xSemaphoreTake(g_wifi_mutex, pdMS_TO_TICKS(100)) == pdTRUE) {
|
||||
if (g_selected_network_index >= 0 && g_selected_network_index < g_network_count) {
|
||||
Serial.printf("Connecting to: %s with password: %s\n",
|
||||
g_scanned_networks[g_selected_network_index].ssid.c_str(),
|
||||
password);
|
||||
|
||||
// Hide keyboard and password field
|
||||
lv_obj_add_flag(uic_Keyboard, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(uic_Wifi_Passkey_TB, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Show loading spinner
|
||||
lv_obj_clear_flag(uic_Settings_Loading_Container, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Trigger WiFi connection
|
||||
connectToWiFi(g_scanned_networks[g_selected_network_index].ssid.c_str(), password);
|
||||
|
||||
// Clear sensitive password data from text area
|
||||
lv_textarea_set_text(uic_Wifi_Passkey_TB, "");
|
||||
}
|
||||
xSemaphoreGive(g_wifi_mutex);
|
||||
}
|
||||
|
||||
} else if (code == LV_EVENT_CANCEL) {
|
||||
// User pressed X/cancel - go back to SSID list
|
||||
Serial.println("Password entry cancelled - returning to SSID list");
|
||||
|
||||
// Hide keyboard and password field
|
||||
lv_obj_add_flag(uic_Keyboard, LV_OBJ_FLAG_HIDDEN);
|
||||
lv_obj_add_flag(uic_Wifi_Passkey_TB, LV_OBJ_FLAG_HIDDEN);
|
||||
|
||||
// Clear password field for security
|
||||
lv_textarea_set_text(uic_Wifi_Passkey_TB, "");
|
||||
|
||||
// Show SSID buttons again
|
||||
for (uint8_t i = 0; i < g_button_count; i++) {
|
||||
if (g_ssid_buttons[i] != NULL) {
|
||||
lv_obj_clear_flag(g_ssid_buttons[i], LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
}
|
||||
|
||||
// Reset selected network index
|
||||
g_selected_network_index = -1;
|
||||
}
|
||||
|
||||
lvgl_port_unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void onRescanRequest(lv_event_t *e) {
|
||||
if (g_wifi_state != WIFI_STATE_OFF) {
|
||||
Serial.println("Manual rescan requested");
|
||||
|
||||
wifi_queue_event_t evt;
|
||||
evt.type = WIFI_UI_EVENT_SCAN_START;
|
||||
evt.data = NULL;
|
||||
|
||||
if (g_wifi_event_queue != NULL) {
|
||||
xQueueSend(g_wifi_event_queue, &evt, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void onSelectSSID(lv_event_t *e) {
|
||||
// This function is called from SquareLine Studio but we now use dynamic buttons
|
||||
Serial.println("onSelectSSID() called - but dynamic buttons handle clicks now");
|
||||
}
|
||||
|
||||
} // namespace WiFiManager
|
||||
Reference in New Issue
Block a user