Bluetooth Enabled Keyboard/Synthesizer for mbed

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

Revision:
3:3aba1d783730
Parent:
2:f06ba516b1ad
Child:
4:406f59c6a1a6
--- a/main.cpp	Wed Apr 27 21:13:45 2016 +0000
+++ b/main.cpp	Wed Apr 27 22:40:03 2016 +0000
@@ -1,11 +1,22 @@
 #include "mbed.h"
+#include "SDFileSystem.h"
+#include "synthesizer.h"
 Serial Blue(p28,p27);
 Serial PC(USBTX,USBRX);
 DigitalOut myled(LED1);
 DigitalOut myled4(LED4);
+
+SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card setup
+
 //global variables for main and interrupt routine
 volatile bool readyFlag = true;
 volatile char keyPress;
+WaveType myWave = sine; // default to sine wave
+volatile int currentOctave = 4; // default to 4 because thats where middle C is
+volatile int currentAttackVal = 3;   // values will range from 1-5, default to 3
+volatile int currentDelayVal = 3;    // values will range from 1-5, default to 3
+volatile int currentSustainVal = 3;  // values will range from 1-5, default to 3
+volatile int currentReleaseVal = 3;  // values will range from 1-5, default to 3
 //Interrupt routine to parse message with one new character per serial RX interrupt
 void parse_message()
 {
@@ -16,40 +27,112 @@
     
     //PC.printf("Value of keyPress is: %c\n\r",keyPress);
 }
- 
- 
+
+
+/*
+This function writes which note was just played to a text file on the SDCard.
+The note played will be encoded in hexadecimal, as well as the octave, Attack Value,
+Delay Value, Sustain Value, and Release Value. The format of the bits will be as follows:
+| 3 bits | 3 bits | 3 bits | 3 bits | 3 bits | 3 bits |
+| Attack | Delay | Susttain | Release | Octave | Note |
+For the 3 bits representing note, A will correspond to 1, B to 2, and so on.
+For example, if the lower 3 bits corresponding to note are 001, then the note is an A.
+
+@param: The note that is being played/recorded into the text file
+*/
+
+void write_to_SDCard(char note)
+{
+    int AttackBits, SustainBits, DelayBits, ReleaseBits, OctaveBits, NoteBits;
  
-WaveType myWave = sine; // default to sine wave
-int currentOctave = 4; // default to 4 because thats where middle C is
- 
- 
+    AttackBits = currentAttackVal;
+    DelayBits = currentDelayVal;
+    SustainBits = currentSustainVal;
+    ReleaseBits = currentReleaseVal;
+    OctaveBits = currentOctave;
+    switch(note){
+        case 'C':
+            NoteBits = 3;
+            break;
+        case 'D':
+            NoteBits = 4;
+            break;
+        case 'E':
+            NoteBits = 5;
+            break;
+        case 'F':
+            NoteBits = 6;
+            break;
+        case 'G':
+            NoteBits = 7;
+            break;
+        case 'A':
+            NoteBits = 1;
+            break;
+        case 'B':
+            NoteBits = 2;
+            break;
+        default:
+            NoteBits = 0;
+            break;
+        }
+    int writeVal;
+    writeVal  = (AttackBits << 15) | (DelayBits << 12) | (SustainBits << 9) | (ReleaseBits << 6) | (OctaveBits << 3) | (NoteBits);
+    
+    FILE *fp = fopen("/sd/noteRecords/note_record_01.txt", "w");
+    if(fp == NULL) {
+        error("Could not open file for write\n");
+    }
+    fprintf(fp,"%X\r\n",writeVal);      // writes value to the text file in hexadecimal
+    fclose(fp);
+}
+
 int main()
 {
+// make directory to hold the record of notes played
+mkdir("/sd/noteRecords", 0777);
+
+
 //attach interrupt function for each new Bluetooth serial character
     Blue.attach(&parse_message,Serial::RxIrq);
     while(1) {
         //check for a new button message ready
-        if((keyPress == C_KEY) && (readyFlag))// button Z pressed
-        {
+        if((keyPress == C_NOTE_KEY) && (readyFlag)){ // button Z pressed
             PC.printf("Got n Z");
+            write_to_SDCard('C');
             readyFlag = false;
             // Play note that corresponds to Z
             }
-        else if((keyPress == D_KEY) && (readyFlag)) // button X pressed
-            {
+        else if((keyPress == D_NOTE_KEY) && (readyFlag)){ // button X pressed
                 PC.printf("Got an X");
+                write_to_SDCard('D');
             // Play note that corresponds to X
             }
-        else if((keyPress == E_KEY && (readyFlag)){} // button C pressed
+        else if((keyPress == E_NOTE_KEY) && (readyFlag)){ // button C pressed
             // Play note that corresponds to C
-        else if((keyPress == F_KEY) && (readyFlag)){} // button V pressed
+            // Make note of which note was played in file on SD Card
+            write_to_SDCard('E');
+            }
+        else if((keyPress == F_NOTE_KEY) && (readyFlag)){ // button V pressed
             // Play note that corresponds to V
-        else if((keyPress == G_KEY) && (readyFlag)){} // button B pressed
+            // Make note of which note was played in file on SD Card
+            write_to_SDCard('F');
+            }
+        else if((keyPress == G_NOTE_KEY) && (readyFlag)){ // button B pressed
             // Play note that corresponds to B
-        else if((keyPress == A_KEY) && (readyFlag)){} // button N pressed
+            // Make note of which note was played in file on SD Card
+            write_to_SDCard('G');
+            }
+        else if((keyPress == A_NOTE_KEY) && (readyFlag)){ // button N pressed
             // Play note that corresponds to N
-        else if((keyPress == B_KEY) && (readyFlag)){} // button M pressed
+            // Make note of which note was played in file on SD Card
+            write_to_SDCard('A');
+            }
+        else if((keyPress == B_NOTE_KEY) && (readyFlag)){ // button M pressed
             // Play note that corresponds to M
+            // Make note of which note was played in file on SD Card
+            write_to_SDCard('B');
+            }
         else if((keyPress == RAISE_OCTAVE_KEY) && (readyFlag)){ // button O pressed
             // Raise an octave
             currentOctave++;