A robot rover with distance sensing and audiovisual effects

Dependencies:   4DGL-uLCD-SE Motordriver PID mbed

Fork of PIDRover by Aaron Berk

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SongPlayer.h Source File

SongPlayer.h

00001 #include "mbed.h"
00002 // new class to play a note on Speaker based on PwmOut class
00003 class SongPlayer
00004 {
00005 public:
00006     SongPlayer(PinName pin) : _pin(pin) {
00007 // _pin(pin) means pass pin to the constructor
00008     }
00009 // class method to play a note based on PwmOut class
00010     void PlaySong(float frequency[], float duration[], float volume=1.0) {
00011         vol = volume;
00012         notecount = 0;
00013         _pin.period(1.0/frequency[notecount]);
00014         _pin = volume/2.0;
00015         noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
00016         // setup timer to interrupt for next note to play
00017         frequencyptr = frequency;
00018         durationptr = duration;
00019         //returns after first note starts to play
00020     }
00021     void nextnote();
00022 private:
00023     Timeout noteduration;
00024     PwmOut _pin;
00025     int notecount;
00026     float vol;
00027     float * frequencyptr;
00028     float * durationptr;
00029 };
00030 //Interrupt Routine to play next note
00031 void SongPlayer::nextnote()
00032 {
00033     _pin = 0.0;
00034     notecount++; //setup next note in song
00035     if (durationptr[notecount]!=0.0) {
00036         _pin.period(1.0/frequencyptr[notecount]);
00037         noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
00038         _pin = vol/2.0;
00039     } else
00040         _pin = 0.0; //turn off on last note
00041 }