pabloliva87

Ej6C

May 3rd, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string.h>
  4.  
  5. #define MAX_NAME_LENGTH 20
  6. #define STIMULI_AMOUNT 4
  7. #define STATE_AMOUNT 5
  8.  
  9. enum Stimuli
  10. {
  11.     TurnOn,
  12.     TurnOff,
  13.     SpeedUp,
  14.     SpeedDown
  15. };
  16.  
  17. enum State
  18. {
  19.     Off,
  20.     Stopped,
  21.     Walking,
  22.     Running,
  23.     Error
  24. };
  25.  
  26.  
  27. typedef struct Transition transition;
  28.  
  29. struct FSM
  30. {
  31.     enum State curr_state;
  32.     enum State ** transition_logic;
  33.     const char* printable_stimuli[4];
  34.     const char* printable_states[5];
  35. };
  36.  
  37. void stimulate (struct FSM* fsm, enum Stimuli stimulus);
  38. void print_transition (enum State previous, enum Stimuli stimulus, enum State newState, const char** printable_stimuli, const char** printable_states);
  39. void initialize (struct FSM* fsm);
  40.  
  41. int main (void)
  42. {
  43.    
  44.     int input;
  45.     struct FSM the_machine;
  46.    
  47.     initialize (&the_machine);
  48.     input = 0;
  49.    
  50.     while (input != 4)
  51.     {
  52.         printf("Input stimulus: 0_On, 1_Off, 2_SpUp, 3_SpDn, 4_Exit \n");
  53.         scanf("%i", &input);
  54.  
  55.         if ((0 <= input) && (input < 4))
  56.         {
  57.             stimulate (&the_machine, input);
  58.         }
  59.     }
  60.    
  61.     return EXIT_SUCCESS;
  62.    
  63. }
  64.  
  65. void stimulate(struct FSM* fsm, enum Stimuli stimulus)
  66. {
  67.  
  68.     enum State previous;
  69.  
  70.     previous = (*fsm).curr_state;
  71.  
  72.     (*fsm).curr_state = (((*fsm).transition_logic)[previous][stimulus]);
  73.  
  74.     print_transition (previous, stimulus, (*fsm).curr_state, (*fsm).printable_stimuli, (*fsm).printable_states);
  75.  
  76. }
  77.  
  78. void print_transition (const enum State previous, const enum Stimuli stimulus, const enum State newState, const char** printable_stimuli, const char** printable_states)
  79. {
  80.  
  81.     char prev_state[MAX_NAME_LENGTH];
  82.     char received_stimulus[MAX_NAME_LENGTH];
  83.     char next_state[MAX_NAME_LENGTH];
  84.  
  85.     strncpy (prev_state, printable_states[previous], MAX_NAME_LENGTH);
  86.     strncpy (received_stimulus, printable_stimuli[stimulus], MAX_NAME_LENGTH);
  87.     strncpy (next_state, printable_states[newState], MAX_NAME_LENGTH);
  88.  
  89.     printf("[%s]---[%s]--->[%s]\n", prev_state, received_stimulus, next_state);
  90. }
  91.  
  92. void initialize (struct FSM* fsm)
  93. {
  94.  
  95.     int i;
  96.    
  97.     (*fsm).curr_state = Off;
  98.  
  99.     (*fsm).printable_stimuli[TurnOn] = "TurnOn";
  100.     (*fsm).printable_stimuli[TurnOff] = "TurnOff";
  101.     (*fsm).printable_stimuli[SpeedUp] = "SpeedUp";
  102.     (*fsm).printable_stimuli[SpeedDown] = "SpeedDown";
  103.  
  104.     (*fsm).printable_states[Off] = "Off";
  105.     (*fsm).printable_states[Stopped] = "Stopped";
  106.     (*fsm).printable_states[Walking] = "Walking";
  107.     (*fsm).printable_states[Running] = "Running";
  108.     (*fsm).printable_states[Error] = "Error";
  109.    
  110.     (*fsm).transition_logic = (enum State **) calloc (STATE_AMOUNT ,sizeof(enum State *));
  111.  
  112.     for (i=0; i<STATE_AMOUNT; i++)
  113.     {
  114.         (*fsm).transition_logic[i] = (enum State *) calloc (STIMULI_AMOUNT ,sizeof(enum State));
  115.     }
  116.    
  117.     /** OFF STATE */
  118.     /* TURNON STIMULUS */
  119.     ((*fsm).transition_logic)[Off][TurnOn] = Stopped;
  120.  
  121.     /* TURNOFF STIMULUS */
  122.     ((*fsm).transition_logic)[Off][TurnOff] = Off;
  123.  
  124.     /* SPEEDUP STIMULUS */
  125.     ((*fsm).transition_logic)[Off][SpeedUp] = Error;
  126.  
  127.     /* SPEEDDOWN STIMULUS */
  128.     ((*fsm).transition_logic)[Off][SpeedDown] = Error;
  129.  
  130.     /** STOPPED STATE */
  131.     /* TURNON STIMULUS */
  132.     ((*fsm).transition_logic)[Stopped][TurnOn] = Error;
  133.  
  134.     /* TURNOFF STIMULUS */
  135.     ((*fsm).transition_logic)[Stopped][TurnOff] = Off;
  136.  
  137.     /* SPEEDUP STIMULUS */
  138.     ((*fsm).transition_logic)[Stopped][SpeedUp] = Walking;
  139.  
  140.     /* SPEEDDOWN STIMULUS */
  141.     ((*fsm).transition_logic)[Stopped][SpeedDown] = Error;
  142.  
  143.     /** WALKING STATE */
  144.     /* TURNON STIMULUS */
  145.     ((*fsm).transition_logic)[Walking][TurnOn] = Walking;
  146.  
  147.     /* TURNOFF STIMULUS */
  148.     ((*fsm).transition_logic)[Walking][TurnOff] = Off;
  149.  
  150.     /* SPEEDUP STIMULUS */
  151.     ((*fsm).transition_logic)[Walking][SpeedUp] = Running;
  152.  
  153.     /* SPEEDDOWN STIMULUS */
  154.     ((*fsm).transition_logic)[Walking][SpeedDown] = Stopped;
  155.  
  156.     /** RUNNING STATE */
  157.     /* TURNON STIMULUS */
  158.     ((*fsm).transition_logic)[Running][TurnOn] = Running;
  159.  
  160.     /* TURNOFF STIMULUS */
  161.     ((*fsm).transition_logic)[Running][TurnOff] = Off;
  162.  
  163.     /* SPEEDUP STIMULUS */
  164.     ((*fsm).transition_logic)[Running][SpeedUp] = Error;
  165.  
  166.     /* SPEEDDOWN STIMULUS */
  167.     ((*fsm).transition_logic)[Running][SpeedDown] = Walking;
  168.  
  169.     /** ERROR STATE */
  170.     /* TURNON STIMULUS */
  171.     ((*fsm).transition_logic)[Error][TurnOn] = Error;
  172.  
  173.     /* TURNOFF STIMULUS */
  174.     ((*fsm).transition_logic)[Error][TurnOff] = Off;
  175.  
  176.     /* SPEEDUP STIMULUS */
  177.     ((*fsm).transition_logic)[Error][SpeedUp] = Error;
  178.  
  179.     /* SPEEDDOWN STIMULUS */
  180.     ((*fsm).transition_logic)[Error][SpeedDown] = Error;
  181.  
  182. }
Add Comment
Please, Sign In to add comment