for publish

Dependencies:   HC_SR04_Ultrasonic_Library mbed

Committer:
jasonbx
Date:
Tue Mar 14 15:49:33 2017 +0000
Revision:
0:fded9bf799e2
For publish

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jasonbx 0:fded9bf799e2 1 #include "mbed.h"
jasonbx 0:fded9bf799e2 2 // new class to play a note on Speaker based on PwmOut class
jasonbx 0:fded9bf799e2 3 class SongPlayer
jasonbx 0:fded9bf799e2 4 {
jasonbx 0:fded9bf799e2 5 public:
jasonbx 0:fded9bf799e2 6 SongPlayer(PinName pin) : _pin(pin) {
jasonbx 0:fded9bf799e2 7 // _pin(pin) means pass pin to the constructor
jasonbx 0:fded9bf799e2 8 }
jasonbx 0:fded9bf799e2 9 // class method to play a note based on PwmOut class
jasonbx 0:fded9bf799e2 10 void PlaySong(float frequency[], float duration[], float volume=1.0) {
jasonbx 0:fded9bf799e2 11 vol = volume;
jasonbx 0:fded9bf799e2 12 notecount = 0;
jasonbx 0:fded9bf799e2 13 _pin.period(1.0/frequency[notecount]);
jasonbx 0:fded9bf799e2 14 _pin = volume/2.0;
jasonbx 0:fded9bf799e2 15 noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
jasonbx 0:fded9bf799e2 16 // setup timer to interrupt for next note to play
jasonbx 0:fded9bf799e2 17 frequencyptr = frequency;
jasonbx 0:fded9bf799e2 18 durationptr = duration;
jasonbx 0:fded9bf799e2 19 //returns after first note starts to play
jasonbx 0:fded9bf799e2 20 }
jasonbx 0:fded9bf799e2 21 void nextnote();
jasonbx 0:fded9bf799e2 22 private:
jasonbx 0:fded9bf799e2 23 Timeout noteduration;
jasonbx 0:fded9bf799e2 24 PwmOut _pin;
jasonbx 0:fded9bf799e2 25 int notecount;
jasonbx 0:fded9bf799e2 26 float vol;
jasonbx 0:fded9bf799e2 27 float * frequencyptr;
jasonbx 0:fded9bf799e2 28 float * durationptr;
jasonbx 0:fded9bf799e2 29 };
jasonbx 0:fded9bf799e2 30 //Interrupt Routine to play next note
jasonbx 0:fded9bf799e2 31 void SongPlayer::nextnote()
jasonbx 0:fded9bf799e2 32 {
jasonbx 0:fded9bf799e2 33 _pin = 0.0;
jasonbx 0:fded9bf799e2 34 notecount++; //setup next note in song
jasonbx 0:fded9bf799e2 35 if (durationptr[notecount]!=0.0) {
jasonbx 0:fded9bf799e2 36 _pin.period(1.0/frequencyptr[notecount]);
jasonbx 0:fded9bf799e2 37 noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
jasonbx 0:fded9bf799e2 38 _pin = vol/2.0;
jasonbx 0:fded9bf799e2 39 } else
jasonbx 0:fded9bf799e2 40 _pin = 0.0; //turn off on last note
jasonbx 0:fded9bf799e2 41 }