M0 Board

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 BusOut lb(P1_7,P1_6,P1_4,P1_3,P1_1,P1_0,LED4,LED3,LED2,LED1);
00005 
00006 BusOut stLED(P1_13,P1_12);
00007 
00008 BtnEventM0 sw4(P1_16), sw3(P0_23);
00009 
00010 class FahrradLeuchte
00011 {
00012     public:
00013         void Init()
00014         {
00015             state = 1; t1.start();   
00016         }
00017     public:
00018         void State1Func();
00019         void State2Func();
00020         void State3Func();
00021     public:  
00022         void State1Action(); // Bit0 ( LED ) mit 10 Hz blinken
00023         void State2Action(); // Bit2 ( LED ) mit 5 Hz blinken
00024         void State3Action(); // Bit4 ( LED ) mit 2 Hz blinken
00025     public:
00026         // State sagt uns in welchem Zustand sich die FahrradLeuchte gerade befindet
00027         int state;
00028         Timer t1;
00029 };
00030 
00031 // Eine FahrradLeuchte anlegen
00032 FahrradLeuchte fl;
00033 
00034 int main(void)
00035 {
00036     sw3.Init(); sw4.Init();
00037     fl.Init();
00038     lb = 0;
00039     while(1)
00040     {
00041         if (fl.state == 1)
00042             fl.State1Func();
00043         if (fl.state == 2)
00044             fl.State2Func();
00045         if (fl.state == 3)
00046             fl.State3Func();
00047     }
00048 }
00049 
00050 void FahrradLeuchte::State1Func()
00051 {
00052     // einmalige Aktion beim Eintritt in die Zustandsfunktion
00053     stLED = 1;  // Anzeigen, dass im Zustand 1 sind
00054     t1.reset();
00055     while(1)
00056     {
00057         // Btn's abfragen und möglicherweise Zustand ändern
00058         State1Action();
00059         if (sw4.CheckFlag())
00060         {
00061             state = 2; return;
00062         }
00063         if (sw3.CheckFlag())
00064         {
00065             state = 3; return;
00066         }
00067     }
00068 }
00069 
00070 
00071 void FahrradLeuchte::State1Action()
00072 {
00073    if (t1.read_ms() > 100)
00074    {
00075     t1.reset();
00076     if (lb == 0)
00077         lb = 1; // Mit Bit0 blinken
00078     else
00079         lb = 0;
00080     }
00081 }
00082 
00083 void FahrradLeuchte::State2Func()
00084 {
00085     
00086     // einmalige Aktion beim Eintritt in die Zustandsfunktion
00087     stLED = 2;  // Anzeigen, dass im Zustand 1 sind
00088     t1.reset();
00089     while(1)
00090     {
00091         // Btn's abfragen und möglicherweise Zustand ändern
00092         State2Action();
00093         if (sw4.CheckFlag())
00094         {
00095             state = 3; return;
00096         }
00097         if (sw3.CheckFlag())
00098         {
00099             state = 1; return;
00100         }
00101     }
00102     
00103 }
00104 
00105 void FahrradLeuchte::State2Action()
00106 {
00107    if (t1.read_ms() > 50)
00108    {
00109     t1.reset();
00110     if (lb == 0)
00111         lb = 2; // Mit Bit0 blinken
00112     else
00113         lb = 0;
00114     }
00115 }
00116 
00117 void FahrradLeuchte::State3Func()
00118 {
00119     // einmalige Aktion beim Eintritt in die Zustandsfunktion
00120     stLED = 3;  // Anzeigen, dass im Zustand 1 sind
00121     t1.reset();
00122     while(1)
00123     {
00124         // Btn's abfragen und möglicherweise Zustand ändern
00125         State3Action();
00126         if (sw4.CheckFlag())
00127         {
00128             state = 1; return;
00129         }
00130         if (sw3.CheckFlag())
00131         {
00132             state = 2; return;
00133         }
00134     }
00135 }
00136 
00137 void FahrradLeuchte::State3Action()
00138 {
00139    if (t1.read_ms() > 20)
00140    {
00141     t1.reset();
00142     if (lb == 0)
00143         lb = 4; // Mit Bit0 blinken
00144     else
00145         lb = 0;
00146     }
00147 }