Oliver Ainhirn
/
85_Event_State
erweitert um RGB LED
main.cpp
- Committer:
- corsa1600
- Date:
- 2019-04-02
- Revision:
- 0:f0345f20337e
- Child:
- 1:dc7c5869f9e1
File content as of revision 0:f0345f20337e:
#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); }