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