mirror of
https://github.com/SemvdH/OBD2-car-display.git
synced 2025-12-15 04:01:04 +00:00
61 lines
1.7 KiB
C
61 lines
1.7 KiB
C
#ifndef STATEMACHINE_H
|
|
#define STATEMACHINE_H
|
|
#ifdef __cplusplus
|
|
extern "C"
|
|
{
|
|
#endif
|
|
|
|
#include "Arduino.h"
|
|
|
|
#define STATE_INIT 0
|
|
#define STATE_CAR_INFO 1
|
|
|
|
#define STATE_AMOUNT 2 /* change when more states are added */
|
|
|
|
/* two displays:
|
|
- initialisation display, shows the state of booting up
|
|
- main display, shows the data from the OBD2 scanner
|
|
|
|
each state has a function that can be called when it enters that state, while it's running and when it exits that state
|
|
Example: https://stackoverflow.com/questions/1371460/state-machines-tutorials
|
|
*/
|
|
|
|
/**
|
|
* @brief State struct. Contains the id of the state, and the functions that are called when the state is entered, running and exited.
|
|
*/
|
|
typedef struct SM_STATE
|
|
{
|
|
int id;
|
|
int next;
|
|
void (*on_enter)();
|
|
void (*on_run)();
|
|
void (*on_exit)();
|
|
} state_t;
|
|
|
|
/**
|
|
* @brief Initialises the state machine. Initializes the statemachine struct.
|
|
*/
|
|
__UINT8_TYPE__ statemachine_init();
|
|
/**
|
|
* @brief Main loop of the state machine. executes the on_run function of the current state.
|
|
*/
|
|
void statemachine_loop();
|
|
|
|
/**
|
|
* @brief Changes the state of the state machine to the next state. Calls the exit function of the current state and the enter function of the next state.
|
|
*/
|
|
void statemachine_next();
|
|
|
|
/**
|
|
* @brief Registers a state in the state machine.
|
|
* @param state The state to add
|
|
* @param index The index of the state in the array. Must be the same as the id of the state.
|
|
* @return 1 if the state was added successfully, 0 if not.
|
|
*/
|
|
__UINT8_TYPE__ statemachine_register_state(state_t *state, int index);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
#endif // !STATEMACHINE_H
|