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

Dependencies:   mbed C12832

Files at this revision

API Documentation at this revision

Comitter:
kunphil
Date:
Thu Nov 29 17:16:35 2018 +0000
Parent:
2:7dafc3e72a72
Commit message:
Erster TINF-Test, Semester 7; ;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 7dafc3e72a72 -r b5c28432ee25 main.cpp
--- a/main.cpp	Thu Nov 15 16:30:33 2018 +0000
+++ b/main.cpp	Thu Nov 29 17:16:35 2018 +0000
@@ -1,19 +1,18 @@
 #include "mbed.h"
 #include "C12832.h"
-
+/*
 DigitalOut Led1(LED1, 0);
 DigitalOut Led2(LED2, 0);
 DigitalOut Led3(LED3, 0);
 DigitalOut Led4(LED4, 0);
+*/
 InterruptIn  SW1(p14);
 InterruptIn  SW2(p15); 
 InterruptIn  SW3(p12); 
 InterruptIn  SW4(p16); 
 
-Ticker T1;
-Timer T2;
-volatile float timerValue = 0;
-volatile int blinkCount = 0; 
+//************* BusOut ******************
+BusOut Leds (LED1, LED2, LED3, LED4);
 
 //************** LCD ****************
 
@@ -21,52 +20,26 @@
 
 //************* VARIABLEN ****************
 
-enum State {ST_AUS=0, ST_EIN, ST_TIMEOUT};
+enum State {ST_EIN=0, ST_AUS};
 State state;
 
-bool pressed1 = false, pressed2 = false;
-
-//************* andere Funktionen ****************
+int LedWert = 3, btnCount = 0;
 
-void blink()
-{
-    blinkCount++;
-    
-    Led4 = !Led4;
-}
+bool pressed = false;
 
 
 //************* EREIGNISSE ****************
  
-void rise1(void)
+void rise(void)
 {  
-    wait_ms(100); //Entprellung
-    pressed1 = true;
-    
-    timerValue = T2.read();
-    T2.reset();
-    T2.start();
+    wait_ms(50);
+    pressed = true;
 }
 
-bool CheckFlag1() 
+bool CheckFlag() 
 {
-    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;
+    if (pressed) {
+        pressed=false;
         return true;
         }
     return false;
@@ -74,20 +47,48 @@
 
 //************* STATES ****************
 
+void ST_Ein (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: 1 (EIN)");
+    printf("State: 1 (EIN)");
+    // entry
+    
+    // do
+    while(true) {
+        Leds = LedWert;
+    
+        if(CheckFlag()) {
+            btnCount++;
+            if(btnCount >= 3){
+                btnCount = 0;
+                LedWert++;
+                state = ST_AUS;
+            
+    // exit
+                return;
+            }
+        }
+    }
+}
+
 void ST_Aus (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: 1 (Aus)");
-    printf("State: 1 (Aus)");
+    lcd.printf("State: 2 (AUS)");
+    printf("State: 2 (AUS)");
+    
     // entry
     
     // do
     while(true) {
-        Led1 = 0;
+        Leds = 0;
     
-        if(CheckFlag1()) {
+        if(CheckFlag()) {
             state = ST_EIN;
             
     // exit
@@ -96,63 +97,6 @@
         }
 }
 
-void ST_Ein (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: 2 (Ein)");
-    printf("State: 2 (Ein)");
-    
-    // entry
-    T1.attach(&blink, 0.3);
-    while (true)
-    {
-      if(blinkCount >= 4) break;
-    }
-    T1.detach();
-    blinkCount = 0;
-    
-    // do
-    while(true) {
-        Led1 = 1;
-        
-    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
@@ -163,30 +107,29 @@
     return;
 }
 
-//************* STATE MACHINE ****************
-
 void stateMachine()
 {
     switch (state)
     {
-    case ST_AUS: ST_Aus();
-        break;
     case ST_EIN: ST_Ein();
         break;
-    case ST_TIMEOUT: ST_TimeOut();
+    case ST_AUS: ST_Aus();
         break;
     default: ST_Error();  // sollte nicht auftreten :-)
         break;
     }
 }
 
-//************* MAIN ****************
+
  
 int main()
 {
-    SW1.rise(&rise1);      //.fall(&fall);
-    SW2.rise(&rise2);
-    state = ST_AUS;
+    SW1.rise(&rise);      //.fall(&fall);
+    
+    Leds = 15;
+    wait_ms(500);
+    
+    state = ST_EIN;
     while(true){
         stateMachine(); 
         }