plays a fucking annoying sound for 10 seconds and loops.

Dependencies:   mbed

Fork of song_demo_PWM by jim hamblen

Committer:
mganseij
Date:
Thu Oct 29 10:34:57 2015 +0000
Revision:
7:29dd200668db
Parent:
3:89b8dea1cf17
newest version thingy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 1:2e6ea42675c7 1 #include "mbed.h"
mganseij 3:89b8dea1cf17 2 bool keepplaying = true;
4180_1 1:2e6ea42675c7 3 // new class to play a note on Speaker based on PwmOut class
4180_1 1:2e6ea42675c7 4 class SongPlayer
4180_1 1:2e6ea42675c7 5 {
4180_1 1:2e6ea42675c7 6 public:
4180_1 1:2e6ea42675c7 7 SongPlayer(PinName pin) : _pin(pin) {
4180_1 1:2e6ea42675c7 8 // _pin(pin) means pass pin to the constructor
4180_1 1:2e6ea42675c7 9 }
4180_1 1:2e6ea42675c7 10 // class method to play a note based on PwmOut class
4180_1 1:2e6ea42675c7 11 void PlaySong(float frequency[], float duration[], float volume=1.0) {
4180_1 1:2e6ea42675c7 12 vol = volume;
4180_1 1:2e6ea42675c7 13 notecount = 0;
4180_1 1:2e6ea42675c7 14 _pin.period(1.0/frequency[notecount]);
4180_1 1:2e6ea42675c7 15 _pin = volume/2.0;
4180_1 1:2e6ea42675c7 16 noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
4180_1 1:2e6ea42675c7 17 // setup timer to interrupt for next note to play
4180_1 1:2e6ea42675c7 18 frequencyptr = frequency;
4180_1 1:2e6ea42675c7 19 durationptr = duration;
4180_1 1:2e6ea42675c7 20 //returns after first note starts to play
4180_1 1:2e6ea42675c7 21 }
4180_1 1:2e6ea42675c7 22 void nextnote();
4180_1 1:2e6ea42675c7 23 private:
4180_1 1:2e6ea42675c7 24 Timeout noteduration;
4180_1 1:2e6ea42675c7 25 PwmOut _pin;
4180_1 1:2e6ea42675c7 26 int notecount;
4180_1 1:2e6ea42675c7 27 float vol;
4180_1 1:2e6ea42675c7 28 float * frequencyptr;
4180_1 1:2e6ea42675c7 29 float * durationptr;
4180_1 1:2e6ea42675c7 30 };
4180_1 1:2e6ea42675c7 31 //Interrupt Routine to play next note
4180_1 1:2e6ea42675c7 32 void SongPlayer::nextnote()
4180_1 1:2e6ea42675c7 33 {
mganseij 3:89b8dea1cf17 34 if(keepplaying==true){
mganseij 3:89b8dea1cf17 35 _pin = 0.0;
mganseij 3:89b8dea1cf17 36 notecount++; //setup next note in song
mganseij 3:89b8dea1cf17 37 if (durationptr[notecount]!=0.0) {
mganseij 3:89b8dea1cf17 38 _pin.period(1.0/frequencyptr[notecount]);
mganseij 3:89b8dea1cf17 39 noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
mganseij 3:89b8dea1cf17 40 _pin = vol/2.0;
mganseij 3:89b8dea1cf17 41 } else
mganseij 3:89b8dea1cf17 42 _pin = 0.0; //turn off on last note
mganseij 3:89b8dea1cf17 43 }
4180_1 1:2e6ea42675c7 44 }