Fahrradleuchte

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "BtnEventM0.h"
00003 
00004 //        LSB                                           MSB
00005 BusOut lb(P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1);
00006 
00007 // Statusled zeigt uns in welchem Zustand due Statemachine gerade ist
00008 BusOut stLED(P1_13,P1_12);
00009 
00010 // BtnEventM0 erledigt für uns die Abfrage der positiven Flanke
00011 BtnEventM0 sw4(P1_16), sw3(P0_23);
00012 // sw4==forew sw3==backward
00013 
00014 
00015 class FahrradLeuchte {
00016     public:
00017         void init()
00018         {
00019             state=1; t1.start();    
00020         }
00021         void State1Func();
00022         void State2Func();
00023         void State3Func();
00024     public:
00025         void State1Action(); // Bit0 (LED) mit 10 Hz blinken
00026         void State2Action(); // Bit2 (LED) mit 5 Hz blinken
00027         void State3Action(); // Bit4 (LED) mit 2 Hz blinken
00028     public:
00029     // State sagt uns in welchem Zustand sich die Fahrradleuchte gerade befindet
00030     int state;
00031     Timer t1;
00032 };
00033 
00034 // eine Fahrradleuchte anlegen
00035 FahrradLeuchte fl;
00036 
00037 int main(void)
00038 {
00039     sw3.Init(); sw4.Init();
00040     lb = 0;
00041     while(1)
00042     {
00043         if (fl.state==1)
00044             fl.State1Func();
00045         if (fl.state==2)
00046             fl.State2Func();
00047         if (fl.state==3)
00048             fl.State3Func();
00049     }
00050 }
00051 
00052 void FahrradLeuchte::State1Func()
00053 {
00054     
00055     // Einmalige Aktion beim Eintritt in die Zustandsfunktion
00056     stLED = 1; // Anzeigen, dass ir im Zustand 1 sind
00057     t1.reset();
00058     while(1)
00059     {
00060         State1Action();
00061         // Btn's abfragen und möglicherweise Zustand ändern
00062         if(sw4.CheckFlag())
00063         {state= 2; return;}
00064         if(sw3.CheckFlag())
00065         {state= 3; return;}
00066     }
00067     
00068 }
00069 
00070 void FahrradLeuchte:: State1Action()
00071 {
00072     if (t1.read_ms() > 100 )
00073     {
00074         t1.reset();
00075         if (lb == 0)
00076            lb = 1; // Mit Bit0 blinken
00077         else
00078             lb = 0;
00079     }
00080 }
00081 
00082 void FahrradLeuchte::State2Func()
00083 {
00084     
00085 }
00086 
00087 void FahrradLeuchte::State3Func()
00088 {
00089     
00090 }