Demo code for a Wi-Fi controlled wall outlet that monitors light intensity, temperature, and motion and can control which plugs are supplied with power based off of user preference.
Dependencies: BH1750 DHT11_Lib mbed
Fork of ECE4180_Lab4_ESP8266_IoT_Outlets by
SongPlayer.h@15:5a337e99f6f3, 2016-04-30 (annotated)
- Committer:
- tpettet3
- Date:
- Sat Apr 30 22:37:39 2016 +0000
- Revision:
- 15:5a337e99f6f3
Final/Demo code version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tpettet3 | 15:5a337e99f6f3 | 1 | #include "mbed.h" |
tpettet3 | 15:5a337e99f6f3 | 2 | // new class to play a note on Speaker based on PwmOut class |
tpettet3 | 15:5a337e99f6f3 | 3 | class SongPlayer |
tpettet3 | 15:5a337e99f6f3 | 4 | { |
tpettet3 | 15:5a337e99f6f3 | 5 | public: |
tpettet3 | 15:5a337e99f6f3 | 6 | SongPlayer(PinName pin) : _pin(pin) { |
tpettet3 | 15:5a337e99f6f3 | 7 | // _pin(pin) means pass pin to the constructor |
tpettet3 | 15:5a337e99f6f3 | 8 | } |
tpettet3 | 15:5a337e99f6f3 | 9 | // class method to play a note based on PwmOut class |
tpettet3 | 15:5a337e99f6f3 | 10 | void PlaySong(float frequency[], float duration[], float volume=1.0) { |
tpettet3 | 15:5a337e99f6f3 | 11 | vol = volume; |
tpettet3 | 15:5a337e99f6f3 | 12 | notecount = 0; |
tpettet3 | 15:5a337e99f6f3 | 13 | _pin.period(1.0/frequency[notecount]); |
tpettet3 | 15:5a337e99f6f3 | 14 | _pin = volume/2.0; |
tpettet3 | 15:5a337e99f6f3 | 15 | noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); |
tpettet3 | 15:5a337e99f6f3 | 16 | // setup timer to interrupt for next note to play |
tpettet3 | 15:5a337e99f6f3 | 17 | frequencyptr = frequency; |
tpettet3 | 15:5a337e99f6f3 | 18 | durationptr = duration; |
tpettet3 | 15:5a337e99f6f3 | 19 | //returns after first note starts to play |
tpettet3 | 15:5a337e99f6f3 | 20 | } |
tpettet3 | 15:5a337e99f6f3 | 21 | void nextnote(); |
tpettet3 | 15:5a337e99f6f3 | 22 | private: |
tpettet3 | 15:5a337e99f6f3 | 23 | Timeout noteduration; |
tpettet3 | 15:5a337e99f6f3 | 24 | PwmOut _pin; |
tpettet3 | 15:5a337e99f6f3 | 25 | int notecount; |
tpettet3 | 15:5a337e99f6f3 | 26 | float vol; |
tpettet3 | 15:5a337e99f6f3 | 27 | float * frequencyptr; |
tpettet3 | 15:5a337e99f6f3 | 28 | float * durationptr; |
tpettet3 | 15:5a337e99f6f3 | 29 | }; |
tpettet3 | 15:5a337e99f6f3 | 30 | //Interrupt Routine to play next note |
tpettet3 | 15:5a337e99f6f3 | 31 | void SongPlayer::nextnote() |
tpettet3 | 15:5a337e99f6f3 | 32 | { |
tpettet3 | 15:5a337e99f6f3 | 33 | _pin = 0.0; |
tpettet3 | 15:5a337e99f6f3 | 34 | notecount++; //setup next note in song |
tpettet3 | 15:5a337e99f6f3 | 35 | if (durationptr[notecount]!=0.0) { |
tpettet3 | 15:5a337e99f6f3 | 36 | _pin.period(1.0/frequencyptr[notecount]); |
tpettet3 | 15:5a337e99f6f3 | 37 | noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); |
tpettet3 | 15:5a337e99f6f3 | 38 | _pin = vol/2.0; |
tpettet3 | 15:5a337e99f6f3 | 39 | } else |
tpettet3 | 15:5a337e99f6f3 | 40 | _pin = 0.0; //turn off on last note |
tpettet3 | 15:5a337e99f6f3 | 41 | } |