This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

FSM_v2

By: a guest on Jul 17th, 2011  |  syntax: C++  |  size: 2.53 KB  |  views: 66  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2.  
  3. void char_put(int);                                        // Our putchar() function
  4. void (*pFunc)(int);                                        // Function pointer
  5.  
  6. enum states { before = 0, inside = 1, after = 2 };
  7. enum events {event_space = 0, event_cr = 1, event_char = 2};
  8.  
  9. class StateMachine
  10. {
  11.   private:
  12.     enum states state_curr;                                 // Current state
  13.     struct branch
  14.     {
  15.         int event_type:3;
  16.         enum states state_new:2;
  17.         int do_func:1;
  18.     };
  19.     static struct branch transition_table[3][3];           // Transition map
  20.  
  21.   public:
  22.     StateMachine(states state_init) : state_curr(before)   // Constructor with starting state
  23.     {
  24.       state_curr = state_init;
  25.     }
  26.          
  27.     void process_event(int c)
  28.     {
  29.       printf("\nCurrent State %i \n", state_curr);
  30.       int idx = (c == ' ') ? event_space : (c == '\n') ? event_cr : event_char;
  31.       printf("Event Recieved: %i \n", idx);
  32.       struct branch *b = &transition_table[state_curr][idx];
  33.       state_curr = b->state_new;
  34.       printf("Next State: %i \n", state_curr);
  35.       if(b->do_func)
  36.       {
  37.         // pFunc = b->do_func; // ERROR: Invalid conversion from ‘int’ to ‘void (*)(int)
  38.         printf("pFunc Address: %0X \n", pFunc);
  39.         // pFunc(c);           // Should work if I can assign the func address above
  40.         char_put(c);
  41.       }
  42.     }
  43. };
  44.  
  45. struct StateMachine::branch StateMachine::transition_table[3][3] =
  46. {
  47.  
  48.   /* Current state
  49.        Recieved event, New state, pFunc to exec */
  50.  
  51.   { // State: before
  52.     {event_space, before, 0},
  53.     {event_cr,    before, 1},
  54.     {event_char,  inside, 1}
  55. //  {event_char,  inside, &char_put} // ERROR: Invalid conversion from ‘void (*)(int)’ to ‘int’
  56.   },
  57.   { // State: inside
  58.     {event_space, after,  0},
  59.     {event_cr,    before, 1},
  60.     {event_char,  inside, 1}
  61.   },
  62.   { // State: after
  63.     {event_space, after,  0},
  64.     {event_cr,    before, 1},
  65.     {event_char,  after,  0}
  66.   }
  67. };
  68.  
  69. int main(void)
  70. {
  71.   printf("\n    Welcome To The FSM Test");
  72.   printf("\n     To exit the program press 'Esc'");
  73.   printf("\n\n");
  74.  
  75.   int c;
  76.   StateMachine FSM(before);                              // Create & initlize state machine
  77.  
  78.   printf("Type a message and I will discover the first word! \n Input: ");
  79.   while((c = getchar()) != '\33')                        // Read input untill ESC is recieved
  80.   {
  81.     FSM.process_event(c);
  82.   }
  83.  
  84.   return 0;
  85. }
  86.  
  87. void char_put(int c)
  88. {
  89.   printf("FSM Says: %c \n", c);
  90. }
clone this paste RAW Paste Data