Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 11:a07dfec403e5, committed 2018-11-15
- Comitter:
- martwerl
- Date:
- Thu Nov 15 17:59:38 2018 +0000
- Parent:
- 10:6495a89d66ed
- Commit message:
- Fahrradleuchte_Class
Changed in this revision
--- a/BtnEventM0.h Wed Feb 01 07:20:51 2017 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,174 +0,0 @@ - -// V4.0 - -class BtnEventM0 { - public: - int16_t pressed; - - BtnEventM0(PinName pin) : _isr(pin) - { pressed=0; } - - // Ist eine steigende Flanke aufgetreten ? - int CheckFlag(); - - // 1..Button is pressed else 0 - int CheckButton() - { return _isr.read(); } - - void Init(); - // { _isr.rise(this,&BtnEventM0::RisingISR); } - - void RisingISR(); - - protected: - InterruptIn _isr; -}; - -void BtnEventM0::Init() - { _isr.rise(this,&BtnEventM0::RisingISR); } - -void BtnEventM0::RisingISR() -{ - if( _isr.read() ) - pressed = 1; -} - -int BtnEventM0::CheckFlag() -{ - if( pressed ) - { pressed=0; return 1; } - return 0; -} - - - - - -/* -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; - } -}; - -class BtnEventM0S { - public: - BtnEventM0S(PinName pin) : _btn(pin) { } - - void Init() {} - - // Ist eine steigende Flanke aufgetreten ? - int CheckFlag() - { - if( _btn ) - { wait_ms(100); return 1; } - else - return 0; - } - - protected: - DigitalIn _btn; -}; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Fahrradleuchte.h Thu Nov 15 17:59:38 2018 +0000 @@ -0,0 +1,174 @@ + +// V4.0 + +class BtnEventM0 { + public: + int16_t pressed; + + BtnEventM0(PinName pin) : _isr(pin) + { pressed=0; } + + // Ist eine steigende Flanke aufgetreten ? + int CheckFlag(); + + // 1..Button is pressed else 0 + int CheckButton() + { return _isr.read(); } + + void Init(); + // { _isr.rise(this,&BtnEventM0::RisingISR); } + + void RisingISR(); + + protected: + InterruptIn _isr; +}; + +void BtnEventM0::Init() + { _isr.rise(this,&BtnEventM0::RisingISR); } + +void BtnEventM0::RisingISR() +{ + if( _isr.read() ) + pressed = 1; +} + +int BtnEventM0::CheckFlag() +{ + if( pressed ) + { pressed=0; return 1; } + return 0; +} + + + + + +/* +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; + } +}; + +class BtnEventM0S { + public: + BtnEventM0S(PinName pin) : _btn(pin) { } + + void Init() {} + + // Ist eine steigende Flanke aufgetreten ? + int CheckFlag() + { + if( _btn ) + { wait_ms(100); return 1; } + else + return 0; + } + + protected: + DigitalIn _btn; +}; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
--- a/main.cpp Wed Feb 01 07:20:51 2017 +0000 +++ b/main.cpp Thu Nov 15 17:59:38 2018 +0000 @@ -1,67 +1,130 @@ - #include "mbed.h" -#include "BtnEventM0.h" - -// V4.0 +#include "Fahrradleuchte.h" // 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 di eAbfrage der positiven Flanke BtnEventM0 sw4(P1_16), sw2(P0_15), sw1(P0_10), sw3(P0_23); +//sw4 = foreward, sw3 = backward -Timer t1; -void OneLeftStep(); -void OneRightStep(); -void ExecuteAutoButtons(); +class Fahrradleuchte +{ + public: + void Init(); + void State1Func();//Funktionen, die auch von außerhalb der Klasse aufgerufen werden können + void State2Func(); + void State3Func(); + void State1Action();//Aktionen, die auch von außen aufgerufen werden können Bit2 (LED) mit 10 Hz blinken + void State2Action();//Bit2(LED) mit 5 Hz blinken + void State3Action();//Bit6(LED) mit 2 Hz blinken + public: + int state;//zeigt an, in welchem Zustand sich die Leuchte befindet. + private://Kann nur in dieser Klasse aufgerufen werden + Timer t1; +};//HIER: Klasse mit Strichpunkt beenden + +Fahrradleuchte fl;//Objekt fl anlegen +//Fahrradleuchte fl2; zB... int main(void) { - lb = 1; - sw4.Init(); - sw2.Init(); - sw1.Init(); - sw3.Init(); + sw3.Init();//P0_23 backward + sw4.Init();//P1_16 foreward + fl.Init();//Objekt von Fahrradleuchte + //fl2.Init(); zB... + 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; + //fl.state=1; zB... + //fl2.state =14; zB... +} - 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, dass wir im "state 1" sind + while(1) + { + State1Action(); + //Buttons abfragen und möglicherweise Zustand ändern + if(sw4.CheckFlag()) + { + state=2; + return; + } + if(sw3.CheckFlag()) + { + state=3; + return; + } + } +} +void Fahrradleuchte::State2Func() +{ + //einmalige Aktion beim Eintritt in die Zustandsfunktion + stLED=1;//Anzeigen, dass wir im "state 1" sind + while(1) + { + State1Action(); + //Buttons abfragen und möglicherweise Zustand ändern + if(sw4.CheckFlag()) + { + state=2; + return; + } + if(sw3.CheckFlag()) + { + state=3; + return; + } + } +} +void Fahrradleuchte::State3Func() +{ + //einmalige Aktion beim Eintritt in die Zustandsfunktion + stLED=1;//Anzeigen, dass wir im "state 1" sind + while(1) + { + State1Action(); + //Buttons 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(); */ -} - -void OneLeftStep() -{ - if( lb==2048 ) { - lb = 1; - return; + if(t1.read_ms()>100) //10Hz + { + t1.reset(); + if(lb==0) + lb=4; + else + lb=0; } - lb = lb << 1; -} - -void OneRightStep() -{ - if( lb==1 ) { - lb = 2048; - return; - } - lb = lb >> 1; -} - - - - +} \ No newline at end of file