State Maschine Übungen von fPucher https://os.mbed.com/users/fpucher/code/HIM0Board/wiki/STM-Schalter

Dependencies:   mbed C12832

Revision:
2:7dafc3e72a72
Parent:
1:fdfc2454217b
Child:
3:b5c28432ee25
--- a/main.cpp	Thu Nov 08 18:01:57 2018 +0000
+++ b/main.cpp	Thu Nov 15 16:30:33 2018 +0000
@@ -11,6 +11,8 @@
 InterruptIn  SW4(p16); 
 
 Ticker T1;
+Timer T2;
+volatile float timerValue = 0;
 volatile int blinkCount = 0; 
 
 //************** LCD ****************
@@ -19,10 +21,10 @@
 
 //************* VARIABLEN ****************
 
-enum State {ST_AUS=0, ST_EIN};
+enum State {ST_AUS=0, ST_EIN, ST_TIMEOUT};
 State state;
 
-bool pressed = false;
+bool pressed1 = false, pressed2 = false;
 
 //************* andere Funktionen ****************
 
@@ -36,16 +38,35 @@
 
 //************* EREIGNISSE ****************
  
-void rise(void)
+void rise1(void)
 {  
-    wait_ms(50);
-    pressed = true;
+    wait_ms(100); //Entprellung
+    pressed1 = true;
+    
+    timerValue = T2.read();
+    T2.reset();
+    T2.start();
 }
 
-bool CheckFlag() 
+bool CheckFlag1() 
 {
-    if (pressed) {
-        pressed=false;
+    if (pressed1) {
+        pressed1=false;
+        return true;
+        }
+    return false;
+}
+
+void rise2(void)
+{  
+    wait_ms(100);
+    if(state == ST_EIN) pressed2 = true;
+}
+
+bool CheckFlag2() 
+{
+    if (pressed2) {
+        pressed2=false;
         return true;
         }
     return false;
@@ -66,7 +87,7 @@
     while(true) {
         Led1 = 0;
     
-        if(CheckFlag()) {
+        if(CheckFlag1()) {
             state = ST_EIN;
             
     // exit
@@ -91,19 +112,47 @@
     }
     T1.detach();
     blinkCount = 0;
+    
     // do
     while(true) {
         Led1 = 1;
-    
-        if(CheckFlag()) {
+        
+    if(CheckFlag1()) {
             state = ST_AUS;
+    // exit
+            return;
+            }
             
+    if(CheckFlag2()) {
+            state = ST_TIMEOUT;
     // exit
             return;
             }
         }
 }
 
+void ST_TimeOut (void)
+{
+    //Status auf LCD und Serial
+    lcd.cls();      // löscht lcd (clear screen)
+    lcd.locate(0,0);   // x-position, y-position (x: 0-128; y: 0-32)
+    lcd.printf("State: 3 (TimeOut)");
+    printf("State: 3 (TimeOut)");
+    
+    // entry
+    
+    
+    // do
+    while(true) {
+        lcd.locate(0,12);   // x-position, y-position (x: 0-128; y: 0-32)
+        lcd.printf("Time: %f", timerValue);
+        printf("Time: %f", timerValue);
+           
+    // exit
+            
+            }
+}
+
 void ST_Error (void)
 {
     //Status auf LCD und Serial
@@ -114,6 +163,8 @@
     return;
 }
 
+//************* STATE MACHINE ****************
+
 void stateMachine()
 {
     switch (state)
@@ -122,16 +173,19 @@
         break;
     case ST_EIN: ST_Ein();
         break;
+    case ST_TIMEOUT: ST_TimeOut();
+        break;
     default: ST_Error();  // sollte nicht auftreten :-)
         break;
     }
 }
 
-
+//************* MAIN ****************
  
 int main()
 {
-    SW1.rise(&rise);      //.fall(&fall);
+    SW1.rise(&rise1);      //.fall(&fall);
+    SW2.rise(&rise2);
     state = ST_AUS;
     while(true){
         stateMachine();