Advertisement
uMinded

FSM_Linked_List

Jul 23rd, 2011
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.87 KB | None | 0 0
  1. //============================================================================
  2. // Name        : FSM_Test.cpp
  3. // Author      : uMinded
  4. // Version     : 0.1
  5. // Description : FSM using linked lists
  6. // Props       : Matthieu M. on StackOverflow
  7. //============================================================================
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11.  
  12. struct Event
  13. {
  14.     Event(const char* a, const char* n):
  15.         action(a), name(n) {}
  16.  
  17.     const char* action;
  18.     const char* name;
  19.     void (*func)(Event);
  20. };
  21.  
  22. struct StateNode;
  23. struct State
  24. {
  25.     State(const char* n):
  26.         me(0), name(n) {}
  27.  
  28.     StateNode* me;
  29.     const char* name;
  30.     void (*in_func)(State);
  31.     void (*out_func)(State);
  32. };
  33.  
  34. struct StateNode
  35. {
  36.     StateNode(Event e, State& s, StateNode* n):
  37.         event(e), state(s), next(n) {}
  38.  
  39.     Event event;
  40.     State& state;
  41.     StateNode* next;
  42. };
  43.  
  44. struct Transition
  45. {
  46.     Transition(State& origin, Event e, State& destination):
  47.         node(e, destination, origin.me) { origin.me = &node; }
  48.  
  49.     StateNode node;
  50. };
  51.  
  52. // Global variable to hold the current state
  53. State State_Curr = NULL;
  54.  
  55. void FSM_Init(State Head)
  56. {
  57.     State_Curr = Head;
  58.     State_Curr.in_func(State_Curr);
  59. }
  60.  
  61. void FSM_Feed(char event)
  62. {
  63.     StateNode* State_Find = State_Curr.me;
  64.     int found = 0;
  65.     do {
  66.         if( (*State_Find->event.action) == event ) {
  67.             State_Curr.out_func(State_Curr);
  68.             State_Find->event.func(State_Find->event);
  69.             State_Curr = State_Find->state;
  70.             State_Curr.in_func(State_Curr);
  71.             found = 1;
  72.         }
  73.         else if (State_Find->next != NULL) {
  74.             State_Find = State_Find->next;
  75.         }
  76.         else if (State_Find->next == NULL) {
  77.             return;
  78.         }
  79.         else { continue; }
  80.     } while(found == 0);
  81. }
  82.  
  83. void s_in_print(State state)
  84. {
  85.     printf("Entering State: %s \n", state.name);
  86. }
  87.  
  88. void s_out_print(State state)
  89. {
  90.     printf("Exiting State:  %s \n", state.name);
  91. }
  92.  
  93. void e_print(Event event)
  94. {
  95.     printf("Event Recieved: %s \n", event.name);
  96. }
  97.  
  98. int main ( void )
  99. {
  100. /* ASCII ART!!                *** STATE CHART ***
  101.  
  102.                                   +---------+
  103.                                   | state1: |
  104.                                   | Welcome |
  105.                                   +---------+
  106.  
  107.                                      |  ^
  108.                            KEY_DOWN  |  | KEY_UP
  109.                                      V  |
  110.  
  111. +-------------+                   +---------+                    +--------------+
  112. | state3:     | <--- KEY_LEFT --- | state2: |  <--- KEY_LEFT --- |  state4:     |
  113. | View - Left | --- KEY_RIGHT --- | View    | --- KEY_RIGHT ---> | View - Right |
  114. +-------------+                   +---------+                    +--------------+
  115.  
  116.                                      |  ^
  117.                            KEY_DOWN  |  | KEY_UP
  118.                                      V  |
  119.  
  120. +-------------+                   +---------+                    +---------------+
  121. |state6 :     | <--- KEY_LEFT --- | state5: | <--- KEY_LEFT ---  | state7:       |
  122. |Setup - Left | --- KEY_RIGHT --- | Setup   | --- KEY_RIGHT ---> | Setup - Right |
  123. +-------------+                   +---------+                    +---------------+
  124.  
  125.  */
  126.     State state1("Welcome");
  127.         state1.in_func = &s_in_print;
  128.         state1.out_func = &s_out_print;
  129.     State state2("View");
  130.         state2.in_func = &s_in_print;
  131.         state2.out_func = &s_out_print;
  132.     State state3("View - Left");
  133.         state3.in_func = &s_in_print;
  134.         state3.out_func = &s_out_print;
  135.     State state4("View - Right");
  136.         state4.in_func = &s_in_print;
  137.         state4.out_func = &s_out_print;
  138.     State state5("Setup");
  139.         state5.in_func = &s_in_print;
  140.         state5.out_func = &s_out_print;
  141.     State state6("Setup - Left");
  142.         state6.in_func = &s_in_print;
  143.         state6.out_func = &s_out_print;
  144.     State state7("Setup - Right");
  145.         state7.in_func = &s_in_print;
  146.         state7.out_func = &s_out_print;
  147.  
  148.     Event KEY_UP("w","UP");
  149.         KEY_UP.func = &e_print;
  150.     Event KEY_DOWN("s","DOWN");
  151.         KEY_DOWN.func = &e_print;
  152.     Event KEY_LEFT("a","LEFT");
  153.         KEY_LEFT.func = &e_print;
  154.     Event KEY_RIGHT("d","RIGHT");
  155.         KEY_RIGHT.func = &e_print;
  156.  
  157.     Transition t0(state1, KEY_DOWN, state2);
  158.  
  159.     Transition t1(state2, KEY_LEFT, state3);
  160.     Transition t2(state3, KEY_RIGHT, state2);
  161.     Transition t3(state2, KEY_RIGHT, state4);
  162.     Transition t4(state4, KEY_LEFT, state2);
  163.     Transition t5(state2, KEY_UP, state1);
  164.     Transition t6(state2, KEY_DOWN, state5);
  165.  
  166.     Transition t7(state5, KEY_LEFT, state6);
  167.     Transition t8(state6, KEY_RIGHT, state5);
  168.     Transition t9(state5, KEY_RIGHT, state7);
  169.     Transition t10(state7, KEY_LEFT, state5);
  170.     Transition t11(state5, KEY_UP, state2);
  171.  
  172.     printf("Example FSM using linked lists \n");
  173.     printf("To navigate use 'wads' & 'x' to exit \n\n");
  174.  
  175.     char feed = '0';
  176.     FSM_Init(state1);
  177.     while((feed = getchar()) != 'x') // Press 'x' to exit program!
  178.     {
  179.         FSM_Feed(feed);
  180.     }
  181.  
  182.   return(0);
  183. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement