@@ -15,18 +15,20 @@ typedef struct {
15
15
/// when the state transitions into this state.
16
16
/// @warning This has to stay in sync with the state_t enum!
17
17
static stateFunctionRow_t stateFunctionA [] = {
18
+ // NAME // FUNC
18
19
{ "ST_INIT" , & Led_Init }, // ST_INIT
19
20
{ "ST_IDLE" , & Led_Idle }, // ST_IDLE
20
21
{ "ST_LED_ON" , & Led_On }, // ST_LED_ON
21
22
{ "ST_LED_OFF" , & Led_Off }, // ST_LED_OFF
22
23
};
23
24
24
-
25
+ /// @brief All the possible events that can occur for this state machine.
26
+ /// @details Unlike states_t, these do not need to be kept in a special order.
25
27
typedef enum {
26
28
EV_ANY ,
29
+ EV_NONE ,
27
30
EV_BUTTON_PUSHED ,
28
31
EV_TIME_OUT ,
29
- EV_NONE
30
32
} event_t ;
31
33
32
34
typedef struct {
@@ -36,7 +38,7 @@ typedef struct {
36
38
} stateTransMatrixRow_t ;
37
39
38
40
static stateTransMatrixRow_t stateTransMatrix [] = {
39
- // CURR STATE // EVENT // NEXT STATE
41
+ // CURR STATE // EVENT // NEXT STATE
40
42
{ ST_INIT , EV_ANY , ST_IDLE },
41
43
{ ST_IDLE , EV_BUTTON_PUSHED , ST_LED_ON },
42
44
{ ST_LED_ON , EV_TIME_OUT , ST_LED_OFF },
@@ -53,19 +55,21 @@ void StateMachine_Init(stateMachine_t * stateMachine) {
53
55
}
54
56
55
57
event_t StateMachine_GetEvent () {
56
- printf ("StateMachine_GetEvent() called.\r\n" );
57
-
58
- if (buttonPushed )
58
+ if (buttonPushed ) {
59
+ buttonPushed = false;
59
60
return EV_BUTTON_PUSHED ;
61
+ }
60
62
61
63
// No event
62
64
return EV_NONE ;
63
65
}
64
66
65
- void StateMachine_Run (stateMachine_t * stateMachine ) {
66
- // Run the main state machine (handles everything else)
67
+ void StateMachine_RunIteration (stateMachine_t * stateMachine ) {
68
+ // Get an event
67
69
event = StateMachine_GetEvent ();
68
70
71
+ // Iterate through the state transition matrix, checking if there is both a match with the current state
72
+ // and the event
69
73
for (int i = 0 ; i < sizeof (stateTransMatrix )/sizeof (stateTransMatrix [0 ]); i ++ ) {
70
74
if (stateTransMatrix [i ].currState == stateMachine -> currState ) {
71
75
if ((stateTransMatrix [i ].event == event ) || (stateTransMatrix [i ].event == EV_ANY )) {
0 commit comments