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