/* * UI State Manager Module Implementation */ #include "ui_state_manager.h" #include "display/lvgl_v8_port.h" #include "settings_manager.h" #include "wifi_manager.h" #include "uart_manager.h" #include "drivers/can_driver.h" // External UI element references (from ui_Settings_Screen.c) extern "C" { extern lv_obj_t *uic_Main_Container; extern lv_obj_t *uic_Toggle_Wifi_Switch; extern lv_obj_t *uic_Toggle_UART_Switch; extern lv_obj_t *uic_Toggle_CAN_Bus_Switch; extern lv_obj_t *uic_Toggle_Debug_Switch; } namespace UIStateManager { // ============================================================================ // PRIVATE MODULE STATE // ============================================================================ // Track which screen instance we synced (to prevent redundant syncing) static lv_obj_t* g_last_synced_screen = NULL; // Debug mode state static bool g_debug_enabled = false; // ============================================================================ // PUBLIC API IMPLEMENTATION // ============================================================================ void init() { Serial.println("[UIStateManager] Initialized"); g_last_synced_screen = NULL; g_debug_enabled = false; } void applySavedStatesToUI() { // Wait for UI widgets to be created if (uic_Toggle_Wifi_Switch == NULL || uic_Toggle_UART_Switch == NULL || uic_Toggle_CAN_Bus_Switch == NULL) { // Reset last synced screen when widgets are NULL (screen unloaded) g_last_synced_screen = NULL; return; // Settings screen not loaded yet } if (uic_Main_Container == NULL) { return; // UI not ready } // Get the Settings screen pointer (parent of Main Container) lv_obj_t* settings_screen = lv_obj_get_parent(uic_Main_Container); if (settings_screen == NULL) { return; } // Check if we've already synced this screen instance if (g_last_synced_screen == settings_screen) { return; // Already synced this instance } Serial.println("Settings screen detected - syncing toggle states with services..."); // Sync UI with the actual running state of services bool current_wifi_state = WiFiManager::getState() != WiFiManager::WIFI_STATE_OFF; bool current_uart_state = UARTManager::getState() != UARTManager::UART_STATE_OFF; bool current_can_state = CANDriver::isActive(); bool current_debug_state = g_debug_enabled; lvgl_port_lock(-1); // The SquareLine project ships the WiFi switch with LV_STATE_DISABLED (since // the first export), which swallows every touch. Wi-Fi is needed to join a // network for OTA updates, so re-enable it here - survives re-exports. lv_obj_clear_state(uic_Toggle_Wifi_Switch, LV_STATE_DISABLED); // Apply WiFi state if (current_wifi_state) { lv_obj_add_state(uic_Toggle_Wifi_Switch, LV_STATE_CHECKED); } else { lv_obj_clear_state(uic_Toggle_Wifi_Switch, LV_STATE_CHECKED); } // Apply UART state if (current_uart_state) { lv_obj_add_state(uic_Toggle_UART_Switch, LV_STATE_CHECKED); } else { lv_obj_clear_state(uic_Toggle_UART_Switch, LV_STATE_CHECKED); } // Apply CAN Bus state if (current_can_state) { lv_obj_add_state(uic_Toggle_CAN_Bus_Switch, LV_STATE_CHECKED); } else { lv_obj_clear_state(uic_Toggle_CAN_Bus_Switch, LV_STATE_CHECKED); } // Apply Debug state if (uic_Toggle_Debug_Switch != NULL) { if (current_debug_state) { lv_obj_add_state(uic_Toggle_Debug_Switch, LV_STATE_CHECKED); } else { lv_obj_clear_state(uic_Toggle_Debug_Switch, LV_STATE_CHECKED); } } lvgl_port_unlock(); // Mark this screen instance as synced g_last_synced_screen = settings_screen; Serial.printf("Toggle states synced: WiFi=%s, UART=%s, CAN=%s, Debug=%s\n", current_wifi_state ? "ON" : "OFF", current_uart_state ? "ON" : "OFF", current_can_state ? "ON" : "OFF", current_debug_state ? "ON" : "OFF"); } void setDebugEnabled(bool enabled) { g_debug_enabled = enabled; Serial.printf("[UIStateManager] Debug mode %s\n", enabled ? "ENABLED" : "DISABLED"); // Notify UART manager to include debug flag in transmitted packets UARTManager::setDebugEnabled(enabled); lvgl_port_lock(-1); #if LV_USE_PERF_MONITOR // Enable/disable LVGL performance monitor (FPS, CPU) lv_obj_t* perf_monitor = lv_disp_get_layer_sys(NULL); if (perf_monitor) { if (enabled) { lv_obj_clear_flag(perf_monitor, LV_OBJ_FLAG_HIDDEN); } else { lv_obj_add_flag(perf_monitor, LV_OBJ_FLAG_HIDDEN); } } #endif #if LV_USE_MEM_MONITOR // Enable/disable LVGL memory monitor // Note: Memory monitor visibility is controlled by the same mechanism #endif lvgl_port_unlock(); } bool isDebugEnabled() { return g_debug_enabled; } // ============================================================================ // EVENT HANDLERS // ============================================================================ void onDebugToggle(lv_event_t *e) { lv_obj_t *target = lv_event_get_target(e); if (target == NULL) { Serial.println("ERROR: Debug toggle target is NULL!"); return; } bool state = lv_obj_has_state(target, LV_STATE_CHECKED); Serial.printf("===== Debug Toggle Event =====\n"); Serial.printf("Debug Mode: %s\n", state ? "ON" : "OFF"); // Save state to NVS SettingsManager::saveDebugState(state); // Update global state and enable/disable monitors setDebugEnabled(state); Serial.printf("==============================\n"); } } // namespace UIStateManager