an mbed tile music game using a capacitive touchpad and uLCD

Dependencies:   SDFileSystem mbed wave_player

Committer:
clu67
Date:
Mon Mar 14 00:26:24 2016 +0000
Revision:
0:a1c374b9a4fe
Initial Release

Who changed what in which revision?

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