Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
trivla
Date:
Mon Mar 16 15:27:20 2015 +0000
Commit message:
ampel2

Changed in this revision

BtnEventM0.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 adf5179f29bb BtnEventM0.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/BtnEventM0.h	Mon Mar 16 15:27:20 2015 +0000
@@ -0,0 +1,96 @@
+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;
+    }
+};
\ No newline at end of file
diff -r 000000000000 -r adf5179f29bb main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Mar 16 15:27:20 2015 +0000
@@ -0,0 +1,211 @@
+
+#include "mbed.h"
+#include "BtnEventM0.h"
+
+BusOut lb(P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1);   //Ledbar definieren ohne P1_13 und P1_12
+BusOut stLED(P1_13,P1_12);                                      //P1_13 und P1_12 definieren
+
+Serial pc(USBTX, USBRX);                                        //ein serielles Objekt anlegen
+BtnEventM0 Btn1(P0_15), Btn2(P0_23);                            //2 Buttons unter einer Custom Library definieren
+
+//Custom Library: BtnEventM0.h
+
+class Ampel
+{
+public:
+    void Init() {
+        state=1;
+        t1.start();
+        t2.start();
+        t3.start();
+    }
+
+    void rot();
+    void gelb();
+    void gruen();
+    void gruen_bl();
+public:
+    void rotAction();
+    void gelbAction();
+    void gruenAction();
+    void gruen_blAction();
+public:
+    //state sagt uns in welchem Zustand sich die Ampel gerade befindet
+    int state;
+    Timer t1; 
+    Timer t2;
+    Timer t3;
+
+} ;
+
+
+Ampel amp;
+
+int main()                                      //Main-Funktion
+{
+    pc.baud(125000);                           // auf 125000 Bits/sec setzen
+    Btn1.Init();
+    amp.Init();                                 //Btn1 und Btn2 mit Init() ansprechen
+    while(1) {                                  //Die ganze Zeit durchlaufen
+        if( amp.state==1 )
+            amp.rot();
+        if( amp.state==2 )
+            amp.gelb();
+        if( amp.state==3 )
+            amp.gruen();
+        if( amp.state==4 )
+            amp.gruen_bl();
+
+        amp.state=1;
+    }
+
+}
+
+
+void Ampel::rot()
+{
+    pc.printf("ROT\n");
+    t3.reset();
+    while(1) {
+
+        if(t3.read_ms()>3200) { // 3 sec sind abgelaufen
+            state=2;
+            return;
+        }
+        if(Btn1.CheckFlag()) {
+            state=1;
+            return;
+        }
+        rotAction();
+    }
+
+
+}
+void Ampel::rotAction()
+{
+    if(t1.read_ms()>200) {   //Blinken
+        t1.reset();
+
+        if (lb==0)
+            lb=1;
+        else
+            lb=0;
+    }
+    if(t2.read_ms()>100) {
+        t2.reset();
+        pc.printf("2 %d \n" , t3.read_ms());
+
+    }
+}
+
+void Ampel::gelb()
+{
+    pc.printf("gelb\n");
+    t3.reset();
+    while(1) {
+
+        if(t3.read_ms()>4200) { // 3 sec sind abgelaufen
+            state=3;
+            return;
+        }
+        if(Btn1.CheckFlag()) {
+            state=4;
+            return;
+        }
+        gelbAction();
+    }
+
+}
+
+void Ampel::gelbAction()
+{
+    if(t1.read_ms()>200) {   //Blinken
+        t1.reset();
+
+        if (lb==0)
+            lb=2;
+        else
+            lb=0;
+    }
+    if(t2.read_ms()>100) {
+        t2.reset();
+        pc.printf("2 %d \n" , t3.read_ms());
+
+    }
+}
+
+void Ampel::gruen()
+{
+    pc.printf("gruen \n");
+    t3.reset();
+    while(1) {
+
+        if(t3.read_ms()>5200) { // 3 sec sind abgelaufen
+            state=3;
+            return;
+        }
+        if(Btn1.CheckFlag()) {
+            state=4;
+            return;
+        }
+        gruenAction();
+
+
+
+    }
+
+}
+
+void Ampel::gruenAction()
+{
+    if(t1.read_ms()>200) {   //Blinken
+        t1.reset();
+
+        if (lb==0)
+            lb=4;
+        else
+            lb=0;
+    }
+    if(t2.read_ms()>100) {
+        t2.reset();
+        pc.printf("2 %d \n" , t3.read_ms());
+
+    }
+
+}
+
+void Ampel::gruen_bl()
+{
+    pc.printf("gruen BLinken \n");
+    t3.reset();
+    while(1) {
+
+        if(t3.read_ms()>5200) { // 3 sec sind abgelaufen
+            state=1;
+            return;
+        }
+
+        gruen_blAction();
+
+
+
+    }
+
+}
+void Ampel::gruen_blAction()
+{
+    if(t1.read_ms()>200) {   //Blinken
+        t1.reset();
+
+        if (lb==0)
+            lb=3;
+        else
+            lb=0;
+    }
+    if(t2.read_ms()>100) {
+        t2.reset();
+        pc.printf("2 %d \n" , t3.read_ms());
+
+    }
+
+}
diff -r 000000000000 -r adf5179f29bb mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Mar 16 15:27:20 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9ad691361fac
\ No newline at end of file