Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #define MAX_NAME_LENGTH 20
- #define STIMULI_AMOUNT 4
- #define STATE_AMOUNT 5
- enum Stimuli
- {
- TurnOn,
- TurnOff,
- SpeedUp,
- SpeedDown
- };
- enum State
- {
- Off,
- Stopped,
- Walking,
- Running,
- Error
- };
- typedef struct Transition transition;
- struct FSM
- {
- enum State curr_state;
- enum State ** transition_logic;
- const char* printable_stimuli[4];
- const char* printable_states[5];
- };
- void stimulate (struct FSM* fsm, enum Stimuli stimulus);
- void print_transition (enum State previous, enum Stimuli stimulus, enum State newState, const char** printable_stimuli, const char** printable_states);
- void initialize (struct FSM* fsm);
- int main (void)
- {
- int input;
- struct FSM the_machine;
- initialize (&the_machine);
- input = 0;
- while (input != 4)
- {
- printf("Input stimulus: 0_On, 1_Off, 2_SpUp, 3_SpDn, 4_Exit \n");
- scanf("%i", &input);
- if ((0 <= input) && (input < 4))
- {
- stimulate (&the_machine, input);
- }
- }
- return EXIT_SUCCESS;
- }
- void stimulate(struct FSM* fsm, enum Stimuli stimulus)
- {
- enum State previous;
- previous = (*fsm).curr_state;
- (*fsm).curr_state = (((*fsm).transition_logic)[previous][stimulus]);
- print_transition (previous, stimulus, (*fsm).curr_state, (*fsm).printable_stimuli, (*fsm).printable_states);
- }
- void print_transition (const enum State previous, const enum Stimuli stimulus, const enum State newState, const char** printable_stimuli, const char** printable_states)
- {
- char prev_state[MAX_NAME_LENGTH];
- char received_stimulus[MAX_NAME_LENGTH];
- char next_state[MAX_NAME_LENGTH];
- strncpy (prev_state, printable_states[previous], MAX_NAME_LENGTH);
- strncpy (received_stimulus, printable_stimuli[stimulus], MAX_NAME_LENGTH);
- strncpy (next_state, printable_states[newState], MAX_NAME_LENGTH);
- printf("[%s]---[%s]--->[%s]\n", prev_state, received_stimulus, next_state);
- }
- void initialize (struct FSM* fsm)
- {
- int i;
- (*fsm).curr_state = Off;
- (*fsm).printable_stimuli[TurnOn] = "TurnOn";
- (*fsm).printable_stimuli[TurnOff] = "TurnOff";
- (*fsm).printable_stimuli[SpeedUp] = "SpeedUp";
- (*fsm).printable_stimuli[SpeedDown] = "SpeedDown";
- (*fsm).printable_states[Off] = "Off";
- (*fsm).printable_states[Stopped] = "Stopped";
- (*fsm).printable_states[Walking] = "Walking";
- (*fsm).printable_states[Running] = "Running";
- (*fsm).printable_states[Error] = "Error";
- (*fsm).transition_logic = (enum State **) calloc (STATE_AMOUNT ,sizeof(enum State *));
- for (i=0; i<STATE_AMOUNT; i++)
- {
- (*fsm).transition_logic[i] = (enum State *) calloc (STIMULI_AMOUNT ,sizeof(enum State));
- }
- /** OFF STATE */
- /* TURNON STIMULUS */
- ((*fsm).transition_logic)[Off][TurnOn] = Stopped;
- /* TURNOFF STIMULUS */
- ((*fsm).transition_logic)[Off][TurnOff] = Off;
- /* SPEEDUP STIMULUS */
- ((*fsm).transition_logic)[Off][SpeedUp] = Error;
- /* SPEEDDOWN STIMULUS */
- ((*fsm).transition_logic)[Off][SpeedDown] = Error;
- /** STOPPED STATE */
- /* TURNON STIMULUS */
- ((*fsm).transition_logic)[Stopped][TurnOn] = Error;
- /* TURNOFF STIMULUS */
- ((*fsm).transition_logic)[Stopped][TurnOff] = Off;
- /* SPEEDUP STIMULUS */
- ((*fsm).transition_logic)[Stopped][SpeedUp] = Walking;
- /* SPEEDDOWN STIMULUS */
- ((*fsm).transition_logic)[Stopped][SpeedDown] = Error;
- /** WALKING STATE */
- /* TURNON STIMULUS */
- ((*fsm).transition_logic)[Walking][TurnOn] = Walking;
- /* TURNOFF STIMULUS */
- ((*fsm).transition_logic)[Walking][TurnOff] = Off;
- /* SPEEDUP STIMULUS */
- ((*fsm).transition_logic)[Walking][SpeedUp] = Running;
- /* SPEEDDOWN STIMULUS */
- ((*fsm).transition_logic)[Walking][SpeedDown] = Stopped;
- /** RUNNING STATE */
- /* TURNON STIMULUS */
- ((*fsm).transition_logic)[Running][TurnOn] = Running;
- /* TURNOFF STIMULUS */
- ((*fsm).transition_logic)[Running][TurnOff] = Off;
- /* SPEEDUP STIMULUS */
- ((*fsm).transition_logic)[Running][SpeedUp] = Error;
- /* SPEEDDOWN STIMULUS */
- ((*fsm).transition_logic)[Running][SpeedDown] = Walking;
- /** ERROR STATE */
- /* TURNON STIMULUS */
- ((*fsm).transition_logic)[Error][TurnOn] = Error;
- /* TURNOFF STIMULUS */
- ((*fsm).transition_logic)[Error][TurnOff] = Off;
- /* SPEEDUP STIMULUS */
- ((*fsm).transition_logic)[Error][SpeedUp] = Error;
- /* SPEEDDOWN STIMULUS */
- ((*fsm).transition_logic)[Error][SpeedDown] = Error;
- }
Add Comment
Please, Sign In to add comment