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