Vishwa Iyer / Mbed 2 deprecated MBED_Pinball_4180_final_project

Dependencies:   mbed TextLCD

Committer:
vishiswoz
Date:
Tue Apr 30 03:31:51 2019 +0000
Revision:
1:b3e26728357c
final project code published

Who changed what in which revision?

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