Martin Werluschnig
/
STM_Statemachine
STM_Statemachine
main.cpp@0:99a1359232c8, 2018-11-15 (annotated)
- Committer:
- martwerl
- Date:
- Thu Nov 15 18:10:10 2018 +0000
- Revision:
- 0:99a1359232c8
STM_Statemachine
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martwerl | 0:99a1359232c8 | 1 | #include "mbed.h" |
martwerl | 0:99a1359232c8 | 2 | #include "LM75B.h" |
martwerl | 0:99a1359232c8 | 3 | |
martwerl | 0:99a1359232c8 | 4 | LM75B tmp(p28, p27); |
martwerl | 0:99a1359232c8 | 5 | |
martwerl | 0:99a1359232c8 | 6 | DigitalOut led1(LED1); |
martwerl | 0:99a1359232c8 | 7 | DigitalOut led2(LED2); |
martwerl | 0:99a1359232c8 | 8 | DigitalOut led3(LED3); |
martwerl | 0:99a1359232c8 | 9 | DigitalOut led4(LED4); |
martwerl | 0:99a1359232c8 | 10 | |
martwerl | 0:99a1359232c8 | 11 | InterruptIn sw1(p14); |
martwerl | 0:99a1359232c8 | 12 | InterruptIn sw2(p15); |
martwerl | 0:99a1359232c8 | 13 | |
martwerl | 0:99a1359232c8 | 14 | void flipper(void); |
martwerl | 0:99a1359232c8 | 15 | void ON(void); |
martwerl | 0:99a1359232c8 | 16 | void IDLE(void); |
martwerl | 0:99a1359232c8 | 17 | void sw1Rise(void); |
martwerl | 0:99a1359232c8 | 18 | void sw2Rise(void); |
martwerl | 0:99a1359232c8 | 19 | void Time(void); |
martwerl | 0:99a1359232c8 | 20 | void TimeOut(void); |
martwerl | 0:99a1359232c8 | 21 | void Blink(void); |
martwerl | 0:99a1359232c8 | 22 | void tempOut(void); |
martwerl | 0:99a1359232c8 | 23 | void timeOutError(void); |
martwerl | 0:99a1359232c8 | 24 | void errorBlink(void); |
martwerl | 0:99a1359232c8 | 25 | void timeover(void); |
martwerl | 0:99a1359232c8 | 26 | bool checker(void); |
martwerl | 0:99a1359232c8 | 27 | bool checkerSW2(void); |
martwerl | 0:99a1359232c8 | 28 | bool volatile sw2Pressed; |
martwerl | 0:99a1359232c8 | 29 | bool volatile sw1Pressed; |
martwerl | 0:99a1359232c8 | 30 | |
martwerl | 0:99a1359232c8 | 31 | Timeout removeTime; |
martwerl | 0:99a1359232c8 | 32 | Timer gettime; |
martwerl | 0:99a1359232c8 | 33 | Ticker tickerflipper; |
martwerl | 0:99a1359232c8 | 34 | int counter; |
martwerl | 0:99a1359232c8 | 35 | |
martwerl | 0:99a1359232c8 | 36 | enum Status{ST_IDLE=0, ST_ON, ST_TimeOut, ST_TempOut, ST_Error}; |
martwerl | 0:99a1359232c8 | 37 | Status volatile status; |
martwerl | 0:99a1359232c8 | 38 | |
martwerl | 0:99a1359232c8 | 39 | int main(void) |
martwerl | 0:99a1359232c8 | 40 | { |
martwerl | 0:99a1359232c8 | 41 | sw1.rise(&sw1Rise); |
martwerl | 0:99a1359232c8 | 42 | sw2.rise(&sw2Rise); |
martwerl | 0:99a1359232c8 | 43 | status = ST_IDLE; |
martwerl | 0:99a1359232c8 | 44 | sw1Pressed = 0; |
martwerl | 0:99a1359232c8 | 45 | sw2Pressed = 0; |
martwerl | 0:99a1359232c8 | 46 | counter=0; |
martwerl | 0:99a1359232c8 | 47 | led1=0, led2=0, led3=0, led4=0; |
martwerl | 0:99a1359232c8 | 48 | |
martwerl | 0:99a1359232c8 | 49 | while(1) |
martwerl | 0:99a1359232c8 | 50 | { |
martwerl | 0:99a1359232c8 | 51 | switch(status) |
martwerl | 0:99a1359232c8 | 52 | { |
martwerl | 0:99a1359232c8 | 53 | case ST_IDLE:IDLE() ; break; |
martwerl | 0:99a1359232c8 | 54 | case ST_ON:ON() ; break; |
martwerl | 0:99a1359232c8 | 55 | case ST_TimeOut:TimeOut() ; break; |
martwerl | 0:99a1359232c8 | 56 | case ST_TempOut:tempOut() ; break; |
martwerl | 0:99a1359232c8 | 57 | case ST_Error:timeover() ; break; |
martwerl | 0:99a1359232c8 | 58 | } |
martwerl | 0:99a1359232c8 | 59 | } |
martwerl | 0:99a1359232c8 | 60 | } |
martwerl | 0:99a1359232c8 | 61 | |
martwerl | 0:99a1359232c8 | 62 | void timeover(void) |
martwerl | 0:99a1359232c8 | 63 | { |
martwerl | 0:99a1359232c8 | 64 | printf("Ende gut, Alles gut\n"); |
martwerl | 0:99a1359232c8 | 65 | while(1) |
martwerl | 0:99a1359232c8 | 66 | {} |
martwerl | 0:99a1359232c8 | 67 | } |
martwerl | 0:99a1359232c8 | 68 | |
martwerl | 0:99a1359232c8 | 69 | void errorBlink(void) |
martwerl | 0:99a1359232c8 | 70 | { |
martwerl | 0:99a1359232c8 | 71 | led4 = !led4; |
martwerl | 0:99a1359232c8 | 72 | |
martwerl | 0:99a1359232c8 | 73 | } |
martwerl | 0:99a1359232c8 | 74 | void timeOutError(void) |
martwerl | 0:99a1359232c8 | 75 | { |
martwerl | 0:99a1359232c8 | 76 | status = ST_Error; |
martwerl | 0:99a1359232c8 | 77 | printf("Error TimeOut\n"); |
martwerl | 0:99a1359232c8 | 78 | printf("Status: %i\n", status); |
martwerl | 0:99a1359232c8 | 79 | } |
martwerl | 0:99a1359232c8 | 80 | |
martwerl | 0:99a1359232c8 | 81 | void tempOut(void) |
martwerl | 0:99a1359232c8 | 82 | { |
martwerl | 0:99a1359232c8 | 83 | int countertmp = 0; |
martwerl | 0:99a1359232c8 | 84 | |
martwerl | 0:99a1359232c8 | 85 | printf("void tempOut\n"); |
martwerl | 0:99a1359232c8 | 86 | while(1) |
martwerl | 0:99a1359232c8 | 87 | { |
martwerl | 0:99a1359232c8 | 88 | printf("Temperatur: %f\n",tmp.read()); |
martwerl | 0:99a1359232c8 | 89 | if(checkerSW2() == true) |
martwerl | 0:99a1359232c8 | 90 | { |
martwerl | 0:99a1359232c8 | 91 | status = ST_ON; |
martwerl | 0:99a1359232c8 | 92 | return; |
martwerl | 0:99a1359232c8 | 93 | } |
martwerl | 0:99a1359232c8 | 94 | countertmp++; |
martwerl | 0:99a1359232c8 | 95 | wait(1); |
martwerl | 0:99a1359232c8 | 96 | } |
martwerl | 0:99a1359232c8 | 97 | } |
martwerl | 0:99a1359232c8 | 98 | |
martwerl | 0:99a1359232c8 | 99 | void flipper(void) |
martwerl | 0:99a1359232c8 | 100 | { |
martwerl | 0:99a1359232c8 | 101 | printf("void flipper\n"); |
martwerl | 0:99a1359232c8 | 102 | led4 = !led4; |
martwerl | 0:99a1359232c8 | 103 | if(counter == 3) |
martwerl | 0:99a1359232c8 | 104 | { |
martwerl | 0:99a1359232c8 | 105 | tickerflipper.detach(); |
martwerl | 0:99a1359232c8 | 106 | counter=0; |
martwerl | 0:99a1359232c8 | 107 | } |
martwerl | 0:99a1359232c8 | 108 | else |
martwerl | 0:99a1359232c8 | 109 | counter++; |
martwerl | 0:99a1359232c8 | 110 | } |
martwerl | 0:99a1359232c8 | 111 | |
martwerl | 0:99a1359232c8 | 112 | void TimeOut(void) |
martwerl | 0:99a1359232c8 | 113 | { |
martwerl | 0:99a1359232c8 | 114 | printf("void TimeOut\n"); |
martwerl | 0:99a1359232c8 | 115 | int counterto = 0; |
martwerl | 0:99a1359232c8 | 116 | while(1) |
martwerl | 0:99a1359232c8 | 117 | { |
martwerl | 0:99a1359232c8 | 118 | printf("TimeOut: %i\n", counterto); |
martwerl | 0:99a1359232c8 | 119 | counterto++; |
martwerl | 0:99a1359232c8 | 120 | wait(1); |
martwerl | 0:99a1359232c8 | 121 | |
martwerl | 0:99a1359232c8 | 122 | if(checkerSW2() == true) |
martwerl | 0:99a1359232c8 | 123 | { |
martwerl | 0:99a1359232c8 | 124 | status = ST_TempOut; |
martwerl | 0:99a1359232c8 | 125 | return; |
martwerl | 0:99a1359232c8 | 126 | } |
martwerl | 0:99a1359232c8 | 127 | } |
martwerl | 0:99a1359232c8 | 128 | } |
martwerl | 0:99a1359232c8 | 129 | |
martwerl | 0:99a1359232c8 | 130 | void Time(void) |
martwerl | 0:99a1359232c8 | 131 | { |
martwerl | 0:99a1359232c8 | 132 | printf("void Time\n"); |
martwerl | 0:99a1359232c8 | 133 | if (gettime.read() ==0) |
martwerl | 0:99a1359232c8 | 134 | { |
martwerl | 0:99a1359232c8 | 135 | gettime.start(); |
martwerl | 0:99a1359232c8 | 136 | } |
martwerl | 0:99a1359232c8 | 137 | |
martwerl | 0:99a1359232c8 | 138 | else |
martwerl | 0:99a1359232c8 | 139 | printf("Secunden: %f\n", gettime.read()); |
martwerl | 0:99a1359232c8 | 140 | gettime.reset(); |
martwerl | 0:99a1359232c8 | 141 | } |
martwerl | 0:99a1359232c8 | 142 | |
martwerl | 0:99a1359232c8 | 143 | bool checker(void) |
martwerl | 0:99a1359232c8 | 144 | { |
martwerl | 0:99a1359232c8 | 145 | if(sw1Pressed == true) |
martwerl | 0:99a1359232c8 | 146 | { |
martwerl | 0:99a1359232c8 | 147 | printf("void checker\n"); |
martwerl | 0:99a1359232c8 | 148 | Time(); |
martwerl | 0:99a1359232c8 | 149 | sw1Pressed = false; |
martwerl | 0:99a1359232c8 | 150 | return true; |
martwerl | 0:99a1359232c8 | 151 | } |
martwerl | 0:99a1359232c8 | 152 | return false; |
martwerl | 0:99a1359232c8 | 153 | } |
martwerl | 0:99a1359232c8 | 154 | |
martwerl | 0:99a1359232c8 | 155 | bool checkerSW2(void) |
martwerl | 0:99a1359232c8 | 156 | { |
martwerl | 0:99a1359232c8 | 157 | if(sw2Pressed == true) |
martwerl | 0:99a1359232c8 | 158 | { |
martwerl | 0:99a1359232c8 | 159 | printf("void checkerSW2\n"); |
martwerl | 0:99a1359232c8 | 160 | sw2Pressed = false; |
martwerl | 0:99a1359232c8 | 161 | return true; |
martwerl | 0:99a1359232c8 | 162 | } |
martwerl | 0:99a1359232c8 | 163 | return false; |
martwerl | 0:99a1359232c8 | 164 | } |
martwerl | 0:99a1359232c8 | 165 | |
martwerl | 0:99a1359232c8 | 166 | |
martwerl | 0:99a1359232c8 | 167 | void sw1Rise(void) |
martwerl | 0:99a1359232c8 | 168 | { |
martwerl | 0:99a1359232c8 | 169 | printf("sw1Rise\n"); |
martwerl | 0:99a1359232c8 | 170 | |
martwerl | 0:99a1359232c8 | 171 | if(status == ST_ON || status == ST_IDLE) sw1Pressed = 1; |
martwerl | 0:99a1359232c8 | 172 | wait_ms(200); |
martwerl | 0:99a1359232c8 | 173 | } |
martwerl | 0:99a1359232c8 | 174 | |
martwerl | 0:99a1359232c8 | 175 | void sw2Rise(void) |
martwerl | 0:99a1359232c8 | 176 | { |
martwerl | 0:99a1359232c8 | 177 | printf("void sw2Rise\n"); |
martwerl | 0:99a1359232c8 | 178 | if(status == ST_ON || status == ST_TimeOut || status == ST_TempOut) sw2Pressed = 1; |
martwerl | 0:99a1359232c8 | 179 | wait_ms(200); |
martwerl | 0:99a1359232c8 | 180 | } |
martwerl | 0:99a1359232c8 | 181 | |
martwerl | 0:99a1359232c8 | 182 | void ON(void) |
martwerl | 0:99a1359232c8 | 183 | { |
martwerl | 0:99a1359232c8 | 184 | printf("void ON\n"); |
martwerl | 0:99a1359232c8 | 185 | tickerflipper.attach(&flipper, 0.5); |
martwerl | 0:99a1359232c8 | 186 | removeTime.attach(&timeOutError, 10); |
martwerl | 0:99a1359232c8 | 187 | led1 = true; |
martwerl | 0:99a1359232c8 | 188 | |
martwerl | 0:99a1359232c8 | 189 | while(1) |
martwerl | 0:99a1359232c8 | 190 | { |
martwerl | 0:99a1359232c8 | 191 | if(status == ST_Error) |
martwerl | 0:99a1359232c8 | 192 | { |
martwerl | 0:99a1359232c8 | 193 | printf("IDLE verlassen\n"); |
martwerl | 0:99a1359232c8 | 194 | tickerflipper.attach(&errorBlink, 0.3); |
martwerl | 0:99a1359232c8 | 195 | led1=false; |
martwerl | 0:99a1359232c8 | 196 | return; |
martwerl | 0:99a1359232c8 | 197 | } |
martwerl | 0:99a1359232c8 | 198 | if(checkerSW2() == true) |
martwerl | 0:99a1359232c8 | 199 | { |
martwerl | 0:99a1359232c8 | 200 | status = ST_TimeOut; |
martwerl | 0:99a1359232c8 | 201 | removeTime.detach(); |
martwerl | 0:99a1359232c8 | 202 | return; |
martwerl | 0:99a1359232c8 | 203 | } |
martwerl | 0:99a1359232c8 | 204 | if (checker() == true) |
martwerl | 0:99a1359232c8 | 205 | { |
martwerl | 0:99a1359232c8 | 206 | status = ST_IDLE; |
martwerl | 0:99a1359232c8 | 207 | removeTime.detach(); |
martwerl | 0:99a1359232c8 | 208 | led1=false; |
martwerl | 0:99a1359232c8 | 209 | return; |
martwerl | 0:99a1359232c8 | 210 | } |
martwerl | 0:99a1359232c8 | 211 | } |
martwerl | 0:99a1359232c8 | 212 | } |
martwerl | 0:99a1359232c8 | 213 | |
martwerl | 0:99a1359232c8 | 214 | void IDLE(void) |
martwerl | 0:99a1359232c8 | 215 | { |
martwerl | 0:99a1359232c8 | 216 | printf("void IDLE\n"); |
martwerl | 0:99a1359232c8 | 217 | removeTime.attach(&timeOutError, 10); |
martwerl | 0:99a1359232c8 | 218 | while(1) |
martwerl | 0:99a1359232c8 | 219 | { |
martwerl | 0:99a1359232c8 | 220 | if(status == ST_Error) |
martwerl | 0:99a1359232c8 | 221 | { |
martwerl | 0:99a1359232c8 | 222 | printf("IDLE verlassen\n"); |
martwerl | 0:99a1359232c8 | 223 | tickerflipper.attach(&errorBlink, 0.3); |
martwerl | 0:99a1359232c8 | 224 | led1=false; |
martwerl | 0:99a1359232c8 | 225 | return; |
martwerl | 0:99a1359232c8 | 226 | } |
martwerl | 0:99a1359232c8 | 227 | if (checker() == true) |
martwerl | 0:99a1359232c8 | 228 | { |
martwerl | 0:99a1359232c8 | 229 | status = ST_ON; |
martwerl | 0:99a1359232c8 | 230 | removeTime.detach(); |
martwerl | 0:99a1359232c8 | 231 | return; |
martwerl | 0:99a1359232c8 | 232 | } |
martwerl | 0:99a1359232c8 | 233 | |
martwerl | 0:99a1359232c8 | 234 | } |
martwerl | 0:99a1359232c8 | 235 | } |