Matthias Reichstam
/
STMSchalter_TINF_25102018
abc
main.cpp@0:f4216e9ad3a4, 2018-10-25 (annotated)
- Committer:
- Reichi19
- Date:
- Thu Oct 25 18:03:19 2018 +0000
- Revision:
- 0:f4216e9ad3a4
- Child:
- 1:8c028fd2f964
abc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Reichi19 | 0:f4216e9ad3a4 | 1 | #include "mbed.h" |
Reichi19 | 0:f4216e9ad3a4 | 2 | #include "C12832.h" |
Reichi19 | 0:f4216e9ad3a4 | 3 | |
Reichi19 | 0:f4216e9ad3a4 | 4 | DigitalOut Led1(LED1, 0); |
Reichi19 | 0:f4216e9ad3a4 | 5 | DigitalOut Led2(LED2, 0); |
Reichi19 | 0:f4216e9ad3a4 | 6 | DigitalOut Led3(LED3, 0); |
Reichi19 | 0:f4216e9ad3a4 | 7 | DigitalOut Led4(LED4, 0); |
Reichi19 | 0:f4216e9ad3a4 | 8 | InterruptIn SW1(p14); |
Reichi19 | 0:f4216e9ad3a4 | 9 | InterruptIn SW2(p15); |
Reichi19 | 0:f4216e9ad3a4 | 10 | InterruptIn SW3(p12); |
Reichi19 | 0:f4216e9ad3a4 | 11 | InterruptIn SW4(p16); |
Reichi19 | 0:f4216e9ad3a4 | 12 | |
Reichi19 | 0:f4216e9ad3a4 | 13 | //***** LCD ******* |
Reichi19 | 0:f4216e9ad3a4 | 14 | |
Reichi19 | 0:f4216e9ad3a4 | 15 | C12832 lcd(p5, p7, p6, p8, p11); |
Reichi19 | 0:f4216e9ad3a4 | 16 | |
Reichi19 | 0:f4216e9ad3a4 | 17 | //***** VARIABLEN ****** |
Reichi19 | 0:f4216e9ad3a4 | 18 | |
Reichi19 | 0:f4216e9ad3a4 | 19 | enum State {ST_AUS=0, ST_EIN}; |
Reichi19 | 0:f4216e9ad3a4 | 20 | State state; |
Reichi19 | 0:f4216e9ad3a4 | 21 | |
Reichi19 | 0:f4216e9ad3a4 | 22 | bool pressed = false; |
Reichi19 | 0:f4216e9ad3a4 | 23 | |
Reichi19 | 0:f4216e9ad3a4 | 24 | |
Reichi19 | 0:f4216e9ad3a4 | 25 | //***** EREIGNISSE ****** |
Reichi19 | 0:f4216e9ad3a4 | 26 | |
Reichi19 | 0:f4216e9ad3a4 | 27 | void rise(void) |
Reichi19 | 0:f4216e9ad3a4 | 28 | { |
Reichi19 | 0:f4216e9ad3a4 | 29 | wait_ms(50); |
Reichi19 | 0:f4216e9ad3a4 | 30 | pressed = true; |
Reichi19 | 0:f4216e9ad3a4 | 31 | } |
Reichi19 | 0:f4216e9ad3a4 | 32 | |
Reichi19 | 0:f4216e9ad3a4 | 33 | bool CheckFlag() |
Reichi19 | 0:f4216e9ad3a4 | 34 | { |
Reichi19 | 0:f4216e9ad3a4 | 35 | if (pressed) { |
Reichi19 | 0:f4216e9ad3a4 | 36 | pressed=false; |
Reichi19 | 0:f4216e9ad3a4 | 37 | return true; |
Reichi19 | 0:f4216e9ad3a4 | 38 | } |
Reichi19 | 0:f4216e9ad3a4 | 39 | return false; |
Reichi19 | 0:f4216e9ad3a4 | 40 | } |
Reichi19 | 0:f4216e9ad3a4 | 41 | |
Reichi19 | 0:f4216e9ad3a4 | 42 | //***** STATES ****** |
Reichi19 | 0:f4216e9ad3a4 | 43 | |
Reichi19 | 0:f4216e9ad3a4 | 44 | void ST_Aus (void) |
Reichi19 | 0:f4216e9ad3a4 | 45 | { |
Reichi19 | 0:f4216e9ad3a4 | 46 | //Status auf LCD |
Reichi19 | 0:f4216e9ad3a4 | 47 | lcd.cls(); // löscht lcd (clear screen) |
Reichi19 | 0:f4216e9ad3a4 | 48 | lcd.locate(0,0); // x-position, y-position (x: 0-128; y: 0-32) |
Reichi19 | 0:f4216e9ad3a4 | 49 | lcd.printf("State: 1 (Aus)"); |
Reichi19 | 0:f4216e9ad3a4 | 50 | // entry |
Reichi19 | 0:f4216e9ad3a4 | 51 | |
Reichi19 | 0:f4216e9ad3a4 | 52 | // do |
Reichi19 | 0:f4216e9ad3a4 | 53 | while(true) { |
Reichi19 | 0:f4216e9ad3a4 | 54 | Led1 = 0; |
Reichi19 | 0:f4216e9ad3a4 | 55 | |
Reichi19 | 0:f4216e9ad3a4 | 56 | if(CheckFlag()) { |
Reichi19 | 0:f4216e9ad3a4 | 57 | state = ST_EIN; |
Reichi19 | 0:f4216e9ad3a4 | 58 | |
Reichi19 | 0:f4216e9ad3a4 | 59 | // exit |
Reichi19 | 0:f4216e9ad3a4 | 60 | return; |
Reichi19 | 0:f4216e9ad3a4 | 61 | } |
Reichi19 | 0:f4216e9ad3a4 | 62 | } |
Reichi19 | 0:f4216e9ad3a4 | 63 | } |
Reichi19 | 0:f4216e9ad3a4 | 64 | |
Reichi19 | 0:f4216e9ad3a4 | 65 | void ST_Ein (void) |
Reichi19 | 0:f4216e9ad3a4 | 66 | { |
Reichi19 | 0:f4216e9ad3a4 | 67 | //Status auf LCD |
Reichi19 | 0:f4216e9ad3a4 | 68 | lcd.cls(); // löscht lcd (clear screen) |
Reichi19 | 0:f4216e9ad3a4 | 69 | lcd.locate(0,0); // x-position, y-position (x: 0-128; y: 0-32) |
Reichi19 | 0:f4216e9ad3a4 | 70 | lcd.printf("State: 2 (Ein)"); |
Reichi19 | 0:f4216e9ad3a4 | 71 | |
Reichi19 | 0:f4216e9ad3a4 | 72 | // entry |
Reichi19 | 0:f4216e9ad3a4 | 73 | Led4 = 1; |
Reichi19 | 0:f4216e9ad3a4 | 74 | wait_ms(200); |
Reichi19 | 0:f4216e9ad3a4 | 75 | Led4 = 0; |
Reichi19 | 0:f4216e9ad3a4 | 76 | wait_ms(200); |
Reichi19 | 0:f4216e9ad3a4 | 77 | Led4 = 1; |
Reichi19 | 0:f4216e9ad3a4 | 78 | wait_ms(200); |
Reichi19 | 0:f4216e9ad3a4 | 79 | Led4 = 0; |
Reichi19 | 0:f4216e9ad3a4 | 80 | wait_ms(200); |
Reichi19 | 0:f4216e9ad3a4 | 81 | // do |
Reichi19 | 0:f4216e9ad3a4 | 82 | while(true) { |
Reichi19 | 0:f4216e9ad3a4 | 83 | Led1 = 1; |
Reichi19 | 0:f4216e9ad3a4 | 84 | |
Reichi19 | 0:f4216e9ad3a4 | 85 | if(CheckFlag()) { |
Reichi19 | 0:f4216e9ad3a4 | 86 | state = ST_AUS; |
Reichi19 | 0:f4216e9ad3a4 | 87 | |
Reichi19 | 0:f4216e9ad3a4 | 88 | // exit |
Reichi19 | 0:f4216e9ad3a4 | 89 | return; |
Reichi19 | 0:f4216e9ad3a4 | 90 | } |
Reichi19 | 0:f4216e9ad3a4 | 91 | } |
Reichi19 | 0:f4216e9ad3a4 | 92 | |
Reichi19 | 0:f4216e9ad3a4 | 93 | } |
Reichi19 | 0:f4216e9ad3a4 | 94 | |
Reichi19 | 0:f4216e9ad3a4 | 95 | void ST_Error (void) |
Reichi19 | 0:f4216e9ad3a4 | 96 | { |
Reichi19 | 0:f4216e9ad3a4 | 97 | //Status auf LCD |
Reichi19 | 0:f4216e9ad3a4 | 98 | lcd.cls(); // löscht lcd (clear screen) |
Reichi19 | 0:f4216e9ad3a4 | 99 | lcd.locate(0,0); // x-position, y-position (x: 0-128; y: 0-32) |
Reichi19 | 0:f4216e9ad3a4 | 100 | lcd.printf("State: ERROR"); |
Reichi19 | 0:f4216e9ad3a4 | 101 | return; |
Reichi19 | 0:f4216e9ad3a4 | 102 | } |
Reichi19 | 0:f4216e9ad3a4 | 103 | |
Reichi19 | 0:f4216e9ad3a4 | 104 | void stateMachine() |
Reichi19 | 0:f4216e9ad3a4 | 105 | { |
Reichi19 | 0:f4216e9ad3a4 | 106 | switch (state) |
Reichi19 | 0:f4216e9ad3a4 | 107 | { |
Reichi19 | 0:f4216e9ad3a4 | 108 | case ST_AUS: ST_Aus(); |
Reichi19 | 0:f4216e9ad3a4 | 109 | break; |
Reichi19 | 0:f4216e9ad3a4 | 110 | case ST_EIN: ST_Ein(); |
Reichi19 | 0:f4216e9ad3a4 | 111 | break; |
Reichi19 | 0:f4216e9ad3a4 | 112 | default: ST_Error(); // sollte nicht auftreten :-) |
Reichi19 | 0:f4216e9ad3a4 | 113 | break; |
Reichi19 | 0:f4216e9ad3a4 | 114 | } |
Reichi19 | 0:f4216e9ad3a4 | 115 | } |
Reichi19 | 0:f4216e9ad3a4 | 116 | |
Reichi19 | 0:f4216e9ad3a4 | 117 | |
Reichi19 | 0:f4216e9ad3a4 | 118 | |
Reichi19 | 0:f4216e9ad3a4 | 119 | int main() |
Reichi19 | 0:f4216e9ad3a4 | 120 | { |
Reichi19 | 0:f4216e9ad3a4 | 121 | SW1.rise(&rise); //.fall(&fall); |
Reichi19 | 0:f4216e9ad3a4 | 122 | state = ST_AUS; |
Reichi19 | 0:f4216e9ad3a4 | 123 | while(true){ |
Reichi19 | 0:f4216e9ad3a4 | 124 | stateMachine(); |
Reichi19 | 0:f4216e9ad3a4 | 125 | } |
Reichi19 | 0:f4216e9ad3a4 | 126 | |
Reichi19 | 0:f4216e9ad3a4 | 127 | |
Reichi19 | 0:f4216e9ad3a4 | 128 | } |