-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachine.c
40 lines (38 loc) · 1.35 KB
/
StateMachine.c
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
#include "debug.h"
#include "StateMachine.h"
/*
Run tests to check if we need to transition states. The first positive test will cause a transition. If all tests come back negative the
current state operation will run.
*/
void FSMProcess(struct FSMContext * c) {
(void) c;
int j = 0;
while (c->currentState->tests[j].test) {
// Execute the test function and transition state if it returns true
if (c->currentState->tests[j].test(c)) {
debug("Test \"%s\" returned true.\n", c->currentState->tests[j].name);
FSMTransition(c, c->currentState->tests[j].nextState);
return; // Return. Don't run any more tests or the main operation.
}
j++;
}
// If an operation exists for the current state, run it.
if (c->currentState->op) c->currentState->op(c);
}
void FSMTransition(struct FSMContext * c, const struct FSMState * s) {
if (c && s) { // NULL check
// Call the exit function of the current state, if it exists
if (c->currentState && c->currentState->exit) {
c->currentState->exit(c);
debug ("%s exit function ran.\n", c->currentState->name);
}
// Transistion to the new state
c->currentState = s;
debug ("State transition to %s.\n", c->currentState->name);
// Call the entry function of the new state
if (c->currentState->entry) {
c->currentState->entry(c);
debug ("%s entry function ran.\n", c->currentState->name);
}
}
}