Philipp Kundegraber
/
TINF_PE_20181025_STM_Schalter
State Maschine Übungen von fPucher https://os.mbed.com/users/fpucher/code/HIM0Board/wiki/STM-Schalter
main.cpp@2:7dafc3e72a72, 2018-11-15 (annotated)
- Committer:
- kunphil
- Date:
- Thu Nov 15 16:30:33 2018 +0000
- Revision:
- 2:7dafc3e72a72
- Parent:
- 1:fdfc2454217b
- Child:
- 3:b5c28432ee25
Erste Aufgabe zu State Machine;; Mit drittem State (TimeOut)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kunphil | 0:7f1867e68546 | 1 | #include "mbed.h" |
kunphil | 0:7f1867e68546 | 2 | #include "C12832.h" |
kunphil | 0:7f1867e68546 | 3 | |
kunphil | 0:7f1867e68546 | 4 | DigitalOut Led1(LED1, 0); |
kunphil | 0:7f1867e68546 | 5 | DigitalOut Led2(LED2, 0); |
kunphil | 0:7f1867e68546 | 6 | DigitalOut Led3(LED3, 0); |
kunphil | 0:7f1867e68546 | 7 | DigitalOut Led4(LED4, 0); |
kunphil | 0:7f1867e68546 | 8 | InterruptIn SW1(p14); |
kunphil | 0:7f1867e68546 | 9 | InterruptIn SW2(p15); |
kunphil | 0:7f1867e68546 | 10 | InterruptIn SW3(p12); |
kunphil | 0:7f1867e68546 | 11 | InterruptIn SW4(p16); |
kunphil | 0:7f1867e68546 | 12 | |
kunphil | 1:fdfc2454217b | 13 | Ticker T1; |
kunphil | 2:7dafc3e72a72 | 14 | Timer T2; |
kunphil | 2:7dafc3e72a72 | 15 | volatile float timerValue = 0; |
kunphil | 1:fdfc2454217b | 16 | volatile int blinkCount = 0; |
kunphil | 1:fdfc2454217b | 17 | |
kunphil | 0:7f1867e68546 | 18 | //************** LCD **************** |
kunphil | 0:7f1867e68546 | 19 | |
kunphil | 0:7f1867e68546 | 20 | C12832 lcd(p5, p7, p6, p8, p11); |
kunphil | 0:7f1867e68546 | 21 | |
kunphil | 0:7f1867e68546 | 22 | //************* VARIABLEN **************** |
kunphil | 0:7f1867e68546 | 23 | |
kunphil | 2:7dafc3e72a72 | 24 | enum State {ST_AUS=0, ST_EIN, ST_TIMEOUT}; |
kunphil | 0:7f1867e68546 | 25 | State state; |
kunphil | 0:7f1867e68546 | 26 | |
kunphil | 2:7dafc3e72a72 | 27 | bool pressed1 = false, pressed2 = false; |
kunphil | 0:7f1867e68546 | 28 | |
kunphil | 1:fdfc2454217b | 29 | //************* andere Funktionen **************** |
kunphil | 1:fdfc2454217b | 30 | |
kunphil | 1:fdfc2454217b | 31 | void blink() |
kunphil | 1:fdfc2454217b | 32 | { |
kunphil | 1:fdfc2454217b | 33 | blinkCount++; |
kunphil | 1:fdfc2454217b | 34 | |
kunphil | 1:fdfc2454217b | 35 | Led4 = !Led4; |
kunphil | 1:fdfc2454217b | 36 | } |
kunphil | 1:fdfc2454217b | 37 | |
kunphil | 0:7f1867e68546 | 38 | |
kunphil | 0:7f1867e68546 | 39 | //************* EREIGNISSE **************** |
kunphil | 0:7f1867e68546 | 40 | |
kunphil | 2:7dafc3e72a72 | 41 | void rise1(void) |
kunphil | 0:7f1867e68546 | 42 | { |
kunphil | 2:7dafc3e72a72 | 43 | wait_ms(100); //Entprellung |
kunphil | 2:7dafc3e72a72 | 44 | pressed1 = true; |
kunphil | 2:7dafc3e72a72 | 45 | |
kunphil | 2:7dafc3e72a72 | 46 | timerValue = T2.read(); |
kunphil | 2:7dafc3e72a72 | 47 | T2.reset(); |
kunphil | 2:7dafc3e72a72 | 48 | T2.start(); |
kunphil | 0:7f1867e68546 | 49 | } |
kunphil | 0:7f1867e68546 | 50 | |
kunphil | 2:7dafc3e72a72 | 51 | bool CheckFlag1() |
kunphil | 0:7f1867e68546 | 52 | { |
kunphil | 2:7dafc3e72a72 | 53 | if (pressed1) { |
kunphil | 2:7dafc3e72a72 | 54 | pressed1=false; |
kunphil | 2:7dafc3e72a72 | 55 | return true; |
kunphil | 2:7dafc3e72a72 | 56 | } |
kunphil | 2:7dafc3e72a72 | 57 | return false; |
kunphil | 2:7dafc3e72a72 | 58 | } |
kunphil | 2:7dafc3e72a72 | 59 | |
kunphil | 2:7dafc3e72a72 | 60 | void rise2(void) |
kunphil | 2:7dafc3e72a72 | 61 | { |
kunphil | 2:7dafc3e72a72 | 62 | wait_ms(100); |
kunphil | 2:7dafc3e72a72 | 63 | if(state == ST_EIN) pressed2 = true; |
kunphil | 2:7dafc3e72a72 | 64 | } |
kunphil | 2:7dafc3e72a72 | 65 | |
kunphil | 2:7dafc3e72a72 | 66 | bool CheckFlag2() |
kunphil | 2:7dafc3e72a72 | 67 | { |
kunphil | 2:7dafc3e72a72 | 68 | if (pressed2) { |
kunphil | 2:7dafc3e72a72 | 69 | pressed2=false; |
kunphil | 0:7f1867e68546 | 70 | return true; |
kunphil | 0:7f1867e68546 | 71 | } |
kunphil | 0:7f1867e68546 | 72 | return false; |
kunphil | 0:7f1867e68546 | 73 | } |
kunphil | 0:7f1867e68546 | 74 | |
kunphil | 0:7f1867e68546 | 75 | //************* STATES **************** |
kunphil | 0:7f1867e68546 | 76 | |
kunphil | 0:7f1867e68546 | 77 | void ST_Aus (void) |
kunphil | 0:7f1867e68546 | 78 | { |
kunphil | 0:7f1867e68546 | 79 | //Status auf LCD und Serial |
kunphil | 0:7f1867e68546 | 80 | lcd.cls(); // löscht lcd (clear screen) |
kunphil | 0:7f1867e68546 | 81 | lcd.locate(0,0); // x-position, y-position (x: 0-128; y: 0-32) |
kunphil | 0:7f1867e68546 | 82 | lcd.printf("State: 1 (Aus)"); |
kunphil | 0:7f1867e68546 | 83 | printf("State: 1 (Aus)"); |
kunphil | 0:7f1867e68546 | 84 | // entry |
kunphil | 0:7f1867e68546 | 85 | |
kunphil | 0:7f1867e68546 | 86 | // do |
kunphil | 0:7f1867e68546 | 87 | while(true) { |
kunphil | 0:7f1867e68546 | 88 | Led1 = 0; |
kunphil | 0:7f1867e68546 | 89 | |
kunphil | 2:7dafc3e72a72 | 90 | if(CheckFlag1()) { |
kunphil | 0:7f1867e68546 | 91 | state = ST_EIN; |
kunphil | 0:7f1867e68546 | 92 | |
kunphil | 0:7f1867e68546 | 93 | // exit |
kunphil | 0:7f1867e68546 | 94 | return; |
kunphil | 0:7f1867e68546 | 95 | } |
kunphil | 0:7f1867e68546 | 96 | } |
kunphil | 0:7f1867e68546 | 97 | } |
kunphil | 0:7f1867e68546 | 98 | |
kunphil | 0:7f1867e68546 | 99 | void ST_Ein (void) |
kunphil | 0:7f1867e68546 | 100 | { |
kunphil | 0:7f1867e68546 | 101 | //Status auf LCD und Serial |
kunphil | 0:7f1867e68546 | 102 | lcd.cls(); // löscht lcd (clear screen) |
kunphil | 0:7f1867e68546 | 103 | lcd.locate(0,0); // x-position, y-position (x: 0-128; y: 0-32) |
kunphil | 0:7f1867e68546 | 104 | lcd.printf("State: 2 (Ein)"); |
kunphil | 0:7f1867e68546 | 105 | printf("State: 2 (Ein)"); |
kunphil | 0:7f1867e68546 | 106 | |
kunphil | 0:7f1867e68546 | 107 | // entry |
kunphil | 1:fdfc2454217b | 108 | T1.attach(&blink, 0.3); |
kunphil | 1:fdfc2454217b | 109 | while (true) |
kunphil | 1:fdfc2454217b | 110 | { |
kunphil | 1:fdfc2454217b | 111 | if(blinkCount >= 4) break; |
kunphil | 1:fdfc2454217b | 112 | } |
kunphil | 1:fdfc2454217b | 113 | T1.detach(); |
kunphil | 1:fdfc2454217b | 114 | blinkCount = 0; |
kunphil | 2:7dafc3e72a72 | 115 | |
kunphil | 0:7f1867e68546 | 116 | // do |
kunphil | 0:7f1867e68546 | 117 | while(true) { |
kunphil | 0:7f1867e68546 | 118 | Led1 = 1; |
kunphil | 2:7dafc3e72a72 | 119 | |
kunphil | 2:7dafc3e72a72 | 120 | if(CheckFlag1()) { |
kunphil | 0:7f1867e68546 | 121 | state = ST_AUS; |
kunphil | 2:7dafc3e72a72 | 122 | // exit |
kunphil | 2:7dafc3e72a72 | 123 | return; |
kunphil | 2:7dafc3e72a72 | 124 | } |
kunphil | 0:7f1867e68546 | 125 | |
kunphil | 2:7dafc3e72a72 | 126 | if(CheckFlag2()) { |
kunphil | 2:7dafc3e72a72 | 127 | state = ST_TIMEOUT; |
kunphil | 0:7f1867e68546 | 128 | // exit |
kunphil | 0:7f1867e68546 | 129 | return; |
kunphil | 0:7f1867e68546 | 130 | } |
kunphil | 0:7f1867e68546 | 131 | } |
kunphil | 0:7f1867e68546 | 132 | } |
kunphil | 0:7f1867e68546 | 133 | |
kunphil | 2:7dafc3e72a72 | 134 | void ST_TimeOut (void) |
kunphil | 2:7dafc3e72a72 | 135 | { |
kunphil | 2:7dafc3e72a72 | 136 | //Status auf LCD und Serial |
kunphil | 2:7dafc3e72a72 | 137 | lcd.cls(); // löscht lcd (clear screen) |
kunphil | 2:7dafc3e72a72 | 138 | lcd.locate(0,0); // x-position, y-position (x: 0-128; y: 0-32) |
kunphil | 2:7dafc3e72a72 | 139 | lcd.printf("State: 3 (TimeOut)"); |
kunphil | 2:7dafc3e72a72 | 140 | printf("State: 3 (TimeOut)"); |
kunphil | 2:7dafc3e72a72 | 141 | |
kunphil | 2:7dafc3e72a72 | 142 | // entry |
kunphil | 2:7dafc3e72a72 | 143 | |
kunphil | 2:7dafc3e72a72 | 144 | |
kunphil | 2:7dafc3e72a72 | 145 | // do |
kunphil | 2:7dafc3e72a72 | 146 | while(true) { |
kunphil | 2:7dafc3e72a72 | 147 | lcd.locate(0,12); // x-position, y-position (x: 0-128; y: 0-32) |
kunphil | 2:7dafc3e72a72 | 148 | lcd.printf("Time: %f", timerValue); |
kunphil | 2:7dafc3e72a72 | 149 | printf("Time: %f", timerValue); |
kunphil | 2:7dafc3e72a72 | 150 | |
kunphil | 2:7dafc3e72a72 | 151 | // exit |
kunphil | 2:7dafc3e72a72 | 152 | |
kunphil | 2:7dafc3e72a72 | 153 | } |
kunphil | 2:7dafc3e72a72 | 154 | } |
kunphil | 2:7dafc3e72a72 | 155 | |
kunphil | 0:7f1867e68546 | 156 | void ST_Error (void) |
kunphil | 0:7f1867e68546 | 157 | { |
kunphil | 0:7f1867e68546 | 158 | //Status auf LCD und Serial |
kunphil | 0:7f1867e68546 | 159 | lcd.cls(); // löscht lcd (clear screen) |
kunphil | 0:7f1867e68546 | 160 | lcd.locate(0,0); // x-position, y-position (x: 0-128; y: 0-32) |
kunphil | 0:7f1867e68546 | 161 | lcd.printf("State: ERROR!!!"); |
kunphil | 0:7f1867e68546 | 162 | printf("State: ERROR!!!"); |
kunphil | 0:7f1867e68546 | 163 | return; |
kunphil | 0:7f1867e68546 | 164 | } |
kunphil | 0:7f1867e68546 | 165 | |
kunphil | 2:7dafc3e72a72 | 166 | //************* STATE MACHINE **************** |
kunphil | 2:7dafc3e72a72 | 167 | |
kunphil | 0:7f1867e68546 | 168 | void stateMachine() |
kunphil | 0:7f1867e68546 | 169 | { |
kunphil | 0:7f1867e68546 | 170 | switch (state) |
kunphil | 0:7f1867e68546 | 171 | { |
kunphil | 0:7f1867e68546 | 172 | case ST_AUS: ST_Aus(); |
kunphil | 0:7f1867e68546 | 173 | break; |
kunphil | 0:7f1867e68546 | 174 | case ST_EIN: ST_Ein(); |
kunphil | 0:7f1867e68546 | 175 | break; |
kunphil | 2:7dafc3e72a72 | 176 | case ST_TIMEOUT: ST_TimeOut(); |
kunphil | 2:7dafc3e72a72 | 177 | break; |
kunphil | 0:7f1867e68546 | 178 | default: ST_Error(); // sollte nicht auftreten :-) |
kunphil | 0:7f1867e68546 | 179 | break; |
kunphil | 0:7f1867e68546 | 180 | } |
kunphil | 0:7f1867e68546 | 181 | } |
kunphil | 0:7f1867e68546 | 182 | |
kunphil | 2:7dafc3e72a72 | 183 | //************* MAIN **************** |
kunphil | 0:7f1867e68546 | 184 | |
kunphil | 0:7f1867e68546 | 185 | int main() |
kunphil | 0:7f1867e68546 | 186 | { |
kunphil | 2:7dafc3e72a72 | 187 | SW1.rise(&rise1); //.fall(&fall); |
kunphil | 2:7dafc3e72a72 | 188 | SW2.rise(&rise2); |
kunphil | 0:7f1867e68546 | 189 | state = ST_AUS; |
kunphil | 0:7f1867e68546 | 190 | while(true){ |
kunphil | 0:7f1867e68546 | 191 | stateMachine(); |
kunphil | 0:7f1867e68546 | 192 | } |
kunphil | 0:7f1867e68546 | 193 | } |