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@1:fdfc2454217b, 2018-11-08 (annotated)
- Committer:
- kunphil
- Date:
- Thu Nov 08 18:01:57 2018 +0000
- Revision:
- 1:fdfc2454217b
- Parent:
- 0:7f1867e68546
- Child:
- 2:7dafc3e72a72
Erste Aufgabe zu State Machine; mit Ticker blinken
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 | 1:fdfc2454217b | 14 | volatile int blinkCount = 0; |
kunphil | 1:fdfc2454217b | 15 | |
kunphil | 0:7f1867e68546 | 16 | //************** LCD **************** |
kunphil | 0:7f1867e68546 | 17 | |
kunphil | 0:7f1867e68546 | 18 | C12832 lcd(p5, p7, p6, p8, p11); |
kunphil | 0:7f1867e68546 | 19 | |
kunphil | 0:7f1867e68546 | 20 | //************* VARIABLEN **************** |
kunphil | 0:7f1867e68546 | 21 | |
kunphil | 0:7f1867e68546 | 22 | enum State {ST_AUS=0, ST_EIN}; |
kunphil | 0:7f1867e68546 | 23 | State state; |
kunphil | 0:7f1867e68546 | 24 | |
kunphil | 0:7f1867e68546 | 25 | bool pressed = false; |
kunphil | 0:7f1867e68546 | 26 | |
kunphil | 1:fdfc2454217b | 27 | //************* andere Funktionen **************** |
kunphil | 1:fdfc2454217b | 28 | |
kunphil | 1:fdfc2454217b | 29 | void blink() |
kunphil | 1:fdfc2454217b | 30 | { |
kunphil | 1:fdfc2454217b | 31 | blinkCount++; |
kunphil | 1:fdfc2454217b | 32 | |
kunphil | 1:fdfc2454217b | 33 | Led4 = !Led4; |
kunphil | 1:fdfc2454217b | 34 | } |
kunphil | 1:fdfc2454217b | 35 | |
kunphil | 0:7f1867e68546 | 36 | |
kunphil | 0:7f1867e68546 | 37 | //************* EREIGNISSE **************** |
kunphil | 0:7f1867e68546 | 38 | |
kunphil | 0:7f1867e68546 | 39 | void rise(void) |
kunphil | 0:7f1867e68546 | 40 | { |
kunphil | 0:7f1867e68546 | 41 | wait_ms(50); |
kunphil | 0:7f1867e68546 | 42 | pressed = true; |
kunphil | 0:7f1867e68546 | 43 | } |
kunphil | 0:7f1867e68546 | 44 | |
kunphil | 0:7f1867e68546 | 45 | bool CheckFlag() |
kunphil | 0:7f1867e68546 | 46 | { |
kunphil | 0:7f1867e68546 | 47 | if (pressed) { |
kunphil | 0:7f1867e68546 | 48 | pressed=false; |
kunphil | 0:7f1867e68546 | 49 | return true; |
kunphil | 0:7f1867e68546 | 50 | } |
kunphil | 0:7f1867e68546 | 51 | return false; |
kunphil | 0:7f1867e68546 | 52 | } |
kunphil | 0:7f1867e68546 | 53 | |
kunphil | 0:7f1867e68546 | 54 | //************* STATES **************** |
kunphil | 0:7f1867e68546 | 55 | |
kunphil | 0:7f1867e68546 | 56 | void ST_Aus (void) |
kunphil | 0:7f1867e68546 | 57 | { |
kunphil | 0:7f1867e68546 | 58 | //Status auf LCD und Serial |
kunphil | 0:7f1867e68546 | 59 | lcd.cls(); // löscht lcd (clear screen) |
kunphil | 0:7f1867e68546 | 60 | lcd.locate(0,0); // x-position, y-position (x: 0-128; y: 0-32) |
kunphil | 0:7f1867e68546 | 61 | lcd.printf("State: 1 (Aus)"); |
kunphil | 0:7f1867e68546 | 62 | printf("State: 1 (Aus)"); |
kunphil | 0:7f1867e68546 | 63 | // entry |
kunphil | 0:7f1867e68546 | 64 | |
kunphil | 0:7f1867e68546 | 65 | // do |
kunphil | 0:7f1867e68546 | 66 | while(true) { |
kunphil | 0:7f1867e68546 | 67 | Led1 = 0; |
kunphil | 0:7f1867e68546 | 68 | |
kunphil | 0:7f1867e68546 | 69 | if(CheckFlag()) { |
kunphil | 0:7f1867e68546 | 70 | state = ST_EIN; |
kunphil | 0:7f1867e68546 | 71 | |
kunphil | 0:7f1867e68546 | 72 | // exit |
kunphil | 0:7f1867e68546 | 73 | return; |
kunphil | 0:7f1867e68546 | 74 | } |
kunphil | 0:7f1867e68546 | 75 | } |
kunphil | 0:7f1867e68546 | 76 | } |
kunphil | 0:7f1867e68546 | 77 | |
kunphil | 0:7f1867e68546 | 78 | void ST_Ein (void) |
kunphil | 0:7f1867e68546 | 79 | { |
kunphil | 0:7f1867e68546 | 80 | //Status auf LCD und Serial |
kunphil | 0:7f1867e68546 | 81 | lcd.cls(); // löscht lcd (clear screen) |
kunphil | 0:7f1867e68546 | 82 | lcd.locate(0,0); // x-position, y-position (x: 0-128; y: 0-32) |
kunphil | 0:7f1867e68546 | 83 | lcd.printf("State: 2 (Ein)"); |
kunphil | 0:7f1867e68546 | 84 | printf("State: 2 (Ein)"); |
kunphil | 0:7f1867e68546 | 85 | |
kunphil | 0:7f1867e68546 | 86 | // entry |
kunphil | 1:fdfc2454217b | 87 | T1.attach(&blink, 0.3); |
kunphil | 1:fdfc2454217b | 88 | while (true) |
kunphil | 1:fdfc2454217b | 89 | { |
kunphil | 1:fdfc2454217b | 90 | if(blinkCount >= 4) break; |
kunphil | 1:fdfc2454217b | 91 | } |
kunphil | 1:fdfc2454217b | 92 | T1.detach(); |
kunphil | 1:fdfc2454217b | 93 | blinkCount = 0; |
kunphil | 0:7f1867e68546 | 94 | // do |
kunphil | 0:7f1867e68546 | 95 | while(true) { |
kunphil | 0:7f1867e68546 | 96 | Led1 = 1; |
kunphil | 0:7f1867e68546 | 97 | |
kunphil | 0:7f1867e68546 | 98 | if(CheckFlag()) { |
kunphil | 0:7f1867e68546 | 99 | state = ST_AUS; |
kunphil | 0:7f1867e68546 | 100 | |
kunphil | 0:7f1867e68546 | 101 | // exit |
kunphil | 0:7f1867e68546 | 102 | return; |
kunphil | 0:7f1867e68546 | 103 | } |
kunphil | 0:7f1867e68546 | 104 | } |
kunphil | 0:7f1867e68546 | 105 | } |
kunphil | 0:7f1867e68546 | 106 | |
kunphil | 0:7f1867e68546 | 107 | void ST_Error (void) |
kunphil | 0:7f1867e68546 | 108 | { |
kunphil | 0:7f1867e68546 | 109 | //Status auf LCD und Serial |
kunphil | 0:7f1867e68546 | 110 | lcd.cls(); // löscht lcd (clear screen) |
kunphil | 0:7f1867e68546 | 111 | lcd.locate(0,0); // x-position, y-position (x: 0-128; y: 0-32) |
kunphil | 0:7f1867e68546 | 112 | lcd.printf("State: ERROR!!!"); |
kunphil | 0:7f1867e68546 | 113 | printf("State: ERROR!!!"); |
kunphil | 0:7f1867e68546 | 114 | return; |
kunphil | 0:7f1867e68546 | 115 | } |
kunphil | 0:7f1867e68546 | 116 | |
kunphil | 0:7f1867e68546 | 117 | void stateMachine() |
kunphil | 0:7f1867e68546 | 118 | { |
kunphil | 0:7f1867e68546 | 119 | switch (state) |
kunphil | 0:7f1867e68546 | 120 | { |
kunphil | 0:7f1867e68546 | 121 | case ST_AUS: ST_Aus(); |
kunphil | 0:7f1867e68546 | 122 | break; |
kunphil | 0:7f1867e68546 | 123 | case ST_EIN: ST_Ein(); |
kunphil | 0:7f1867e68546 | 124 | break; |
kunphil | 0:7f1867e68546 | 125 | default: ST_Error(); // sollte nicht auftreten :-) |
kunphil | 0:7f1867e68546 | 126 | break; |
kunphil | 0:7f1867e68546 | 127 | } |
kunphil | 0:7f1867e68546 | 128 | } |
kunphil | 0:7f1867e68546 | 129 | |
kunphil | 0:7f1867e68546 | 130 | |
kunphil | 0:7f1867e68546 | 131 | |
kunphil | 0:7f1867e68546 | 132 | int main() |
kunphil | 0:7f1867e68546 | 133 | { |
kunphil | 0:7f1867e68546 | 134 | SW1.rise(&rise); //.fall(&fall); |
kunphil | 0:7f1867e68546 | 135 | state = ST_AUS; |
kunphil | 0:7f1867e68546 | 136 | while(true){ |
kunphil | 0:7f1867e68546 | 137 | stateMachine(); |
kunphil | 0:7f1867e68546 | 138 | } |
kunphil | 0:7f1867e68546 | 139 | } |