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