Plays a song using PWM and timer interrupts. Returns once song starts.

Dependencies:   mbed

Fork of speaker_demo_PWM by jim hamblen

Files at this revision

API Documentation at this revision

Comitter:
4180_1
Date:
Tue Oct 21 03:00:15 2014 +0000
Parent:
0:b2fdf3770282
Commit message:
ver 1.0

Changed in this revision

SongPlayer.h Show annotated file Show diff for this revision Revisions of this file
Speaker.h Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r b2fdf3770282 -r 2e6ea42675c7 SongPlayer.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SongPlayer.h	Tue Oct 21 03:00:15 2014 +0000
@@ -0,0 +1,41 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class SongPlayer
+{
+public:
+    SongPlayer(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the constructor
+    }
+// class method to play a note based on PwmOut class
+    void PlaySong(float frequency[], float duration[], float volume=1.0) {
+        vol = volume;
+        notecount = 0;
+        _pin.period(1.0/frequency[notecount]);
+        _pin = volume/2.0;
+        noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
+        // setup timer to interrupt for next note to play
+        frequencyptr = frequency;
+        durationptr = duration;
+        //returns after first note starts to play
+    }
+    void nextnote();
+private:
+    Timeout noteduration;
+    PwmOut _pin;
+    int notecount;
+    float vol;
+    float * frequencyptr;
+    float * durationptr;
+};
+//Interrupt Routine to play next note
+void SongPlayer::nextnote()
+{
+    _pin = 0.0;
+    notecount++; //setup next note in song
+    if (durationptr[notecount]!=0.0) {
+        _pin.period(1.0/frequencyptr[notecount]);
+        noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
+        _pin = vol/2.0;
+    } else
+        _pin = 0.0; //turn off on last note
+}
\ No newline at end of file
diff -r b2fdf3770282 -r 2e6ea42675c7 Speaker.h
--- a/Speaker.h	Sun Jan 20 03:02:37 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,19 +0,0 @@
-#include "mbed.h"
-// new class to play a note on Speaker based on PwmOut class
-class Speaker
-{
-public:
-    Speaker(PinName pin) : _pin(pin) {
-// _pin(pin) means pass pin to the Speaker Constructor
-    }
-// class method to play a note based on PwmOut class
-    void PlayNote(float frequency, float duration, float volume) {
-        _pin.period(1.0/frequency);
-        _pin = volume/2.0;
-        wait(duration);
-        _pin = 0.0;
-    }
-
-private:
-    PwmOut _pin;
-};
\ No newline at end of file
diff -r b2fdf3770282 -r 2e6ea42675c7 main.cpp
--- a/main.cpp	Sun Jan 20 03:02:37 2013 +0000
+++ b/main.cpp	Tue Oct 21 03:00:15 2014 +0000
@@ -1,19 +1,32 @@
 #include "mbed.h"
-#include "Speaker.h"
+#include "SongPlayer.h"
 
-// Speaker test program - euro police style siren now using new Speaker class method
+// Song test program - plays a song using PWM and timer interrupts
 // for documentation see http://mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/
-// can also be used to play a song, if you have the notes and durations
+// can be used to play a song, if you have the notes and durations
 // for musical note frequencies see http://en.wikipedia.org/wiki/Piano_key_frequencies
 
+//Set up notes and durations for sample song to play
+// A 0.0 duration note at end terminates song play
+float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9,
+                 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0
+                };
+float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24,
+                     0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0
+                    };
+
+DigitalOut led1(LED1);
 int main()
 {
-// setup instance of new Speaker class, mySpeaker using pin 21
+// setup instance of new SongPlayer class, mySpeaker using pin 26
 // the pin must be a PWM output pin
-    Speaker mySpeaker(p21);
-    // loops forever playing two notes on speaker
+    SongPlayer mySpeaker(p26);
+// Start song and return once playing starts
+    mySpeaker.PlaySong(note,duration);
+    // loops forever while song continues to play to end using interrupts
     while(1) {
-        mySpeaker.PlayNote(969.0,0.5,0.5);
-        mySpeaker.PlayNote(800.0,0.5,0.5);
+        led1 = !led1;
+        wait(.1);
     }
 }
+