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}}

Dependencies:   mbed

/media/uploads/yoonghm/state_machine_example.jpg

Committer:
yoonghm
Date:
Wed Dec 07 10:10:02 2011 +0000
Revision:
0:8e509fb9a48c
Child:
1:8cfb6bfdb4e0
Initial

Who changed what in which revision?

UserRevisionLine numberNew 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 0:8e509fb9a48c 26 void do_task(int n) {task = n; wait(2.0); task = 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 0:8e509fb9a48c 45
yoonghm 0:8e509fb9a48c 46 while(1)
yoonghm 0:8e509fb9a48c 47 {
yoonghm 0:8e509fb9a48c 48 /* While I am not triggered, sleep */
yoonghm 0:8e509fb9a48c 49 while (!trigger)
yoonghm 0:8e509fb9a48c 50 sleep();
yoonghm 0:8e509fb9a48c 51
yoonghm 0:8e509fb9a48c 52 /* I am awake now. Check which button has triggered*/
yoonghm 0:8e509fb9a48c 53 switch (state)
yoonghm 0:8e509fb9a48c 54 {
yoonghm 0:8e509fb9a48c 55 //////////////////////////////////////////////////////////////////
yoonghm 0:8e509fb9a48c 56 case STATE_A: ///////////////////////////////////////////////////
yoonghm 0:8e509fb9a48c 57 //////////////////////////////////////////////////////////////////
yoonghm 0:8e509fb9a48c 58 if (trigger & 0x01) /* Button 1 down */
yoonghm 0:8e509fb9a48c 59 {
yoonghm 0:8e509fb9a48c 60 do_task(1);
yoonghm 0:8e509fb9a48c 61 state = STATE_B; /* Transit to STATE_B */
yoonghm 0:8e509fb9a48c 62 }
yoonghm 0:8e509fb9a48c 63 trigger = 0; /* Ignore other triggers in this state */
yoonghm 0:8e509fb9a48c 64 break;
yoonghm 0:8e509fb9a48c 65
yoonghm 0:8e509fb9a48c 66 //////////////////////////////////////////////////////////////////
yoonghm 0:8e509fb9a48c 67 case STATE_B: ///////////////////////////////////////////////////
yoonghm 0:8e509fb9a48c 68 //////////////////////////////////////////////////////////////////
yoonghm 0:8e509fb9a48c 69 if (trigger & 0x01) /* Button 1 down */
yoonghm 0:8e509fb9a48c 70 {
yoonghm 0:8e509fb9a48c 71 do_task(2);
yoonghm 0:8e509fb9a48c 72 state = STATE_A;
yoonghm 0:8e509fb9a48c 73 }
yoonghm 0:8e509fb9a48c 74 if (trigger & 0x02) /* Button 2 down */
yoonghm 0:8e509fb9a48c 75 {
yoonghm 0:8e509fb9a48c 76 do_task(3);
yoonghm 0:8e509fb9a48c 77 /* Remain in the same state */
yoonghm 0:8e509fb9a48c 78 }
yoonghm 0:8e509fb9a48c 79 if (trigger & 0x04) /* Button 3 down */
yoonghm 0:8e509fb9a48c 80 {
yoonghm 0:8e509fb9a48c 81 do_task(4);
yoonghm 0:8e509fb9a48c 82 state = STATE_C;
yoonghm 0:8e509fb9a48c 83 }
yoonghm 0:8e509fb9a48c 84 trigger = 0; /* Ignore other triggers in this state */
yoonghm 0:8e509fb9a48c 85 break;
yoonghm 0:8e509fb9a48c 86
yoonghm 0:8e509fb9a48c 87 //////////////////////////////////////////////////////////////////
yoonghm 0:8e509fb9a48c 88 case STATE_C: ///////////////////////////////////////////////////
yoonghm 0:8e509fb9a48c 89 //////////////////////////////////////////////////////////////////
yoonghm 0:8e509fb9a48c 90 if (trigger & 0x04) /* Button 3 down */
yoonghm 0:8e509fb9a48c 91 {
yoonghm 0:8e509fb9a48c 92 do_task(5);
yoonghm 0:8e509fb9a48c 93 state = STATE_A;
yoonghm 0:8e509fb9a48c 94 }
yoonghm 0:8e509fb9a48c 95 break;
yoonghm 0:8e509fb9a48c 96 } /* while (!trigger) */
yoonghm 0:8e509fb9a48c 97 } /* while (1) */
yoonghm 0:8e509fb9a48c 98 }