Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
SongPlayer.h@0:21fea82c85fc, 2016-05-15 (annotated)
- Committer:
- Elecia
- Date:
- Sun May 15 18:06:06 2016 +0000
- Revision:
- 0:21fea82c85fc
Blinking works, colors work. No speaker yet.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Elecia | 0:21fea82c85fc | 1 | #include "mbed.h" |
| Elecia | 0:21fea82c85fc | 2 | // new class to play a note on Speaker based on PwmOut class |
| Elecia | 0:21fea82c85fc | 3 | // https://developer.mbed.org/users/4180_1/notebook/using-a-speaker-for-audio-output/ |
| Elecia | 0:21fea82c85fc | 4 | class SongPlayer |
| Elecia | 0:21fea82c85fc | 5 | { |
| Elecia | 0:21fea82c85fc | 6 | public: |
| Elecia | 0:21fea82c85fc | 7 | SongPlayer(PinName pin) : _pin(pin) { |
| Elecia | 0:21fea82c85fc | 8 | // _pin(pin) means pass pin to the constructor |
| Elecia | 0:21fea82c85fc | 9 | } |
| Elecia | 0:21fea82c85fc | 10 | // class method to play a note based on PwmOut class |
| Elecia | 0:21fea82c85fc | 11 | void PlaySong(float frequency[], float duration[], float volume=1.0) { |
| Elecia | 0:21fea82c85fc | 12 | vol = volume; |
| Elecia | 0:21fea82c85fc | 13 | notecount = 0; |
| Elecia | 0:21fea82c85fc | 14 | _pin.period(1.0/frequency[notecount]); |
| Elecia | 0:21fea82c85fc | 15 | _pin = volume/2.0; |
| Elecia | 0:21fea82c85fc | 16 | noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); |
| Elecia | 0:21fea82c85fc | 17 | // setup timer to interrupt for next note to play |
| Elecia | 0:21fea82c85fc | 18 | frequencyptr = frequency; |
| Elecia | 0:21fea82c85fc | 19 | durationptr = duration; |
| Elecia | 0:21fea82c85fc | 20 | //returns after first note starts to play |
| Elecia | 0:21fea82c85fc | 21 | } |
| Elecia | 0:21fea82c85fc | 22 | void nextnote(); |
| Elecia | 0:21fea82c85fc | 23 | private: |
| Elecia | 0:21fea82c85fc | 24 | Timeout noteduration; |
| Elecia | 0:21fea82c85fc | 25 | PwmOut _pin; |
| Elecia | 0:21fea82c85fc | 26 | int notecount; |
| Elecia | 0:21fea82c85fc | 27 | float vol; |
| Elecia | 0:21fea82c85fc | 28 | float * frequencyptr; |
| Elecia | 0:21fea82c85fc | 29 | float * durationptr; |
| Elecia | 0:21fea82c85fc | 30 | }; |
| Elecia | 0:21fea82c85fc | 31 | //Interrupt Routine to play next note |
| Elecia | 0:21fea82c85fc | 32 | void SongPlayer::nextnote() |
| Elecia | 0:21fea82c85fc | 33 | { |
| Elecia | 0:21fea82c85fc | 34 | _pin = 0.0; |
| Elecia | 0:21fea82c85fc | 35 | notecount++; //setup next note in song |
| Elecia | 0:21fea82c85fc | 36 | if (durationptr[notecount]!=0.0) { |
| Elecia | 0:21fea82c85fc | 37 | _pin.period(1.0/frequencyptr[notecount]); |
| Elecia | 0:21fea82c85fc | 38 | noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); |
| Elecia | 0:21fea82c85fc | 39 | _pin = vol/2.0; |
| Elecia | 0:21fea82c85fc | 40 | } else |
| Elecia | 0:21fea82c85fc | 41 | _pin = 0.0; //turn off on last note |
| Elecia | 0:21fea82c85fc | 42 | } |