ECE3872 HW/SW Project Code
Dependencies: mbed Servo mbed-rtos 4DGL-uLCD-SE PinDetect X_NUCLEO_53L0A1
main.cpp@26:2063ee8419cc, 2020-04-14 (annotated)
- Committer:
- trmontgomery
- Date:
- Tue Apr 14 01:40:26 2020 +0000
- Revision:
- 26:2063ee8419cc
- Parent:
- 25:7764baddb7bc
map of distinct movements is playable. can record and play music at the same time.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nnguyen99 | 3:7486869c8027 | 1 | #include "mbed.h" |
nnguyen99 | 5:d34d14d33a89 | 2 | #include <iostream> |
trmontgomery | 25:7764baddb7bc | 3 | #include <vector> |
trmontgomery | 18:0e0edd4f9e4d | 4 | #include "speaker_out.h" |
trmontgomery | 16:62976c3c029e | 5 | #include "motor_ctl.h" |
trmontgomery | 26:2063ee8419cc | 6 | #include "SongPlayer.h" |
trmontgomery | 25:7764baddb7bc | 7 | |
trmontgomery | 16:62976c3c029e | 8 | DigitalOut myled1(LED1); |
trmontgomery | 16:62976c3c029e | 9 | DigitalOut myled2(LED2); |
trmontgomery | 16:62976c3c029e | 10 | DigitalOut myled3(LED3); |
trmontgomery | 16:62976c3c029e | 11 | DigitalOut myled4(LED4); |
nnguyen99 | 10:f10105ec435f | 12 | |
nnguyen99 | 3:7486869c8027 | 13 | // State Machine control global variables |
nnguyen99 | 3:7486869c8027 | 14 | bool X; //reset |
nnguyen99 | 3:7486869c8027 | 15 | bool P; //play |
nnguyen99 | 3:7486869c8027 | 16 | bool S; //stop |
nnguyen99 | 3:7486869c8027 | 17 | bool R; //record |
nnguyen99 | 3:7486869c8027 | 18 | bool E; //erase |
nnguyen99 | 3:7486869c8027 | 19 | |
trmontgomery | 16:62976c3c029e | 20 | enum sm_state {sRESET, sSTOP, sRECORD, sPLAY, sERASE}; |
trmontgomery | 26:2063ee8419cc | 21 | int curr_state = 1; |
trmontgomery | 26:2063ee8419cc | 22 | |
trmontgomery | 26:2063ee8419cc | 23 | Serial pc(USBTX,USBRX); |
trmontgomery | 26:2063ee8419cc | 24 | Speaker mySpeaker(p26); |
trmontgomery | 26:2063ee8419cc | 25 | float time_limit = 30; |
trmontgomery | 26:2063ee8419cc | 26 | //Timer t; |
nnguyen99 | 3:7486869c8027 | 27 | |
trmontgomery | 25:7764baddb7bc | 28 | //SongPlayer music; |
trmontgomery | 25:7764baddb7bc | 29 | //Puppet puppet; |
trmontgomery | 25:7764baddb7bc | 30 | vector<float> song; //stores all notes recorded by user |
trmontgomery | 26:2063ee8419cc | 31 | vector<string> dance; |
trmontgomery | 26:2063ee8419cc | 32 | |
trmontgomery | 25:7764baddb7bc | 33 | |
nnguyen99 | 3:7486869c8027 | 34 | void reset(){ |
nnguyen99 | 3:7486869c8027 | 35 | /* reset state: |
nnguyen99 | 3:7486869c8027 | 36 | Initial state upon powering up the device |
nnguyen99 | 3:7486869c8027 | 37 | 1. Cease all motion |
nnguyen99 | 3:7486869c8027 | 38 | 2. Reset all motors to initial position |
nnguyen99 | 3:7486869c8027 | 39 | 3. Mutes all audio |
nnguyen99 | 3:7486869c8027 | 40 | 4. Goes to erase state if reset button is held down for at least 3 seconds |
nnguyen99 | 3:7486869c8027 | 41 | 5. Goes to stop state according to rotary switch input |
nnguyen99 | 3:7486869c8027 | 42 | 6. LED goes from green to red |
nnguyen99 | 3:7486869c8027 | 43 | NOTE: ONLY exits to stop or erase state |
nnguyen99 | 3:7486869c8027 | 44 | */ |
trmontgomery | 25:7764baddb7bc | 45 | //puppet.reset(); |
trmontgomery | 25:7764baddb7bc | 46 | //music.reset(); |
trmontgomery | 25:7764baddb7bc | 47 | //led = red; //use rgb red for this |
nnguyen99 | 3:7486869c8027 | 48 | } |
nnguyen99 | 3:7486869c8027 | 49 | void stop(){ |
nnguyen99 | 3:7486869c8027 | 50 | /* stop state: |
nnguyen99 | 3:7486869c8027 | 51 | Initiated by rotary switch |
nnguyen99 | 3:7486869c8027 | 52 | 1. Cease all motion |
nnguyen99 | 3:7486869c8027 | 53 | 2. Stop recording |
nnguyen99 | 3:7486869c8027 | 54 | 3. Mute all audio |
nnguyen99 | 3:7486869c8027 | 55 | */ |
trmontgomery | 25:7764baddb7bc | 56 | //puppet.stop(); |
trmontgomery | 25:7764baddb7bc | 57 | //music.stop(); |
nnguyen99 | 3:7486869c8027 | 58 | } |
nnguyen99 | 3:7486869c8027 | 59 | void record(){ |
nnguyen99 | 3:7486869c8027 | 60 | /* record state: |
nnguyen99 | 3:7486869c8027 | 61 | Initiated by rotary switch |
nnguyen99 | 3:7486869c8027 | 62 | 1. Cease all motion |
nnguyen99 | 3:7486869c8027 | 63 | 2. Begin recording ToF inputs (distance and time) |
nnguyen99 | 3:7486869c8027 | 64 | 3. Convert distances to corresponding audio frequencies |
nnguyen99 | 3:7486869c8027 | 65 | 4. Append to list of frequencies |
nnguyen99 | 3:7486869c8027 | 66 | 5. |
nnguyen99 | 3:7486869c8027 | 67 | */ |
trmontgomery | 26:2063ee8419cc | 68 | float freq; |
trmontgomery | 26:2063ee8419cc | 69 | char note; |
trmontgomery | 26:2063ee8419cc | 70 | t.start(); |
trmontgomery | 26:2063ee8419cc | 71 | while(t.read() < 30){ |
trmontgomery | 26:2063ee8419cc | 72 | note = pc.getc(); |
trmontgomery | 26:2063ee8419cc | 73 | switch (note){ |
trmontgomery | 26:2063ee8419cc | 74 | case 'a': |
trmontgomery | 26:2063ee8419cc | 75 | freq = 523.25; |
trmontgomery | 26:2063ee8419cc | 76 | dance.push_back(move_map[0]); |
trmontgomery | 26:2063ee8419cc | 77 | break; |
trmontgomery | 26:2063ee8419cc | 78 | |
trmontgomery | 26:2063ee8419cc | 79 | case 's': |
trmontgomery | 26:2063ee8419cc | 80 | freq = 554.37; |
trmontgomery | 26:2063ee8419cc | 81 | dance.push_back(move_map[1]); |
trmontgomery | 26:2063ee8419cc | 82 | break; |
trmontgomery | 26:2063ee8419cc | 83 | |
trmontgomery | 26:2063ee8419cc | 84 | case 'd': |
trmontgomery | 26:2063ee8419cc | 85 | freq = 587.33; |
trmontgomery | 26:2063ee8419cc | 86 | dance.push_back(move_map[2]); |
trmontgomery | 26:2063ee8419cc | 87 | break; |
trmontgomery | 26:2063ee8419cc | 88 | |
trmontgomery | 26:2063ee8419cc | 89 | case 'f': |
trmontgomery | 26:2063ee8419cc | 90 | freq = 622.25; |
trmontgomery | 26:2063ee8419cc | 91 | dance.push_back(move_map[3]); |
trmontgomery | 26:2063ee8419cc | 92 | break; |
trmontgomery | 26:2063ee8419cc | 93 | |
trmontgomery | 26:2063ee8419cc | 94 | case 'g': |
trmontgomery | 26:2063ee8419cc | 95 | freq = 659.26; |
trmontgomery | 26:2063ee8419cc | 96 | dance.push_back(move_map[4]); |
trmontgomery | 26:2063ee8419cc | 97 | break; |
trmontgomery | 26:2063ee8419cc | 98 | |
trmontgomery | 26:2063ee8419cc | 99 | case 'h': |
trmontgomery | 26:2063ee8419cc | 100 | freq = 698.46; |
trmontgomery | 26:2063ee8419cc | 101 | dance.push_back(move_map[5]); |
trmontgomery | 26:2063ee8419cc | 102 | break; |
trmontgomery | 26:2063ee8419cc | 103 | |
trmontgomery | 26:2063ee8419cc | 104 | case 'j': |
trmontgomery | 26:2063ee8419cc | 105 | freq = 739.99; |
trmontgomery | 26:2063ee8419cc | 106 | dance.push_back(move_map[6]); |
trmontgomery | 26:2063ee8419cc | 107 | break; |
trmontgomery | 26:2063ee8419cc | 108 | |
trmontgomery | 26:2063ee8419cc | 109 | case 'k': |
trmontgomery | 26:2063ee8419cc | 110 | freq = 783.99; |
trmontgomery | 26:2063ee8419cc | 111 | dance.push_back(move_map[7]); |
trmontgomery | 26:2063ee8419cc | 112 | break; |
trmontgomery | 26:2063ee8419cc | 113 | |
trmontgomery | 26:2063ee8419cc | 114 | case 'l': |
trmontgomery | 26:2063ee8419cc | 115 | freq = 830.61; |
trmontgomery | 26:2063ee8419cc | 116 | dance.push_back(move_map[8]); |
trmontgomery | 26:2063ee8419cc | 117 | break; |
trmontgomery | 26:2063ee8419cc | 118 | |
trmontgomery | 26:2063ee8419cc | 119 | case 'q': |
trmontgomery | 26:2063ee8419cc | 120 | freq = 880.00; |
trmontgomery | 26:2063ee8419cc | 121 | dance.push_back(move_map[9]); |
trmontgomery | 26:2063ee8419cc | 122 | break; |
trmontgomery | 26:2063ee8419cc | 123 | |
trmontgomery | 26:2063ee8419cc | 124 | case 'w': |
trmontgomery | 26:2063ee8419cc | 125 | freq = 932.33; |
trmontgomery | 26:2063ee8419cc | 126 | dance.push_back(move_map[10]); |
trmontgomery | 26:2063ee8419cc | 127 | break; |
trmontgomery | 26:2063ee8419cc | 128 | |
trmontgomery | 26:2063ee8419cc | 129 | case 'e': |
trmontgomery | 26:2063ee8419cc | 130 | freq = 987.77; |
trmontgomery | 26:2063ee8419cc | 131 | dance.push_back(move_map[11]); |
trmontgomery | 26:2063ee8419cc | 132 | break; |
trmontgomery | 26:2063ee8419cc | 133 | } |
trmontgomery | 26:2063ee8419cc | 134 | song.push_back(freq); |
trmontgomery | 26:2063ee8419cc | 135 | mySpeaker.PlayNote(freq,0.2,0.5); |
trmontgomery | 26:2063ee8419cc | 136 | |
trmontgomery | 26:2063ee8419cc | 137 | } |
trmontgomery | 26:2063ee8419cc | 138 | t.stop(); |
trmontgomery | 26:2063ee8419cc | 139 | //curr_state = 3; |
trmontgomery | 26:2063ee8419cc | 140 | |
nnguyen99 | 3:7486869c8027 | 141 | } |
nnguyen99 | 3:7486869c8027 | 142 | void play(){ |
nnguyen99 | 3:7486869c8027 | 143 | /* play state: |
nnguyen99 | 3:7486869c8027 | 144 | Initiated by rotary switch |
nnguyen99 | 3:7486869c8027 | 145 | 1. wait a few seconds |
nnguyen99 | 3:7486869c8027 | 146 | 2. begin reading list of frequencies while concurrently: |
nnguyen99 | 3:7486869c8027 | 147 | - moving servo motors accordingly |
nnguyen99 | 3:7486869c8027 | 148 | - playing corresponding sounds |
nnguyen99 | 3:7486869c8027 | 149 | */ |
trmontgomery | 26:2063ee8419cc | 150 | SongPlayer mySpeaker(p26, song); |
trmontgomery | 26:2063ee8419cc | 151 | mySpeaker.PlaySong(DUR); |
nnguyen99 | 3:7486869c8027 | 152 | } |
nnguyen99 | 3:7486869c8027 | 153 | void erase(){ |
nnguyen99 | 3:7486869c8027 | 154 | /* erase state: |
nnguyen99 | 3:7486869c8027 | 155 | erases entire audio recording |
nnguyen99 | 3:7486869c8027 | 156 | */ |
trmontgomery | 25:7764baddb7bc | 157 | //song.erase(); |
nnguyen99 | 3:7486869c8027 | 158 | } |
nnguyen99 | 3:7486869c8027 | 159 | |
trmontgomery | 4:1790aa9234a3 | 160 | void state_machine_mgr(){ |
trmontgomery | 26:2063ee8419cc | 161 | |
trmontgomery | 18:0e0edd4f9e4d | 162 | char key_input; |
trmontgomery | 18:0e0edd4f9e4d | 163 | X = 1; |
trmontgomery | 18:0e0edd4f9e4d | 164 | S = 0; |
trmontgomery | 18:0e0edd4f9e4d | 165 | R = 0; |
trmontgomery | 18:0e0edd4f9e4d | 166 | P = 0; |
trmontgomery | 18:0e0edd4f9e4d | 167 | E = 0; |
nnguyen99 | 3:7486869c8027 | 168 | while(1) { |
trmontgomery | 18:0e0edd4f9e4d | 169 | if (pc.readable()) { // if they have pressed a key |
trmontgomery | 18:0e0edd4f9e4d | 170 | key_input = pc.getc(); |
trmontgomery | 18:0e0edd4f9e4d | 171 | if(key_input == '1') X=1; //toggle reset |
trmontgomery | 18:0e0edd4f9e4d | 172 | if(key_input == '2') S=1; //toggle stop |
trmontgomery | 18:0e0edd4f9e4d | 173 | if(key_input == '3') R=1; //toogle record |
trmontgomery | 18:0e0edd4f9e4d | 174 | if(key_input == '4') P=1; //toggle play |
trmontgomery | 18:0e0edd4f9e4d | 175 | if(key_input == '5') E=1; //toggle erase |
trmontgomery | 18:0e0edd4f9e4d | 176 | } |
trmontgomery | 18:0e0edd4f9e4d | 177 | //pc.printf("X: %d\n",X); |
trmontgomery | 18:0e0edd4f9e4d | 178 | // pc.printf("S: %d\n",S); |
trmontgomery | 18:0e0edd4f9e4d | 179 | // pc.printf("R: %d\n",R); |
trmontgomery | 18:0e0edd4f9e4d | 180 | // pc.printf("P: %d\n",P); |
trmontgomery | 18:0e0edd4f9e4d | 181 | // pc.printf("E: %d\n",E); |
trmontgomery | 18:0e0edd4f9e4d | 182 | // pc.printf("Current State %d\n",curr_state); |
nnguyen99 | 3:7486869c8027 | 183 | switch(curr_state){ |
trmontgomery | 18:0e0edd4f9e4d | 184 | case 1: |
trmontgomery | 18:0e0edd4f9e4d | 185 | pc.printf("RESET\n"); |
trmontgomery | 18:0e0edd4f9e4d | 186 | P = 0; |
trmontgomery | 18:0e0edd4f9e4d | 187 | R = 0; |
trmontgomery | 18:0e0edd4f9e4d | 188 | if(E){ |
trmontgomery | 18:0e0edd4f9e4d | 189 | curr_state = 5; |
trmontgomery | 18:0e0edd4f9e4d | 190 | X = 0; |
trmontgomery | 18:0e0edd4f9e4d | 191 | }else if(S){ |
trmontgomery | 18:0e0edd4f9e4d | 192 | curr_state = 2; |
trmontgomery | 18:0e0edd4f9e4d | 193 | X = 0; |
trmontgomery | 18:0e0edd4f9e4d | 194 | } |
trmontgomery | 18:0e0edd4f9e4d | 195 | reset(); |
trmontgomery | 16:62976c3c029e | 196 | break; |
trmontgomery | 18:0e0edd4f9e4d | 197 | case 2: |
trmontgomery | 18:0e0edd4f9e4d | 198 | pc.printf("STOP\n"); |
trmontgomery | 18:0e0edd4f9e4d | 199 | E = 0; |
nnguyen99 | 13:3ae48dd67009 | 200 | if(X){ |
trmontgomery | 18:0e0edd4f9e4d | 201 | curr_state = 1; |
trmontgomery | 18:0e0edd4f9e4d | 202 | S = 0; |
trmontgomery | 18:0e0edd4f9e4d | 203 | }else if(R){ |
trmontgomery | 18:0e0edd4f9e4d | 204 | curr_state = 3; |
trmontgomery | 18:0e0edd4f9e4d | 205 | S = 0; |
trmontgomery | 18:0e0edd4f9e4d | 206 | }else if(P){ |
trmontgomery | 18:0e0edd4f9e4d | 207 | curr_state = 4; |
trmontgomery | 18:0e0edd4f9e4d | 208 | S = 0; |
nnguyen99 | 13:3ae48dd67009 | 209 | } |
trmontgomery | 18:0e0edd4f9e4d | 210 | stop(); |
nnguyen99 | 3:7486869c8027 | 211 | break; |
trmontgomery | 18:0e0edd4f9e4d | 212 | case 3: |
trmontgomery | 18:0e0edd4f9e4d | 213 | pc.printf("RECORD\n"); |
trmontgomery | 18:0e0edd4f9e4d | 214 | E = 0; |
trmontgomery | 18:0e0edd4f9e4d | 215 | P = 0; |
nnguyen99 | 13:3ae48dd67009 | 216 | if(X){ |
trmontgomery | 18:0e0edd4f9e4d | 217 | curr_state = 1; |
trmontgomery | 18:0e0edd4f9e4d | 218 | R = 0; |
trmontgomery | 18:0e0edd4f9e4d | 219 | }else if(S){ |
trmontgomery | 18:0e0edd4f9e4d | 220 | curr_state = 2; |
trmontgomery | 18:0e0edd4f9e4d | 221 | R = 0; |
nnguyen99 | 13:3ae48dd67009 | 222 | } |
trmontgomery | 18:0e0edd4f9e4d | 223 | record(); |
nnguyen99 | 3:7486869c8027 | 224 | break; |
trmontgomery | 18:0e0edd4f9e4d | 225 | case 4: |
trmontgomery | 18:0e0edd4f9e4d | 226 | pc.printf("PLAY\n"); |
trmontgomery | 18:0e0edd4f9e4d | 227 | E = 0; |
trmontgomery | 18:0e0edd4f9e4d | 228 | R = 0; |
nnguyen99 | 13:3ae48dd67009 | 229 | if(X){ |
trmontgomery | 18:0e0edd4f9e4d | 230 | curr_state = 1; |
trmontgomery | 18:0e0edd4f9e4d | 231 | P = 0; |
trmontgomery | 18:0e0edd4f9e4d | 232 | }else if(S){ |
trmontgomery | 18:0e0edd4f9e4d | 233 | curr_state = 2; |
trmontgomery | 18:0e0edd4f9e4d | 234 | P = 0; |
nnguyen99 | 13:3ae48dd67009 | 235 | } |
trmontgomery | 18:0e0edd4f9e4d | 236 | play(); |
trmontgomery | 18:0e0edd4f9e4d | 237 | break; |
trmontgomery | 18:0e0edd4f9e4d | 238 | case 5: |
trmontgomery | 18:0e0edd4f9e4d | 239 | pc.printf("ERASE\n"); |
trmontgomery | 18:0e0edd4f9e4d | 240 | S = 0; |
trmontgomery | 18:0e0edd4f9e4d | 241 | P = 0; |
trmontgomery | 18:0e0edd4f9e4d | 242 | R = 0; |
trmontgomery | 18:0e0edd4f9e4d | 243 | if(X){ |
trmontgomery | 18:0e0edd4f9e4d | 244 | curr_state = 1; |
trmontgomery | 18:0e0edd4f9e4d | 245 | E = 0; |
trmontgomery | 18:0e0edd4f9e4d | 246 | } |
trmontgomery | 18:0e0edd4f9e4d | 247 | erase(); |
trmontgomery | 18:0e0edd4f9e4d | 248 | break; |
nnguyen99 | 3:7486869c8027 | 249 | } |
nnguyen99 | 3:7486869c8027 | 250 | } |
nnguyen99 | 3:7486869c8027 | 251 | } |
trmontgomery | 4:1790aa9234a3 | 252 | |
trmontgomery | 4:1790aa9234a3 | 253 | int main() { |
trmontgomery | 26:2063ee8419cc | 254 | //initialize map of freq to movements |
trmontgomery | 26:2063ee8419cc | 255 | move_map.push_back("1111"); |
trmontgomery | 26:2063ee8419cc | 256 | move_map.push_back("1011"); |
trmontgomery | 26:2063ee8419cc | 257 | move_map.push_back("0111"); |
trmontgomery | 26:2063ee8419cc | 258 | move_map.push_back("1122"); |
trmontgomery | 26:2063ee8419cc | 259 | move_map.push_back("1022"); |
trmontgomery | 26:2063ee8419cc | 260 | move_map.push_back("0122"); |
trmontgomery | 26:2063ee8419cc | 261 | move_map.push_back("1133"); |
trmontgomery | 26:2063ee8419cc | 262 | move_map.push_back("1033"); |
trmontgomery | 26:2063ee8419cc | 263 | move_map.push_back("0133"); |
trmontgomery | 26:2063ee8419cc | 264 | move_map.push_back("0031"); |
trmontgomery | 26:2063ee8419cc | 265 | move_map.push_back("0013"); |
trmontgomery | 26:2063ee8419cc | 266 | move_map.push_back("1101"); |
trmontgomery | 26:2063ee8419cc | 267 | state_machine_mgr(); |
trmontgomery | 26:2063ee8419cc | 268 | //puppet_move(); |
trmontgomery | 26:2063ee8419cc | 269 | //record(); |
trmontgomery | 4:1790aa9234a3 | 270 | } |