Created
February 5, 2019 20:50
-
-
Save nvandoorn/5b6bd65cde77ea4eeb1d316f7a8687d0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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