Christian Weiß / Mbed 2 deprecated Fahrradleuchte2

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Wizo
Date:
Thu Nov 15 17:57:32 2018 +0000
Parent:
10:6495a89d66ed
Commit message:
Fahrradleuchte2

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 6495a89d66ed -r be1ba47f4156 main.cpp
--- a/main.cpp	Wed Feb 01 07:20:51 2017 +0000
+++ b/main.cpp	Thu Nov 15 17:57:32 2018 +0000
@@ -1,67 +1,120 @@
-
 #include "mbed.h"
 #include "BtnEventM0.h"
 
-// V4.0
+//        LSB                                                      MSB
+BusOut lb(P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1);
 
-//        LSB                                                      MSB
-BusOut lb(P1_13,P1_12,P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1);
+// Statusled zeigt uns in welchem Zustand die Statemachine gerade ist
+BusOut stLED(P1_13,P1_12);
+
+// BtnEventM0 erledigt für uns die Abfrage der positiven Flanke
+BtnEventM0 sw4(P1_16), sw3(P0_23);
+// sw4==forward sw3==backward
+
 
-BtnEventM0 sw4(P1_16), sw2(P0_15), sw1(P0_10), sw3(P0_23);
+class Fahrradleuchte
+{
+    public:
+        void Init();
+    // Funktionen, die auch außerhalb einer Klasse aufgerufen werden können
+        void State1Func();
+        void State2Func();
+        void State3Func();
+    public:
+    // Aktionen, die auch von außen aufgerufen werden können
+        void State1Action(); // Bit2 (LED) mit 10Hz blinken
+        void State2Action(); // Bit4 (LED) mit 5Hz blinken
+        void State3Action(); // Bit6 (LED) mit 2Hz blinken
+    public:
+        int state; // state sagt uns in welchem Zustand sich die Fahrradleuchte gerade befindet
+    private:
+        Timer t1; // Kann nur in dieser Klasse aufgerufen werden
+};
 
-Timer t1;
-void OneLeftStep();
-void OneRightStep();
-void ExecuteAutoButtons();
+Fahrradleuchte fl; // eine Fahrradleuchte anlegen
 
 int main(void)
 {
-    lb = 1;
-    sw4.Init();
-    sw2.Init();
-    sw1.Init();
-    sw3.Init();
-    t1.start();
+    sw4.Init(); sw3.Init();
+    fl.Init();
+    while(1)
+    {
+        if(fl.state==1)
+            fl.State1Func();
+        if(fl.state==2)
+            fl.State2Func();
+        if(fl.state==3)
+            fl.State3Func();
+    }
+}
+
+void Fahrradleuchte::Init()
+{
+    t1.start(); state = 1;
+}
 
-    while(1) {
-        if( sw4.CheckFlag() )
-            OneRightStep();
-        if( sw3.CheckFlag() )
-            OneLeftStep();
-        if( sw1.CheckFlag() )
-            OneRightStep();
-        if( sw2.CheckFlag() )
-            OneLeftStep();
-        ExecuteAutoButtons();
+void Fahrradleuchte::State1Func()
+{
+    // Einmalige Aktion beim Eintritt in die Zustandsfunktion
+    stLED = 1; // Anzeigen das wir im State1 sind
+    
+    while(1)
+    {
+        State1Action();
+            // Btn's abfragen und möglicherweise Zustand ändern
+            if(sw4.CheckFlag())
+                {
+                    state = 2; return;
+                }
+            if(sw3.CheckFlag())
+                {
+                    state = 3; return;
+                }
     }
 }
 
-void ExecuteAutoButtons()
+void Fahrradleuchte::State1Action()
 {
-    /* sw1.CheckButton();
-    sw2.CheckButton();
-    sw3.CheckButton();
-    sw4.CheckButton(); */
+    if(t1.read_ms()>100) //10Hz
+        {
+            t1.reset();   
+                if(lb==0)
+                    lb = 2;
+                else
+                    lb = 0;
+        }
+        
 }
 
-void OneLeftStep()
+void Fahrradleuchte::State2Action()
 {
-    if( lb==2048 ) {
-        lb = 1;
-        return;
-    }
-    lb = lb << 1;
+    if(t1.read_ms()>200) //5Hz
+        {
+            t1.reset();   
+                if(lb==0)
+                    lb = 4;
+                else
+                    lb = 0;
+        }
+        
 }
 
-void OneRightStep()
+void Fahrradleuchte::State3Action()
 {
-    if( lb==1 ) {
-        lb = 2048;
-        return;
-    }
-    lb = lb >> 1;
+    if(t1.read_ms()>500) //2Hz
+        {
+            t1.reset();   
+                if(lb==0)
+                    lb = 8;
+                else
+                    lb = 0;
+        }
+        
 }
 
 
 
 
+
+
+