Interactive Alarm Clock Code
Dependencies: 4DGL-uLCD-SE mbed
SongPlayer.h@0:68a3851de2ee, 2015-12-10 (annotated)
- Committer:
- tshin7
- Date:
- Thu Dec 10 08:59:55 2015 +0000
- Revision:
- 0:68a3851de2ee
Alarm Clock
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tshin7 | 0:68a3851de2ee | 1 | #include "mbed.h" |
tshin7 | 0:68a3851de2ee | 2 | // new class to play a note on Speaker based on PwmOut class |
tshin7 | 0:68a3851de2ee | 3 | class SongPlayer |
tshin7 | 0:68a3851de2ee | 4 | { |
tshin7 | 0:68a3851de2ee | 5 | public: |
tshin7 | 0:68a3851de2ee | 6 | SongPlayer(PinName pin) : _pin(pin) { |
tshin7 | 0:68a3851de2ee | 7 | // _pin(pin) means pass pin to the constructor |
tshin7 | 0:68a3851de2ee | 8 | } |
tshin7 | 0:68a3851de2ee | 9 | // class method to play a note based on PwmOut class |
tshin7 | 0:68a3851de2ee | 10 | void PlaySong(float frequency[], float duration[], float volume=1.0) { |
tshin7 | 0:68a3851de2ee | 11 | vol = volume; |
tshin7 | 0:68a3851de2ee | 12 | notecount = 0; |
tshin7 | 0:68a3851de2ee | 13 | _pin.period(1.0/frequency[notecount]); |
tshin7 | 0:68a3851de2ee | 14 | _pin = volume/2.0; |
tshin7 | 0:68a3851de2ee | 15 | noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); |
tshin7 | 0:68a3851de2ee | 16 | // setup timer to interrupt for next note to play |
tshin7 | 0:68a3851de2ee | 17 | frequencyptr = frequency; |
tshin7 | 0:68a3851de2ee | 18 | durationptr = duration; |
tshin7 | 0:68a3851de2ee | 19 | //returns after first note starts to play |
tshin7 | 0:68a3851de2ee | 20 | } |
tshin7 | 0:68a3851de2ee | 21 | void nextnote(); |
tshin7 | 0:68a3851de2ee | 22 | private: |
tshin7 | 0:68a3851de2ee | 23 | Timeout noteduration; |
tshin7 | 0:68a3851de2ee | 24 | PwmOut _pin; |
tshin7 | 0:68a3851de2ee | 25 | int notecount; |
tshin7 | 0:68a3851de2ee | 26 | float vol; |
tshin7 | 0:68a3851de2ee | 27 | float * frequencyptr; |
tshin7 | 0:68a3851de2ee | 28 | float * durationptr; |
tshin7 | 0:68a3851de2ee | 29 | }; |
tshin7 | 0:68a3851de2ee | 30 | //Interrupt Routine to play next note |
tshin7 | 0:68a3851de2ee | 31 | void SongPlayer::nextnote() |
tshin7 | 0:68a3851de2ee | 32 | { |
tshin7 | 0:68a3851de2ee | 33 | _pin = 0.0; |
tshin7 | 0:68a3851de2ee | 34 | notecount++; //setup next note in song |
tshin7 | 0:68a3851de2ee | 35 | if (durationptr[notecount]!=0.0) { |
tshin7 | 0:68a3851de2ee | 36 | _pin.period(1.0/frequencyptr[notecount]); |
tshin7 | 0:68a3851de2ee | 37 | noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); |
tshin7 | 0:68a3851de2ee | 38 | _pin = vol/2.0; |
tshin7 | 0:68a3851de2ee | 39 | } else |
tshin7 | 0:68a3851de2ee | 40 | _pin = 0.0; //turn off on last note |
tshin7 | 0:68a3851de2ee | 41 | } |