Skip to content

Instantly share code, notes, and snippets.

@jetpax
Created November 10, 2016 14:56
Show Gist options
  • Save jetpax/701d65c3270bd2c150447f67c44017a2 to your computer and use it in GitHub Desktop.
Save jetpax/701d65c3270bd2c150447f67c44017a2 to your computer and use it in GitHub Desktop.
Function pointer state machine
typedef void (*FSM)(void);
/*****************************************************
* FSM: test pin
* States */
void smTestPinInit(void);
void smTestPinLo(void);
void smTestPinHi(void);
//FSM Variables
unsigned long test_tmr;
//Initial state
FSM testPinFSM = smTestPinInit;
/******************************************************
State functions */
void smTestPinInit(void) {
DDRD |= 0x80; // ***DEBUG*** toggle test pin
testPinFSM = smTestPinLo; // change state
test_tmr = millis();
}
void smTestPinLo(void) {
if (millis() - test_tmr >= 30){
test_tmr = millis();
PORTD &= ~0x80; // ***DEBUG*** toggle test pin
testPinFSM = smTestPinHi; // change state
}
}
void smTestPinHi(void) {
if (millis() - test_tmr >= 50){
test_tmr = millis();
PORTD |= 0x80; // ***DEBUG*** toggle test pin
testPinFSM = smTestPinLo; // change state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment