Guest User

Untitled

a guest
Oct 23rd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.78 KB | None | 0 0
  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. const unsigned int MAX_SIZE_STATE_STRING = 100;
  7. typedef enum Stimuli
  8. {
  9.   TurnOn,
  10.   TurnOff,
  11.   SpeedUp,
  12.   SpeedDown
  13. }   StimuliType;
  14.  
  15. typedef enum State
  16. {
  17.   Off,
  18.   Stopped,
  19.   Walking,
  20.   Running,
  21.   Error
  22. }  StateType;
  23.  
  24. char* getStringFromStimulus(StimuliType stimulus)
  25. {
  26.   char* message = (char*)malloc(sizeof(char)*MAX_SIZE_STATE_STRING);
  27.   switch(stimulus)
  28.   {
  29.   case TurnOff: strcpy(message, "TurnOff");
  30.     break;
  31.   case TurnOn: strcpy(message, "TurnOn");
  32.     break;
  33.   case SpeedUp: strcpy(message, "SpeedUp");
  34.     break;
  35.   case SpeedDown: strcpy(message, "SpeedDown");
  36.     break;
  37.   }
  38.  
  39.   return message;
  40.  
  41. }
  42.  
  43. char* getStringFromState(StateType state)
  44. {
  45.   char* message = (char*)malloc(sizeof(char)*MAX_SIZE_STATE_STRING);
  46.  
  47.   switch(state)
  48.   {
  49.   case Off  : strcpy(message, "Off");
  50.     break;
  51.   case Stopped  : strcpy(message, "Stopped");
  52.     break;
  53.   case Running  : strcpy(message, "Running");
  54.     break;
  55.   case Walking  : strcpy(message, "Walking");
  56.     break;
  57.   case Error  : strcpy(message, "Error");
  58.     break;
  59.   }
  60.   return message;
  61. }
  62.  
  63.  
  64. typedef struct FSM
  65. {
  66.   StateType currentState;
  67.  
  68.  
  69.  
  70. }  FSMType;
  71.  
  72.  
  73.  
  74. /*This function has to be called to setup or reset the state machine*/
  75. void initStateMachine(FSMType* fsm)
  76. {
  77.   fsm->currentState = Off;
  78. }
  79.  
  80. /*Manages Off state*/
  81. StateType   nextStateOff(StimuliType stimulus)
  82. {
  83.   switch(stimulus)
  84.   {
  85.   case TurnOn: return Stopped;
  86.     break;
  87.   default:
  88.     return Off;
  89.   }
  90. }
  91.  
  92. /*Manages Stopped state*/
  93. StateType   nextStateStoppped(StimuliType stimulus)
  94. {
  95.   switch(stimulus)
  96.   {
  97.   case TurnOff: return Off;
  98.     break;
  99.   case SpeedDown: return Error;
  100.     break;
  101.   case SpeedUp: return Walking;
  102.     break;
  103.     default:
  104.     return Stopped;
  105.   }
  106. }
  107. /*Manages Walking state*/
  108. StateType   nextStateWalking(StimuliType stimulus)
  109. {
  110.   switch(stimulus)
  111.   {
  112.   case SpeedDown: return Stopped;
  113.     break;
  114.   case SpeedUp: return Running;
  115.     break;
  116.   case TurnOff: return Off;
  117.     break;
  118.   default:
  119.     return Walking;
  120.   }
  121. }
  122. /*Manages Running state*/
  123. StateType   nextStateRunning(StimuliType stimulus)
  124. {
  125.   switch(stimulus)
  126.   {
  127.   case SpeedUp: return Error;
  128.     break;
  129.   case SpeedDown: return Walking;
  130.     break;
  131.   case TurnOff: return Off;
  132.     break;
  133.   default:
  134.     return Running;
  135.   }
  136. }
  137.  
  138. /*Manages Running state*/
  139. StateType   nextStateError(StimuliType stimulus)
  140. {
  141.   switch(stimulus)
  142.   {
  143.   case TurnOff: return Off;
  144.     break;
  145.   default:
  146.     return Error;
  147.   }
  148. }
  149.  
  150. /*Main function of the State Machine*/
  151. void nextState(FSMType* fsm, StimuliType stimulus)
  152. {
  153.   switch (fsm->currentState)
  154.   {
  155.   case Off:
  156.     fsm->currentState = nextStateOff(stimulus);
  157.     break;
  158.   case Stopped:
  159.     fsm->currentState = nextStateStoppped(stimulus);
  160.     break;
  161.   case Walking:
  162.     fsm->currentState = nextStateWalking(stimulus);
  163.     break;
  164.   case Running:
  165.     fsm->currentState = nextStateRunning(stimulus);
  166.     break;
  167.  
  168.   case Error:
  169.     fsm->currentState = nextStateError(stimulus);
  170.     break;
  171.   }
  172.  
  173. }
  174. void stimulate(FSMType* fsm, StimuliType stimulus)
  175. {
  176.   printf("\n");
  177.   printf("Previous State: ");
  178.   printf( getStringFromState(fsm->currentState));
  179.  
  180.   printf(" -- ");
  181.  
  182.   nextState(fsm, stimulus);
  183.  
  184.   printf("Stimulus : ");
  185.  
  186.   printf( getStringFromStimulus(stimulus));
  187.  
  188.   printf(" --> ");
  189.   printf("Current State: ");
  190.   printf( getStringFromState(fsm->currentState));
  191.  
  192.   printf("\n");
  193. }
  194.  
  195. void Test1(const char* tittle)
  196. {
  197.   FSMType *fsm;
  198.   printf("\n");
  199.   printf(tittle);
  200.   printf("\n");
  201.  
  202.  
  203.  
  204.   fsm = (FSMType*)malloc(sizeof(FSMType));
  205.  
  206.   initStateMachine(fsm);
  207.  
  208.   stimulate(fsm, TurnOn);
  209.   stimulate(fsm, SpeedUp);
  210.   stimulate(fsm, SpeedUp);
  211.   stimulate(fsm, TurnOff);
  212.  
  213.   printf("\n");
  214. }
  215.  
  216. void Test2(const char* tittle)
  217. {
  218.   FSMType* fsm;
  219.  
  220.   printf("\n");
  221.   printf(tittle);
  222.   printf("\n");
  223.  
  224.  
  225.  
  226.   fsm = (FSMType*)malloc(sizeof(FSMType));
  227.  
  228.   initStateMachine(fsm);
  229.  
  230.   stimulate(fsm, TurnOn);
  231.   stimulate(fsm, TurnOff);
  232.  
  233.   printf("\n");
  234. }
  235.  
  236.  
  237. void Test3(const char* tittle)
  238. {
  239.   FSMType* fsm;
  240.   printf("\n");
  241.   printf(tittle);
  242.   printf("\n");
  243.  
  244.  
  245.   fsm = (FSMType*)malloc(sizeof(FSMType));
  246.  
  247.   initStateMachine(fsm);
  248.  
  249.  
  250.   stimulate(fsm, TurnOn);
  251.   stimulate(fsm, SpeedDown);
  252.  
  253.   printf("\n");
  254. }
  255.  
  256. void Test4(const char* tittle)
  257. {
  258.   FSMType* fsm;
  259.   printf("\n");
  260.   printf(tittle);
  261.   printf("\n");
  262.  
  263.  
  264.   fsm  = (FSMType*)malloc(sizeof(FSMType));
  265.   initStateMachine(fsm);
  266.  
  267.  
  268.  
  269.   stimulate(fsm, TurnOn);
  270.   stimulate(fsm, SpeedUp);
  271.   stimulate(fsm, SpeedUp);
  272.   stimulate(fsm, SpeedDown);
  273.   stimulate(fsm, SpeedDown);
  274.   stimulate(fsm, TurnOff);
  275.  
  276.   printf("\n");
  277. }
  278. int main(void)
  279. {
  280.  
  281.   Test1("Test 1");
  282.   Test2("Test 2");
  283.   Test3("Test 3");
  284.   Test4("Test 4");
  285.  
  286.   return 0;
  287. }
Add Comment
Please, Sign In to add comment