Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Name : FSM_Test.cpp
- // Author : uMinded
- // Version : 0.1
- // Description : FSM using linked lists
- // Props : Matthieu M. on StackOverflow
- //============================================================================
- #include <stdio.h>
- #include <stdlib.h>
- struct Event
- {
- Event(const char* a, const char* n):
- action(a), name(n) {}
- const char* action;
- const char* name;
- void (*func)(Event);
- };
- struct StateNode;
- struct State
- {
- State(const char* n):
- me(0), name(n) {}
- StateNode* me;
- const char* name;
- void (*in_func)(State);
- void (*out_func)(State);
- };
- struct StateNode
- {
- StateNode(Event e, State& s, StateNode* n):
- event(e), state(s), next(n) {}
- Event event;
- State& state;
- StateNode* next;
- };
- struct Transition
- {
- Transition(State& origin, Event e, State& destination):
- node(e, destination, origin.me) { origin.me = &node; }
- StateNode node;
- };
- // Global variable to hold the current state
- State State_Curr = NULL;
- void FSM_Init(State Head)
- {
- State_Curr = Head;
- State_Curr.in_func(State_Curr);
- }
- void FSM_Feed(char event)
- {
- StateNode* State_Find = State_Curr.me;
- int found = 0;
- do {
- if( (*State_Find->event.action) == event ) {
- State_Curr.out_func(State_Curr);
- State_Find->event.func(State_Find->event);
- State_Curr = State_Find->state;
- State_Curr.in_func(State_Curr);
- found = 1;
- }
- else if (State_Find->next != NULL) {
- State_Find = State_Find->next;
- }
- else if (State_Find->next == NULL) {
- return;
- }
- else { continue; }
- } while(found == 0);
- }
- void s_in_print(State state)
- {
- printf("Entering State: %s \n", state.name);
- }
- void s_out_print(State state)
- {
- printf("Exiting State: %s \n", state.name);
- }
- void e_print(Event event)
- {
- printf("Event Recieved: %s \n", event.name);
- }
- int main ( void )
- {
- /* ASCII ART!! *** STATE CHART ***
- +---------+
- | state1: |
- | Welcome |
- +---------+
- | ^
- KEY_DOWN | | KEY_UP
- V |
- +-------------+ +---------+ +--------------+
- | state3: | <--- KEY_LEFT --- | state2: | <--- KEY_LEFT --- | state4: |
- | View - Left | --- KEY_RIGHT --- | View | --- KEY_RIGHT ---> | View - Right |
- +-------------+ +---------+ +--------------+
- | ^
- KEY_DOWN | | KEY_UP
- V |
- +-------------+ +---------+ +---------------+
- |state6 : | <--- KEY_LEFT --- | state5: | <--- KEY_LEFT --- | state7: |
- |Setup - Left | --- KEY_RIGHT --- | Setup | --- KEY_RIGHT ---> | Setup - Right |
- +-------------+ +---------+ +---------------+
- */
- State state1("Welcome");
- state1.in_func = &s_in_print;
- state1.out_func = &s_out_print;
- State state2("View");
- state2.in_func = &s_in_print;
- state2.out_func = &s_out_print;
- State state3("View - Left");
- state3.in_func = &s_in_print;
- state3.out_func = &s_out_print;
- State state4("View - Right");
- state4.in_func = &s_in_print;
- state4.out_func = &s_out_print;
- State state5("Setup");
- state5.in_func = &s_in_print;
- state5.out_func = &s_out_print;
- State state6("Setup - Left");
- state6.in_func = &s_in_print;
- state6.out_func = &s_out_print;
- State state7("Setup - Right");
- state7.in_func = &s_in_print;
- state7.out_func = &s_out_print;
- Event KEY_UP("w","UP");
- KEY_UP.func = &e_print;
- Event KEY_DOWN("s","DOWN");
- KEY_DOWN.func = &e_print;
- Event KEY_LEFT("a","LEFT");
- KEY_LEFT.func = &e_print;
- Event KEY_RIGHT("d","RIGHT");
- KEY_RIGHT.func = &e_print;
- Transition t0(state1, KEY_DOWN, state2);
- Transition t1(state2, KEY_LEFT, state3);
- Transition t2(state3, KEY_RIGHT, state2);
- Transition t3(state2, KEY_RIGHT, state4);
- Transition t4(state4, KEY_LEFT, state2);
- Transition t5(state2, KEY_UP, state1);
- Transition t6(state2, KEY_DOWN, state5);
- Transition t7(state5, KEY_LEFT, state6);
- Transition t8(state6, KEY_RIGHT, state5);
- Transition t9(state5, KEY_RIGHT, state7);
- Transition t10(state7, KEY_LEFT, state5);
- Transition t11(state5, KEY_UP, state2);
- printf("Example FSM using linked lists \n");
- printf("To navigate use 'wads' & 'x' to exit \n\n");
- char feed = '0';
- FSM_Init(state1);
- while((feed = getchar()) != 'x') // Press 'x' to exit program!
- {
- FSM_Feed(feed);
- }
- return(0);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement