Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
#include <stdlib.h>
// this enumeration should dictate
// the main state of your robot,
// however, you may need to store
// additional state info, so
// use the struct below for that purpose
typedef enum { STATE1, STATE2 } RobotStateEnum;
// Store info that may
// need to be passed between states here
typedef struct {
RobotStateEnum stateEnum;
} RobotState;
void state1Handler(RobotState *state) {}
void state2Handler(RobotState *state) {}
void startStateMachine(RobotState *state) {
// continuously map the state enum
// to a handler function, and pass
// it the struct we use to store other state info
while (true) {
switch (state->stateEnum) {
case STATE1:
state1Handler(state);
break;
case STATE2:
state2Handler(state);
break;
}
}
}
int main() {
// set the initial state
RobotState state = {.stateEnum = STATE1}
// pass a pointer to the initial state
startStateMachine(&state);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.