Lab2Part14

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
TCNoodleshop
Date:
Mon Oct 07 02:40:31 2019 +0000
Commit message:
Lab2Part14;

Changed in this revision

SongPlayer.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SongPlayer.h	Mon Oct 07 02:40:31 2019 +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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Oct 07 02:40:31 2019 +0000
@@ -0,0 +1,32 @@
+#include "mbed.h"
+#include "SongPlayer.h"
+
+// 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 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 SongPlayer class, mySpeaker using pin 26
+// the pin must be a PWM output pin
+    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) {
+        led1 = !led1;
+        wait(.1);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Oct 07 02:40:31 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file