ECE3872 HW/SW Project Code
Dependencies: mbed Servo mbed-rtos 4DGL-uLCD-SE PinDetect X_NUCLEO_53L0A1
Revision 26:2063ee8419cc, committed 2020-04-14
- Comitter:
- trmontgomery
- Date:
- Tue Apr 14 01:40:26 2020 +0000
- Parent:
- 25:7764baddb7bc
- Commit message:
- map of distinct movements is playable. can record and play music at the same time.
Changed in this revision
diff -r 7764baddb7bc -r 2063ee8419cc SongPlayer.h --- a/SongPlayer.h Mon Apr 13 21:32:00 2020 +0000 +++ b/SongPlayer.h Tue Apr 14 01:40:26 2020 +0000 @@ -1,41 +1,34 @@ #include "mbed.h" -// new class to play a note on Speaker based on PwmOut class +#include <vector> + class SongPlayer { public: - SongPlayer(PinName pin) : _pin(pin) { -// _pin(pin) means pass pin to the constructor + SongPlayer(PinName pin, vector<float> freq) : _pin(pin), frequency(freq) { } -// class method to play a note based on PwmOut class - void PlaySong(float frequency[], float duration[], float volume=1.0) { + void PlaySong(float duration, float volume=1.0) { vol = volume; notecount = 0; _pin.period(1.0/frequency[notecount]); _pin = volume/2.0; - noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]); - // setup timer to interrupt for next note to play - frequencyptr = frequency; - durationptr = duration; - //returns after first note starts to play + noteduration.attach(this,&SongPlayer::nextnote, DUR); } void nextnote(); + private: Timeout noteduration; PwmOut _pin; int notecount; float vol; - float * frequencyptr; - float * durationptr; + vector<float> frequency; }; -//Interrupt Routine to play next note + void SongPlayer::nextnote() { _pin = 0.0; notecount++; //setup next note in song - if (durationptr[notecount]!=0.0) { - _pin.period(1.0/frequencyptr[notecount]); - noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]); - _pin = vol/2.0; - } else - _pin = 0.0; //turn off on last note + if (frequency[notecount]==0.0) notecount = 0; + _pin.period(1.0/frequency[notecount]); + noteduration.attach(this,&SongPlayer::nextnote, DUR); + _pin = vol/2.0; } \ No newline at end of file
diff -r 7764baddb7bc -r 2063ee8419cc main.cpp --- a/main.cpp Mon Apr 13 21:32:00 2020 +0000 +++ b/main.cpp Tue Apr 14 01:40:26 2020 +0000 @@ -3,6 +3,7 @@ #include <vector> #include "speaker_out.h" #include "motor_ctl.h" +#include "SongPlayer.h" DigitalOut myled1(LED1); DigitalOut myled2(LED2); @@ -17,10 +18,18 @@ bool E; //erase enum sm_state {sRESET, sSTOP, sRECORD, sPLAY, sERASE}; +int curr_state = 1; + +Serial pc(USBTX,USBRX); +Speaker mySpeaker(p26); +float time_limit = 30; +//Timer t; //SongPlayer music; //Puppet puppet; vector<float> song; //stores all notes recorded by user +vector<string> dance; + void reset(){ /* reset state: @@ -56,7 +65,79 @@ 4. Append to list of frequencies 5. */ - //music.record(); //need to play as recording happens + float freq; + char note; + t.start(); + while(t.read() < 30){ + note = pc.getc(); + switch (note){ + case 'a': + freq = 523.25; + dance.push_back(move_map[0]); + break; + + case 's': + freq = 554.37; + dance.push_back(move_map[1]); + break; + + case 'd': + freq = 587.33; + dance.push_back(move_map[2]); + break; + + case 'f': + freq = 622.25; + dance.push_back(move_map[3]); + break; + + case 'g': + freq = 659.26; + dance.push_back(move_map[4]); + break; + + case 'h': + freq = 698.46; + dance.push_back(move_map[5]); + break; + + case 'j': + freq = 739.99; + dance.push_back(move_map[6]); + break; + + case 'k': + freq = 783.99; + dance.push_back(move_map[7]); + break; + + case 'l': + freq = 830.61; + dance.push_back(move_map[8]); + break; + + case 'q': + freq = 880.00; + dance.push_back(move_map[9]); + break; + + case 'w': + freq = 932.33; + dance.push_back(move_map[10]); + break; + + case 'e': + freq = 987.77; + dance.push_back(move_map[11]); + break; + } + song.push_back(freq); + mySpeaker.PlayNote(freq,0.2,0.5); + + } + t.stop(); + //curr_state = 3; + } void play(){ /* play state: @@ -66,8 +147,8 @@ - moving servo motors accordingly - playing corresponding sounds */ - //puppet.dance(); - //music.play(); + SongPlayer mySpeaker(p26, song); + mySpeaker.PlaySong(DUR); } void erase(){ /* erase state: @@ -77,7 +158,7 @@ } void state_machine_mgr(){ - char curr_state = 1; + char key_input; X = 1; S = 0; @@ -170,6 +251,20 @@ } int main() { - //state_machine_mgr(); - puppet_move(); + //initialize map of freq to movements + move_map.push_back("1111"); + move_map.push_back("1011"); + move_map.push_back("0111"); + move_map.push_back("1122"); + move_map.push_back("1022"); + move_map.push_back("0122"); + move_map.push_back("1133"); + move_map.push_back("1033"); + move_map.push_back("0133"); + move_map.push_back("0031"); + move_map.push_back("0013"); + move_map.push_back("1101"); + state_machine_mgr(); + //puppet_move(); + //record(); }
diff -r 7764baddb7bc -r 2063ee8419cc motor_ctl.h --- a/motor_ctl.h Mon Apr 13 21:32:00 2020 +0000 +++ b/motor_ctl.h Tue Apr 14 01:40:26 2020 +0000 @@ -1,6 +1,7 @@ #include "mbed.h" #include "Servo.h" #include "rtos.h" +#include <string> Servo r_arm(p24); Servo l_arm(p23); @@ -12,85 +13,49 @@ Thread t_r_leg; Thread t_l_leg; -DigitalOut led(LED4); +#define DUR 200 + +vector<string> move_map; //initialized in main +string note; -unsigned short move_map1[] = {0x149, 0x14A, 0x14C, 0x151, 0x152, 0x154, 0x291, 0x292, 0x294, 0x2A1, 0x2A2, 0x2A4}; -unsigned short ra_mask = 0x0C0; -unsigned short la_mask = 0x300; -unsigned short rl_mask = 0x03C; -unsigned short ll_mask = 0x007; - -unsigned short move_map[] = {1,2,4,1,2,4,1,2,4,1,2,4,1,2,4,1,2,4}; +void la_dance() { + switch(note[0]){ + case 0: l_arm.write(1.0); break; + case 1: l_arm.write(0.0); break; + } + Thread::wait(DUR); +} void ra_dance() { - //r_arm = 0; - //wait(1); - for(int p=0; p<12; ++p) { - unsigned short pos = move_map[p]; - switch(pos){ - case 1: r_arm.write(1.0); - case 2: r_arm.write(0.5); - case 4: r_arm.write(1.0); - } - wait(0.5); - r_arm = 0.5; + switch(note[1]){ + case '0': r_arm.write(1.0); break; + case '1': r_arm.write(0.0); break; } - - r_arm = 1; - wait(0.5); - r_arm = 0.5; - wait(0.5); - r_arm = 1; - r_arm.write(.50); - r_arm.write() - + Thread::wait(DUR); } -void la_dance() { - l_arm = 0; - wait(1); - for(float p=0; p<1.0; p += 0.1) { - l_arm = p; - wait(0.2); +void ll_dance() { + switch(note[2]){ + case 1: r_leg.write(1.0); break; + case 2: r_leg.write(0.6); break; + case 3: r_leg.write(0.1); break; } - l_arm.write(.50); - + Thread::wait(DUR); } void rl_dance() { - r_leg = 0; - wait(1); - for(float p=0; p<1.0; p += 0.1) { - r_leg = p; - wait(0.2); + switch(note[3]){ + case 1: l_leg.write(1.0); break; + case 2: l_leg.write(0.6); break; + case 3: l_leg.write(0.1); break; } - r_leg.write(.50); - + Thread::wait(DUR); } -void ll_dance() { - l_leg = 0; - wait(1); - for(float p=0; p<1.0; p += 0.1) { - l_leg = p; - wait(0.2); - } - l_leg.write(.50); - +void puppet_move(){ + t_r_arm.start(ra_dance); + t_l_arm.start(la_dance); + t_r_leg.start(rl_dance); + t_l_leg.start(ll_dance); } - -void puppet_move(){ - //while(1){ - //t_r_arm.start(ra_dance); - //wait(1); - //t_l_arm.start(la_dance); - //wait(1); - //t_r_leg.start(rl_dance); - //wait(1); - //t_l_leg.start(ll_dance); - //wait(1); - // } - ra_dance(); -} -
diff -r 7764baddb7bc -r 2063ee8419cc speaker_out.h --- a/speaker_out.h Mon Apr 13 21:32:00 2020 +0000 +++ b/speaker_out.h Tue Apr 14 01:40:26 2020 +0000 @@ -1,81 +1,68 @@ #include "mbed.h" #include "Speaker.h" - - -Serial pc(USBTX,USBRX); -Speaker mySpeaker(p26); - -Timer t; - - -char note; -float freq [100]; -float freq2; -float limit; - -float *record(float freq1[100]) { +#include <vector> +Timer t; +/* +void record(vector<float>& song) { + float freq; + char note; t.start(); - for(int i = 0; i<100; i++){ - pc.printf("Please enter note\n"); + while(t.read() < 30){ note = pc.getc(); switch (note){ case 'a': - freq2 = 523.25; + freq = 523.25; break; - case 'b': - freq2 = 554.37; - break; - - case 'c': - freq2 = 587.33; + case 's': + freq = 554.37; break; case 'd': - freq2 = 622.25; + freq = 587.33; + break; + + case 'f': + freq = 622.25; + break; + + case 'g': + freq = 659.26; + break; + + case 'h': + freq = 698.46; + break; + + case 'j': + freq = 739.99; + break; + + case 'k': + freq = 783.99; + break; + + case 'l': + freq = 830.61; + break; + + case 'q': + freq = 880.00; + break; + + case 'w': + freq = 932.33; break; case 'e': - freq2 = 659.26; - break; - - case 'f': - freq2 = 698.46; - break; - - case 'g': - freq2 = 739.99; - break; - - case 'h': - freq2 = 783.99; - break; - - case 'i': - freq2 = 830.61; - break; - - case 'j': - freq2 = 880.00; - break; - - case 'k': - freq2 = 932.33; - break; - - case 'l': - freq2 = 987.77; + freq = 987.77; break; } - freq1[i] = freq2; - limit = t.read(); - if(limit > 30){ - t.stop(); - break; - } + song.push_back(freq); + mySpeaker.PlayNote(freq,1.0,0.2); } - return freq1; - } + t.stop(); +} void audio_out() { @@ -88,3 +75,4 @@ } } } +*/ \ No newline at end of file