Oliver Ainhirn
/
85_Event_State
erweitert um RGB LED
Diff: main.cpp
- Revision:
- 0:f0345f20337e
- Child:
- 1:dc7c5869f9e1
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Apr 02 17:24:04 2019 +0000 @@ -0,0 +1,60 @@ +#include "mbed.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); + +DigitalOut red(p23); +DigitalOut green(p24); +DigitalOut blue(p25); + +class StopLight { +private: + EventQueue _queue; + int _pending; + + enum state { RED, GREEN, YELLOW }; + + // The _step function is a traditional FSM which enqueues transitions onto the event queue + void _step(state transition) { + switch (transition) { + case RED: + set_red(); + _pending = _queue.call_in(1000, this, &StopLight::_step, GREEN); + break; + + case GREEN: + set_green(); + _pending = _queue.call_in(1000, this, &StopLight::_step, YELLOW); + break; + + case YELLOW: + set_yellow(); + _pending = _queue.call_in(500, this, &StopLight::_step, RED); + break; + } + } + void set_red(){ + red = 0; green = 1; blue = 1; + } + void set_green(){ + red = 1; green = 0; blue = 1; + } + void set_yellow() { + red = 0; green = 0; blue = 1; + } +public: + // The fire function can immediately change states in the FSM + void fire() { + _queue.cancel(_pending); + _queue.call(this, &StopLight::_step, GREEN); + _queue.dispatch(); + } +}; + +StopLight sl; + +int main (void) { + sl.fire(); + wait(osWaitForever); +} \ No newline at end of file