an mbed tile music game using a capacitive touchpad and uLCD

Dependencies:   SDFileSystem mbed wave_player

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 PlaySong1(float *frequency, float *duration, float volume=1.0) {
00022         vol = volume;
00023         notecount = 0;
00024         _pin.period(1.0/(*frequency));
00025         _pin = volume/2.0;
00026         noteduration.attach(this,&SongPlayer::nextnote, *duration);
00027         // setup timer to interrupt for next note to play
00028         frequencyptr = frequency;
00029         durationptr = duration;
00030         //returns after first note starts to play
00031     }
00032     void nextnote();
00033 private:
00034     Timeout noteduration;
00035     PwmOut _pin;
00036     int notecount;
00037     float vol;
00038     float * frequencyptr;
00039     float * durationptr;
00040 };
00041 //Interrupt Routine to play next note
00042 void SongPlayer::nextnote()
00043 {
00044     _pin = 0.0;
00045     notecount++; //setup next note in song
00046     if (durationptr[notecount]!=0.0) {
00047         _pin.period(1.0/frequencyptr[notecount]);
00048         noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
00049         _pin = vol/2.0;
00050     } else
00051         _pin = 0.0; //turn off on last note
00052 }