AUP_Lab6_Music

Dependencies:   C12832 MMA7660 mbed

Fork of AUP_Lab5_MMA7660 by Lei Lei

Committer:
BrentLei
Date:
Wed Jul 08 00:09:57 2015 +0000
Revision:
3:640558c1c0d3
test

Who changed what in which revision?

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