Nick Zhao
/
Lab2Part14
Lab2Part14
Revision 0:59e9c82b3e5f, committed 2019-10-07
- Comitter:
- TCNoodleshop
- Date:
- Mon Oct 07 02:40:31 2019 +0000
- Commit message:
- Lab2Part14;
Changed in this revision
diff -r 000000000000 -r 59e9c82b3e5f SongPlayer.h --- /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
diff -r 000000000000 -r 59e9c82b3e5f main.cpp --- /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); + } +} +
diff -r 000000000000 -r 59e9c82b3e5f mbed.bld --- /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