ChordJoy is a keyboard application that accepts chordal input from a set of digital ports, outputting letters and the chords that correspond to the piano keyboard keys the user has pressed.

Dependencies:   PinDetect_KL25Z mbed-rtos mbed

Revision:
1:b6b866a58a87
Parent:
0:5ffec551c755
Child:
2:49e2b1273f16
--- a/main.cpp	Thu Sep 18 05:48:12 2014 +0000
+++ b/main.cpp	Sat Sep 20 19:57:18 2014 +0000
@@ -1,24 +1,36 @@
 #include "mbed.h"
-#include "PinDetect.h"
 #include "rtos.h"
 
+
+const int BUTTONS = 7;
 const int BUZZERS = 3;
 const int LETTERS = 27;
 
+// Flag that says whether we're waiting to collect input from other buttons
+// following one upward edge.
+bool flag = false;
+
+InterruptIn buttons [BUTTONS] = {
+    InterruptIn(D4),
+    InterruptIn(D5),
+    InterruptIn(D8),
+    InterruptIn(D9),
+    InterruptIn(D10),
+    InterruptIn(D11),
+    InterruptIn(D2)
+};
 Serial pc(USBTX, USBRX);
-PinDetect buttons [3] = {
-    PinDetect(D15),
-    PinDetect(D14),
-    PinDetect(D13)
+
+// Note: there are only THREE PWMs on the board. So this is all we can use
+// for this particular method of sound output. The pins chosen are important
+// because the same PWM will map to multiple outputs. See the reference
+// manual for Mbed for which pins map to distinct PWMs.
+PwmOut buzzers [3] = {
+    PwmOut(D0),
+    PwmOut(D7),
+    PwmOut(D3)
 };
 
-// Note: there are only THREE PWMs on the board. So this is all we can use
-// for this particular method of sound output.
-PwmOut buzzers [3] = {
-    PwmOut(D0),
-    PwmOut(D2),
-    PwmOut(D3)
-};
 // Periods of notes in microseconds
 int notePeriods [8] = {
     3818,   // C4
@@ -31,7 +43,7 @@
     10000000 // inaudible pitch
 };
 int chords [LETTERS][BUZZERS] = {
-    {0, 7, 7}, // single notes
+    {0, 2, 4}, // single notes
     {1, 7, 7},
     {2, 7, 7},
     {3, 7, 7},
@@ -74,15 +86,43 @@
                 buzzers[j].period_us(period);
             }
         }
-        wait_ms(1000);
+        wait_ms(10000);
     }
 }
 
-int main() {
+void setFlag() {
+    flag = true;
+}
+
+void silenceBuzzers(void) {
     // Initialize all duty cycles to off, so we hear nothing.
     for (int i = 0; i < BUZZERS; i++) {
         buzzers[i] = 0.0f;
     }
-    pc.printf("%c\n", 97);
-    // playAllChords();
+}
+
+void registerButtonInterrupts(void) {
+    for (int i =  0; i < BUTTONS; i++) {
+        buttons[i].rise(&setFlag);
+    }   
+}
+
+void waitForChord(void) {
+     /* Should be run in the main loop so that it can print */
+     while (true) {
+        if (flag == true) {
+            pc.printf("Pressed\n");
+            flag = false;
+        }
+        else {
+            pc.printf("Not pressed\n");   
+        }
+        wait_ms(500);
+    }   
+}
+
+int main() {
+    silenceBuzzers();
+    registerButtonInterrupts();
+    waitForChord();
 }
\ No newline at end of file