![](/media/cache/profiles/RDKH0109.JPG.50x50_q85.jpg)
erweitert um RGB LED
main.cpp@0:f0345f20337e, 2019-04-02 (annotated)
- Committer:
- corsa1600
- Date:
- Tue Apr 02 17:24:04 2019 +0000
- Revision:
- 0:f0345f20337e
- Child:
- 1:dc7c5869f9e1
mit RGB LED
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
corsa1600 | 0:f0345f20337e | 1 | #include "mbed.h" |
corsa1600 | 0:f0345f20337e | 2 | |
corsa1600 | 0:f0345f20337e | 3 | DigitalOut led1(LED1); |
corsa1600 | 0:f0345f20337e | 4 | DigitalOut led2(LED2); |
corsa1600 | 0:f0345f20337e | 5 | DigitalOut led3(LED3); |
corsa1600 | 0:f0345f20337e | 6 | |
corsa1600 | 0:f0345f20337e | 7 | DigitalOut red(p23); |
corsa1600 | 0:f0345f20337e | 8 | DigitalOut green(p24); |
corsa1600 | 0:f0345f20337e | 9 | DigitalOut blue(p25); |
corsa1600 | 0:f0345f20337e | 10 | |
corsa1600 | 0:f0345f20337e | 11 | class StopLight { |
corsa1600 | 0:f0345f20337e | 12 | private: |
corsa1600 | 0:f0345f20337e | 13 | EventQueue _queue; |
corsa1600 | 0:f0345f20337e | 14 | int _pending; |
corsa1600 | 0:f0345f20337e | 15 | |
corsa1600 | 0:f0345f20337e | 16 | enum state { RED, GREEN, YELLOW }; |
corsa1600 | 0:f0345f20337e | 17 | |
corsa1600 | 0:f0345f20337e | 18 | // The _step function is a traditional FSM which enqueues transitions onto the event queue |
corsa1600 | 0:f0345f20337e | 19 | void _step(state transition) { |
corsa1600 | 0:f0345f20337e | 20 | switch (transition) { |
corsa1600 | 0:f0345f20337e | 21 | case RED: |
corsa1600 | 0:f0345f20337e | 22 | set_red(); |
corsa1600 | 0:f0345f20337e | 23 | _pending = _queue.call_in(1000, this, &StopLight::_step, GREEN); |
corsa1600 | 0:f0345f20337e | 24 | break; |
corsa1600 | 0:f0345f20337e | 25 | |
corsa1600 | 0:f0345f20337e | 26 | case GREEN: |
corsa1600 | 0:f0345f20337e | 27 | set_green(); |
corsa1600 | 0:f0345f20337e | 28 | _pending = _queue.call_in(1000, this, &StopLight::_step, YELLOW); |
corsa1600 | 0:f0345f20337e | 29 | break; |
corsa1600 | 0:f0345f20337e | 30 | |
corsa1600 | 0:f0345f20337e | 31 | case YELLOW: |
corsa1600 | 0:f0345f20337e | 32 | set_yellow(); |
corsa1600 | 0:f0345f20337e | 33 | _pending = _queue.call_in(500, this, &StopLight::_step, RED); |
corsa1600 | 0:f0345f20337e | 34 | break; |
corsa1600 | 0:f0345f20337e | 35 | } |
corsa1600 | 0:f0345f20337e | 36 | } |
corsa1600 | 0:f0345f20337e | 37 | void set_red(){ |
corsa1600 | 0:f0345f20337e | 38 | red = 0; green = 1; blue = 1; |
corsa1600 | 0:f0345f20337e | 39 | } |
corsa1600 | 0:f0345f20337e | 40 | void set_green(){ |
corsa1600 | 0:f0345f20337e | 41 | red = 1; green = 0; blue = 1; |
corsa1600 | 0:f0345f20337e | 42 | } |
corsa1600 | 0:f0345f20337e | 43 | void set_yellow() { |
corsa1600 | 0:f0345f20337e | 44 | red = 0; green = 0; blue = 1; |
corsa1600 | 0:f0345f20337e | 45 | } |
corsa1600 | 0:f0345f20337e | 46 | public: |
corsa1600 | 0:f0345f20337e | 47 | // The fire function can immediately change states in the FSM |
corsa1600 | 0:f0345f20337e | 48 | void fire() { |
corsa1600 | 0:f0345f20337e | 49 | _queue.cancel(_pending); |
corsa1600 | 0:f0345f20337e | 50 | _queue.call(this, &StopLight::_step, GREEN); |
corsa1600 | 0:f0345f20337e | 51 | _queue.dispatch(); |
corsa1600 | 0:f0345f20337e | 52 | } |
corsa1600 | 0:f0345f20337e | 53 | }; |
corsa1600 | 0:f0345f20337e | 54 | |
corsa1600 | 0:f0345f20337e | 55 | StopLight sl; |
corsa1600 | 0:f0345f20337e | 56 | |
corsa1600 | 0:f0345f20337e | 57 | int main (void) { |
corsa1600 | 0:f0345f20337e | 58 | sl.fire(); |
corsa1600 | 0:f0345f20337e | 59 | wait(osWaitForever); |
corsa1600 | 0:f0345f20337e | 60 | } |