Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Committer:
jmpin
Date:
Thu Mar 17 21:12:59 2016 +0000
Revision:
1:94b1ec213686
Game is pretty much finished, minus the sound coming from .wav files from the SD card

Who changed what in which revision?

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