-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonManager.ino
More file actions
421 lines (357 loc) · 12.2 KB
/
ButtonManager.ino
File metadata and controls
421 lines (357 loc) · 12.2 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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// button_manager.ino - Button Handling and Debouncing for Songbird
// Part of USB Audio Loopback Demo
//
// This module handles all button input processing including:
// - Hardware debouncing for reliable button detection
// - State machine management for system states
// - Non-blocking button scanning
// - Edge detection for button press/release events
#include "config.h"
// =============================================================================
// BUTTON DEFINITIONS
// =============================================================================
// Button information array - makes it easy to add/remove buttons
ButtonInfo buttons[] = {
{BTN_UP_PIN, BUTTON_IDLE, BUTTON_IDLE, 0, false, false, "UP"},
{BTN_DOWN_PIN, BUTTON_IDLE, BUTTON_IDLE, 0, false, false, "DOWN"},
{BTN_LEFT_PIN, BUTTON_IDLE, BUTTON_IDLE, 0, false, false, "LEFT"},
{BTN_RIGHT_PIN, BUTTON_IDLE, BUTTON_IDLE, 0, false, false, "RIGHT"}
};
const int NUM_BUTTONS = sizeof(buttons) / sizeof(buttons[0]);
// Button scanning timing
unsigned long last_button_scan = 0;
// Button system status
bool button_system_ready = false;
// =============================================================================
// BUTTON INITIALIZATION
// =============================================================================
/**
* Initialize the button input system
* Sets up GPIO pins with internal pullups and initializes button states
* Must be called once during system startup
*/
void setupButtons() {
Serial.println("Initializing Songbird button system...");
// Configure all button pins as inputs with internal pullups
for (int i = 0; i < NUM_BUTTONS; i++) {
pinMode(buttons[i].pin, INPUT_PULLUP);
// Initialize button states
buttons[i].current_state = BUTTON_IDLE;
buttons[i].previous_state = BUTTON_IDLE;
buttons[i].last_change_time = 0;
buttons[i].physical_state = false;
buttons[i].debounced_state = false;
Serial.print("Button ");
Serial.print(buttons[i].name);
Serial.print(" configured on pin ");
Serial.println(buttons[i].pin);
}
button_system_ready = true;
Serial.println("Button system initialized successfully");
}
// =============================================================================
// BUTTON SCANNING AND DEBOUNCING
// =============================================================================
/**
* Scan all buttons and update their states
* Implements proper debouncing to prevent false triggers
* Should be called regularly (20Hz) for responsive button feel
*/
void scanButtons() {
// Check if it's time to scan buttons
if (millis() - last_button_scan < BUTTON_SCAN_INTERVAL_MS) {
return; // Too soon for next scan
}
if (!button_system_ready) {
return; // Button system not initialized
}
last_button_scan = millis();
// Process each button
for (int i = 0; i < NUM_BUTTONS; i++) {
processButton(&buttons[i]);
}
}
/**
* Process a single button's state including debouncing
* @param button Pointer to ButtonInfo structure to process
*/
void processButton(ButtonInfo* button)
{
// Read current physical state (buttons are active LOW with pullups)
bool current_physical = !digitalRead(button->pin);
// Check if physical state has changed
if (current_physical != button->physical_state) {
button->physical_state = current_physical;
button->last_change_time = millis();
}
// Apply debouncing - only update debounced state after minimum time
if (millis() - button->last_change_time >= BUTTON_DEBOUNCE_MS) {
button->debounced_state = button->physical_state;
}
// Update button state machine
button->previous_state = button->current_state;
if (button->debounced_state) {
// Button is pressed
if (button->current_state == BUTTON_IDLE) {
button->current_state = BUTTON_PRESSED;
}
} else {
// Button is not pressed
if (button->current_state == BUTTON_PRESSED) {
button->current_state = BUTTON_RELEASED;
} else if (button->current_state == BUTTON_RELEASED) {
button->current_state = BUTTON_IDLE;
}
}
}
// =============================================================================
// BUTTON STATE QUERY FUNCTIONS
// =============================================================================
/**
* Check if a button was just pressed (press edge detection)
* @param button_index Index of button to check (0=UP, 1=DOWN, 2=LEFT, 3=RIGHT)
* @return true if button was just pressed
*/
bool wasButtonPressed(int button_index) {
if (button_index < 0 || button_index >= NUM_BUTTONS) {
return false;
}
return (buttons[button_index].current_state == BUTTON_PRESSED &&
buttons[button_index].previous_state == BUTTON_IDLE);
}
/**
* Check if a button was just released (release edge detection)
* @param button_index Index of button to check
* @return true if button was just released
*/
bool wasButtonReleased(int button_index) {
if (button_index < 0 || button_index >= NUM_BUTTONS) {
return false;
}
return (buttons[button_index].current_state == BUTTON_RELEASED);
}
/**
* Check if a button is currently pressed (level detection)
* @param button_index Index of button to check
* @return true if button is currently pressed
*/
bool isButtonPressed(int button_index) {
if (button_index < 0 || button_index >= NUM_BUTTONS) {
return false;
}
return (buttons[button_index].current_state == BUTTON_PRESSED);
}
// =============================================================================
// SPECIFIC BUTTON QUERY FUNCTIONS
// =============================================================================
/**
* Check if UP button was just pressed
* @return true if UP button was just pressed
*/
bool wasUpButtonPressed() {
return wasButtonPressed(0); // UP button is index 0
}
/**
* Check if DOWN button was just pressed
* @return true if DOWN button was just pressed
*/
bool wasDownButtonPressed() {
return wasButtonPressed(1); // DOWN button is index 1
}
/**
* Check if LEFT button was just pressed
* @return true if LEFT button was just pressed
*/
bool wasLeftButtonPressed() {
return wasButtonPressed(2); // LEFT button is index 2
}
/**
* Check if RIGHT button was just pressed
* @return true if RIGHT button was just pressed
*/
bool wasRightButtonPressed() {
return wasButtonPressed(3); // RIGHT button is index 3
}
/**
* Check if UP button is currently pressed
* @return true if UP button is currently pressed
*/
bool isUpButtonPressed() {
return isButtonPressed(0);
}
/**
* Check if DOWN button is currently pressed
* @return true if DOWN button is currently pressed
*/
bool isDownButtonPressed() {
return isButtonPressed(1);
}
// =============================================================================
// BUTTON SYSTEM STATUS FUNCTIONS
// =============================================================================
/**
* Check if button system is ready
* @return true if button system initialized successfully
*/
bool isButtonSystemReady() {
return button_system_ready;
}
/**
* Get button name for debugging
* @param button_index Index of button
* @return pointer to button name string
*/
const char* getButtonName(int button_index) {
if (button_index < 0 || button_index >= NUM_BUTTONS) {
return "INVALID";
}
return buttons[button_index].name;
}
// =============================================================================
// BUTTON DIAGNOSTICS AND TESTING
// =============================================================================
/**
* Print current button states to serial port
* Useful for debugging button input issues
*/
void printButtonStates() {
Serial.print("Button States: ");
for (int i = 0; i < NUM_BUTTONS; i++) {
Serial.print(buttons[i].name);
Serial.print("=");
if (buttons[i].current_state == BUTTON_PRESSED) {
Serial.print("PRESSED");
} else if (buttons[i].current_state == BUTTON_RELEASED) {
Serial.print("RELEASED");
} else {
Serial.print("IDLE");
}
if (i < NUM_BUTTONS - 1) {
Serial.print(", ");
}
}
Serial.println();
}
/**
* Test button functionality
* Monitors button presses and reports them to serial
* @param test_duration_ms Duration of test in milliseconds
*/
void testButtons(unsigned long test_duration_ms) {
Serial.println("Starting button test...");
Serial.println("Press buttons to test - results will be displayed");
unsigned long start_time = millis();
while (millis() - start_time < test_duration_ms) {
// Scan buttons as normal
scanButtons();
// Report any button presses
for (int i = 0; i < NUM_BUTTONS; i++) {
if (wasButtonPressed(i)) {
Serial.print("Button ");
Serial.print(buttons[i].name);
Serial.println(" PRESSED");
}
if (wasButtonReleased(i)) {
Serial.print("Button ");
Serial.print(buttons[i].name);
Serial.println(" RELEASED");
}
}
delay(10); // Small delay for serial output
}
Serial.println("Button test complete");
}
/**
* Print button system diagnostics
* Shows detailed information about button system state
*/
void printButtonDiagnostics() {
Serial.println("=== Songbird Button Diagnostics ===");
Serial.print("Button System Ready: "); Serial.println(button_system_ready ? "YES" : "NO");
Serial.print("Number of Buttons: "); Serial.println(NUM_BUTTONS);
Serial.println("\n--- Button States ---");
for (int i = 0; i < NUM_BUTTONS; i++) {
Serial.print(buttons[i].name);
Serial.print(" (Pin ");
Serial.print(buttons[i].pin);
Serial.print("): ");
Serial.print("Physical=");
Serial.print(buttons[i].physical_state ? "HIGH" : "LOW");
Serial.print(", Debounced=");
Serial.print(buttons[i].debounced_state ? "HIGH" : "LOW");
Serial.print(", State=");
switch (buttons[i].current_state) {
case BUTTON_IDLE:
Serial.print("IDLE");
break;
case BUTTON_PRESSED:
Serial.print("PRESSED");
break;
case BUTTON_RELEASED:
Serial.print("RELEASED");
break;
}
Serial.print(", Last Change: ");
Serial.print(millis() - buttons[i].last_change_time);
Serial.println("ms ago");
}
Serial.println("======================================\n");
}
/**
* Wait for any button press
* Blocks until a button is pressed, useful for user confirmation
* @param timeout_ms Maximum time to wait in milliseconds (0 = no timeout)
* @return true if button was pressed, false if timeout occurred
*/
bool waitForButtonPress(unsigned long timeout_ms = 0) {
unsigned long start_time = millis();
Serial.println("Waiting for button press...");
while (true) {
scanButtons();
// Check if any button was pressed
for (int i = 0; i < NUM_BUTTONS; i++) {
if (wasButtonPressed(i)) {
Serial.print("Button ");
Serial.print(buttons[i].name);
Serial.println(" pressed!");
return true;
}
}
// Check timeout
if (timeout_ms > 0 && (millis() - start_time) >= timeout_ms) {
Serial.println("Button press timeout");
return false;
}
delay(10);
}
}
// =============================================================================
// ADVANCED BUTTON FEATURES
// =============================================================================
/**
* Check if a button has been held for a specific duration
* @param button_index Index of button to check
* @param hold_duration_ms Required hold duration in milliseconds
* @return true if button has been held for the specified duration
*/
bool isButtonHeld(int button_index, unsigned long hold_duration_ms) {
if (button_index < 0 || button_index >= NUM_BUTTONS) {
return false;
}
if (buttons[button_index].current_state != BUTTON_PRESSED) {
return false;
}
// Check if button has been in pressed state long enough
return (millis() - buttons[button_index].last_change_time) >= hold_duration_ms;
}
/**
* Reset button states (useful for clearing any stuck states)
*/
void resetButtonStates() {
for (int i = 0; i < NUM_BUTTONS; i++) {
buttons[i].current_state = BUTTON_IDLE;
buttons[i].previous_state = BUTTON_IDLE;
buttons[i].last_change_time = millis();
}
Serial.println("Button states reset");
}