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
00001 #include "mbed.h" 00002 00003 /* Various states */ 00004 typedef enum tState 00005 { 00006 STATE_A, 00007 STATE_B, 00008 STATE_C 00009 } tState; 00010 00011 /* Variables */ 00012 tState state; /* Current state */ 00013 char trigger; /* Bit set to indicate which button triggers */ 00014 00015 InterruptIn button1(p5); /* interrupt for button 1 at pin 5 */ 00016 InterruptIn button2(p6); /* interrupt for button 1 at pin 6 */ 00017 InterruptIn button3(p7); /* interrupt for button 1 at pin 7 */ 00018 BusOut task(LED1, LED2, LED3, LED4); 00019 00020 /* Interrupt handlers when buttons is pressed */ 00021 void int_button1() {trigger |= 0x01;} 00022 void int_button2() {trigger |= 0x02;} 00023 void int_button3() {trigger |= 0x04;} 00024 00025 /* task to be executed during each state transition */ 00026 void do_task(int n) {task = n; wait(2.0);} 00027 00028 void sleep(void) {__WFI();} /* Power-saving sleeping mode */ 00029 00030 00031 int main() 00032 { 00033 /* Enable pull-down resistor for each button */ 00034 button1.mode(PullDown); 00035 button2.mode(PullDown); 00036 button3.mode(PullDown); 00037 00038 /* Assign ISR to each button */ 00039 button1.rise(&int_button1); 00040 button2.rise(&int_button2); 00041 button3.rise(&int_button3); 00042 00043 /* initial the state machine */ 00044 state = STATE_A; 00045 do_task(STATE_A); 00046 00047 while(1) 00048 { 00049 /* While I am not triggered, sleep */ 00050 while (!trigger) 00051 sleep(); 00052 00053 /* I am awake now. Check which button has triggered*/ 00054 switch (state) 00055 { 00056 ////////////////////////////////////////////////////////////////// 00057 case STATE_A: /////////////////////////////////////////////////// 00058 ////////////////////////////////////////////////////////////////// 00059 if (trigger & 0x01) /* Button 1 down */ 00060 { 00061 do_task(1); 00062 state = STATE_B; /* Transit to STATE_B */ 00063 } 00064 trigger = 0; /* Ignore other triggers in this state */ 00065 break; 00066 00067 ////////////////////////////////////////////////////////////////// 00068 case STATE_B: /////////////////////////////////////////////////// 00069 ////////////////////////////////////////////////////////////////// 00070 if (trigger & 0x01) /* Button 1 down */ 00071 { 00072 do_task(2); 00073 state = STATE_A; 00074 } 00075 if (trigger & 0x02) /* Button 2 down */ 00076 { 00077 do_task(3); 00078 /* Remain in the same state */ 00079 } 00080 if (trigger & 0x04) /* Button 3 down */ 00081 { 00082 do_task(4); 00083 state = STATE_C; 00084 } 00085 trigger = 0; /* Ignore other triggers in this state */ 00086 break; 00087 00088 ////////////////////////////////////////////////////////////////// 00089 case STATE_C: /////////////////////////////////////////////////// 00090 ////////////////////////////////////////////////////////////////// 00091 if (trigger & 0x04) /* Button 3 down */ 00092 { 00093 do_task(5); 00094 state = STATE_A; 00095 } 00096 break; 00097 } /* while (!trigger) */ 00098 } /* while (1) */ 00099 }
Generated on Wed Jul 13 2022 13:26:50 by 1.7.2