-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSongbirdLoopback.ino
More file actions
583 lines (490 loc) · 17.8 KB
/
SongbirdLoopback.ino
File metadata and controls
583 lines (490 loc) · 17.8 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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
// usb_loopback_demo.ino - Main Arduino File for Songbird USB Audio Loopback Demo
// Part of USB Audio Loopback Demo
//
// The Songbird device demonstrates real-time USB audio processing with:
// - Direct audio loopback from USB input to USB output
// - Visual feedback via OLED display and LEDs
// - Interactive controls via buttons
// - Professional audio processing using Teensy Audio Library
//
// Hardware Requirements:
// - Songbird device (Teensy 4.0 + Audio Shield + custom hardware)
// - USB-C connection to host computer
// - Built-in OLED display, buttons, and LEDs
//
// Educational Purpose:
// This demo showcases embedded audio processing concepts including:
// - Real-time audio buffering and routing
// - RMS level detection and visualization
// - State machine design for audio applications
// =============================================================================
// SYSTEM INCLUDES
// =============================================================================
// Standard Arduino and Teensy libraries
#include <Arduino.h>
// Audio processing libraries
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// Display libraries
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "config.h"
// =============================================================================
// GLOBAL SYSTEM STATE
// =============================================================================
// Current system state - controls overall behavior
SystemState current_system_state = STATE_STANDBY;
SystemState previous_system_state = STATE_STANDBY;
// =============================================================================
// SYSTEM TIMING CONSTANTS
// =============================================================================
// Main loop timing - all updates synchronized to this base rate
const unsigned long MAIN_LOOP_INTERVAL_MS = 20; // 50 Hz main loop
// Display update timing - prevents excessive OLED refresh
const unsigned long DISPLAY_UPDATE_INTERVAL_MS = 100; // 10 Hz display updates
// Button scanning timing - fast enough for responsive feel
const unsigned long BUTTON_SCAN_INTERVAL_MS = 50; // 20 Hz button scanning
// LED update timing - matches display for smooth visual feedback
const unsigned long LED_UPDATE_INTERVAL_MS = 100; // 10 Hz LED updates
// Button debouncing - prevents false triggers from mechanical contacts
const unsigned long BUTTON_DEBOUNCE_MS = 50; // 50ms debounce time
// Timing variables for main loop control
unsigned long last_main_loop = 0;
unsigned long system_start_time = 0;
// System status flags
bool system_initialized = false;
bool system_error = false;
String last_error_message = "";
// =============================================================================
// ARDUINO SETUP FUNCTION
// =============================================================================
/**
* Arduino setup function - called once at startup
* Initializes all system components and prepares for operation
*/
void setup() {
// Initialize serial communication for debugging
Serial.begin(115200);
// Wait a moment for serial connection (useful for debugging)
delay(1000);
Serial.println("========================================");
Serial.println("Songbird USB Audio Loopback Demo");
Serial.println("Initializing system...");
Serial.println("========================================");
// Record system start time
system_start_time = millis();
// Initialize all system components in order
if (initializeSystem()) {
system_initialized = true;
current_system_state = STATE_STANDBY;
Serial.println("========================================");
Serial.println("System initialization complete!");
Serial.println("Songbird ready for operation");
Serial.println("========================================");
// Show welcome message on display
displayMessage("Songbird Ready!");
delay(2000);
// Perform LED startup sequence
ledStartupSequence();
} else {
system_error = true;
Serial.println("========================================");
Serial.println("ERROR: System initialization failed!");
Serial.println("Check hardware connections and try again");
Serial.println("========================================");
// Show error on display if possible
displayError("Init Failed");
// Error indication with LEDs
ledErrorIndication();
}
Serial.println("Starting main loop...");
}
// =============================================================================
// ARDUINO MAIN LOOP
// =============================================================================
/**
* Arduino main loop function - called continuously
* Implements the main system state machine and coordinates all components
*/
void loop() {
// Check if it's time for main loop update
if (millis() - last_main_loop < MAIN_LOOP_INTERVAL_MS) {
return; // Too soon for next update
}
last_main_loop = millis();
// Exit early if system not initialized or in error state
if (!system_initialized || system_error) {
handleSystemError();
return;
}
// Process all input/output components
processInputs();
processSystemState();
updateOutputs();
// Periodic diagnostics (every 10 seconds)
static unsigned long last_diagnostics = 0;
if (millis() - last_diagnostics > 10000) {
last_diagnostics = millis();
printSystemDiagnostics();
}
}
// =============================================================================
// SYSTEM INITIALIZATION
// =============================================================================
/**
* Initialize all system components
* @return true if all components initialized successfully
*/
bool initializeSystem() {
bool all_successful = true;
Serial.println("Initializing system components...");
// Initialize configuration (pin assignments, constants)
Serial.println("Loading configuration...");
// Configuration is loaded from config.ino automatically
// Initialize audio system
Serial.println("Initializing audio system...");
setupAudio();
if (!isAudioSystemReady()) {
Serial.println("ERROR: Audio system initialization failed");
all_successful = false;
}
// Initialize display system
Serial.println("Initializing display system...");
setupDisplay();
if (!isDisplayReady()) {
Serial.println("ERROR: Display system initialization failed");
all_successful = false;
}
// Initialize LED system
Serial.println("Initializing LED system...");
setupLEDs();
if (!isLEDSystemReady()) {
Serial.println("ERROR: LED system initialization failed");
all_successful = false;
}
// Initialize button system
Serial.println("Initializing button system...");
setupButtons();
if (!isButtonSystemReady()) {
Serial.println("ERROR: Button system initialization failed");
all_successful = false;
}
// Test all systems if initialization was successful
if (all_successful) {
Serial.println("Running system tests...");
// Brief display test
displayMessage("Testing...");
delay(500);
// Brief LED test
setLEDBrightness(128, 128);
delay(500);
setLEDBrightness(0, 0);
Serial.println("System tests complete");
}
return all_successful;
}
// =============================================================================
// INPUT PROCESSING
// =============================================================================
/**
* Process all system inputs (buttons, audio levels, USB status)
*/
void processInputs() {
// Scan buttons for state changes
scanButtons();
// Update audio level measurements
updateAudioLevels();
// Check for button presses and handle state transitions
handleButtonPresses();
}
/**
* Handle button press events and state transitions
*/
void handleButtonPresses() {
// UP button: Toggle between STANDBY and ACTIVE states
if (wasUpButtonPressed()) {
Serial.println("UP button pressed");
if (current_system_state == STATE_STANDBY) {
// Transition to ACTIVE state
current_system_state = STATE_ACTIVE;
Serial.println("State: STANDBY -> ACTIVE");
} else if (current_system_state == STATE_ACTIVE || current_system_state == STATE_MUTED) {
// Transition to STANDBY state
current_system_state = STATE_STANDBY;
Serial.println("State: -> STANDBY");
}
}
// DOWN button: Toggle MUTED state (only when active)
if (wasDownButtonPressed()) {
Serial.println("DOWN button pressed");
if (current_system_state == STATE_ACTIVE) {
// Transition to MUTED state
current_system_state = STATE_MUTED;
Serial.println("State: ACTIVE -> MUTED");
} else if (current_system_state == STATE_MUTED) {
// Transition back to ACTIVE state
current_system_state = STATE_ACTIVE;
Serial.println("State: MUTED -> ACTIVE");
}
}
// LEFT and RIGHT buttons reserved for future use
if (wasLeftButtonPressed()) {
Serial.println("LEFT button pressed (reserved)");
}
if (wasRightButtonPressed()) {
Serial.println("RIGHT button pressed (reserved)");
}
}
// =============================================================================
// STATE MACHINE PROCESSING
// =============================================================================
/**
* Process system state machine and handle state transitions
*/
void processSystemState() {
// Handle state entry actions (executed once when entering a state)
if (current_system_state != previous_system_state) {
handleStateEntry(current_system_state);
previous_system_state = current_system_state;
}
// Handle state-specific processing (executed continuously while in state)
handleStateProcessing(current_system_state);
}
/**
* Handle actions when entering a new state
* @param new_state The state being entered
*/
void handleStateEntry(SystemState new_state) {
Serial.print("Entering state: ");
switch (new_state) {
case STATE_STANDBY:
Serial.println("STANDBY");
// Disable audio processing
setAudioMuted(true);
break;
case STATE_ACTIVE:
Serial.println("ACTIVE");
// Enable audio processing
setAudioMuted(false);
break;
case STATE_MUTED:
Serial.println("MUTED");
// Mute audio output but keep processing for display
setAudioMuted(true);
break;
}
}
/**
* Handle continuous processing for current state
* @param current_state The current system state
*/
void handleStateProcessing(SystemState current_state) {
// State-specific processing can be added here
// For now, most processing is handled in updateOutputs()
switch (current_state) {
case STATE_STANDBY:
// In standby, we might want to check for USB connection
// and automatically transition to active when audio is detected
if (isUSBAudioConnected() && getInputLevel() > LEVEL_THRESHOLD) {
// Auto-transition to active when audio is detected
// (This is optional - can be disabled for manual control only)
// current_system_state = STATE_ACTIVE;
}
break;
case STATE_ACTIVE:
// In active state, monitor for loss of USB connection
if (!isUSBAudioConnected()) {
// Could auto-transition to standby if USB is disconnected
// (This is optional - depends on desired behavior)
// current_system_state = STATE_STANDBY;
}
break;
case STATE_MUTED:
// In muted state, continue processing input for display
// but output remains muted
break;
}
}
// =============================================================================
// OUTPUT PROCESSING
// =============================================================================
/**
* Update all system outputs (display, LEDs, audio routing)
*/
void updateOutputs() {
// Update display with current state and audio levels
updateDisplay(current_system_state);
// Update LED brightness based on audio levels and state
updateLEDs(current_system_state);
// Audio routing is handled automatically by the audio library
// and state-specific muting in handleStateEntry()
}
// =============================================================================
// ERROR HANDLING
// =============================================================================
/**
* Handle system error conditions
*/
void handleSystemError() {
// Flash error indication on LEDs
static unsigned long last_error_flash = 0;
if (millis() - last_error_flash > 500) {
last_error_flash = millis();
ledErrorIndication();
}
// Show error message on display
displayError(last_error_message.c_str());
// Print error information to serial
static unsigned long last_error_print = 0;
if (millis() - last_error_print > 5000) {
last_error_print = millis();
Serial.println("SYSTEM ERROR - Check hardware and restart");
}
}
/**
* Set system error state with message
* @param error_message Description of the error
*/
void setSystemError(const String& error_message) {
system_error = true;
last_error_message = error_message;
Serial.print("SYSTEM ERROR: ");
Serial.println(error_message);
}
// =============================================================================
// SYSTEM DIAGNOSTICS
// =============================================================================
/**
* Print comprehensive system diagnostics
*/
void printSystemDiagnostics() {
Serial.println("\n=== SONGBIRD SYSTEM DIAGNOSTICS ===");
// System status
Serial.print("System Initialized: "); Serial.println(system_initialized ? "YES" : "NO");
Serial.print("System Error: "); Serial.println(system_error ? "YES" : "NO");
Serial.print("Current State: ");
switch (current_system_state) {
case STATE_STANDBY: Serial.println("STANDBY"); break;
case STATE_ACTIVE: Serial.println("ACTIVE"); break;
case STATE_MUTED: Serial.println("MUTED"); break;
}
// Timing information
Serial.print("System Uptime: "); Serial.print((millis() - system_start_time) / 1000); Serial.println(" seconds");
Serial.print("Main Loop Rate: "); Serial.print(1000.0 / MAIN_LOOP_INTERVAL_MS); Serial.println(" Hz");
// Component status
Serial.print("Audio System Ready: "); Serial.println(isAudioSystemReady() ? "YES" : "NO");
Serial.print("Display Ready: "); Serial.println(isDisplayReady() ? "YES" : "NO");
Serial.print("LED System Ready: "); Serial.println(isLEDSystemReady() ? "YES" : "NO");
Serial.print("Button System Ready: "); Serial.println(isButtonSystemReady() ? "YES" : "NO");
// Print detailed diagnostics from each component
printAudioDiagnostics();
printLEDDiagnostics();
printButtonDiagnostics();
Serial.println("=====================================\n");
}
// =============================================================================
// UTILITY FUNCTIONS
// =============================================================================
/**
* Get current system state as string
* @return String representation of current state
*/
String getSystemStateString() {
switch (current_system_state) {
case STATE_STANDBY: return "STANDBY";
case STATE_ACTIVE: return "ACTIVE";
case STATE_MUTED: return "MUTED";
default: return "UNKNOWN";
}
}
/**
* Get system uptime in seconds
* @return System uptime in seconds
*/
unsigned long getSystemUptime() {
return (millis() - system_start_time) / 1000;
}
/**
* Check if system is ready for operation
* @return true if all systems are ready
*/
bool isSystemReady() {
return system_initialized && !system_error &&
isAudioSystemReady() && isDisplayReady() &&
isLEDSystemReady() && isButtonSystemReady();
}
// =============================================================================
// DEVELOPMENT AND TESTING FUNCTIONS
// =============================================================================
/**
* Run comprehensive system tests
* Useful for hardware verification and troubleshooting
*/
void runSystemTests() {
Serial.println("Running comprehensive system tests...");
// Test display
Serial.println("Testing display system...");
testDisplay();
// Test LEDs
Serial.println("Testing LED system...");
testLEDs();
// Test buttons
Serial.println("Testing button system...");
testButtons(5000); // 5 second button test
// Test audio (basic verification)
Serial.println("Testing audio system...");
printAudioDiagnostics();
Serial.println("System tests complete!");
}
/**
* Enter diagnostic mode
* Provides interactive system testing and monitoring
*/
void enterDiagnosticMode() {
Serial.println("Entering diagnostic mode...");
Serial.println("Press any button to exit diagnostic mode");
displayMessage("Diagnostic Mode");
while (true) {
// Update all systems
scanButtons();
updateAudioLevels();
// Show diagnostics on display
displayDiagnostics();
// Check for button press to exit
for (int i = 0; i < 4; i++) {
if (wasButtonPressed(i)) {
Serial.println("Exiting diagnostic mode");
return;
}
}
// Print diagnostics every 2 seconds
static unsigned long last_diag_print = 0;
if (millis() - last_diag_print > 2000) {
last_diag_print = millis();
printSystemDiagnostics();
}
delay(100);
}
}
/**
* Check for special button combinations at startup
* Allows entering test modes for hardware verification
*/
void checkStartupButtonCombinations() {
Serial.println("Checking for startup button combinations...");
// Scan buttons
scanButtons();
// Check for test mode combinations
if (isButtonPressed(0) && isButtonPressed(1)) {
// UP + DOWN = Run system tests
Serial.println("Test mode detected - running system tests");
runSystemTests();
} else if (isButtonPressed(2) && isButtonPressed(3)) {
// LEFT + RIGHT = Enter diagnostic mode
Serial.println("Diagnostic mode detected");
enterDiagnosticMode();
}
}