Bluetooth Enabled Keyboard/Synthesizer for mbed

Dependencies:   mbed 4DGL-uLCD-SE SDFileSystem mbed-rtos

Revision:
11:c87f55a3b9e0
Parent:
10:085c49fe2509
Child:
12:d60a9d0052a7
--- a/main.cpp	Thu Apr 28 23:44:55 2016 +0000
+++ b/main.cpp	Fri Apr 29 16:23:20 2016 +0000
@@ -4,22 +4,22 @@
 #include <vector>
 #include "uLCD_4DGL.h"
 #include "synthesizer.h"
-Serial Blue(p28,p27);
-Serial PC(USBTX,USBRX);
+RawSerial Blue(p13,p14);
+RawSerial PC(USBTX,USBRX);
 DigitalOut myled(LED1);
 DigitalOut myled4(LED4);
 
 SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
 
-Mutex mtx;  //Mutex lock
+//Mutex mtx;  //Mutex lock
 
-uLCD_4DGL uLCD(p13,p14,p11); // serial tx, serial rx, reset pin;
+uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin;
 
 AnalogOut synthPin(p18);     // p18 is the pin that will have the output voltages on it
 
 
 //global variables for main and interrupt routine
-volatile bool readyFlag = true;
+volatile bool readyFlag = false;
 volatile char keyPress;
 WaveType myWave = sine; // default to sine wave
 volatile int currentOctave = 4; // default to 4 because thats where middle C is
@@ -212,16 +212,18 @@
 };
 
 void uLCD_Display_Thread(void const *args){
-    mtx.lock();
-    uLCD.locate(0,0);
-    uLCD.printf("Shape: %s\r\n",myWave);
-    uLCD.printf("Octave: %i\r\n",currentOctave);
-    uLCD.printf("Attack: %i\r\n",currentAttackVal);
-    uLCD.printf("Decay: %i\r\n",currentDecayVal);
-    uLCD.printf("Sustain: %i\r\n",currentSustainVal);
-    uLCD.printf("Release: %i\r\n",currentReleaseVal);
-    mtx.unlock();
-    T1hread::wait(250);
+    //mtx.lock();
+    while(1){
+        uLCD.locate(0,0);
+        uLCD.printf("Shape: %i\r\n",myWave);
+        uLCD.printf("Octave: %i\r\n",currentOctave);
+        uLCD.printf("Attack: %i\r\n",currentAttackVal);
+        uLCD.printf("Decay: %i\r\n",currentDecayVal);
+        uLCD.printf("Sustain: %i\r\n",currentSustainVal);
+        uLCD.printf("Release: %i\r\n",currentReleaseVal);
+    }
+    //mtx.unlock();
+    //Thread::wait(250);
     }
 
 void clear_Buffer(void){
@@ -229,27 +231,27 @@
 }
 
 void set_Note_Freq(int frequency){
-    accumulator_reset();
+    //accumulator_reset();
     noteFreq = frequency;
-    set_Frequency_Tuner();
+    //set_Frequency_Tuner();
     clear_Buffer();
 }
 
-void change_Wave(const WaveType currentWave) {
-  switch(currentWave) {
-  case sine:
-    currentLookupTable = sineTable;
-    break;
-  case square:
-    currentLookupTable = squareTable;
-    break;
-  case sawtooth:
-    currentLookupTable = sawtoothTable;
-    break;
-  default:
-    break;
-  }
-}
+//void change_Wave(const WaveType currentWave) {
+//  switch(currentWave) {
+//  case sine:
+//    currentLookupTable = sineTable;
+//    break;
+//  case square:
+//    currentLookupTable = squareTable;
+//    break;
+//  case sawtooth:
+//    currentLookupTable = sawtoothTable;
+//    break;
+//  default:
+//    break;
+//  }
+//}
 
 void change_Attack_Table(int attackVal)
 {
@@ -361,7 +363,7 @@
     
 void buffer_Samples(void){
     for(int j=0;j<num_samples;j++){
-    accumulator_Increment(); // Increment the phase accumulator
+    //accumulator_Increment(); // Increment the phase accumulator
     lookupTableIndex = phaseAccumulator >> 24; // Get address into wavetable    
     sampleBuffer.push_back(currentLookupTable[lookupTableIndex] / 255);  // divide by 255 so that we get values between 0 and 1
     }
@@ -466,13 +468,14 @@
 void parse_message()
 {
     PC.printf("Parse_message was called");
-    if(Blue.readable())
+    while(Blue.readable())
     {
-    keyPress = Blue.getc();
-    PC.putc(keyPress);
-    readyFlag = false;
-    PC.printf("\n\r Value of readyFlag is: %i",readyFlag);
-    PC.printf("Value of keyPress is: %c\n\r",keyPress);
+        keyPress = Blue.getc();
+        PC.putc(keyPress);
+        readyFlag = true;
+        PC.printf("\n\r Value of readyFlag is: %i",readyFlag);
+        PC.printf("Value of keyPress is: %c\n\r",keyPress);
+        wait(1);
     }
 }
 
@@ -537,34 +540,34 @@
 
 int main()
 {
-    
     Thread thread1(uLCD_Display_Thread);
     
     // make directory to hold the record of notes played
     mkdir("/sd/noteRecords", 0777);
 
     initialize_sustainVals();      // fill the lookup tables with the sustain values in them
+    
+    PC.baud(9600);
+    Blue.baud(9600);
 
     //attach interrupt function for each new Bluetooth serial character
     Blue.attach(&parse_message,Serial::RxIrq);
     while(1) {
-        //PC.printf("Main loop reached.");
         //check for a new button message ready
-        if((keyPress == 'Z') && (readyFlag)){ // button Z pressed
-            PC.printf("Got a Z");
+        if((keyPress == C_NOTE_KEY) && (readyFlag)){ // button Z pressed
             set_Note_Freq(noteArray[currentOctave-1][0]);
             create_samples(noteFreq, myWave);
             write_to_SDCard('C');
-            readyFlag = true;
+            readyFlag = false;
             // Play note that corresponds to Z
-            }
+        }
         else if((keyPress == D_NOTE_KEY) && (readyFlag)){ // button X pressed
                 set_Note_Freq(noteArray[currentOctave-1][1]);
                 create_samples(noteFreq, myWave);
                 write_to_SDCard('D');
                 readyFlag = false;
             // Play note that corresponds to X
-            }
+        }
         else if((keyPress == E_NOTE_KEY) && (readyFlag)){ // button C pressed
             set_Note_Freq(noteArray[currentOctave-1][2]);
             create_samples(noteFreq, myWave);
@@ -572,7 +575,7 @@
             // Make note of which note was played in file on SD Card
             write_to_SDCard('E');
             readyFlag = false;
-            }
+        }
         else if((keyPress == F_NOTE_KEY) && (readyFlag)){ // button V pressed
             set_Note_Freq(noteArray[currentOctave-1][3]);
             create_samples(noteFreq, myWave);
@@ -580,7 +583,7 @@
             // Make note of which note was played in file on SD Card
             write_to_SDCard('F');
             readyFlag = false;
-            }
+        }
         else if((keyPress == G_NOTE_KEY) && (readyFlag)){ // button B pressed
             set_Note_Freq(noteArray[currentOctave-1][4]);
             create_samples(noteFreq, myWave);
@@ -588,7 +591,7 @@
             // Make note of which note was played in file on SD Card
             write_to_SDCard('G');
             readyFlag = false;
-            }
+        }
         else if((keyPress == A_NOTE_KEY) && (readyFlag)){ // button N pressed
             set_Note_Freq(noteArray[currentOctave][5]);
             create_samples(noteFreq, myWave);
@@ -596,7 +599,7 @@
             // Make note of which note was played in file on SD Card
             write_to_SDCard('A');
             readyFlag = false;
-            }
+        }
         else if((keyPress == B_NOTE_KEY) && (readyFlag)){ // button M pressed
             set_Note_Freq(noteArray[currentOctave][6]);
             create_samples(noteFreq, myWave);
@@ -604,21 +607,23 @@
             // Make note of which note was played in file on SD Card
             write_to_SDCard('B');
             readyFlag = false;
-            }
+        }
         else if((keyPress == RAISE_OCTAVE_KEY) && (readyFlag)){ // button O pressed
             // Raise an octave
             if(currentOctave < 7)
             currentOctave++;
             else
             printf("Cannot raise octave above 7.\r\n");
-            }
+            readyFlag = false;
+        }
         else if((keyPress == LOWER_OCTAVE_KEY) && (readyFlag)){ // button L pressed
             // Lower an octave
             if(currentOctave > 1)
             currentOctave--;
             else
             printf("Cannot lower octave below 1.\r\n");
-            }
+            readyFlag = false;
+        }
         else if((keyPress == RAISE_ATTACK_KEY) && (readyFlag)){ // button Q pressed
             // Raise Attack Value
             if(currentAttackVal < 5){
@@ -627,7 +632,8 @@
             }
             else
             printf("Cannot raise value above 5.\r\n");
-            }
+            readyFlag = false;
+        }
         else if((keyPress == LOWER_ATTACK_KEY) && (readyFlag)){ // button A pressed
             // Lower Attack Value
             if(currentAttackVal > 1){
@@ -636,7 +642,8 @@
             }
             else
             printf("Cannot lower value below 1.\r\n");
-            }
+            readyFlag = false;
+        }
         else if((keyPress == RAISE_DELAY_KEY) && (readyFlag)){ // button W pressed
             // Raise Delay Value
             if(currentDecayVal < 5){
@@ -645,7 +652,8 @@
             }
             else
             printf("Cannot raise value above 5.\r\n");
-            }
+            readyFlag = false;
+        }
         else if((keyPress == LOWER_DELAY_KEY) && (readyFlag)){ // button S pressed
             // Lower Delay Value
             if(currentDecayVal > 1){
@@ -654,7 +662,8 @@
             }
             else
             printf("Cannot lower value below 1.\r\n");
-            }
+            readyFlag = false;
+        }
         else if((keyPress == RAISE_SUSTAIN_KEY) && (readyFlag)){ // button E pressed
             // Raise Sustain Value
             if(currentSustainVal < 5){
@@ -663,7 +672,8 @@
             }
             else
             printf("Cannot raise value above 5.\r\n");
-            }
+            readyFlag = false;
+        }
         else if((keyPress == LOWER_SUSTAIN_KEY) && (readyFlag)){ // button D pressed
             // Lower Sustain Value
             if(currentSustainVal > 1){
@@ -672,7 +682,8 @@
             }
             else
             printf("Cannot lower value below 1.\r\n");
-            }
+            readyFlag = false;
+        }
         else if((keyPress == RAISE_RELEASE_KEY) && (readyFlag)){ // button R pressed
             // Raise Release Value
             if(currentReleaseVal < 5){
@@ -681,7 +692,8 @@
             }
             else
             printf("Cannot raise value above 5.\r\n");
-            }
+            readyFlag = false;
+        }
         else if((keyPress == LOWER_RELEASE_KEY) && (readyFlag)){ // button F pressed
             // Lower Release Value
             if(currentReleaseVal > 1){
@@ -690,50 +702,54 @@
             }
             else
             printf("Cannot lower value below 1.\r\n");
-            }
+            readyFlag = false;
+        }
         else if((keyPress == CHANGE_WAVESHAPE_UP) && (readyFlag)){ // button T pressed
             // Change waveform shape to next waveform type
             switch(myWave){
                 case sine:
                     myWave = square;
-                    change_Wave(myWave);
+                    //change_Wave(myWave);
                     break;
                 case square:
                     myWave = sawtooth;
-                    change_Wave(myWave);
+                    //change_Wave(myWave);
                     break;
                 case sawtooth:
                     myWave = sine;
-                    change_Wave(myWave);
+                    //change_Wave(myWave);
                     break;
                 default:
                 break;
                 }
-            }
+                readyFlag = false;
+        }
         else if((keyPress == CHANGE_WAVESHAPE_DOWN) && (readyFlag)){ // button G pressed
             // Change waveform shape to previous waveform type
             switch(myWave){
                 case sine:
                     myWave = sawtooth;
-                    change_Wave(myWave);
+                    //change_Wave(myWave);
                     break;
                 case square:
                     myWave = sine;
-                    change_Wave(myWave);
+                    //change_Wave(myWave);
                     break;
                 case sawtooth:
                     myWave = square;
-                    change_Wave(myWave);
+                    //change_Wave(myWave);
                     break;
                 default:
                 break;
                 }
-            }
+                readyFlag = false;
+                
+        }
         
         //do other tasks in main - interrupts will process button message characters
-        myled = 1;
-        wait(2);
-        myled = 0;
-        wait(2);
+        //myled = 1;
+//        wait(2);
+//        myled = 0;
+//        wait(2);
     }
 }