finished state machine

This commit is contained in:
Sem van der Hoeven
2023-10-30 23:45:38 +01:00
parent 892ea7d725
commit ef4ed9b617
6 changed files with 264 additions and 96 deletions

View File

@@ -1,11 +1,17 @@
#ifndef STATEMACHINE_H
#define STATEMACHINE_H
#ifdef __cplusplus
extern "C" {
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
@@ -16,44 +22,39 @@
/**
* @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 Transition struct. Contains the id of the state it transitions to, and the next state.
*/
typedef struct SM_TRANSITION
{
int state_id;
state_t *next_state;
} transition_t;
/**
* @brief State machine struct. Contains the current state and the transitions.
*/
typedef struct SM_STATE_MACHINE
{
state_t *current_state;
transition_t *transitions; /* Transitions between states. Every state's ID is also it's position in the array.*/
} statemachine_t;
/**
* @brief Initialises the state machine. Initializes the statemachine struct.
*/
void statemachine_init();
void __state_none()
{
/* do nothing*/
}
__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
#endif // !STATEMACHINE_H