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