Skip to content

Instantly share code, notes, and snippets.

@nvandoorn
Created February 5, 2019 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvandoorn/5b6bd65cde77ea4eeb1d316f7a8687d0 to your computer and use it in GitHub Desktop.
Save nvandoorn/5b6bd65cde77ea4eeb1d316f7a8687d0 to your computer and use it in GitHub Desktop.
#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