Oliver Ainhirn
/
85_Event_State
erweitert um RGB LED
main.cpp@1:dc7c5869f9e1, 2019-04-02 (annotated)
- Committer:
- corsa1600
- Date:
- Tue Apr 02 18:07:35 2019 +0000
- Revision:
- 1:dc7c5869f9e1
- Parent:
- 0:f0345f20337e
LED_Blink
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 | 1:dc7c5869f9e1 | 11 | class StopLight |
corsa1600 | 1:dc7c5869f9e1 | 12 | { |
corsa1600 | 0:f0345f20337e | 13 | private: |
corsa1600 | 1:dc7c5869f9e1 | 14 | Timer t1; //mein Timer |
corsa1600 | 0:f0345f20337e | 15 | EventQueue _queue; |
corsa1600 | 0:f0345f20337e | 16 | int _pending; |
corsa1600 | 0:f0345f20337e | 17 | |
corsa1600 | 1:dc7c5869f9e1 | 18 | enum state { RED, GREEN, YELLOW,GREENBLINK }; |
corsa1600 | 0:f0345f20337e | 19 | |
corsa1600 | 0:f0345f20337e | 20 | // The _step function is a traditional FSM which enqueues transitions onto the event queue |
corsa1600 | 0:f0345f20337e | 21 | void _step(state transition) { |
corsa1600 | 0:f0345f20337e | 22 | switch (transition) { |
corsa1600 | 0:f0345f20337e | 23 | case RED: |
corsa1600 | 0:f0345f20337e | 24 | set_red(); |
corsa1600 | 0:f0345f20337e | 25 | _pending = _queue.call_in(1000, this, &StopLight::_step, GREEN); |
corsa1600 | 0:f0345f20337e | 26 | break; |
corsa1600 | 0:f0345f20337e | 27 | |
corsa1600 | 0:f0345f20337e | 28 | case GREEN: |
corsa1600 | 0:f0345f20337e | 29 | set_green(); |
corsa1600 | 1:dc7c5869f9e1 | 30 | _pending = _queue.call_in(1000, this, &StopLight::_step, GREENBLINK); |
corsa1600 | 1:dc7c5869f9e1 | 31 | break; |
corsa1600 | 1:dc7c5869f9e1 | 32 | |
corsa1600 | 1:dc7c5869f9e1 | 33 | case GREENBLINK: |
corsa1600 | 1:dc7c5869f9e1 | 34 | set_greenBlink(); |
corsa1600 | 1:dc7c5869f9e1 | 35 | _pending = _queue.call_in(3000, this, &StopLight::_step, YELLOW); |
corsa1600 | 0:f0345f20337e | 36 | break; |
corsa1600 | 0:f0345f20337e | 37 | |
corsa1600 | 0:f0345f20337e | 38 | case YELLOW: |
corsa1600 | 0:f0345f20337e | 39 | set_yellow(); |
corsa1600 | 0:f0345f20337e | 40 | _pending = _queue.call_in(500, this, &StopLight::_step, RED); |
corsa1600 | 0:f0345f20337e | 41 | break; |
corsa1600 | 0:f0345f20337e | 42 | } |
corsa1600 | 0:f0345f20337e | 43 | } |
corsa1600 | 1:dc7c5869f9e1 | 44 | |
corsa1600 | 1:dc7c5869f9e1 | 45 | void set_red() |
corsa1600 | 1:dc7c5869f9e1 | 46 | { |
corsa1600 | 0:f0345f20337e | 47 | red = 0; green = 1; blue = 1; |
corsa1600 | 0:f0345f20337e | 48 | } |
corsa1600 | 1:dc7c5869f9e1 | 49 | void set_green() |
corsa1600 | 1:dc7c5869f9e1 | 50 | { |
corsa1600 | 0:f0345f20337e | 51 | red = 1; green = 0; blue = 1; |
corsa1600 | 1:dc7c5869f9e1 | 52 | |
corsa1600 | 0:f0345f20337e | 53 | } |
corsa1600 | 1:dc7c5869f9e1 | 54 | void set_greenBlink() |
corsa1600 | 1:dc7c5869f9e1 | 55 | { |
corsa1600 | 1:dc7c5869f9e1 | 56 | t1.start(); // ich starte meinen Timer |
corsa1600 | 1:dc7c5869f9e1 | 57 | //red = 1; green = 1; blue = 1; |
corsa1600 | 1:dc7c5869f9e1 | 58 | if(t1.read_ms()>200) // erst wenn 10Hz |
corsa1600 | 1:dc7c5869f9e1 | 59 | { |
corsa1600 | 1:dc7c5869f9e1 | 60 | |
corsa1600 | 1:dc7c5869f9e1 | 61 | if(green=1) |
corsa1600 | 1:dc7c5869f9e1 | 62 | { |
corsa1600 | 1:dc7c5869f9e1 | 63 | red = 1; green = 1; blue = 1; |
corsa1600 | 1:dc7c5869f9e1 | 64 | } |
corsa1600 | 1:dc7c5869f9e1 | 65 | else |
corsa1600 | 1:dc7c5869f9e1 | 66 | { |
corsa1600 | 1:dc7c5869f9e1 | 67 | red = 1; green = 0; blue = 1; |
corsa1600 | 1:dc7c5869f9e1 | 68 | } |
corsa1600 | 1:dc7c5869f9e1 | 69 | t1.reset(); // ich resete meinen Timer |
corsa1600 | 1:dc7c5869f9e1 | 70 | } |
corsa1600 | 1:dc7c5869f9e1 | 71 | |
corsa1600 | 1:dc7c5869f9e1 | 72 | if ( green=0) |
corsa1600 | 1:dc7c5869f9e1 | 73 | green=1; |
corsa1600 | 1:dc7c5869f9e1 | 74 | else |
corsa1600 | 1:dc7c5869f9e1 | 75 | green=0; |
corsa1600 | 1:dc7c5869f9e1 | 76 | } |
corsa1600 | 1:dc7c5869f9e1 | 77 | void set_yellow() |
corsa1600 | 1:dc7c5869f9e1 | 78 | { |
corsa1600 | 0:f0345f20337e | 79 | red = 0; green = 0; blue = 1; |
corsa1600 | 1:dc7c5869f9e1 | 80 | |
corsa1600 | 1:dc7c5869f9e1 | 81 | /* |
corsa1600 | 1:dc7c5869f9e1 | 82 | wait_ms(150); |
corsa1600 | 1:dc7c5869f9e1 | 83 | red = 1; green = 1; blue = 1; |
corsa1600 | 1:dc7c5869f9e1 | 84 | wait_ms(150); |
corsa1600 | 1:dc7c5869f9e1 | 85 | red = 0; green = 0; blue = 1; |
corsa1600 | 1:dc7c5869f9e1 | 86 | wait_ms(150); |
corsa1600 | 1:dc7c5869f9e1 | 87 | red = 1; green = 1; blue = 1; |
corsa1600 | 1:dc7c5869f9e1 | 88 | */ |
corsa1600 | 0:f0345f20337e | 89 | } |
corsa1600 | 0:f0345f20337e | 90 | public: |
corsa1600 | 0:f0345f20337e | 91 | // The fire function can immediately change states in the FSM |
corsa1600 | 1:dc7c5869f9e1 | 92 | void fire() |
corsa1600 | 1:dc7c5869f9e1 | 93 | { |
corsa1600 | 0:f0345f20337e | 94 | _queue.cancel(_pending); |
corsa1600 | 0:f0345f20337e | 95 | _queue.call(this, &StopLight::_step, GREEN); |
corsa1600 | 0:f0345f20337e | 96 | _queue.dispatch(); |
corsa1600 | 0:f0345f20337e | 97 | } |
corsa1600 | 0:f0345f20337e | 98 | }; |
corsa1600 | 0:f0345f20337e | 99 | |
corsa1600 | 0:f0345f20337e | 100 | StopLight sl; |
corsa1600 | 0:f0345f20337e | 101 | |
corsa1600 | 1:dc7c5869f9e1 | 102 | int main (void) |
corsa1600 | 1:dc7c5869f9e1 | 103 | { |
corsa1600 | 0:f0345f20337e | 104 | sl.fire(); |
corsa1600 | 0:f0345f20337e | 105 | wait(osWaitForever); |
corsa1600 | 0:f0345f20337e | 106 | } |