ECE3872 HW/SW Project Code
Dependencies: mbed Servo mbed-rtos 4DGL-uLCD-SE PinDetect X_NUCLEO_53L0A1
main.cpp@23:34aed77d5b2c, 2020-04-11 (annotated)
- Committer:
- trmontgomery
- Date:
- Sat Apr 11 16:43:11 2020 +0000
- Revision:
- 23:34aed77d5b2c
- Parent:
- 22:fd48dd707e4f
pulled nicks stuff
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nnguyen99 | 3:7486869c8027 | 1 | #include "mbed.h" |
trmontgomery | 20:8c3644bf5d28 | 2 | #include <iostream> |
trmontgomery | 23:34aed77d5b2c | 3 | #include "audio_out.h" |
trmontgomery | 20:8c3644bf5d28 | 4 | #include "PinDetect.h" |
trmontgomery | 20:8c3644bf5d28 | 5 | #include "uLCD_4DGL.h" |
trmontgomery | 23:34aed77d5b2c | 6 | DigitalOut myled(LED1); |
trmontgomery | 23:34aed77d5b2c | 7 | //Serial mac(USBTX,USBRX); |
trmontgomery | 23:34aed77d5b2c | 8 | |
nnguyen99 | 3:7486869c8027 | 9 | // States |
trmontgomery | 22:fd48dd707e4f | 10 | #define sRESET 1 |
trmontgomery | 22:fd48dd707e4f | 11 | #define sSTOP 2 |
trmontgomery | 22:fd48dd707e4f | 12 | #define sRECORD 3 |
trmontgomery | 22:fd48dd707e4f | 13 | #define sPLAY 4 |
trmontgomery | 22:fd48dd707e4f | 14 | #define sERASE 5 |
nnguyen99 | 3:7486869c8027 | 15 | // State Machine control global variables |
nnguyen99 | 3:7486869c8027 | 16 | bool X; //reset |
nnguyen99 | 3:7486869c8027 | 17 | bool P; //play |
nnguyen99 | 3:7486869c8027 | 18 | bool S; //stop |
nnguyen99 | 3:7486869c8027 | 19 | bool R; //record |
nnguyen99 | 3:7486869c8027 | 20 | bool E; //erase |
nnguyen99 | 3:7486869c8027 | 21 | |
trmontgomery | 22:fd48dd707e4f | 22 | |
nnguyen99 | 3:7486869c8027 | 23 | |
nnguyen99 | 3:7486869c8027 | 24 | void reset(){ |
nnguyen99 | 3:7486869c8027 | 25 | /* reset state: |
nnguyen99 | 3:7486869c8027 | 26 | Initial state upon powering up the device |
nnguyen99 | 3:7486869c8027 | 27 | 1. Cease all motion |
nnguyen99 | 3:7486869c8027 | 28 | 2. Reset all motors to initial position |
nnguyen99 | 3:7486869c8027 | 29 | 3. Mutes all audio |
nnguyen99 | 3:7486869c8027 | 30 | 4. Goes to erase state if reset button is held down for at least 3 seconds |
nnguyen99 | 3:7486869c8027 | 31 | 5. Goes to stop state according to rotary switch input |
nnguyen99 | 3:7486869c8027 | 32 | 6. LED goes from green to red |
nnguyen99 | 3:7486869c8027 | 33 | NOTE: ONLY exits to stop or erase state |
nnguyen99 | 3:7486869c8027 | 34 | */ |
nnguyen99 | 3:7486869c8027 | 35 | } |
nnguyen99 | 3:7486869c8027 | 36 | void stop(){ |
nnguyen99 | 3:7486869c8027 | 37 | /* stop state: |
nnguyen99 | 3:7486869c8027 | 38 | Initiated by rotary switch |
nnguyen99 | 3:7486869c8027 | 39 | 1. Cease all motion |
nnguyen99 | 3:7486869c8027 | 40 | 2. Stop recording |
nnguyen99 | 3:7486869c8027 | 41 | 3. Mute all audio |
nnguyen99 | 3:7486869c8027 | 42 | */ |
nnguyen99 | 3:7486869c8027 | 43 | } |
trmontgomery | 23:34aed77d5b2c | 44 | void record(){ |
nnguyen99 | 3:7486869c8027 | 45 | /* record state: |
nnguyen99 | 3:7486869c8027 | 46 | Initiated by rotary switch |
nnguyen99 | 3:7486869c8027 | 47 | 1. Cease all motion |
nnguyen99 | 3:7486869c8027 | 48 | 2. Begin recording ToF inputs (distance and time) |
nnguyen99 | 3:7486869c8027 | 49 | 3. Convert distances to corresponding audio frequencies |
nnguyen99 | 3:7486869c8027 | 50 | 4. Append to list of frequencies |
nnguyen99 | 3:7486869c8027 | 51 | 5. |
nnguyen99 | 3:7486869c8027 | 52 | */ |
trmontgomery | 23:34aed77d5b2c | 53 | } |
nnguyen99 | 3:7486869c8027 | 54 | void play(){ |
nnguyen99 | 3:7486869c8027 | 55 | /* play state: |
nnguyen99 | 3:7486869c8027 | 56 | Initiated by rotary switch |
nnguyen99 | 3:7486869c8027 | 57 | 1. wait a few seconds |
nnguyen99 | 3:7486869c8027 | 58 | 2. begin reading list of frequencies while concurrently: |
nnguyen99 | 3:7486869c8027 | 59 | - moving servo motors accordingly |
nnguyen99 | 3:7486869c8027 | 60 | - playing corresponding sounds |
nnguyen99 | 3:7486869c8027 | 61 | */ |
nnguyen99 | 3:7486869c8027 | 62 | } |
nnguyen99 | 3:7486869c8027 | 63 | void erase(){ |
nnguyen99 | 3:7486869c8027 | 64 | /* erase state: |
nnguyen99 | 3:7486869c8027 | 65 | erases entire audio recording |
nnguyen99 | 3:7486869c8027 | 66 | */ |
nnguyen99 | 3:7486869c8027 | 67 | } |
nnguyen99 | 3:7486869c8027 | 68 | |
trmontgomery | 23:34aed77d5b2c | 69 | |
trmontgomery | 23:34aed77d5b2c | 70 | |
trmontgomery | 23:34aed77d5b2c | 71 | |
trmontgomery | 4:1790aa9234a3 | 72 | void state_machine_mgr(){ |
trmontgomery | 23:34aed77d5b2c | 73 | pc.printf("SM\n"); |
trmontgomery | 23:34aed77d5b2c | 74 | char curr_state = 1; |
trmontgomery | 23:34aed77d5b2c | 75 | X = 1; |
trmontgomery | 23:34aed77d5b2c | 76 | S = 0; |
trmontgomery | 23:34aed77d5b2c | 77 | R = 0; |
trmontgomery | 23:34aed77d5b2c | 78 | P = 0; |
trmontgomery | 23:34aed77d5b2c | 79 | E = 0; |
nnguyen99 | 3:7486869c8027 | 80 | while(1) { |
trmontgomery | 23:34aed77d5b2c | 81 | if (pc.readable()) { // if they have pressed a key |
trmontgomery | 23:34aed77d5b2c | 82 | curr_state = pc.getc(); |
trmontgomery | 23:34aed77d5b2c | 83 | if('1') X=!X; //toggle reset |
trmontgomery | 23:34aed77d5b2c | 84 | if('2') S=!S; //toggle stop |
trmontgomery | 23:34aed77d5b2c | 85 | if('3') R=!R; //toogle record |
trmontgomery | 23:34aed77d5b2c | 86 | if('4') P=!P; //toggle play |
trmontgomery | 23:34aed77d5b2c | 87 | if('5') E=!E; //toggle erase |
trmontgomery | 23:34aed77d5b2c | 88 | } |
trmontgomery | 23:34aed77d5b2c | 89 | |
trmontgomery | 23:34aed77d5b2c | 90 | // Implementing state machine logical transitions based on what states can exit to other states |
trmontgomery | 23:34aed77d5b2c | 91 | if(X){ |
trmontgomery | 23:34aed77d5b2c | 92 | if(E){ |
trmontgomery | 23:34aed77d5b2c | 93 | curr_state = 5; |
trmontgomery | 23:34aed77d5b2c | 94 | X = !X; |
trmontgomery | 23:34aed77d5b2c | 95 | }else if(S){ |
trmontgomery | 23:34aed77d5b2c | 96 | curr_state = 2; |
trmontgomery | 23:34aed77d5b2c | 97 | X = !X; |
trmontgomery | 23:34aed77d5b2c | 98 | } |
trmontgomery | 23:34aed77d5b2c | 99 | }else if(S){ |
trmontgomery | 23:34aed77d5b2c | 100 | if(X){ |
trmontgomery | 23:34aed77d5b2c | 101 | curr_state = 1; |
trmontgomery | 23:34aed77d5b2c | 102 | S = !S; |
trmontgomery | 23:34aed77d5b2c | 103 | }else if(R){ |
trmontgomery | 23:34aed77d5b2c | 104 | curr_state = 3; |
trmontgomery | 23:34aed77d5b2c | 105 | S = !S; |
trmontgomery | 23:34aed77d5b2c | 106 | }else if(P){ |
trmontgomery | 23:34aed77d5b2c | 107 | curr_state = 4; |
trmontgomery | 23:34aed77d5b2c | 108 | S = !S; |
trmontgomery | 23:34aed77d5b2c | 109 | } |
trmontgomery | 23:34aed77d5b2c | 110 | }else if(R){ |
trmontgomery | 23:34aed77d5b2c | 111 | if(X){ |
trmontgomery | 23:34aed77d5b2c | 112 | curr_state = 1; |
trmontgomery | 23:34aed77d5b2c | 113 | R = !R; |
trmontgomery | 23:34aed77d5b2c | 114 | }else if(S){ |
trmontgomery | 23:34aed77d5b2c | 115 | curr_state = 2; |
trmontgomery | 23:34aed77d5b2c | 116 | R = !R; |
trmontgomery | 23:34aed77d5b2c | 117 | } |
trmontgomery | 23:34aed77d5b2c | 118 | }else if(P){ |
trmontgomery | 23:34aed77d5b2c | 119 | if(X){ |
trmontgomery | 23:34aed77d5b2c | 120 | curr_state = 1; |
trmontgomery | 23:34aed77d5b2c | 121 | P = !P; |
trmontgomery | 23:34aed77d5b2c | 122 | }else if(S){ |
trmontgomery | 23:34aed77d5b2c | 123 | curr_state = 2; |
trmontgomery | 23:34aed77d5b2c | 124 | P = !P; |
trmontgomery | 23:34aed77d5b2c | 125 | } |
trmontgomery | 23:34aed77d5b2c | 126 | }else if(E){ |
trmontgomery | 23:34aed77d5b2c | 127 | if(X){ |
trmontgomery | 23:34aed77d5b2c | 128 | curr_state = 1; |
trmontgomery | 23:34aed77d5b2c | 129 | E = !E; |
trmontgomery | 23:34aed77d5b2c | 130 | } |
trmontgomery | 23:34aed77d5b2c | 131 | } |
nnguyen99 | 3:7486869c8027 | 132 | switch(curr_state){ |
trmontgomery | 23:34aed77d5b2c | 133 | case 1: |
trmontgomery | 23:34aed77d5b2c | 134 | pc.printf("RESET\n"); |
trmontgomery | 23:34aed77d5b2c | 135 | reset(); |
nnguyen99 | 3:7486869c8027 | 136 | break; |
trmontgomery | 23:34aed77d5b2c | 137 | case 2: |
trmontgomery | 23:34aed77d5b2c | 138 | pc.printf("STOP\n"); |
trmontgomery | 23:34aed77d5b2c | 139 | stop(); |
trmontgomery | 23:34aed77d5b2c | 140 | break; |
trmontgomery | 23:34aed77d5b2c | 141 | case 3: |
trmontgomery | 23:34aed77d5b2c | 142 | pc.printf("RECORD\n"); |
trmontgomery | 23:34aed77d5b2c | 143 | record(); |
nnguyen99 | 3:7486869c8027 | 144 | break; |
trmontgomery | 23:34aed77d5b2c | 145 | case 4: |
trmontgomery | 23:34aed77d5b2c | 146 | pc.printf("PLAY\n"); |
trmontgomery | 23:34aed77d5b2c | 147 | play(); |
nnguyen99 | 3:7486869c8027 | 148 | break; |
trmontgomery | 23:34aed77d5b2c | 149 | case 5: |
trmontgomery | 23:34aed77d5b2c | 150 | pc.printf("ERASE\n"); |
trmontgomery | 23:34aed77d5b2c | 151 | erase(); |
trmontgomery | 23:34aed77d5b2c | 152 | break; |
nnguyen99 | 3:7486869c8027 | 153 | } |
nnguyen99 | 3:7486869c8027 | 154 | } |
nnguyen99 | 3:7486869c8027 | 155 | } |
trmontgomery | 4:1790aa9234a3 | 156 | |
trmontgomery | 4:1790aa9234a3 | 157 | int main() { |
trmontgomery | 23:34aed77d5b2c | 158 | pc.printf("MAIN\n"); |
trmontgomery | 23:34aed77d5b2c | 159 | state_machine_mgr(); |
trmontgomery | 4:1790aa9234a3 | 160 | } |