ECE3872 HW/SW Project Code
Dependencies: mbed Servo mbed-rtos 4DGL-uLCD-SE PinDetect X_NUCLEO_53L0A1
main.cpp@21:a279bb16a37b, 2020-04-08 (annotated)
- Committer:
- trmontgomery
- Date:
- Wed Apr 08 21:28:53 2020 +0000
- Revision:
- 21:a279bb16a37b
- Parent:
- 20:8c3644bf5d28
- Child:
- 22:fd48dd707e4f
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 | 4:1790aa9234a3 | 3 | #include "audio_out.h" |
trmontgomery | 20:8c3644bf5d28 | 4 | #include "PinDetect.h" |
trmontgomery | 20:8c3644bf5d28 | 5 | #include "uLCD_4DGL.h" |
trmontgomery | 21:a279bb16a37b | 6 | #include "motor_ctl.h" |
trmontgomery | 20:8c3644bf5d28 | 7 | DigitalOut myled1(LED1); |
trmontgomery | 20:8c3644bf5d28 | 8 | DigitalOut myled2(LED2); |
trmontgomery | 20:8c3644bf5d28 | 9 | DigitalOut myled3(LED3); |
trmontgomery | 20:8c3644bf5d28 | 10 | DigitalOut myled4(LED4); |
trmontgomery | 21:a279bb16a37b | 11 | |
trmontgomery | 20:8c3644bf5d28 | 12 | // Push buttons init |
trmontgomery | 20:8c3644bf5d28 | 13 | PinDetect pb1(p16); |
trmontgomery | 20:8c3644bf5d28 | 14 | PinDetect pb2(p17); |
trmontgomery | 20:8c3644bf5d28 | 15 | PinDetect pb3(p18); |
trmontgomery | 20:8c3644bf5d28 | 16 | PinDetect pb4(p19); |
nnguyen99 | 3:7486869c8027 | 17 | // States |
trmontgomery | 20:8c3644bf5d28 | 18 | |
nnguyen99 | 3:7486869c8027 | 19 | // State Machine control global variables |
nnguyen99 | 3:7486869c8027 | 20 | bool X; //reset |
nnguyen99 | 3:7486869c8027 | 21 | bool P; //play |
nnguyen99 | 3:7486869c8027 | 22 | bool S; //stop |
nnguyen99 | 3:7486869c8027 | 23 | bool R; //record |
nnguyen99 | 3:7486869c8027 | 24 | bool E; //erase |
nnguyen99 | 3:7486869c8027 | 25 | |
trmontgomery | 20:8c3644bf5d28 | 26 | enum sm_state {sRESET, sSTOP, sRECORD, sPLAY, sERASE}; |
nnguyen99 | 3:7486869c8027 | 27 | |
nnguyen99 | 3:7486869c8027 | 28 | |
nnguyen99 | 3:7486869c8027 | 29 | void reset(){ |
nnguyen99 | 3:7486869c8027 | 30 | /* reset state: |
nnguyen99 | 3:7486869c8027 | 31 | Initial state upon powering up the device |
nnguyen99 | 3:7486869c8027 | 32 | 1. Cease all motion |
nnguyen99 | 3:7486869c8027 | 33 | 2. Reset all motors to initial position |
nnguyen99 | 3:7486869c8027 | 34 | 3. Mutes all audio |
nnguyen99 | 3:7486869c8027 | 35 | 4. Goes to erase state if reset button is held down for at least 3 seconds |
nnguyen99 | 3:7486869c8027 | 36 | 5. Goes to stop state according to rotary switch input |
nnguyen99 | 3:7486869c8027 | 37 | 6. LED goes from green to red |
nnguyen99 | 3:7486869c8027 | 38 | NOTE: ONLY exits to stop or erase state |
nnguyen99 | 3:7486869c8027 | 39 | */ |
nnguyen99 | 3:7486869c8027 | 40 | } |
nnguyen99 | 3:7486869c8027 | 41 | void stop(){ |
nnguyen99 | 3:7486869c8027 | 42 | /* stop state: |
nnguyen99 | 3:7486869c8027 | 43 | Initiated by rotary switch |
nnguyen99 | 3:7486869c8027 | 44 | 1. Cease all motion |
nnguyen99 | 3:7486869c8027 | 45 | 2. Stop recording |
nnguyen99 | 3:7486869c8027 | 46 | 3. Mute all audio |
nnguyen99 | 3:7486869c8027 | 47 | */ |
nnguyen99 | 3:7486869c8027 | 48 | } |
nnguyen99 | 3:7486869c8027 | 49 | void record(){ |
nnguyen99 | 3:7486869c8027 | 50 | /* record state: |
nnguyen99 | 3:7486869c8027 | 51 | Initiated by rotary switch |
nnguyen99 | 3:7486869c8027 | 52 | 1. Cease all motion |
nnguyen99 | 3:7486869c8027 | 53 | 2. Begin recording ToF inputs (distance and time) |
nnguyen99 | 3:7486869c8027 | 54 | 3. Convert distances to corresponding audio frequencies |
nnguyen99 | 3:7486869c8027 | 55 | 4. Append to list of frequencies |
nnguyen99 | 3:7486869c8027 | 56 | 5. |
nnguyen99 | 3:7486869c8027 | 57 | */ |
nnguyen99 | 3:7486869c8027 | 58 | } |
nnguyen99 | 3:7486869c8027 | 59 | void play(){ |
nnguyen99 | 3:7486869c8027 | 60 | /* play state: |
nnguyen99 | 3:7486869c8027 | 61 | Initiated by rotary switch |
nnguyen99 | 3:7486869c8027 | 62 | 1. wait a few seconds |
nnguyen99 | 3:7486869c8027 | 63 | 2. begin reading list of frequencies while concurrently: |
nnguyen99 | 3:7486869c8027 | 64 | - moving servo motors accordingly |
nnguyen99 | 3:7486869c8027 | 65 | - playing corresponding sounds |
nnguyen99 | 3:7486869c8027 | 66 | */ |
nnguyen99 | 3:7486869c8027 | 67 | } |
nnguyen99 | 3:7486869c8027 | 68 | void erase(){ |
nnguyen99 | 3:7486869c8027 | 69 | /* erase state: |
nnguyen99 | 3:7486869c8027 | 70 | erases entire audio recording |
nnguyen99 | 3:7486869c8027 | 71 | */ |
nnguyen99 | 3:7486869c8027 | 72 | } |
nnguyen99 | 3:7486869c8027 | 73 | |
trmontgomery | 20:8c3644bf5d28 | 74 | |
trmontgomery | 20:8c3644bf5d28 | 75 | |
trmontgomery | 20:8c3644bf5d28 | 76 | void pb1_hit_callback (void) |
trmontgomery | 20:8c3644bf5d28 | 77 | { |
trmontgomery | 20:8c3644bf5d28 | 78 | myled1 = !myled1; |
trmontgomery | 21:a279bb16a37b | 79 | //guLCD.printf("REEST"); |
trmontgomery | 20:8c3644bf5d28 | 80 | } |
trmontgomery | 20:8c3644bf5d28 | 81 | |
trmontgomery | 20:8c3644bf5d28 | 82 | void pb2_hit_callback (void) |
trmontgomery | 20:8c3644bf5d28 | 83 | { |
trmontgomery | 20:8c3644bf5d28 | 84 | myled2 = !myled2; |
trmontgomery | 21:a279bb16a37b | 85 | //guLCD.printf("STOP"); |
trmontgomery | 20:8c3644bf5d28 | 86 | } |
trmontgomery | 20:8c3644bf5d28 | 87 | |
trmontgomery | 20:8c3644bf5d28 | 88 | void pb3_hit_callback (void) |
trmontgomery | 20:8c3644bf5d28 | 89 | { |
trmontgomery | 20:8c3644bf5d28 | 90 | myled3 = !myled3; |
trmontgomery | 21:a279bb16a37b | 91 | //guLCD.printf("RECORD"); |
trmontgomery | 20:8c3644bf5d28 | 92 | } |
trmontgomery | 20:8c3644bf5d28 | 93 | |
trmontgomery | 20:8c3644bf5d28 | 94 | void pb4_hit_callback (void) |
trmontgomery | 20:8c3644bf5d28 | 95 | { |
trmontgomery | 20:8c3644bf5d28 | 96 | myled4 = !myled4; |
trmontgomery | 21:a279bb16a37b | 97 | //guLCD.printf("PLAY"); |
trmontgomery | 20:8c3644bf5d28 | 98 | } |
trmontgomery | 20:8c3644bf5d28 | 99 | void hardware_init(){ |
trmontgomery | 20:8c3644bf5d28 | 100 | // Push buttons init |
trmontgomery | 20:8c3644bf5d28 | 101 | pb1.mode(PullUp); |
trmontgomery | 20:8c3644bf5d28 | 102 | pb2.mode(PullUp); |
trmontgomery | 20:8c3644bf5d28 | 103 | pb3.mode(PullUp); |
trmontgomery | 20:8c3644bf5d28 | 104 | pb4.mode(PullUp); |
trmontgomery | 20:8c3644bf5d28 | 105 | pb1.attach_deasserted(&pb1_hit_callback); |
trmontgomery | 20:8c3644bf5d28 | 106 | pb2.attach_deasserted(&pb2_hit_callback); |
trmontgomery | 20:8c3644bf5d28 | 107 | pb3.attach_deasserted(&pb3_hit_callback); |
trmontgomery | 20:8c3644bf5d28 | 108 | pb4.attach_deasserted(&pb4_hit_callback); |
trmontgomery | 20:8c3644bf5d28 | 109 | pb1.setSampleFrequency(); |
trmontgomery | 20:8c3644bf5d28 | 110 | pb2.setSampleFrequency(); |
trmontgomery | 20:8c3644bf5d28 | 111 | pb3.setSampleFrequency(); |
trmontgomery | 20:8c3644bf5d28 | 112 | pb4.setSampleFrequency(); |
trmontgomery | 20:8c3644bf5d28 | 113 | } |
trmontgomery | 20:8c3644bf5d28 | 114 | |
trmontgomery | 4:1790aa9234a3 | 115 | void state_machine_mgr(){ |
nnguyen99 | 3:7486869c8027 | 116 | sm_state curr_state = sRESET; |
nnguyen99 | 3:7486869c8027 | 117 | while(1) { |
nnguyen99 | 3:7486869c8027 | 118 | switch(curr_state){ |
nnguyen99 | 3:7486869c8027 | 119 | case sRESET: |
nnguyen99 | 3:7486869c8027 | 120 | if(S) curr_state = sSTOP; |
nnguyen99 | 3:7486869c8027 | 121 | break; |
nnguyen99 | 3:7486869c8027 | 122 | case sSTOP: |
nnguyen99 | 3:7486869c8027 | 123 | if(X){ |
nnguyen99 | 3:7486869c8027 | 124 | curr_state = sRESET; |
nnguyen99 | 3:7486869c8027 | 125 | }else if(S&R){ |
nnguyen99 | 3:7486869c8027 | 126 | curr_state = sRECORD; |
nnguyen99 | 3:7486869c8027 | 127 | }else if(S&P){ |
nnguyen99 | 3:7486869c8027 | 128 | curr_state = sPLAY; |
nnguyen99 | 3:7486869c8027 | 129 | } |
nnguyen99 | 3:7486869c8027 | 130 | break; |
nnguyen99 | 3:7486869c8027 | 131 | case sRECORD: |
nnguyen99 | 3:7486869c8027 | 132 | if(X){ |
nnguyen99 | 3:7486869c8027 | 133 | curr_state = sRESET; |
nnguyen99 | 3:7486869c8027 | 134 | }else if(R&S){ |
nnguyen99 | 3:7486869c8027 | 135 | curr_state = sSTOP; |
nnguyen99 | 3:7486869c8027 | 136 | }else if(R&P){ |
nnguyen99 | 3:7486869c8027 | 137 | curr_state = sPLAY; |
nnguyen99 | 3:7486869c8027 | 138 | } |
nnguyen99 | 3:7486869c8027 | 139 | break; |
nnguyen99 | 3:7486869c8027 | 140 | case sPLAY: |
nnguyen99 | 3:7486869c8027 | 141 | if(X){ |
nnguyen99 | 3:7486869c8027 | 142 | curr_state = sRESET; |
nnguyen99 | 3:7486869c8027 | 143 | }else if(P&S){ |
nnguyen99 | 3:7486869c8027 | 144 | curr_state = sSTOP; |
nnguyen99 | 3:7486869c8027 | 145 | }else if(P&R){ |
nnguyen99 | 3:7486869c8027 | 146 | curr_state = sPLAY; |
nnguyen99 | 3:7486869c8027 | 147 | } |
nnguyen99 | 3:7486869c8027 | 148 | break; |
nnguyen99 | 3:7486869c8027 | 149 | case sERASE: |
nnguyen99 | 3:7486869c8027 | 150 | if(X){ |
nnguyen99 | 3:7486869c8027 | 151 | curr_state = sRESET; |
nnguyen99 | 3:7486869c8027 | 152 | } |
nnguyen99 | 3:7486869c8027 | 153 | } |
nnguyen99 | 3:7486869c8027 | 154 | } |
nnguyen99 | 3:7486869c8027 | 155 | } |
trmontgomery | 4:1790aa9234a3 | 156 | |
trmontgomery | 21:a279bb16a37b | 157 | |
trmontgomery | 4:1790aa9234a3 | 158 | int main() { |
trmontgomery | 21:a279bb16a37b | 159 | myled1 = 1; |
trmontgomery | 21:a279bb16a37b | 160 | //hardware_init(); |
trmontgomery | 21:a279bb16a37b | 161 | //state_machine_mgr(); |
trmontgomery | 21:a279bb16a37b | 162 | move_motors(); |
trmontgomery | 4:1790aa9234a3 | 163 | } |