Target Practice game for the LPC1768.

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed wave_player

Fork of USBKeyboard_HelloWorld by Samuel Mokrani

Committer:
hwardlaw3
Date:
Thu Mar 16 04:31:56 2017 +0000
Revision:
7:84c2f2ed37d0

        

Who changed what in which revision?

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