diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..cb89910 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "statemachine.h": "c" + } +} \ No newline at end of file diff --git a/due_obd2/due_obd2.ino b/due_obd2/due_obd2.ino index 37f27c0..d838457 100644 --- a/due_obd2/due_obd2.ino +++ b/due_obd2/due_obd2.ino @@ -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 diff --git a/due_obd2/statemachine.c b/due_obd2/statemachine.c new file mode 100644 index 0000000..86bbb62 --- /dev/null +++ b/due_obd2/statemachine.c @@ -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() +{ + +} \ No newline at end of file diff --git a/due_obd2/statemachine.h b/due_obd2/statemachine.h new file mode 100644 index 0000000..57e2ab4 --- /dev/null +++ b/due_obd2/statemachine.h @@ -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