Main.cpp Fahrradleuchte

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
RudiNiki
Date:
Mon Jan 25 16:40:23 2016 +0000
Commit message:
Test Fahrradleuchte

Changed in this revision

FahrradLeuchte.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r fb288e5361c9 FahrradLeuchte.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FahrradLeuchte.h	Mon Jan 25 16:40:23 2016 +0000
@@ -0,0 +1,146 @@
+
+
+class BtnEventM0
+{
+public:
+    int16_t pressed;
+
+    BtnEventM0(PinName pin) : _isr(pin) {
+        pressed=0;
+    }
+
+    // Ist eine steigende Flanke aufgetreten ?
+    int CheckFlag() {
+        if( pressed ) {
+            pressed=0;
+            return 1;
+        }
+        return 0;
+    }
+
+    // 1..Button is pressed  else 0
+    int CheckButton() {
+        return _isr.read();
+    }
+
+    void Init() {
+        _isr.rise(this, &BtnEventM0::RisingISR);
+    }
+
+    void RisingISR() {
+        if( _isr.read() )
+            pressed = 1;
+    }
+protected:
+    InterruptIn _isr;
+};
+
+/*
+class BtnEventM02 : public BtnEventM0
+{
+public:
+    BtnEventM02(PinName pin) : BtnEventM0(pin) {
+        _tm.stop();
+        _tm.reset();
+        _state=1;
+    }
+
+    void Init() {
+        _isr.rise(this, &BtnEventM02::RisingISR);
+    }
+
+    void RisingISR() {
+        if( !_isr.read() )
+            return;
+        pressed = 1;
+        _tm.start();
+        _state = 2;
+    }
+
+    void CheckButton() {
+        if( _state==1 )
+            return;
+        if( _state==2 ) {
+            if( !_isr.read() ) {
+                _state = 1;
+                return;
+            }
+            if( _tm.read_ms()>500 ) {
+                _tm.reset();
+                _state = 3;
+                pressed = 1;
+            }
+        } else if( _state==3 ) {
+            if( !_isr.read() ) {
+                _state = 1;
+                return;
+            }
+            if( _tm.read_ms()>100 ) {
+                _tm.reset();
+                _state = 3;
+                pressed = 1;
+            }
+        }
+    }
+private:
+    int16_t _state;
+    Timer _tm;
+};
+*/
+
+class AnalogInHL : public AnalogIn
+{
+public:
+    AnalogInHL(PinName pin) : AnalogIn(pin) { }
+    int Read() {
+        return read_u16()>>6;
+    }
+};
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff -r 000000000000 -r fb288e5361c9 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Jan 25 16:40:23 2016 +0000
@@ -0,0 +1,150 @@
+
+#include "mbed.h"
+#include "FahrradLeuchte.h"
+
+//        LSB                                                      MSB
+BusOut lb(P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1);
+
+// Statusled zeigt uns in welchen 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
+
+class FahrradLeuchte
+{
+    public:
+        void Init()
+        {
+            state=1; t1.start();
+        }
+        void State1Func(); 
+        void State2Func();
+        void State3Func();  
+    public:     
+        void State1Action();
+        void State2Action();
+        void State3Action();
+    public:
+        int state;  // state sagt uns in welchen Zustand die Fahrradleuchte gerade ist
+        Timer t1;
+};
+
+FahrradLeuchte f1;
+
+int main(void)
+{
+    sw4.Init(); sw3.Init(); f1.Init();
+    while(1) 
+    {
+        if (f1.state==1)
+            f1.State1Func();
+        if (f1.state==2)
+            f1.State2Func();
+        if (f1.state==3)
+            f1.State3Func();   
+    }
+}
+
+void FahrradLeuchte::State1Func()
+{
+    //Einmalige Aktion in der der Zustandsfunktion
+    stLED = 1; // Anzeigen dass wir im Zustand 1 sind
+    t1.reset();   
+    while (1)
+    {
+        State1Action();
+        if (sw4.CheckFlag())
+        {    
+            state=2;
+            return;
+        }
+        if (sw3.CheckFlag())
+        {
+            state=3;
+            return;   
+        }    
+    }
+}
+
+void FahrradLeuchte::State1Action()
+{
+    // 2Hz blinken
+    if (t1.read_ms()>500)
+    {
+        t1.reset();
+        if (lb==0)
+            lb = 0xFFFF;
+        else
+            lb = 0;    
+    } 
+}
+
+void FahrradLeuchte::State2Func()
+{
+    stLED = 2; // Anzeigen dass wir im Zustand 1 sind
+    t1.reset();   
+    while (1)
+    {
+        State2Action();
+        if (sw4.CheckFlag())
+        {    
+            state=3;
+            return;
+        }
+        if (sw3.CheckFlag())
+        {
+            state=1;
+            return;   
+        }    
+    }
+}
+
+void FahrradLeuchte::State2Action()
+{
+    // Runlight Left 5Hz
+    // 2Hz blinken
+    if (t1.read_ms()>200)
+    {
+        t1.reset();
+        if (lb==0)
+            lb = 0xFFFF;
+        else
+            lb = 0;    
+    } 
+}
+
+void FahrradLeuchte::State3Func()
+{
+    stLED = 3; // Anzeigen dass wir im Zustand 1 sind
+    t1.reset();   
+    while (1)
+    {
+        State3Action();
+        if (sw4.CheckFlag())
+        {    
+            state=1;
+            return;
+        }
+        if (sw3.CheckFlag())
+        {
+            state=2;
+            return;   
+        }    
+    }
+}
+
+void FahrradLeuchte::State3Action()
+{
+   // Runlight Right 10Hz
+   // 2Hz blinken
+    if (t1.read_ms()>100)
+    {
+        t1.reset();
+        if (lb==0)
+            lb = 0xFFFF;
+        else
+            lb = 0;    
+    } 
+}
diff -r 000000000000 -r fb288e5361c9 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Jan 25 16:40:23 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6f327212ef96
\ No newline at end of file