ethernet for mbed2

Dependencies:   IoTsecuritySys mbed-rtos mbed

Committer:
jsmith352
Date:
Tue Dec 08 23:27:29 2015 +0000
Revision:
0:009a138526c5
ethernet for mbed2

Who changed what in which revision?

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