-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathStateMachine.c
More file actions
76 lines (63 loc) · 2.4 KB
/
StateMachine.c
File metadata and controls
76 lines (63 loc) · 2.4 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
///
/// \file StateMachine.c
/// \author Geoffrey Hunter (www.mbedded.ninja) <gbmhunter@gmail.com>
/// \edited n/a
/// \created 2017-04-15
/// \last-modified 2018-03-19
/// \brief Contains the StateMachine module.
/// \details
/// See README.md in root dir for more info.
// System includes
#include <stdio.h>
// Local includes
#include "Led.h"
#include "main.h"
#include "StateMachine.h"
typedef struct {
const char * name;
void (*func)(void);
} stateFunctionRow_t;
/// \brief Maps a state to it's state transition function, which should be called
/// when the state transitions into this state.
/// \warning This has to stay in sync with the state_t enum!
static stateFunctionRow_t stateFunctionA[] = {
// NAME // FUNC
{ "ST_IDLE", &Led_Idle }, // ST_IDLE
{ "ST_LED_ON", &Led_On }, // ST_LED_ON
{ "ST_LED_OFF", &Led_Off }, // ST_LED_OFF
};
typedef struct {
state_t currState;
event_t event;
state_t nextState;
} stateTransMatrixRow_t;
static stateTransMatrixRow_t stateTransMatrix[] = {
// CURR STATE // EVENT // NEXT STATE
{ ST_IDLE, EV_BUTTON_PUSHED, ST_LED_ON },
{ ST_LED_ON, EV_TIME_OUT, ST_LED_OFF },
{ ST_LED_ON, EV_BUTTON_PUSHED, ST_IDLE },
{ ST_LED_OFF, EV_TIME_OUT, ST_LED_ON },
{ ST_LED_OFF, EV_BUTTON_PUSHED, ST_IDLE }
};
void StateMachine_Init(stateMachine_t * stateMachine) {
printf("Initialising state machine.\r\n");
stateMachine->currState = ST_IDLE;
}
void StateMachine_RunIteration(stateMachine_t *stateMachine, event_t event) {
// Iterate through the state transition matrix, checking if there is both a match with the current state
// and the event
for(int i = 0; i < sizeof(stateTransMatrix)/sizeof(stateTransMatrix[0]); i++) {
if(stateTransMatrix[i].currState == stateMachine->currState) {
if((stateTransMatrix[i].event == event) || (stateTransMatrix[i].event == EV_ANY)) {
// Transition to the next state
stateMachine->currState = stateTransMatrix[i].nextState;
// Call the function associated with transition
(stateFunctionA[stateMachine->currState].func)();
break;
}
}
}
}
const char * StateMachine_GetStateName(state_t state) {
return stateFunctionA[state].name;
}