A MIDI piano synthesizer that implements the Karplus Strong physical modeling algorithm.

Dependencies:   mbed USBDevice PinDetect

Revision:
17:55e6132c54a8
Parent:
11:f65806ee5833
Child:
20:bf675ba2c454
--- a/LEDController.cpp	Sun Apr 17 22:27:08 2016 +0000
+++ b/LEDController.cpp	Tue Apr 19 17:22:18 2016 +0000
@@ -1,28 +1,33 @@
 #include "mbed.h"
 #include "LEDController.h"
 
-DigitalOut myled1_1(p5);
-DigitalOut myled1_2(p6);
-DigitalOut myled1_3(p7);
+DigitalOut brickled1(p5);
+DigitalOut brickled2(p6);
+DigitalOut brickled3(p7);
+DigitalOut brickled4(p8);
 
-DigitalOut myled2_1(p8);
-DigitalOut myled2_2(p9);
-DigitalOut myled2_3(p10);
+DigitalOut myled1_1(p9);
+DigitalOut myled1_2(p10);
+DigitalOut myled1_3(p11);
 
-DigitalOut myled3_1(p11);
-DigitalOut myled3_2(p12);
-DigitalOut myled3_3(p13);
+DigitalOut myled2_1(p12);
+DigitalOut myled2_2(p13);
+DigitalOut myled2_3(p14);
 
-DigitalOut myled4_1(p14);
-DigitalOut myled4_2(p15);
-DigitalOut myled4_3(p16);
+DigitalOut myled3_1(p15);
+DigitalOut myled3_2(p16);
+DigitalOut myled3_3(p17);
+
+DigitalOut myled4_1(p18);
+DigitalOut myled4_2(p19);
+DigitalOut myled4_3(p20);
 
 void identifyKeyForLed(int key, int type);
 void chooseLedForKey(int colors[3], int type, int signature, int key);
 void setLedToKey(int colors[3], int type, int signature, int led, int key);
 
 //These are the notes numbers and RGB values.
-//int noteC[] = {24,0,0,0};
+//int noteC[] = {24,1,0,1};
 //int noteD[] = {26,1,0,0};
 //int noteE[] = {16,0,1,0};
 //int noteF[] = {17,0,0,1};
@@ -32,22 +37,20 @@
 
 //A larger array of all the keys to be iterated over.
 //int keys[7][4] = {noteC, noteD, noteE, noteF, noteG, noteA, noteB};
-int colors[12][3] = {{0,0,0},{0,0,0},{1,0,0},{1,0,0},{0,1,0},{0,0,1},{0,0,1},{1,1,0},{1,1,0},{0,1,1},{0,1,1},{1,1,1}};
-
+int colors[12][3] = {{1,0,1},{1,0,1},{1,0,0},{1,0,0},{0,1,0},{0,0,1},{0,0,1},{1,1,0},{1,1,0},{0,1,1},{0,1,1},{1,1,1}};
 
-int keys[11][12] = {{0,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,0,0,0,0}};
-
-const int keysSize = 12;
+//Midi Numbers
+//int keys[11][12] = {{0,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,0,0,0,0}};
 
 int ledKeys[4] = {-1,-1,-1,-1};
 
@@ -57,14 +60,13 @@
 
 void LEDController::identifyKeyForLed(int key, int type){
     //pc.printf("\r\nGetting note");
-    int octave = key/12;
-    
-    for(int i = 0; i < keysSize; i++){
-        if(keys[octave][i] == key){
-           //pc.printf("\r\nNote Chosen, no signature");
-           chooseLedForKey(colors[i], type,0, keys[octave][i]);     
-        }
-    }
+    int octave = key%12;
+
+   if(octave == 1 || octave == 3 || octave == 6 || octave == 8 || octave == 10){
+        chooseLedForKey(colors[octave], type,1, key);
+    }else{
+        chooseLedForKey(colors[octave], type,0, key);
+    }     
 } 
 
 //Chooses which LED to use based upon which are set or if they needed to be turned off.
@@ -99,61 +101,77 @@
 
 //This sets the key or clears the previously set key if the type is off.
 void LEDController::setLedToKey(int colors[3], int type, int signature, int led, int key){
-    if(led == 1){
+ if(led == 1){
         if(type == 1){
-            //pc.printf("\r\nTurning on light 1");
+//            pc.printf("\r\nTurning on light 1");
             ledKeys[0] = key;
             myled1_1 = colors[0];
             myled1_2 = colors[1];
             myled1_3 = colors[2];
+            if(signature == 1){
+                brickled1 = 1;
+             }
          }else{
-            //pc.printf("\r\nTurning off light 1");
+//            pc.printf("\r\nTurning off light 1");
             ledKeys[0] = -1;
             myled1_1 = 0;
             myled1_2 = 0;
             myled1_3 = 0;
+            brickled1 = 0;
          }
     }else if(led == 2){
         if(type == 1){
-            //pc.printf("\r\nTurning on light 2");
+//            pc.printf("\r\nTurning on light 2");
             ledKeys[1] = key;
             myled2_1 = colors[0];
             myled2_2 = colors[1];
             myled2_3 = colors[2];
+            if(signature == 1){
+                brickled2 = 1;
+             }
      }else{
-    //        pc.printf("\r\nTurning off light 2");
+//            pc.printf("\r\nTurning off light 2");
             ledKeys[1] = -1;
             myled2_1 = 0;
             myled2_2 = 0;
             myled2_3 = 0;
+            brickled2 = 0;
      }
     }else if(led == 3){
         if(type == 1){
-    //        pc.printf("\r\nTurning on light 3");
+//            pc.printf("\r\nTurning on light 3");
             ledKeys[2] = key;
             myled3_1 = colors[0];
             myled3_2 = colors[1];
             myled3_3 = colors[2];
+            if(signature == 1){
+                brickled3 = 1;
+             }
      }else{
-    //        pc.printf("\r\nTurning off light 3");
+//            pc.printf("\r\nTurning off light 3");
             ledKeys[2] = -1;
             myled3_1 = 0;
             myled3_2 = 0;
             myled3_3 = 0;
+            brickled3 = 0;
      }
     }else if(led == 4){
         if(type == 1){
-    //        pc.printf("\r\nTurning on light 4");
+//            pc.printf("\r\nTurning on light 4");
             ledKeys[3] = key;
             myled4_1 = colors[0];;
             myled4_2 = colors[1];
             myled4_3 = colors[2];
+            if(signature == 1){
+                brickled4 = 1;
+             }
      }else{
-    //        pc.printf("\r\nTurning off light 4");
+//            pc.printf("\r\nTurning off light 4");
             ledKeys[3] = -1;
             myled4_1 = 0;
             myled4_2 = 0;
             myled4_3 = 0;
+            brickled4 = 0;
      }
     }