play songs

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
maclobdell
Date:
Sat May 21 17:20:20 2016 +0000
Parent:
6:406f73a0eb49
Commit message:
remove rtos library. not needed for this bare-metal example.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show diff for this revision Revisions of this file
diff -r 406f73a0eb49 -r 7adef7c066ea main.cpp
--- a/main.cpp	Wed May 18 18:45:55 2016 +0000
+++ b/main.cpp	Sat May 21 17:20:20 2016 +0000
@@ -19,7 +19,39 @@
 static void play_song(int notes_left, int* melody, int* duration) {
     
     // YOUR CODE HERE
-
+ // melody and duration are pointers, they point to the array of tones and durations we declared earlier
+    // every time we play a note we up these pointers (move one element forward)
+    // so the current tone is always the first element of melody (same for duration)
+ 
+    int length;
+ 
+    while(notes_left > 0)
+    {
+ 
+        int tone = melody[0];
+        // BPM is quarter notes per minute, so length in milliseconds is:
+        length = static_cast<int>(static_cast<float>(1000 / duration[0]) * (60000.0f / static_cast<float>(BPM * 1000)));
+ 
+        play_tone(tone);
+ 
+        // after half the length of this tone, we silence
+        wait_ms(length / 2);  
+        silence();
+ 
+        //after the full length of this tone, call next note 
+        wait_ms(length); 
+                   
+        // after the full length of this tone, we up the melody, and down the notes_left
+        
+        notes_left--;
+        melody++;
+        duration++;
+            
+    }  
+ 
+    // we're done! just finish this note and silence
+    wait_ms(length / 2);  
+    silence();   
 }
 
 // this code runs when the microcontroller starts up
diff -r 406f73a0eb49 -r 7adef7c066ea mbed-rtos.lib
--- a/mbed-rtos.lib	Wed May 18 18:45:55 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://developer.mbed.org/users/mbed_official/code/mbed-rtos/#031a41d65add