![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
3872 Play-live--test code - tempting to make this the main one
SongPlayer.h@2:9296823ea33d, 2020-11-11 (annotated)
- Committer:
- chenchen2020
- Date:
- Wed Nov 11 04:50:30 2020 +0000
- Revision:
- 2:9296823ea33d
- Parent:
- 1:c704bea518d8
Final;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chenchen2020 | 1:c704bea518d8 | 1 | #include "mbed.h" |
chenchen2020 | 1:c704bea518d8 | 2 | // new class to play a note on Speaker based on PwmOut class |
chenchen2020 | 1:c704bea518d8 | 3 | class SongPlayer |
chenchen2020 | 1:c704bea518d8 | 4 | { |
chenchen2020 | 1:c704bea518d8 | 5 | public: |
chenchen2020 | 1:c704bea518d8 | 6 | SongPlayer(PinName pin) : _pin(pin) { |
chenchen2020 | 1:c704bea518d8 | 7 | // _pin(pin) means pass pin to the constructor |
chenchen2020 | 1:c704bea518d8 | 8 | } |
chenchen2020 | 1:c704bea518d8 | 9 | // class method to play a note based on PwmOut class |
chenchen2020 | 1:c704bea518d8 | 10 | void PlaySong(float frequency[], float duration[], float volume=1.0) { |
chenchen2020 | 1:c704bea518d8 | 11 | vol = volume; |
chenchen2020 | 1:c704bea518d8 | 12 | notecount = 0; |
chenchen2020 | 1:c704bea518d8 | 13 | _pin.period(1.0/frequency[notecount]); |
chenchen2020 | 1:c704bea518d8 | 14 | _pin = volume/2.0; |
chenchen2020 | 1:c704bea518d8 | 15 | noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); |
chenchen2020 | 1:c704bea518d8 | 16 | // setup timer to interrupt for next note to play |
chenchen2020 | 1:c704bea518d8 | 17 | frequencyptr = frequency; |
chenchen2020 | 1:c704bea518d8 | 18 | durationptr = duration; |
chenchen2020 | 1:c704bea518d8 | 19 | //returns after first note starts to play |
chenchen2020 | 1:c704bea518d8 | 20 | } |
chenchen2020 | 2:9296823ea33d | 21 | void Play_Note(float frequency){ |
chenchen2020 | 2:9296823ea33d | 22 | _pin.period(1.0/frequency); |
chenchen2020 | 1:c704bea518d8 | 23 | _pin = 1.0/2.0; |
chenchen2020 | 1:c704bea518d8 | 24 | } |
chenchen2020 | 1:c704bea518d8 | 25 | void nextnote(); |
chenchen2020 | 1:c704bea518d8 | 26 | private: |
chenchen2020 | 1:c704bea518d8 | 27 | Timeout noteduration; |
chenchen2020 | 1:c704bea518d8 | 28 | PwmOut _pin; |
chenchen2020 | 1:c704bea518d8 | 29 | int notecount; |
chenchen2020 | 1:c704bea518d8 | 30 | float vol; |
chenchen2020 | 1:c704bea518d8 | 31 | float * frequencyptr; |
chenchen2020 | 1:c704bea518d8 | 32 | float * durationptr; |
chenchen2020 | 1:c704bea518d8 | 33 | }; |
chenchen2020 | 1:c704bea518d8 | 34 | //Interrupt Routine to play next note |
chenchen2020 | 1:c704bea518d8 | 35 | void SongPlayer::nextnote() |
chenchen2020 | 1:c704bea518d8 | 36 | { |
chenchen2020 | 1:c704bea518d8 | 37 | _pin = 0.0; |
chenchen2020 | 1:c704bea518d8 | 38 | notecount++; //setup next note in song |
chenchen2020 | 1:c704bea518d8 | 39 | if (durationptr[notecount]!=0.0) { |
chenchen2020 | 1:c704bea518d8 | 40 | _pin.period(1.0/frequencyptr[notecount]); |
chenchen2020 | 1:c704bea518d8 | 41 | noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); |
chenchen2020 | 1:c704bea518d8 | 42 | _pin = vol/2.0; |
chenchen2020 | 1:c704bea518d8 | 43 | } else |
chenchen2020 | 1:c704bea518d8 | 44 | _pin = 0.0; //turn off on last note |
chenchen2020 | 1:c704bea518d8 | 45 | } |