-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstateMachine.c
More file actions
189 lines (169 loc) · 5.81 KB
/
Copy pathstateMachine.c
File metadata and controls
189 lines (169 loc) · 5.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <stdio.h>
#include <ctype.h> /* Provides isdigit() */
#include <stdlib.h> /* Provides atoi() */
#include <string.h> /* Provides strlen() */
/* Number of rows of the "TRANSITION_TABLE" matrix */
#define STATES_AMOUNT 5
/* Number of columns of the "TRANSITION_TABLE" matrix */
#define STIMULUS_AMOUNT 4
/* Enum that represents states */
enum State {
Off,
Stopped,
Walking,
Running,
Error
};
/* Enum that represents stimulus */
enum Stimulus {
TurnOn,
TurnOff,
SpeedUp,
SpeedDown
};
/* Callback function */
typedef void (*FSMActionFunc)();
/* Structure that represents the FSM */
struct FSM {
enum State nextState;
enum State currentState;
FSMActionFunc actionFunction;
};
/* Function handles */
void handleTurnOn();
void handleTurnOff();
void handleSpeedUp();
void handleSpeedDown();
/* Matrix that contains the transitions */
struct FSM TRANSITION_TABLE[STATES_AMOUNT][STIMULUS_AMOUNT];
/* Array that store state names as strings */
const char *STATE_NAMES[STATES_AMOUNT] = {"Off", "Stopped", "Walking", "Running", "Error"};
const int FINISH = 9;
void initializeFSM(struct FSM *);
void stimulateFSM(struct FSM *, enum Stimulus);
int main(void)
{
struct FSM fsm;
unsigned int i;
int invalidInput;
int finishProgram;
int stimulusToSend;
char userInput[32];
/* Initialize FSM */
initializeFSM(&fsm);
finishProgram = 0;
while(!finishProgram) {
/* Verifies that user input only contains digits */
do {
invalidInput = 0;
fgets(userInput, sizeof(userInput), stdin);
/* The '-1' below is due to '\n' of the end */
for(i = 0; i < strlen(userInput) - 1; ++i) {
if(!isdigit(userInput[i])) {
printf("The input is invalid!\n");
invalidInput = 1;
break;
}
}
} while(invalidInput);
/* Converts the user input into a integer */
stimulusToSend = atoi(userInput);
/* Verifies the output option */
finishProgram = (stimulusToSend == FINISH);
if(stimulusToSend < STIMULUS_AMOUNT) {
stimulateFSM(&fsm, stimulusToSend);
}
else if(finishProgram){
printf("Program ended!\n");
}
else {
printf("The stimulus does not exist!\n");
}
}
return 0;
}
void initializeFSM(struct FSM *fsm)
{
/* Set the intial state */
fsm->currentState = Off;
/* Memset to 'TRANSITION_TABLE' to zero */
memset(TRANSITION_TABLE, 0, sizeof(struct FSM));
/* State_'Off' */
TRANSITION_TABLE[Off][TurnOn].actionFunction = handleTurnOn;
TRANSITION_TABLE[Off][TurnOn].nextState = Stopped;
TRANSITION_TABLE[Off][TurnOff].actionFunction = handleTurnOff;
TRANSITION_TABLE[Off][TurnOff].nextState = Off;
TRANSITION_TABLE[Off][SpeedUp].actionFunction = handleSpeedUp;
TRANSITION_TABLE[Off][SpeedUp].nextState = Off;
TRANSITION_TABLE[Off][SpeedDown].actionFunction = handleSpeedDown;
TRANSITION_TABLE[Off][SpeedDown].nextState = Off;
/* State 'Stopped' */
TRANSITION_TABLE[Stopped][TurnOn].actionFunction = handleTurnOn;
TRANSITION_TABLE[Stopped][TurnOn].nextState = Stopped;
TRANSITION_TABLE[Stopped][TurnOff].actionFunction = handleTurnOff;
TRANSITION_TABLE[Stopped][TurnOff].nextState = Off;
TRANSITION_TABLE[Stopped][SpeedUp].actionFunction = handleSpeedUp;
TRANSITION_TABLE[Stopped][SpeedUp].nextState = Walking;
TRANSITION_TABLE[Stopped][SpeedDown].actionFunction = handleSpeedDown;
TRANSITION_TABLE[Stopped][SpeedDown].nextState = Error;
/* State 'Walking' */
TRANSITION_TABLE[Walking][TurnOn].actionFunction = handleTurnOn;
TRANSITION_TABLE[Walking][TurnOn].nextState = Walking;
TRANSITION_TABLE[Walking][TurnOff].actionFunction = handleTurnOff;
TRANSITION_TABLE[Walking][TurnOff].nextState = Off;
TRANSITION_TABLE[Walking][SpeedUp].actionFunction = handleSpeedUp;
TRANSITION_TABLE[Walking][SpeedUp].nextState = Running;
TRANSITION_TABLE[Walking][SpeedDown].actionFunction = handleSpeedDown;
TRANSITION_TABLE[Walking][SpeedDown].nextState = Stopped;
/* State 'Running' */
TRANSITION_TABLE[Running][TurnOn].actionFunction = handleTurnOn;
TRANSITION_TABLE[Running][TurnOn].nextState = Running;
TRANSITION_TABLE[Running][TurnOff].actionFunction = handleTurnOff;
TRANSITION_TABLE[Running][TurnOff].nextState = Off;
TRANSITION_TABLE[Running][SpeedUp].actionFunction = handleSpeedUp;
TRANSITION_TABLE[Running][SpeedUp].nextState = Error;
TRANSITION_TABLE[Running][SpeedDown].actionFunction = handleSpeedDown;
TRANSITION_TABLE[Running][SpeedDown].nextState = Walking;
/* State 'Error' */
TRANSITION_TABLE[Error][TurnOn].actionFunction = handleTurnOn;
TRANSITION_TABLE[Error][TurnOn].nextState = Error;
TRANSITION_TABLE[Error][TurnOff].actionFunction = handleTurnOff;
TRANSITION_TABLE[Error][TurnOff].nextState = Off;
TRANSITION_TABLE[Error][SpeedUp].actionFunction = handleSpeedUp;
TRANSITION_TABLE[Error][SpeedUp].nextState = Error;
TRANSITION_TABLE[Error][SpeedDown].actionFunction = handleSpeedDown;
TRANSITION_TABLE[Error][SpeedDown].nextState = Error;
}
void handleTurnOn()
{
printf(" ----- <TurnOn> -----> ");
}
void handleTurnOff()
{
printf(" ----- <TurnOff> -----> ");
}
void handleSpeedUp()
{
printf(" ----- <SpeedUp> -----> ");
}
void handleSpeedDown()
{
printf(" ----- <SpeedDown> -----> ");
}
/** @brief Stimulates the machine and advances to the next state.
*
* @param fsm Finite State Machine to stimulate.
* @param stimulus Stimulus to send to the machine.
* @return void Does not return any value.
*/
void stimulateFSM(struct FSM *fsm, enum Stimulus stimulus)
{
/* Prints the current state */
printf("[%s]", STATE_NAMES[fsm->currentState]);
/* Call the respective action function */
TRANSITION_TABLE[fsm->currentState][stimulus].actionFunction();
/* Advances to the next state */
fsm->currentState = TRANSITION_TABLE[fsm->currentState][stimulus].nextState;
/* Prints the new state */
printf("[%s]\n", STATE_NAMES[fsm->currentState]);
}