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