This example illustrates the use of simple stage machine to handle event within mbed. When any of the 3 push button is pressed, the stage (number) will be shown via the on-board LEDs. {{/media/uploads/yoonghm/state_machine_example.jpg}}
main.cpp@1:8cfb6bfdb4e0, 2011-12-08 (annotated)
- Committer:
- yoonghm
- Date:
- Thu Dec 08 03:54:37 2011 +0000
- Revision:
- 1:8cfb6bfdb4e0
- Parent:
- 0:8e509fb9a48c
Updated task and circuit diagram
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yoonghm | 0:8e509fb9a48c | 1 | #include "mbed.h" |
yoonghm | 0:8e509fb9a48c | 2 | |
yoonghm | 0:8e509fb9a48c | 3 | /* Various states */ |
yoonghm | 0:8e509fb9a48c | 4 | typedef enum tState |
yoonghm | 0:8e509fb9a48c | 5 | { |
yoonghm | 0:8e509fb9a48c | 6 | STATE_A, |
yoonghm | 0:8e509fb9a48c | 7 | STATE_B, |
yoonghm | 0:8e509fb9a48c | 8 | STATE_C |
yoonghm | 0:8e509fb9a48c | 9 | } tState; |
yoonghm | 0:8e509fb9a48c | 10 | |
yoonghm | 0:8e509fb9a48c | 11 | /* Variables */ |
yoonghm | 0:8e509fb9a48c | 12 | tState state; /* Current state */ |
yoonghm | 0:8e509fb9a48c | 13 | char trigger; /* Bit set to indicate which button triggers */ |
yoonghm | 0:8e509fb9a48c | 14 | |
yoonghm | 0:8e509fb9a48c | 15 | InterruptIn button1(p5); /* interrupt for button 1 at pin 5 */ |
yoonghm | 0:8e509fb9a48c | 16 | InterruptIn button2(p6); /* interrupt for button 1 at pin 6 */ |
yoonghm | 0:8e509fb9a48c | 17 | InterruptIn button3(p7); /* interrupt for button 1 at pin 7 */ |
yoonghm | 0:8e509fb9a48c | 18 | BusOut task(LED1, LED2, LED3, LED4); |
yoonghm | 0:8e509fb9a48c | 19 | |
yoonghm | 0:8e509fb9a48c | 20 | /* Interrupt handlers when buttons is pressed */ |
yoonghm | 0:8e509fb9a48c | 21 | void int_button1() {trigger |= 0x01;} |
yoonghm | 0:8e509fb9a48c | 22 | void int_button2() {trigger |= 0x02;} |
yoonghm | 0:8e509fb9a48c | 23 | void int_button3() {trigger |= 0x04;} |
yoonghm | 0:8e509fb9a48c | 24 | |
yoonghm | 0:8e509fb9a48c | 25 | /* task to be executed during each state transition */ |
yoonghm | 1:8cfb6bfdb4e0 | 26 | void do_task(int n) {task = n; wait(2.0);} |
yoonghm | 0:8e509fb9a48c | 27 | |
yoonghm | 0:8e509fb9a48c | 28 | void sleep(void) {__WFI();} /* Power-saving sleeping mode */ |
yoonghm | 0:8e509fb9a48c | 29 | |
yoonghm | 0:8e509fb9a48c | 30 | |
yoonghm | 0:8e509fb9a48c | 31 | int main() |
yoonghm | 0:8e509fb9a48c | 32 | { |
yoonghm | 0:8e509fb9a48c | 33 | /* Enable pull-down resistor for each button */ |
yoonghm | 0:8e509fb9a48c | 34 | button1.mode(PullDown); |
yoonghm | 0:8e509fb9a48c | 35 | button2.mode(PullDown); |
yoonghm | 0:8e509fb9a48c | 36 | button3.mode(PullDown); |
yoonghm | 0:8e509fb9a48c | 37 | |
yoonghm | 0:8e509fb9a48c | 38 | /* Assign ISR to each button */ |
yoonghm | 0:8e509fb9a48c | 39 | button1.rise(&int_button1); |
yoonghm | 0:8e509fb9a48c | 40 | button2.rise(&int_button2); |
yoonghm | 0:8e509fb9a48c | 41 | button3.rise(&int_button3); |
yoonghm | 0:8e509fb9a48c | 42 | |
yoonghm | 0:8e509fb9a48c | 43 | /* initial the state machine */ |
yoonghm | 0:8e509fb9a48c | 44 | state = STATE_A; |
yoonghm | 1:8cfb6bfdb4e0 | 45 | do_task(STATE_A); |
yoonghm | 0:8e509fb9a48c | 46 | |
yoonghm | 0:8e509fb9a48c | 47 | while(1) |
yoonghm | 0:8e509fb9a48c | 48 | { |
yoonghm | 0:8e509fb9a48c | 49 | /* While I am not triggered, sleep */ |
yoonghm | 0:8e509fb9a48c | 50 | while (!trigger) |
yoonghm | 0:8e509fb9a48c | 51 | sleep(); |
yoonghm | 0:8e509fb9a48c | 52 | |
yoonghm | 0:8e509fb9a48c | 53 | /* I am awake now. Check which button has triggered*/ |
yoonghm | 0:8e509fb9a48c | 54 | switch (state) |
yoonghm | 0:8e509fb9a48c | 55 | { |
yoonghm | 0:8e509fb9a48c | 56 | ////////////////////////////////////////////////////////////////// |
yoonghm | 0:8e509fb9a48c | 57 | case STATE_A: /////////////////////////////////////////////////// |
yoonghm | 0:8e509fb9a48c | 58 | ////////////////////////////////////////////////////////////////// |
yoonghm | 0:8e509fb9a48c | 59 | if (trigger & 0x01) /* Button 1 down */ |
yoonghm | 0:8e509fb9a48c | 60 | { |
yoonghm | 0:8e509fb9a48c | 61 | do_task(1); |
yoonghm | 0:8e509fb9a48c | 62 | state = STATE_B; /* Transit to STATE_B */ |
yoonghm | 0:8e509fb9a48c | 63 | } |
yoonghm | 0:8e509fb9a48c | 64 | trigger = 0; /* Ignore other triggers in this state */ |
yoonghm | 0:8e509fb9a48c | 65 | break; |
yoonghm | 0:8e509fb9a48c | 66 | |
yoonghm | 0:8e509fb9a48c | 67 | ////////////////////////////////////////////////////////////////// |
yoonghm | 0:8e509fb9a48c | 68 | case STATE_B: /////////////////////////////////////////////////// |
yoonghm | 0:8e509fb9a48c | 69 | ////////////////////////////////////////////////////////////////// |
yoonghm | 0:8e509fb9a48c | 70 | if (trigger & 0x01) /* Button 1 down */ |
yoonghm | 0:8e509fb9a48c | 71 | { |
yoonghm | 0:8e509fb9a48c | 72 | do_task(2); |
yoonghm | 0:8e509fb9a48c | 73 | state = STATE_A; |
yoonghm | 0:8e509fb9a48c | 74 | } |
yoonghm | 0:8e509fb9a48c | 75 | if (trigger & 0x02) /* Button 2 down */ |
yoonghm | 0:8e509fb9a48c | 76 | { |
yoonghm | 0:8e509fb9a48c | 77 | do_task(3); |
yoonghm | 0:8e509fb9a48c | 78 | /* Remain in the same state */ |
yoonghm | 0:8e509fb9a48c | 79 | } |
yoonghm | 0:8e509fb9a48c | 80 | if (trigger & 0x04) /* Button 3 down */ |
yoonghm | 0:8e509fb9a48c | 81 | { |
yoonghm | 0:8e509fb9a48c | 82 | do_task(4); |
yoonghm | 0:8e509fb9a48c | 83 | state = STATE_C; |
yoonghm | 0:8e509fb9a48c | 84 | } |
yoonghm | 0:8e509fb9a48c | 85 | trigger = 0; /* Ignore other triggers in this state */ |
yoonghm | 0:8e509fb9a48c | 86 | break; |
yoonghm | 0:8e509fb9a48c | 87 | |
yoonghm | 0:8e509fb9a48c | 88 | ////////////////////////////////////////////////////////////////// |
yoonghm | 0:8e509fb9a48c | 89 | case STATE_C: /////////////////////////////////////////////////// |
yoonghm | 0:8e509fb9a48c | 90 | ////////////////////////////////////////////////////////////////// |
yoonghm | 0:8e509fb9a48c | 91 | if (trigger & 0x04) /* Button 3 down */ |
yoonghm | 0:8e509fb9a48c | 92 | { |
yoonghm | 0:8e509fb9a48c | 93 | do_task(5); |
yoonghm | 0:8e509fb9a48c | 94 | state = STATE_A; |
yoonghm | 0:8e509fb9a48c | 95 | } |
yoonghm | 0:8e509fb9a48c | 96 | break; |
yoonghm | 0:8e509fb9a48c | 97 | } /* while (!trigger) */ |
yoonghm | 0:8e509fb9a48c | 98 | } /* while (1) */ |
yoonghm | 0:8e509fb9a48c | 99 | } |