Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- const unsigned int MAX_SIZE_STATE_STRING = 100;
- typedef enum Stimuli
- {
- TurnOn,
- TurnOff,
- SpeedUp,
- SpeedDown
- } StimuliType;
- typedef enum State
- {
- Off,
- Stopped,
- Walking,
- Running,
- Error
- } StateType;
- char* getStringFromStimulus(StimuliType stimulus)
- {
- char* message = (char*)malloc(sizeof(char)*MAX_SIZE_STATE_STRING);
- switch(stimulus)
- {
- case TurnOff: strcpy(message, "TurnOff");
- break;
- case TurnOn: strcpy(message, "TurnOn");
- break;
- case SpeedUp: strcpy(message, "SpeedUp");
- break;
- case SpeedDown: strcpy(message, "SpeedDown");
- break;
- }
- return message;
- }
- char* getStringFromState(StateType state)
- {
- char* message = (char*)malloc(sizeof(char)*MAX_SIZE_STATE_STRING);
- switch(state)
- {
- case Off : strcpy(message, "Off");
- break;
- case Stopped : strcpy(message, "Stopped");
- break;
- case Running : strcpy(message, "Running");
- break;
- case Walking : strcpy(message, "Walking");
- break;
- case Error : strcpy(message, "Error");
- break;
- }
- return message;
- }
- typedef struct FSM
- {
- StateType currentState;
- } FSMType;
- /*This function has to be called to setup or reset the state machine*/
- void initStateMachine(FSMType* fsm)
- {
- fsm->currentState = Off;
- }
- /*Manages Off state*/
- StateType nextStateOff(StimuliType stimulus)
- {
- switch(stimulus)
- {
- case TurnOn: return Stopped;
- break;
- default:
- return Off;
- }
- }
- /*Manages Stopped state*/
- StateType nextStateStoppped(StimuliType stimulus)
- {
- switch(stimulus)
- {
- case TurnOff: return Off;
- break;
- case SpeedDown: return Error;
- break;
- case SpeedUp: return Walking;
- break;
- default:
- return Stopped;
- }
- }
- /*Manages Walking state*/
- StateType nextStateWalking(StimuliType stimulus)
- {
- switch(stimulus)
- {
- case SpeedDown: return Stopped;
- break;
- case SpeedUp: return Running;
- break;
- case TurnOff: return Off;
- break;
- default:
- return Walking;
- }
- }
- /*Manages Running state*/
- StateType nextStateRunning(StimuliType stimulus)
- {
- switch(stimulus)
- {
- case SpeedUp: return Error;
- break;
- case SpeedDown: return Walking;
- break;
- case TurnOff: return Off;
- break;
- default:
- return Running;
- }
- }
- /*Manages Running state*/
- StateType nextStateError(StimuliType stimulus)
- {
- switch(stimulus)
- {
- case TurnOff: return Off;
- break;
- default:
- return Error;
- }
- }
- /*Main function of the State Machine*/
- void nextState(FSMType* fsm, StimuliType stimulus)
- {
- switch (fsm->currentState)
- {
- case Off:
- fsm->currentState = nextStateOff(stimulus);
- break;
- case Stopped:
- fsm->currentState = nextStateStoppped(stimulus);
- break;
- case Walking:
- fsm->currentState = nextStateWalking(stimulus);
- break;
- case Running:
- fsm->currentState = nextStateRunning(stimulus);
- break;
- case Error:
- fsm->currentState = nextStateError(stimulus);
- break;
- }
- }
- void stimulate(FSMType* fsm, StimuliType stimulus)
- {
- printf("\n");
- printf("Previous State: ");
- printf( getStringFromState(fsm->currentState));
- printf(" -- ");
- nextState(fsm, stimulus);
- printf("Stimulus : ");
- printf( getStringFromStimulus(stimulus));
- printf(" --> ");
- printf("Current State: ");
- printf( getStringFromState(fsm->currentState));
- printf("\n");
- }
- void Test1(const char* tittle)
- {
- FSMType *fsm;
- printf("\n");
- printf(tittle);
- printf("\n");
- fsm = (FSMType*)malloc(sizeof(FSMType));
- initStateMachine(fsm);
- stimulate(fsm, TurnOn);
- stimulate(fsm, SpeedUp);
- stimulate(fsm, SpeedUp);
- stimulate(fsm, TurnOff);
- printf("\n");
- }
- void Test2(const char* tittle)
- {
- FSMType* fsm;
- printf("\n");
- printf(tittle);
- printf("\n");
- fsm = (FSMType*)malloc(sizeof(FSMType));
- initStateMachine(fsm);
- stimulate(fsm, TurnOn);
- stimulate(fsm, TurnOff);
- printf("\n");
- }
- void Test3(const char* tittle)
- {
- FSMType* fsm;
- printf("\n");
- printf(tittle);
- printf("\n");
- fsm = (FSMType*)malloc(sizeof(FSMType));
- initStateMachine(fsm);
- stimulate(fsm, TurnOn);
- stimulate(fsm, SpeedDown);
- printf("\n");
- }
- void Test4(const char* tittle)
- {
- FSMType* fsm;
- printf("\n");
- printf(tittle);
- printf("\n");
- fsm = (FSMType*)malloc(sizeof(FSMType));
- initStateMachine(fsm);
- stimulate(fsm, TurnOn);
- stimulate(fsm, SpeedUp);
- stimulate(fsm, SpeedUp);
- stimulate(fsm, SpeedDown);
- stimulate(fsm, SpeedDown);
- stimulate(fsm, TurnOff);
- printf("\n");
- }
- int main(void)
- {
- Test1("Test 1");
- Test2("Test 2");
- Test3("Test 3");
- Test4("Test 4");
- return 0;
- }
Add Comment
Please, Sign In to add comment