Basic funcitonality

Dependencies:   mbed mbed-rtos SevenSegmentSerial HC_SR04_Ultrasonic_Library

Committer:
glanier9
Date:
Sat May 01 02:58:59 2021 +0000
Revision:
11:1a6658a543e0
Parent:
1:28e137ae5af7
Videoed version of the code. Reset check all the time was unable to work due to a system lockup.;

Who changed what in which revision?

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