-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDeviceState.h
More file actions
361 lines (300 loc) · 12.9 KB
/
Copy pathDeviceState.h
File metadata and controls
361 lines (300 loc) · 12.9 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#ifndef DEVICE_STATE_H
#define DEVICE_STATE_H
#include <Arduino.h>
// ============================================================================
// Device Operating States
// ============================================================================
enum class DeviceState {
INITIALIZING, // During boot, hardware setup
CONNECTING_WIFI, // WiFi connection in progress
READY, // Ready for payments (normal operation)
RECEIVING_PAYMENT, // QR code displayed, awaiting payment
SCREENSAVER, // Screensaver active
HELP_SCREEN, // Help pages displayed
REPORT_SCREEN, // Report/Log screen
CONFIG_MODE, // Configuration editor
ERROR_CRITICAL, // Critical error (WiFi down, API unavailable)
ERROR_RECOVERABLE, // Recoverable error (temporary issue)
DEEP_SLEEP, // Device in deep sleep
PRODUCT_SELECTION, // Multi-product selection screen
BTC_TICKER // Bitcoin ticker display
};
// ============================================================================
// WiFi Connectivity State (Orthogonal to Device State)
// ============================================================================
enum class WiFiState {
DISCONNECTED, // Not connected
CONNECTING, // Attempting connection
CONNECTED, // Connected and ready
ERROR // Connection error
};
// ============================================================================
// State Manager - Central State Machine Handler
// ============================================================================
class StateManager {
private:
DeviceState currentState;
DeviceState previousState;
unsigned long stateEnteredTime;
WiFiState wifiState;
unsigned long wifiStateChangedTime;
public:
StateManager()
: currentState(DeviceState::INITIALIZING),
previousState(DeviceState::INITIALIZING),
stateEnteredTime(millis()),
wifiState(WiFiState::DISCONNECTED),
wifiStateChangedTime(millis()) {}
// ========================================================================
// Main State Management
// ========================================================================
/**
* Transition to new device state with validation and callbacks
* @param newState Target device state
* @return true if transition successful, false if invalid
*/
bool transition(DeviceState newState) {
// No-op if already in this state
if (currentState == newState) return true;
// Validate transition
if (!isValidTransition(currentState, newState)) {
// Silently reject during CONFIG_MODE (loop() keeps trying - expected)
if (currentState != DeviceState::CONFIG_MODE) {
Serial.printf("[STATE_ERROR] Invalid transition: %s -> %s\n",
getDeviceStateName(currentState),
getDeviceStateName(newState));
}
return false;
}
// Log transition (suppress for CONFIG_MODE to keep serial clean during config handshake)
if (newState != DeviceState::CONFIG_MODE) {
Serial.printf("[STATE_TRANSITION] %s -> %s\n",
getDeviceStateName(currentState),
getDeviceStateName(newState));
}
// Suppress invalid transition error output during CONFIG_MODE
// (loop() will keep trying to transition - that's fine, we just block it silently)
// Execute exit callback for previous state
onStateExit(currentState);
// Update state
previousState = currentState;
currentState = newState;
stateEnteredTime = millis();
// Execute entry callback for new state
onStateEnter(currentState);
return true;
}
/**
* Get current device state
*/
DeviceState getState() const { return currentState; }
/**
* Get previous device state
*/
DeviceState getPreviousState() const { return previousState; }
/**
* Check if currently in specific state
*/
bool isInState(DeviceState state) const { return currentState == state; }
/**
* Get time spent in current state (milliseconds)
*/
unsigned long timeInState() const { return millis() - stateEnteredTime; }
/**
* Get human-readable state name for logging
*/
const char* getDeviceStateName(DeviceState state) const {
switch (state) {
case DeviceState::INITIALIZING: return "INITIALIZING";
case DeviceState::CONNECTING_WIFI: return "CONNECTING_WIFI";
case DeviceState::READY: return "READY";
case DeviceState::RECEIVING_PAYMENT: return "RECEIVING_PAYMENT";
case DeviceState::SCREENSAVER: return "SCREENSAVER";
case DeviceState::HELP_SCREEN: return "HELP_SCREEN";
case DeviceState::REPORT_SCREEN: return "REPORT_SCREEN";
case DeviceState::CONFIG_MODE: return "CONFIG_MODE";
case DeviceState::ERROR_CRITICAL: return "ERROR_CRITICAL";
case DeviceState::ERROR_RECOVERABLE: return "ERROR_RECOVERABLE";
case DeviceState::DEEP_SLEEP: return "DEEP_SLEEP";
case DeviceState::PRODUCT_SELECTION: return "PRODUCT_SELECTION";
case DeviceState::BTC_TICKER: return "BTC_TICKER";
default: return "UNKNOWN";
}
}
// ========================================================================
// WiFi State Management (Orthogonal to Device State)
// ========================================================================
/**
* Update WiFi connection state
* Automatically triggers device state transitions on connection loss/gain
*/
void updateWiFiState(WiFiState newWiFiState) {
if (wifiState == newWiFiState) return;
// Ignore WiFi events during CONFIG_MODE - WiFi is intentionally off
if (currentState == DeviceState::CONFIG_MODE) return;
Serial.printf("[WiFi] %s -> %s\n",
getWiFiStateName(wifiState),
getWiFiStateName(newWiFiState));
wifiState = newWiFiState;
wifiStateChangedTime = millis();
// WiFi lost during critical states
if (newWiFiState == WiFiState::DISCONNECTED ||
newWiFiState == WiFiState::ERROR) {
if (currentState == DeviceState::READY ||
currentState == DeviceState::RECEIVING_PAYMENT ||
currentState == DeviceState::BTC_TICKER) {
Serial.println("[STATE] WiFi lost - transitioning to CONNECTING_WIFI");
transition(DeviceState::CONNECTING_WIFI);
}
}
// WiFi restored
if (newWiFiState == WiFiState::CONNECTED) {
if (currentState == DeviceState::CONNECTING_WIFI) {
Serial.println("[STATE] WiFi reconnected - returning to READY");
transition(DeviceState::READY);
}
}
}
/**
* Get current WiFi state
*/
WiFiState getWiFiState() const { return wifiState; }
/**
* Check if WiFi is connected
*/
bool isWiFiConnected() const { return wifiState == WiFiState::CONNECTED; }
/**
* Check if WiFi has issues
*/
bool hasWiFiError() const { return wifiState == WiFiState::ERROR; }
/**
* Get human-readable WiFi state name
*/
const char* getWiFiStateName(WiFiState state) const {
switch (state) {
case WiFiState::DISCONNECTED: return "DISCONNECTED";
case WiFiState::CONNECTING: return "CONNECTING";
case WiFiState::CONNECTED: return "CONNECTED";
case WiFiState::ERROR: return "ERROR";
default: return "UNKNOWN";
}
}
/**
* Get time spent in current WiFi state
*/
unsigned long wifiStateAge() const { return millis() - wifiStateChangedTime; }
private:
// ========================================================================
// Transition Validation
// ========================================================================
/**
* Define valid transitions between states
* Prevents invalid state changes
*/
bool isValidTransition(DeviceState from, DeviceState to) const {
// Can't transition FROM these states at all
if (from == DeviceState::DEEP_SLEEP) {
// Can only wake from deep sleep via initialization
return to == DeviceState::INITIALIZING;
}
// CONFIG_MODE is UNBREAKABLE - only ESP.restart() exits it
// (restart resets to INITIALIZING automatically)
if (from == DeviceState::CONFIG_MODE) {
return to == DeviceState::INITIALIZING;
}
// Can't transition TO DEEP_SLEEP except from READY or SCREENSAVER
if (to == DeviceState::DEEP_SLEEP) {
return from == DeviceState::READY || from == DeviceState::SCREENSAVER;
}
// WiFi failure can jump to CONNECTING_WIFI from most states
if (to == DeviceState::CONNECTING_WIFI) {
// But NOT from CONFIG_MODE, ERROR_CRITICAL, or DEEP_SLEEP
return from != DeviceState::CONFIG_MODE &&
from != DeviceState::ERROR_CRITICAL &&
from != DeviceState::DEEP_SLEEP;
}
// Can't escape ERROR_CRITICAL except via transition to INITIALIZING
if (from == DeviceState::ERROR_CRITICAL) {
return to == DeviceState::INITIALIZING;
}
// Most other transitions are valid
return true;
}
// ========================================================================
// State Callbacks - Execute on State Entry/Exit
// ========================================================================
/**
* Called when exiting a state
* Used for cleanup and state-specific shutdown
*/
void onStateExit(DeviceState state) {
switch (state) {
case DeviceState::CONFIG_MODE:
// Config mode cleanup handled by handleConfigExitButtons()
Serial.println("[STATE_EXIT] CONFIG_MODE");
break;
case DeviceState::SCREENSAVER:
Serial.println("[STATE_EXIT] SCREENSAVER");
break;
case DeviceState::DEEP_SLEEP:
Serial.println("[STATE_EXIT] DEEP_SLEEP");
break;
case DeviceState::HELP_SCREEN:
Serial.println("[STATE_EXIT] HELP_SCREEN");
break;
case DeviceState::REPORT_SCREEN:
Serial.println("[STATE_EXIT] REPORT_SCREEN");
break;
default:
break;
}
}
/**
* Called when entering a new state
* Used for state-specific initialization
* Actual hardware operations (display update, etc.) must be called from main.cpp
*/
void onStateEnter(DeviceState state) {
switch (state) {
case DeviceState::READY:
Serial.println("[STATE_ENTRY] READY - display QR code");
// Actual display update done in main loop
break;
case DeviceState::CONFIG_MODE:
// Suppress serial output - executeConfig() handles all paced output
break;
case DeviceState::HELP_SCREEN:
Serial.println("[STATE_ENTRY] HELP_SCREEN - showing help pages");
break;
case DeviceState::CONNECTING_WIFI:
Serial.println("[STATE_ENTRY] CONNECTING_WIFI - showing connection screen");
break;
case DeviceState::SCREENSAVER:
Serial.println("[STATE_ENTRY] SCREENSAVER - display off");
break;
case DeviceState::DEEP_SLEEP:
Serial.println("[STATE_ENTRY] DEEP_SLEEP - entering deep sleep");
break;
case DeviceState::ERROR_CRITICAL:
case DeviceState::ERROR_RECOVERABLE:
Serial.println("[STATE_ENTRY] ERROR - showing error screen");
break;
case DeviceState::PRODUCT_SELECTION:
Serial.println("[STATE_ENTRY] PRODUCT_SELECTION");
break;
case DeviceState::BTC_TICKER:
Serial.println("[STATE_ENTRY] BTC_TICKER");
break;
case DeviceState::REPORT_SCREEN:
Serial.println("[STATE_ENTRY] REPORT_SCREEN");
break;
case DeviceState::RECEIVING_PAYMENT:
Serial.println("[STATE_ENTRY] RECEIVING_PAYMENT");
break;
default:
Serial.printf("[STATE_ENTRY] %s\n", getDeviceStateName(state));
break;
}
}
};
#endif // DEVICE_STATE_H