Whack a Mole game! Features: - LCD graphics display - Touch pad input - Speaker effects through a class D audio amplifier\ - A high score page maintained by the SD card file system - Analog noise used to seed random numbers
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed
Fork of MPR121_Demo by
SongPlayer.h@5:070755be0a77, 2016-03-14 (annotated)
- Committer:
- tpettet3
- Date:
- Mon Mar 14 05:36:09 2016 +0000
- Revision:
- 5:070755be0a77
before speaker;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tpettet3 | 5:070755be0a77 | 1 | #include "mbed.h" |
tpettet3 | 5:070755be0a77 | 2 | // new class to play a note on Speaker based on PwmOut class |
tpettet3 | 5:070755be0a77 | 3 | class SongPlayer |
tpettet3 | 5:070755be0a77 | 4 | { |
tpettet3 | 5:070755be0a77 | 5 | public: |
tpettet3 | 5:070755be0a77 | 6 | SongPlayer(PinName pin) : _pin(pin) { |
tpettet3 | 5:070755be0a77 | 7 | // _pin(pin) means pass pin to the constructor |
tpettet3 | 5:070755be0a77 | 8 | } |
tpettet3 | 5:070755be0a77 | 9 | // class method to play a note based on PwmOut class |
tpettet3 | 5:070755be0a77 | 10 | void PlaySong(float frequency[], float duration[], float volume=1.0) { |
tpettet3 | 5:070755be0a77 | 11 | vol = volume; |
tpettet3 | 5:070755be0a77 | 12 | notecount = 0; |
tpettet3 | 5:070755be0a77 | 13 | _pin.period(1.0/frequency[notecount]); |
tpettet3 | 5:070755be0a77 | 14 | _pin = volume/2.0; |
tpettet3 | 5:070755be0a77 | 15 | noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); |
tpettet3 | 5:070755be0a77 | 16 | // setup timer to interrupt for next note to play |
tpettet3 | 5:070755be0a77 | 17 | frequencyptr = frequency; |
tpettet3 | 5:070755be0a77 | 18 | durationptr = duration; |
tpettet3 | 5:070755be0a77 | 19 | //returns after first note starts to play |
tpettet3 | 5:070755be0a77 | 20 | } |
tpettet3 | 5:070755be0a77 | 21 | void nextnote(); |
tpettet3 | 5:070755be0a77 | 22 | private: |
tpettet3 | 5:070755be0a77 | 23 | Timeout noteduration; |
tpettet3 | 5:070755be0a77 | 24 | PwmOut _pin; |
tpettet3 | 5:070755be0a77 | 25 | int notecount; |
tpettet3 | 5:070755be0a77 | 26 | float vol; |
tpettet3 | 5:070755be0a77 | 27 | float * frequencyptr; |
tpettet3 | 5:070755be0a77 | 28 | float * durationptr; |
tpettet3 | 5:070755be0a77 | 29 | }; |
tpettet3 | 5:070755be0a77 | 30 | //Interrupt Routine to play next note |
tpettet3 | 5:070755be0a77 | 31 | void SongPlayer::nextnote() |
tpettet3 | 5:070755be0a77 | 32 | { |
tpettet3 | 5:070755be0a77 | 33 | _pin = 0.0; |
tpettet3 | 5:070755be0a77 | 34 | notecount++; //setup next note in song |
tpettet3 | 5:070755be0a77 | 35 | if (durationptr[notecount]!=0.0) { |
tpettet3 | 5:070755be0a77 | 36 | _pin.period(1.0/frequencyptr[notecount]); |
tpettet3 | 5:070755be0a77 | 37 | noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); |
tpettet3 | 5:070755be0a77 | 38 | _pin = vol/2.0; |
tpettet3 | 5:070755be0a77 | 39 | } else |
tpettet3 | 5:070755be0a77 | 40 | _pin = 0.0; //turn off on last note |
tpettet3 | 5:070755be0a77 | 41 | } |
tpettet3 | 5:070755be0a77 | 42 |