ECE3872 HW/SW Project Code

Dependencies:   mbed Servo mbed-rtos 4DGL-uLCD-SE PinDetect X_NUCLEO_53L0A1

Committer:
nnguyen99
Date:
Thu Apr 09 16:24:28 2020 +0000
Revision:
9:770effc3af2d
Parent:
8:a618687c7b19
Child:
10:f10105ec435f
Switched to keyboard inputs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nnguyen99 3:7486869c8027 1 #include "mbed.h"
nnguyen99 5:d34d14d33a89 2 #include <iostream>
trmontgomery 4:1790aa9234a3 3 #include "audio_out.h"
nnguyen99 5:d34d14d33a89 4 #include "PinDetect.h"
nnguyen99 5:d34d14d33a89 5 #include "uLCD_4DGL.h"
nnguyen99 3:7486869c8027 6 DigitalOut myled(LED1);
nnguyen99 9:770effc3af2d 7 Serial pc(USBTX,USBRX);
nnguyen99 5:d34d14d33a89 8 // LCD init
nnguyen99 5:d34d14d33a89 9 // Push buttons init
nnguyen99 3:7486869c8027 10 // States
nnguyen99 5:d34d14d33a89 11 #define sRESET 1
nnguyen99 5:d34d14d33a89 12 #define sSTOP 2
nnguyen99 5:d34d14d33a89 13 #define sRECORD 3
nnguyen99 5:d34d14d33a89 14 #define sPLAY 4
nnguyen99 5:d34d14d33a89 15 #define sERASE 5
nnguyen99 3:7486869c8027 16 // State Machine control global variables
nnguyen99 3:7486869c8027 17 bool X; //reset
nnguyen99 3:7486869c8027 18 bool P; //play
nnguyen99 3:7486869c8027 19 bool S; //stop
nnguyen99 3:7486869c8027 20 bool R; //record
nnguyen99 3:7486869c8027 21 bool E; //erase
nnguyen99 3:7486869c8027 22
nnguyen99 5:d34d14d33a89 23 enum sm_state {sRESET, sSTOP, sRECORD, sPLAY, sERASE};
nnguyen99 3:7486869c8027 24
nnguyen99 3:7486869c8027 25
nnguyen99 3:7486869c8027 26 void reset(){
nnguyen99 3:7486869c8027 27 /* reset state:
nnguyen99 3:7486869c8027 28 Initial state upon powering up the device
nnguyen99 3:7486869c8027 29 1. Cease all motion
nnguyen99 3:7486869c8027 30 2. Reset all motors to initial position
nnguyen99 3:7486869c8027 31 3. Mutes all audio
nnguyen99 3:7486869c8027 32 4. Goes to erase state if reset button is held down for at least 3 seconds
nnguyen99 3:7486869c8027 33 5. Goes to stop state according to rotary switch input
nnguyen99 3:7486869c8027 34 6. LED goes from green to red
nnguyen99 3:7486869c8027 35 NOTE: ONLY exits to stop or erase state
nnguyen99 3:7486869c8027 36 */
nnguyen99 3:7486869c8027 37 }
nnguyen99 3:7486869c8027 38 void stop(){
nnguyen99 3:7486869c8027 39 /* stop state:
nnguyen99 3:7486869c8027 40 Initiated by rotary switch
nnguyen99 3:7486869c8027 41 1. Cease all motion
nnguyen99 3:7486869c8027 42 2. Stop recording
nnguyen99 3:7486869c8027 43 3. Mute all audio
nnguyen99 3:7486869c8027 44 */
nnguyen99 3:7486869c8027 45 }
nnguyen99 3:7486869c8027 46 void record(){
nnguyen99 3:7486869c8027 47 /* record state:
nnguyen99 3:7486869c8027 48 Initiated by rotary switch
nnguyen99 3:7486869c8027 49 1. Cease all motion
nnguyen99 3:7486869c8027 50 2. Begin recording ToF inputs (distance and time)
nnguyen99 3:7486869c8027 51 3. Convert distances to corresponding audio frequencies
nnguyen99 3:7486869c8027 52 4. Append to list of frequencies
nnguyen99 3:7486869c8027 53 5.
nnguyen99 3:7486869c8027 54 */
nnguyen99 3:7486869c8027 55 }
nnguyen99 3:7486869c8027 56 void play(){
nnguyen99 3:7486869c8027 57 /* play state:
nnguyen99 3:7486869c8027 58 Initiated by rotary switch
nnguyen99 3:7486869c8027 59 1. wait a few seconds
nnguyen99 3:7486869c8027 60 2. begin reading list of frequencies while concurrently:
nnguyen99 3:7486869c8027 61 - moving servo motors accordingly
nnguyen99 3:7486869c8027 62 - playing corresponding sounds
nnguyen99 3:7486869c8027 63 */
nnguyen99 3:7486869c8027 64 }
nnguyen99 3:7486869c8027 65 void erase(){
nnguyen99 3:7486869c8027 66 /* erase state:
nnguyen99 3:7486869c8027 67 erases entire audio recording
nnguyen99 3:7486869c8027 68 */
nnguyen99 3:7486869c8027 69 }
nnguyen99 3:7486869c8027 70
nnguyen99 5:d34d14d33a89 71
nnguyen99 5:d34d14d33a89 72
nnguyen99 5:d34d14d33a89 73
trmontgomery 4:1790aa9234a3 74 void state_machine_mgr(){
nnguyen99 3:7486869c8027 75 sm_state curr_state = sRESET;
nnguyen99 3:7486869c8027 76 while(1) {
nnguyen99 9:770effc3af2d 77 curr_state = pc.getc();
nnguyen99 3:7486869c8027 78 switch(curr_state){
nnguyen99 3:7486869c8027 79 case sRESET:
nnguyen99 8:a618687c7b19 80 S = 0;
nnguyen99 8:a618687c7b19 81 R = 0;
nnguyen99 8:a618687c7b19 82 P = 0;
nnguyen99 8:a618687c7b19 83 E = 0;
nnguyen99 3:7486869c8027 84 if(S) curr_state = sSTOP;
nnguyen99 3:7486869c8027 85 break;
nnguyen99 3:7486869c8027 86 case sSTOP:
nnguyen99 8:a618687c7b19 87 X = 0;
nnguyen99 8:a618687c7b19 88 R = 0;
nnguyen99 8:a618687c7b19 89 P = 0;
nnguyen99 8:a618687c7b19 90 E = 0;
nnguyen99 3:7486869c8027 91 if(X){
nnguyen99 3:7486869c8027 92 curr_state = sRESET;
nnguyen99 3:7486869c8027 93 }else if(S&R){
nnguyen99 3:7486869c8027 94 curr_state = sRECORD;
nnguyen99 3:7486869c8027 95 }else if(S&P){
nnguyen99 3:7486869c8027 96 curr_state = sPLAY;
nnguyen99 3:7486869c8027 97 }
nnguyen99 3:7486869c8027 98 break;
nnguyen99 3:7486869c8027 99 case sRECORD:
nnguyen99 8:a618687c7b19 100 S = 0;
nnguyen99 8:a618687c7b19 101 X = 0;
nnguyen99 8:a618687c7b19 102 P = 0;
nnguyen99 8:a618687c7b19 103 E = 0;
nnguyen99 3:7486869c8027 104 if(X){
nnguyen99 3:7486869c8027 105 curr_state = sRESET;
nnguyen99 3:7486869c8027 106 }else if(R&S){
nnguyen99 3:7486869c8027 107 curr_state = sSTOP;
nnguyen99 3:7486869c8027 108 }else if(R&P){
nnguyen99 3:7486869c8027 109 curr_state = sPLAY;
nnguyen99 3:7486869c8027 110 }
nnguyen99 3:7486869c8027 111 break;
nnguyen99 3:7486869c8027 112 case sPLAY:
nnguyen99 8:a618687c7b19 113 S = 0;
nnguyen99 8:a618687c7b19 114 R = 0;
nnguyen99 8:a618687c7b19 115 X = 0;
nnguyen99 8:a618687c7b19 116 E = 0;
nnguyen99 3:7486869c8027 117 if(X){
nnguyen99 3:7486869c8027 118 curr_state = sRESET;
nnguyen99 3:7486869c8027 119 }else if(P&S){
nnguyen99 3:7486869c8027 120 curr_state = sSTOP;
nnguyen99 3:7486869c8027 121 }else if(P&R){
nnguyen99 3:7486869c8027 122 curr_state = sPLAY;
nnguyen99 3:7486869c8027 123 }
nnguyen99 3:7486869c8027 124 break;
nnguyen99 3:7486869c8027 125 case sERASE:
nnguyen99 3:7486869c8027 126 if(X){
nnguyen99 3:7486869c8027 127 curr_state = sRESET;
nnguyen99 3:7486869c8027 128 }
nnguyen99 3:7486869c8027 129 }
nnguyen99 3:7486869c8027 130 }
nnguyen99 3:7486869c8027 131 }
trmontgomery 4:1790aa9234a3 132
trmontgomery 4:1790aa9234a3 133 int main() {
nnguyen99 5:d34d14d33a89 134 hardware_init();
trmontgomery 4:1790aa9234a3 135 state_machine_mgr();
trmontgomery 4:1790aa9234a3 136 }