wangzhibo
2026-10-08 220e4fab27b7f419b3e89e2f2d28ae789aac09d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef WIFI_CONFIG_UI_H
#define WIFI_CONFIG_UI_H
 
#include "tca8418_keyboard.h"
#include "display/lcd_display.h"
#include <string>
#include <vector>
#include <functional>
 
// WiFi scan result structure
struct WifiScanResult {
    std::string ssid;
    int8_t rssi;
    bool is_encrypted;
};
 
// WiFi configuration UI state machine
enum class WifiConfigState {
    Scanning,       // Scanning for WiFi networks
    SelectWifi,     // Selecting from WiFi list
    InputPassword,  // Entering password for selected WiFi
    InputSsid,      // Manual SSID input
    InputManualPwd, // Manual password input (after SSID)
    SavedList,      // Viewing saved WiFi list
    Connecting,     // Connecting to WiFi
    Success,        // Connection successful
    Failed          // Connection failed
};
 
// Result of WiFi configuration
enum class WifiConfigResult {
    None,           // Still in progress
    Connected,      // Successfully connected
    Cancelled       // User cancelled
};
 
class WifiConfigUI {
public:
    using ConnectCallback = std::function<void(const std::string& ssid, const std::string& password)>;
 
    WifiConfigUI(LcdDisplay* display);
    ~WifiConfigUI();
 
    // Start the WiFi configuration UI
    void Start();
 
    // Start directly with saved WiFi list
    void StartWithSavedList();
 
    // Handle keyboard events, returns result
    WifiConfigResult HandleKeyEvent(const KeyEvent& event);
 
    // Set callback for when connection should be attempted
    void SetConnectCallback(ConnectCallback callback) { connect_callback_ = callback; }
 
    // Notify connection result
    void OnConnectResult(bool success);
 
    // Check if UI is active
    bool IsActive() const { return is_active_; }
 
    // Update cursor blink state (call periodically from main loop)
    void UpdateCursor();
 
private:
    LcdDisplay* display_;
    WifiConfigState state_;
    bool is_active_;
    ConnectCallback connect_callback_;
 
    // WiFi scan results
    std::vector<WifiScanResult> scan_results_;
    int selected_index_;
    int scroll_offset_;
 
    // Saved WiFi list
    std::vector<std::pair<std::string, std::string>> saved_wifi_list_;
    int saved_selected_index_;
    int saved_scroll_offset_;
 
    // Input buffers
    std::string input_ssid_;
    std::string input_password_;
    std::string selected_ssid_;
    bool input_focus_on_password_;  // For manual input: true = password field, false = ssid field
 
    // Cursor blinking
    bool cursor_visible_;
    uint32_t last_cursor_toggle_;
    static constexpr uint32_t CURSOR_BLINK_MS = 500;
 
    // Display constants
    static constexpr int MAX_VISIBLE_ITEMS = 4;
    static constexpr int MAX_INPUT_LENGTH = 64;
 
    // State handlers
    void StartScanning();
    void ShowScanResults();
    void ShowPasswordInput();
    void ShowManualInput();
    void ShowSavedList();
    void ShowConnecting();
    void ShowSuccess();
    void ShowFailed();
 
    // Redraw functions (don't reset state/input)
    void RedrawPasswordInput();
    void RedrawManualInput();
 
    // Input handlers
    void HandleScanningKey(const KeyEvent& event);
    void HandleSelectWifiKey(const KeyEvent& event);
    void HandlePasswordInputKey(const KeyEvent& event);
    void HandleManualInputKey(const KeyEvent& event);
    void HandleSavedListKey(const KeyEvent& event);
    void HandleConnectingKey(const KeyEvent& event);
    void HandleResultKey(const KeyEvent& event);
 
    // Helper functions
    void DrawHeader(const char* title);
    void DrawFooter(const char* hint);
    void DrawInputField(const char* label, const std::string& value, bool is_password, bool is_active);
    void DrawWifiList(const std::vector<WifiScanResult>& list, int selected, int scroll_offset);
    void DrawSavedWifiList();
    std::string GetSignalBars(int8_t rssi);
    void LoadSavedWifiList();
    void SaveWifiCredentials(const std::string& ssid, const std::string& password);
    void DeleteSavedWifi(int index);
    void DoWifiScan();
    void AttemptConnection();
};
 
#endif // WIFI_CONFIG_UI_H