Init new era, with wiki integrated.
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* 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
|
||||
*/
|
||||
|
||||
#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_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)
|
||||
|
||||
// ============================================================================
|
||||
// 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;
|
||||
|
||||
/**
|
||||
* @brief WiFi scan result structure
|
||||
*/
|
||||
typedef struct {
|
||||
String ssid;
|
||||
int32_t rssi;
|
||||
wifi_auth_mode_t auth;
|
||||
} wifi_network_t;
|
||||
|
||||
/**
|
||||
* @brief WiFi connection credentials
|
||||
*/
|
||||
typedef struct {
|
||||
char ssid[32];
|
||||
char password[64];
|
||||
} wifi_credentials_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;
|
||||
|
||||
/**
|
||||
* @brief WiFi queue event structure
|
||||
*/
|
||||
typedef struct {
|
||||
wifi_ui_event_type_t type;
|
||||
void* data;
|
||||
} wifi_queue_event_t;
|
||||
|
||||
// ============================================================================
|
||||
// PUBLIC API
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Get current WiFi state
|
||||
* @return Current wifi_state_t value
|
||||
*/
|
||||
wifi_state_t getState();
|
||||
|
||||
/**
|
||||
* @brief Get WiFi network count from last scan
|
||||
* @return Number of networks found
|
||||
*/
|
||||
uint8_t getNetworkCount();
|
||||
|
||||
// ============================================================================
|
||||
// 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
|
||||
Reference in New Issue
Block a user