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