ECE3872 HW/SW Project Code

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

Committer:
nnguyen99
Date:
Tue Mar 31 19:50:37 2020 +0000
Revision:
3:7486869c8027
Child:
4:1790aa9234a3
Added state machine logic and state function

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nnguyen99 3:7486869c8027 1 #include "mbed.h"
nnguyen99 3:7486869c8027 2
nnguyen99 3:7486869c8027 3 DigitalOut myled(LED1);
nnguyen99 3:7486869c8027 4 // States
nnguyen99 3:7486869c8027 5 #define RESET 1
nnguyen99 3:7486869c8027 6 #define STOP 2
nnguyen99 3:7486869c8027 7 #define RECORD 3
nnguyen99 3:7486869c8027 8 #define PLAY 4
nnguyen99 3:7486869c8027 9 #define ERASE 5
nnguyen99 3:7486869c8027 10 // State Machine control global variables
nnguyen99 3:7486869c8027 11 bool X; //reset
nnguyen99 3:7486869c8027 12 bool P; //play
nnguyen99 3:7486869c8027 13 bool S; //stop
nnguyen99 3:7486869c8027 14 bool R; //record
nnguyen99 3:7486869c8027 15 bool E; //erase
nnguyen99 3:7486869c8027 16
nnguyen99 3:7486869c8027 17 enum sm_state {sRESET,sSTOP,sRECORD,sPLAY,sERASE};
nnguyen99 3:7486869c8027 18
nnguyen99 3:7486869c8027 19
nnguyen99 3:7486869c8027 20 void reset(){
nnguyen99 3:7486869c8027 21 /* reset state:
nnguyen99 3:7486869c8027 22 Initial state upon powering up the device
nnguyen99 3:7486869c8027 23 1. Cease all motion
nnguyen99 3:7486869c8027 24 2. Reset all motors to initial position
nnguyen99 3:7486869c8027 25 3. Mutes all audio
nnguyen99 3:7486869c8027 26 4. Goes to erase state if reset button is held down for at least 3 seconds
nnguyen99 3:7486869c8027 27 5. Goes to stop state according to rotary switch input
nnguyen99 3:7486869c8027 28 6. LED goes from green to red
nnguyen99 3:7486869c8027 29 NOTE: ONLY exits to stop or erase state
nnguyen99 3:7486869c8027 30 */
nnguyen99 3:7486869c8027 31 }
nnguyen99 3:7486869c8027 32 void stop(){
nnguyen99 3:7486869c8027 33 /* stop state:
nnguyen99 3:7486869c8027 34 Initiated by rotary switch
nnguyen99 3:7486869c8027 35 1. Cease all motion
nnguyen99 3:7486869c8027 36 2. Stop recording
nnguyen99 3:7486869c8027 37 3. Mute all audio
nnguyen99 3:7486869c8027 38 */
nnguyen99 3:7486869c8027 39 }
nnguyen99 3:7486869c8027 40 void record(){
nnguyen99 3:7486869c8027 41 /* record state:
nnguyen99 3:7486869c8027 42 Initiated by rotary switch
nnguyen99 3:7486869c8027 43 1. Cease all motion
nnguyen99 3:7486869c8027 44 2. Begin recording ToF inputs (distance and time)
nnguyen99 3:7486869c8027 45 3. Convert distances to corresponding audio frequencies
nnguyen99 3:7486869c8027 46 4. Append to list of frequencies
nnguyen99 3:7486869c8027 47 5.
nnguyen99 3:7486869c8027 48 */
nnguyen99 3:7486869c8027 49 }
nnguyen99 3:7486869c8027 50 void play(){
nnguyen99 3:7486869c8027 51 /* play state:
nnguyen99 3:7486869c8027 52 Initiated by rotary switch
nnguyen99 3:7486869c8027 53 1. wait a few seconds
nnguyen99 3:7486869c8027 54 2. begin reading list of frequencies while concurrently:
nnguyen99 3:7486869c8027 55 - moving servo motors accordingly
nnguyen99 3:7486869c8027 56 - playing corresponding sounds
nnguyen99 3:7486869c8027 57 */
nnguyen99 3:7486869c8027 58 }
nnguyen99 3:7486869c8027 59 void erase(){
nnguyen99 3:7486869c8027 60 /* erase state:
nnguyen99 3:7486869c8027 61 erases entire audio recording
nnguyen99 3:7486869c8027 62 */
nnguyen99 3:7486869c8027 63 }
nnguyen99 3:7486869c8027 64
nnguyen99 3:7486869c8027 65 int main() {
nnguyen99 3:7486869c8027 66 sm_state curr_state = sRESET;
nnguyen99 3:7486869c8027 67 while(1) {
nnguyen99 3:7486869c8027 68 switch(curr_state){
nnguyen99 3:7486869c8027 69 case sRESET:
nnguyen99 3:7486869c8027 70 if(S) curr_state = sSTOP;
nnguyen99 3:7486869c8027 71 break;
nnguyen99 3:7486869c8027 72 case sSTOP:
nnguyen99 3:7486869c8027 73 if(X){
nnguyen99 3:7486869c8027 74 curr_state = sRESET;
nnguyen99 3:7486869c8027 75 }else if(S&R){
nnguyen99 3:7486869c8027 76 curr_state = sRECORD;
nnguyen99 3:7486869c8027 77 }else if(S&P){
nnguyen99 3:7486869c8027 78 curr_state = sPLAY;
nnguyen99 3:7486869c8027 79 }
nnguyen99 3:7486869c8027 80 break;
nnguyen99 3:7486869c8027 81 case sRECORD:
nnguyen99 3:7486869c8027 82 if(X){
nnguyen99 3:7486869c8027 83 curr_state = sRESET;
nnguyen99 3:7486869c8027 84 }else if(R&S){
nnguyen99 3:7486869c8027 85 curr_state = sSTOP;
nnguyen99 3:7486869c8027 86 }else if(R&P){
nnguyen99 3:7486869c8027 87 curr_state = sPLAY;
nnguyen99 3:7486869c8027 88 }
nnguyen99 3:7486869c8027 89 break;
nnguyen99 3:7486869c8027 90 case sPLAY:
nnguyen99 3:7486869c8027 91 if(X){
nnguyen99 3:7486869c8027 92 curr_state = sRESET;
nnguyen99 3:7486869c8027 93 }else if(P&S){
nnguyen99 3:7486869c8027 94 curr_state = sSTOP;
nnguyen99 3:7486869c8027 95 }else if(P&R){
nnguyen99 3:7486869c8027 96 curr_state = sPLAY;
nnguyen99 3:7486869c8027 97 }
nnguyen99 3:7486869c8027 98 break;
nnguyen99 3:7486869c8027 99 case sERASE:
nnguyen99 3:7486869c8027 100 if(X){
nnguyen99 3:7486869c8027 101 curr_state = sRESET;
nnguyen99 3:7486869c8027 102 }
nnguyen99 3:7486869c8027 103 }
nnguyen99 3:7486869c8027 104 }
nnguyen99 3:7486869c8027 105 }