Projekt TINF 7ABELI

Dependencies:   mbed C12832

Revision:
1:125e4981a54c
Parent:
0:603870fc1af3
Child:
2:8abff6365ac4
--- a/mAIn_1.cpp	Thu Nov 29 17:15:55 2018 +0000
+++ b/mAIn_1.cpp	Thu Nov 29 18:40:07 2018 +0000
@@ -1,221 +1,150 @@
 #include "mbed.h"
 #include "C12832.h"
 
-BusOut doLeds(LED1,LED2,LED3,LED4);
-InterruptIn  SW1(p14);
-InterruptIn  SW2(p15); 
-InterruptIn  SW3(p12); 
-InterruptIn  SW4(p16); 
+InterruptIn  btn(p14);
+
+Timer T1;
 
-Ticker T1;
-Timer T2;
-volatile float timerValue = 0;
-volatile int blinkCount = 0; 
+BusOut Leds (LED1, LED2, LED3, LED4);
 
-//************** LCD ****************
-
-C12832 lcd(p5, p7, p6, p8, p11);
+C12832 lcd(p5, p7, p6, p8, p11); 
 
-//************* VARIABLEN ****************
-
-enum State {ST_AUS=0, ST_EIN, ST_TIMEOUT};
+enum State {ST_EIN=0, ST_AUS, ST_ENDE};
 State state;
 
-bool pressed1 = false, pressed2 = false;
+int LedWert = 3, btnCount = 0;
 
-//************* andere Funktionen ****************
+bool pressed = false;
 
-void blink()
-{
-    blinkCount++;
-    
-    Led4 = !Led4;
+void rise(void)
+{  
+    wait_ms(50);
+    pressed = true;
 }
 
-
-//************* EREIGNISSE ****************
- 
-void rise1(void)
-{  
-    wait_ms(100); //Entprellung
-    pressed1 = true;
-    
-    timerValue = T2.read();
-    T2.reset();
-    T2.start();
-}
-
-bool CheckFlag1() 
+bool CheckFlag() 
 {
-    if (pressed1) {
-        pressed1=false;
+    if (pressed) {
+        pressed=false;
         return true;
         }
     return false;
 }
 
-void rise2(void)
-{  
-    wait_ms(100);
-    if(state == ST_EIN) pressed2 = true;
-}
-
-bool CheckFlag2() 
+void ST_Ein (void)
 {
-    if (pressed2) {
-        pressed2=false;
-        return true;
-        }
-    return false;
-}
-
-//************* STATES ****************
-
-void ST_Aus (void)
-{
-    //Status auf LCD und Serial
-    lcd.cls();      // löscht lcd (clear screen)
+    //Status auf LCD und Serielle
+    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: 1 (EIN)");
+    printf("State: 1 (EIN)");
     // entry
     
     // do
     while(true) {
-        Led1 = 0;
+        Leds = LedWert;
+    
+        if(CheckFlag()) {
+            btnCount++;
+            if(btnCount >= 3){
+                btnCount = 0;
+                LedWert++;
+                state = ST_AUS;
+            
+    // exit
+                return;
+            }
+        }
+    }
+}
+
+void ST_Aus (void)
+{
+    //Status auf LCD und Serielle
+    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 (AUS)");
+    printf("State: 2 (AUS)");
     
-        if(CheckFlag1()) {
+    // entry
+    
+    // do
+    while(true) {
+        Leds = 0;
+    
+        if(CheckFlag()) {
+            T1.reset();
+            T1.start();
+             while(1){
+                 
+                 if(T1.read() >= 0.2) {
+                        
+                        break;                        
+                     }
+                 
+                 else if(CheckFlag()){
+                     T1.stop();
+                     state = ST_ENDE;
+                     return;
+                     }
+             }
+             
             state = ST_EIN;
             
     // exit
+            T1.stop();
             return;
             }
         }
 }
 
-void ST_Ein (void)
+void ST_Ende (void)
 {
-    //Status auf LCD und Serial
-    lcd.cls();      // löscht lcd (clear screen)
+    //Status auf LCD und Serielle
+    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
-            
-            }
+    lcd.printf("State: Ende");
+    printf("State: Ende");
+    wait_ms(300);
+    return;
 }
 
 void ST_Error (void)
 {
-    //Status auf LCD und Serial
-    lcd.cls();      // löscht lcd (clear screen)
+    //Status auf LCD und Serielle
+    lcd.cls();      // löscht LCD (clear screen)
     lcd.locate(0,0);   // x-position, y-position (x: 0-128; y: 0-32)
-    lcd.printf("State: ERROR!!!");
-    printf("State: ERROR!!!");
+    lcd.printf("State: ERROR");
+    printf("State: ERROR");
     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 :-)
+    case ST_ENDE: ST_Ende();
+        break;
+    default: ST_Error();  
         break;
     }
 }
 
-//************* MAIN ****************
+
  
 int main()
 {
-    SW1.rise(&rise1);      //.fall(&fall);
-    SW2.rise(&rise2);
-    state = ST_AUS;
+    btn.rise(&rise);      
+    
+    Leds = 15;
+    wait_ms(500);
+    
+    state = ST_EIN;
     while(true){
         stateMachine(); 
         }
-}
-
-class IsAnEvent : public InterruptIn {
-        volatile int16_t _pressed;
-        void _RisingISR(); 
-    public:
-        IsAnEvent() : InterruptIn(p15) {};
-        IsAnEvent(PinName pin) : InterruptIn(pin) {
-             rise(callback(this, &IsAnEvent::_RisingISR)); 
-            _pressed=0; 
-        };
-        int CheckFlag();
-        void InitIsr();
-};
-
-void IsAnEvent::InitIsr() { 
-    rise(callback(this, &IsAnEvent::_RisingISR)); 
-}
-
-void IsAnEvent::_RisingISR() {
-    wait_ms(50);
-    if( read() )
-        _pressed = true;
-}
-int IsAnEvent::CheckFlag() {
-    if( _pressed ) { 
-        _pressed = false; 
-        return 1; 
-    }
-    return 0;
-}
-            
\ No newline at end of file
+}
\ No newline at end of file