Print a message to a custom LED matrix and play a song on a piezo buzzer.

Dependencies:   Adafruit_32x8matrix

Files at this revision

API Documentation at this revision

Comitter:
maclobdell
Date:
Mon Nov 08 22:45:16 2021 +0000
Parent:
9:d70273b3133b
Commit message:
uses switch to trigger start.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Nov 03 19:49:24 2021 +0000
+++ b/main.cpp	Mon Nov 08 22:45:16 2021 +0000
@@ -28,7 +28,11 @@
 I2C i2c(D14, D15);
  
 Adafruit_32x8matrix matrix(&i2c, I2C_ADDR1, I2C_ADDR2, ROTATION1, ROTATION2, BRIGHTNESS);
-Thread displayThread;
+Thread t1;
+Thread t2;
+InterruptIn sw(D4);
+EventQueue queue1(32 * EVENTS_EVENT_SIZE);
+EventQueue queue2(32 * EVENTS_EVENT_SIZE);
 
 PwmOut buzzer(D3);                   // our buzzer is a PWM output (pulse-width modulation)
 
@@ -82,22 +86,10 @@
     silence();   
 }
 
-void display_thread()
+void start_song()
 {
-    char buffer [50];
-    snprintf(buffer, 50, "GO TIGERS!\0");   //pass in max chars to prevent overflow
-
-    for (int i = 0; i< 3; i++) {
-        matrix.playText(buffer,strlen(buffer), 1);
-        Thread::wait(250);
-    }
-}
- 
-int main() {
-    
-    
     // declare a melody
-     int melody[] = {
+    int melody[] = {
         NOTE_G4, NOTE_C5, NOTE_E5, NOTE_G5, NOTE_E5, NOTE_G5 
     };
     
@@ -107,7 +99,6 @@
         8, 8, 8, 4, 8, 2
     };    
     
-    
     // melody & duration are on the heap, need to get them on the stack
     int *m = new int[sizeof(melody) / sizeof(int)];
     memcpy(m, melody, sizeof(melody));
@@ -117,17 +108,47 @@
         
     if (sizeof(melody) != sizeof(duration)) {
         printf("Melody and duration do not have same number of elements! Aborting!\r\n");
-        return 1;
     }
    
     for (int i = 0; i< 3; i++)
-    { 
-        displayThread.start(display_thread);
-     
-        play_song(sizeof(melody) / sizeof(int), m, d);
-                               
+    {  
+        play_song(sizeof(melody) / sizeof(int), m, d);                        
         Thread::wait(100);
     }
+    
+}
+    
+void start_display()
+{
+    char buffer [50];
+    snprintf(buffer, 50, "GO TIGERS!\0");   //pass in max chars to prevent overflow
+
+    for (int i = 0; i< 3; i++) {
+        matrix.playText(buffer,strlen(buffer), 1);
+        Thread::wait(250);
+    }
+}
+
+void switch_handler(void)
+{
+    queue1.call(start_song);
+    queue2.call(start_display);
+    
+} 
+
+int main() {
+    
+    // Start the event queues
+    t1.start(callback(&queue1, &EventQueue::dispatch_forever));
+    t2.start(callback(&queue2, &EventQueue::dispatch_forever));
+    
+    // Call handler when the switch is pressed
+    sw.fall(switch_handler);
+        
+    while(1)
+    {//chill
+    }
+ 
     return 0;
  
 }