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