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