Sven Kugathasan / Mbed OS SKAFMO_2

Dependencies:   PID

Revision:
20:d796667e0c4d
Parent:
19:d79692cef6c7
Child:
21:828582e4d4ef
--- a/main.cpp	Fri Mar 24 19:47:50 2017 +0000
+++ b/main.cpp	Fri Mar 24 20:15:40 2017 +0000
@@ -67,9 +67,17 @@
 PID dist_pid(2, 0.0, 0.01, 0.1);        // (P, I, D, WAIT)
 
 //Initialize Threads 
-Thread pid_thread(osPriorityNormal, 512, NULL);
+//Thread pid_thread(osPriorityNormal, 512, NULL);
 Thread melody_thread(osPriorityNormal, 512, NULL);
 
+typedef struct{
+    char note;
+    uint8_t sharp;
+    float dur;
+}note_t;
+
+const float octaveMap[] = {27.50, 30.87, 16.35, 18.35, 20.60, 21.83, 24.50};
+
 /*________________________Motor Drive States__________________________________*/
 
 //Mapping from sequential drive states to motor phase outputs
@@ -132,6 +140,7 @@
 bool flag = false;
 float test_time = 0.0f;
 int8_t test = 0;
+int songLen = 0;
 
 // Timer interrupt
 Ticker calcPID_timer;
@@ -167,6 +176,23 @@
     return stateMap[InterruptI1.read() + 2*InterruptI2.read() + 4*InterruptI3.read()];
 }
 
+float returnNote(note_t n)
+{
+    unsigned octMult = 4;
+    //for (uint8_t i=0; i<n.oct; ++i){
+        //octMult = octMult << 1;
+    //}
+    return 1.0f/( octMult*octaveMap[n.note - 'A']);
+}
+
+void playMelody(note_t* song){
+    for (int i=0; i<songLen; ++i){
+        PWM_freq = returnNote(song[i]);
+        wait(song[i].dur);
+    }
+    delete[] song;
+}
+
 void calculatePID(){
     TIME = 1;
     //Calculate new PID Control Point
@@ -380,7 +406,10 @@
 
         if(t_loc==0 && input.length()>1){ //if melody marker present and there is something after it
             printf("Note sequence detected\r\n"); 
-            
+            //note_t* song = (note_t*)malloc(sizeof(note_t)*input.length());
+            note_t* song = new note_t[input.length()];
+            songLen = input.length();
+            uint8_t noteIter = 0;
             for(int i=1;i<input.length();i++){
             
                 if(accent_marker==false && dur_marker==false && note_marker==false && 
@@ -403,6 +432,11 @@
                     note_marker=false;
                     dur_marker=false;
                     accent_marker=false;
+                    
+                    song[noteIter].note = note[0];
+                    song[noteIter].sharp = 1;
+                    song[noteIter].dur = duration;
+                    noteIter++;
                 }
                 else if(note_marker==true && dur_marker==true && accent_marker==false){
                     note=input.substr(i-1,1);
@@ -412,8 +446,14 @@
                     note_marker=false;
                     dur_marker=false;
                     accent_marker=false;
+                    
+                    song[noteIter].note = note[0];
+                    song[noteIter].sharp = 0;
+                    song[noteIter].dur = duration;
+                    noteIter++;
                 }
             }
+            melody_thread.start(callback(playMelody, song));
         }   
         else if(t_loc==-1){ //if no melody marker present
             pc.printf("Note sequence NOT detected\r\n");