Created
March 21, 2024 15:32
-
-
Save mcavalcantib/4664c3345e61c3b96929010c724dc57b to your computer and use it in GitHub Desktop.
State machine using function pointers
This file contains hidden or 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> | |
| long stateStart = 0; | |
| // fun_ptr is a pointer to function fun() | |
| void (*UpdateFunction)(void); | |
| void ManualMode(){ | |
| //Acoes de exibir e controlar o modo manual | |
| if(selectButton && option == 2){ //opção 2 cancelar | |
| UpdateFunction = &MainMenu; | |
| } | |
| } | |
| void MainMenu(){ | |
| // funções de exibir as opções na tela | |
| //... | |
| if(selectButton && option == 1){ // opção 1 manual | |
| UpdateFunction = &ManualMode; | |
| } | |
| } | |
| void SplashScreen() | |
| { | |
| if(millis() - stateStart < 3000){ | |
| PrintSplash(); | |
| }else{ | |
| UpdateFunction = &MainMenu; | |
| } | |
| } | |
| int main() | |
| { | |
| //setup | |
| UpdateFunction = &SplashScreen; | |
| //loop | |
| while(1){ | |
| UpdateFunction(); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment