void main(void) { unsigned int j; unsigned int anEvent[MAX_EVENTS]; initTimer(); /* initialize timer */ FSM(RESET); /* initialize fsm */ while (1) { /* wait for next timer interrupt */ waitTimerTic(); /* evaluate all events */ anEvent[0] = getRightKeyEvent(); anEvent[1] = getTimerEvent(); /* for all events call fsm */ for (j = 0; j < MAX_EVENTS; j++) { if (anEvent[j] != NOEVENT) { FSM(anEvent[j]); anEvent[j] = NOEVENT; } } } }
/** * Move the FSM to new state @state and call all registered hooks for it. * * Iterates over all priorities for current state of top (current) FSM and * switch to the registered FSMs. * * Currently there is TFW_GFSM_WC_BMAP_SZ priorities (and each priority * has 32-bit states bitmap), so we use this fact to speedup the iteration. */ int tfw_gfsm_move(TfwGState *st, unsigned short state, struct sk_buff *skb, unsigned int off) { int r = TFW_PASS, p, fsm; unsigned int *hooks = fsm_hooks_bm[FSM(st)]; unsigned long mask = 1 << state; TFW_DBG("GFSM move from %#x to %#x\n", FSM_STATE(st), state); /* Remember current FSM context. */ SET_STATE(st, state); /* Start from higest priority. */ for (p = TFW_GFSM_HOOK_PRIORITY_HIGH; p < TFW_GFSM_HOOK_PRIORITY_NUM; ++p) { /* * TODO Handle different priorities by ordering the hooks, * rather than fixed priority levels to avoid spinning in vain. */ if (!(hooks[p] & mask)) return TFW_PASS; /* Switch context to other FSM. */ fsm = tfw_gfsm_switch(st, state, p); /* * Don't execute FSM handler who executed us, * the FSM will just continue it's processing when all other * executed FSMs exit. */ if (FSM_STATE(st) & TFW_GFSM_ONSTACK) continue; switch (__gfsm_fsm_exec(st, fsm, skb, off)) { case TFW_BLOCK: return TFW_BLOCK; case TFW_POSTPONE: /* * Postpone processing if at least one FSM * needs more data. */ r = TFW_POSTPONE; } } return r; }
int main() { int p,i,st,l; char s[256]; FSM(); scanf("%d ",&p); while(p--) { gets(s); l=strlen(s); st=0; for(i=0;i<l && st!=-1;i++)st=state[st][s[i]]; if(st==1 || st==3 || st==6 || st==7) puts("YES"); else puts("NO"); } return 0; }
void FSM_Test() { int FSM_input; unsigned int temp; srand(time(0)); do { temp = rand() % 100; if (temp < 5) { FSM_input = 1; } else if (temp < 10) { FSM_input = -1; } else { FSM_input = 0; } } while (FSM(FSM_input) != STATE_CLOSE); } // End of FSM_Test()
/** * Context switch from current FSM at state @state to next FSM. */ static int tfw_gfsm_switch(TfwGState *st, int state, int prio) { int shift = prio * TFW_GFSM_PRIO_N + (state & TFW_GFSM_STATE_MASK); int fsm_next = fsm_hooks[FSM(st)][shift].fsm_id; int fsm_curr = state >> TFW_GFSM_FSM_SHIFT; int free_slot; TFW_DBG("GFSM switch from fsm %d at state %d to fsm %d at state %#x\n", fsm_curr, state, fsm_next, fsm_hooks[fsm_curr][shift].st0); st->curr = __gfsm_fsm_lookup(st, fsm_next, &free_slot); if (st->curr < 0) { /* Create new clear state for the next FSM. */ BUG_ON(free_slot < 0); st->curr = free_slot; FSM_STATE(st) = fsm_hooks[fsm_curr][shift].st0; } return fsm_next; }
transition upalpha_fsm[] = { { 0, SINGLE_CHARACTER("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), -1, -1, ACCEPT }, {-1} }; transition lowalpha_fsm[] = { { 0, SINGLE_CHARACTER("abcdefghijklmnopqrstuvwxyz"), -1, -1, ACCEPT }, {-1} }; transition alpha_fsm[] = { {0, FSM(lowalpha_fsm), -1, -1, ACCEPT }, {0, FSM(upalpha_fsm), -1, -1, ACCEPT }, {-1} }; transition alphanum_fsm[] = { {0, FSM(alpha_fsm), -1, -1, ACCEPT }, {0, FSM(digit_fsm), -1, -1, ACCEPT }, {-1} }; transition hex_fsm[] = { {0, FSM(digit_fsm), -1, -1, ACCEPT }, {0, SINGLE_CHARACTER("ABCDEFabcdef"), -1, -1, ACCEPT },
transition alpha_fsm[] = { { 0, SINGLE_CHARACTER("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"), -1, -1, ACCEPT, NULL, NULL, "single character-alpha" }, {-1} }; transition digit_fsm[] = { { 0, SINGLE_CHARACTER("0123456789"), -1, -1, ACCEPT, NULL, NULL, "single character-digit" }, {-1} }; transition hexdig_fsm[] = { {0, FSM(digit_fsm), -1, -1, ACCEPT }, {0, SINGLE_CHARACTER("ABCDEFabcdef"), -1, -1, ACCEPT }, {-1} }; /* the RFC3986 specific def's */ transition sub_delims_fsm[] = { { 0, SINGLE_CHARACTER("!$&\'()*+,;="), -1, -1, ACCEPT, NULL, NULL, "single character-sub delims"}, {-1} }; transition gen_delims_fsm[] = { { 0, SINGLE_CHARACTER(":/?#[]@"), -1, -1, ACCEPT, NULL, NULL, "single character-gen delims" },
void changeStopLedState(); void changeFadeInState(); void changeFadeOutState(); State _emptyState = State(emptyState); State _waitState = State(waitState); // We have to add NULL for update and exit functions of the state // or the fsm will continually call menuState when in this state State _menuState = State(menuState, NULL, NULL); State _changeHourState = State(changeHourState, NULL, NULL); State _changeStartLedState = State(changeStartLedState, NULL, NULL); State _changeStopLedState = State(changeStopLedState, NULL, NULL); State _changeFadeInState = State(changeFadeInState, NULL, NULL); State _changeFadeOutState = State(changeFadeOutState, NULL, NULL); FSM fsm = FSM(_waitState); // Menu declaration typedef struct menuItem { uint8_t id; String message; }menuItem; QueueList <menuItem> menuItems; /********************* * Global definition *********************/ #define NUMBER_OF_MENU_LINES 3 /*********************