simple game using 2 mbed devices
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed LSM9DS0
player2/SongPlayer.h@0:8016d44e8294, 2015-10-20 (annotated)
- Committer:
- bfoley13
- Date:
- Tue Oct 20 20:04:10 2015 +0000
- Revision:
- 0:8016d44e8294
Project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bfoley13 | 0:8016d44e8294 | 1 | //#include "mbed.h" |
bfoley13 | 0:8016d44e8294 | 2 | // new class to play a note on Speaker based on PwmOut class |
bfoley13 | 0:8016d44e8294 | 3 | class SongPlayer |
bfoley13 | 0:8016d44e8294 | 4 | { |
bfoley13 | 0:8016d44e8294 | 5 | public: |
bfoley13 | 0:8016d44e8294 | 6 | SongPlayer(PinName pin) : _pin(pin) { |
bfoley13 | 0:8016d44e8294 | 7 | // _pin(pin) means pass pin to the constructor |
bfoley13 | 0:8016d44e8294 | 8 | } |
bfoley13 | 0:8016d44e8294 | 9 | // class method to play a note based on PwmOut class |
bfoley13 | 0:8016d44e8294 | 10 | void PlaySong(float frequency[], float duration[], float volume=1.0) { |
bfoley13 | 0:8016d44e8294 | 11 | vol = volume; |
bfoley13 | 0:8016d44e8294 | 12 | notecount = 0; |
bfoley13 | 0:8016d44e8294 | 13 | _pin.period(1.0/frequency[notecount]); |
bfoley13 | 0:8016d44e8294 | 14 | _pin = volume/2.0; |
bfoley13 | 0:8016d44e8294 | 15 | noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); |
bfoley13 | 0:8016d44e8294 | 16 | // setup timer to interrupt for next note to play |
bfoley13 | 0:8016d44e8294 | 17 | frequencyptr = frequency; |
bfoley13 | 0:8016d44e8294 | 18 | durationptr = duration; |
bfoley13 | 0:8016d44e8294 | 19 | //returns after first note starts to play |
bfoley13 | 0:8016d44e8294 | 20 | } |
bfoley13 | 0:8016d44e8294 | 21 | void nextnote(); |
bfoley13 | 0:8016d44e8294 | 22 | int getDuration(); |
bfoley13 | 0:8016d44e8294 | 23 | int getNoteCount(); |
bfoley13 | 0:8016d44e8294 | 24 | |
bfoley13 | 0:8016d44e8294 | 25 | private: |
bfoley13 | 0:8016d44e8294 | 26 | Timeout noteduration; |
bfoley13 | 0:8016d44e8294 | 27 | PwmOut _pin; |
bfoley13 | 0:8016d44e8294 | 28 | int notecount; |
bfoley13 | 0:8016d44e8294 | 29 | float vol; |
bfoley13 | 0:8016d44e8294 | 30 | float * frequencyptr; |
bfoley13 | 0:8016d44e8294 | 31 | float * durationptr; |
bfoley13 | 0:8016d44e8294 | 32 | }; |
bfoley13 | 0:8016d44e8294 | 33 | //Interrupt Routine to play next note |
bfoley13 | 0:8016d44e8294 | 34 | void SongPlayer::nextnote() |
bfoley13 | 0:8016d44e8294 | 35 | { |
bfoley13 | 0:8016d44e8294 | 36 | _pin = 0.0; |
bfoley13 | 0:8016d44e8294 | 37 | notecount++; //setup next note in song |
bfoley13 | 0:8016d44e8294 | 38 | if (durationptr[notecount]!=0.0) { |
bfoley13 | 0:8016d44e8294 | 39 | _pin.period(1.0/frequencyptr[notecount]); |
bfoley13 | 0:8016d44e8294 | 40 | noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); |
bfoley13 | 0:8016d44e8294 | 41 | _pin = vol/2.0; |
bfoley13 | 0:8016d44e8294 | 42 | } else |
bfoley13 | 0:8016d44e8294 | 43 | _pin = 0.0; //turn off on last note |
bfoley13 | 0:8016d44e8294 | 44 | } |
bfoley13 | 0:8016d44e8294 | 45 | |
bfoley13 | 0:8016d44e8294 | 46 | int SongPlayer::getDuration(){ |
bfoley13 | 0:8016d44e8294 | 47 | return sizeof(durationptr)/sizeof(durationptr[0]); |
bfoley13 | 0:8016d44e8294 | 48 | } |
bfoley13 | 0:8016d44e8294 | 49 | |
bfoley13 | 0:8016d44e8294 | 50 | int SongPlayer::getNoteCount(){ |
bfoley13 | 0:8016d44e8294 | 51 | return notecount; |
bfoley13 | 0:8016d44e8294 | 52 | } |