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