add start of state machine

This commit is contained in:
Sem van der Hoeven
2023-10-28 17:47:45 +02:00
parent 37248ea771
commit 892ea7d725
4 changed files with 84 additions and 0 deletions

5
.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,5 @@
{
"files.associations": {
"statemachine.h": "c"
}
}

View File

@@ -12,6 +12,7 @@ Program to create a car monitor display using:
#include "obd2_display.h"
#include "obd2_timer.h"
#include "statemachine.h"
#define DEBUG 1
// #define RTT_MR 0x400E1A30U

19
due_obd2/statemachine.c Normal file
View File

@@ -0,0 +1,19 @@
#include "statemachine.h"
state_t none_state =
{
.id = 0,
.on_enter = &__state_none,
.on_run = &__state_none,
.on_exit = &__state_none,
}
statemachine_t statemachine =
{
.current_state =
};
void statemachine_init()
{
}

59
due_obd2/statemachine.h Normal file
View File

@@ -0,0 +1,59 @@
#ifndef STATEMACHINE_H
#define STATEMACHINE_H
#ifdef __cplusplus
extern "C" {
#endif
#include "Arduino.h"
/* 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;
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*/
}
#ifdef __cplusplus
}
#endif
#endif // !STATEMACHINE_H