Skip to content

Commit bbcd22b

Browse files
committed
Tidied up code comments, got rid of un-used state enumeration.
1 parent 1e2aea7 commit bbcd22b

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

src/StateMachine.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@ typedef struct {
1515
/// when the state transitions into this state.
1616
/// @warning This has to stay in sync with the state_t enum!
1717
static stateFunctionRow_t stateFunctionA[] = {
18+
// NAME // FUNC
1819
{ "ST_INIT", &Led_Init }, // ST_INIT
1920
{ "ST_IDLE", &Led_Idle }, // ST_IDLE
2021
{ "ST_LED_ON", &Led_On }, // ST_LED_ON
2122
{ "ST_LED_OFF", &Led_Off }, // ST_LED_OFF
2223
};
2324

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.
2527
typedef enum {
2628
EV_ANY,
29+
EV_NONE,
2730
EV_BUTTON_PUSHED,
2831
EV_TIME_OUT,
29-
EV_NONE
3032
} event_t;
3133

3234
typedef struct {
@@ -36,7 +38,7 @@ typedef struct {
3638
} stateTransMatrixRow_t;
3739

3840
static stateTransMatrixRow_t stateTransMatrix[] = {
39-
// CURR STATE // EVENT // NEXT STATE
41+
// CURR STATE // EVENT // NEXT STATE
4042
{ ST_INIT, EV_ANY, ST_IDLE },
4143
{ ST_IDLE, EV_BUTTON_PUSHED, ST_LED_ON },
4244
{ ST_LED_ON, EV_TIME_OUT, ST_LED_OFF },
@@ -53,19 +55,21 @@ void StateMachine_Init(stateMachine_t * stateMachine) {
5355
}
5456

5557
event_t StateMachine_GetEvent() {
56-
printf("StateMachine_GetEvent() called.\r\n");
57-
58-
if(buttonPushed)
58+
if(buttonPushed) {
59+
buttonPushed = false;
5960
return EV_BUTTON_PUSHED;
61+
}
6062

6163
// No event
6264
return EV_NONE;
6365
}
6466

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
6769
event = StateMachine_GetEvent();
6870

71+
// Iterate through the state transition matrix, checking if there is both a match with the current state
72+
// and the event
6973
for(int i = 0; i < sizeof(stateTransMatrix)/sizeof(stateTransMatrix[0]); i++) {
7074
if(stateTransMatrix[i].currState == stateMachine->currState) {
7175
if((stateTransMatrix[i].event == event) || (stateTransMatrix[i].event == EV_ANY)) {

src/StateMachine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ typedef struct {
1313
} stateMachine_t;
1414

1515
void StateMachine_Init(stateMachine_t * stateMachine);
16-
void StateMachine_Run(stateMachine_t * stateMachine);
16+
void StateMachine_RunIteration(stateMachine_t *stateMachine);
1717
const char * StateMachine_GetStateName(state_t state);
1818

1919
#endif

src/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ int main() {
1515
printf("State is now %s.\r\n", StateMachine_GetStateName(stateMachine.currState));
1616

1717
// Run first iteration
18-
StateMachine_Run(&stateMachine);
18+
StateMachine_RunIteration(&stateMachine);
1919
printf("State is now %s.\r\n", StateMachine_GetStateName(stateMachine.currState));
2020

2121
// Let's pretend a button was pushed
2222
buttonPushed = true;
2323
printf("Button pushed.\r\n");
24-
StateMachine_Run(&stateMachine);
24+
StateMachine_RunIteration(&stateMachine);
2525
printf("State is now %s.\r\n", StateMachine_GetStateName(stateMachine.currState));
2626

2727
return 0;

0 commit comments

Comments
 (0)