add waiting for ELM327 before init state

This commit is contained in:
Sem van der Hoeven
2024-01-21 13:59:49 +01:00
parent 3f19ab4b7d
commit 5fbafa3dc9
5 changed files with 29 additions and 10 deletions

View File

@@ -30,6 +30,7 @@ Program to create a car monitor display using:
#define FLAG_DEVICE_LABEL_UPDATE_POS 0x04 #define FLAG_DEVICE_LABEL_UPDATE_POS 0x04
#define FLAG_DEVICE_LABEL_SHOULD_UPDATE_POS 0x05 /* 0 if label should be updated, 1 if valyue should be updated */ #define FLAG_DEVICE_LABEL_SHOULD_UPDATE_POS 0x05 /* 0 if label should be updated, 1 if valyue should be updated */
#define FLAG_DEVICE_LABEL_UPDATE_KB_POS 0x06 /* count KB up */ #define FLAG_DEVICE_LABEL_UPDATE_KB_POS 0x06 /* count KB up */
#define FLAG_INIT_ELM327_DONE_POS 0x07 /* elm327 done initializing */
#define LOGO_MIN_STEP 20 #define LOGO_MIN_STEP 20
#define LOGO_MAX_POS_Y 124 #define LOGO_MAX_POS_Y 124
@@ -100,7 +101,7 @@ void __state_none()
state_t init_state = state_t init_state =
{ {
.id = STATE_INIT, .id = STATE_INIT,
.next = STATE_INIT, .next = STATE_CAR_INFO,
.on_enter = &on_init_enter, .on_enter = &on_init_enter,
.on_run = &on_init_run, .on_run = &on_init_run,
.on_exit = &on_init_exit}; .on_exit = &on_init_exit};
@@ -123,6 +124,9 @@ UTFT display(ILI9486, 38, 39, 40, 41);
void on_init_enter() void on_init_enter()
{ {
#if (DEBUG == 1)
Serial.println("Entering init state!");
#endif
display.clrScr(); display.clrScr();
Timer0.attachInterrupt(update_init_text); Timer0.attachInterrupt(update_init_text);
@@ -138,9 +142,6 @@ void on_init_enter()
Timer3.attachInterrupt(update_ram_kb); Timer3.attachInterrupt(update_ram_kb);
logo_flag |= (1 << FLAG_LOGO_UPDATE); logo_flag |= (1 << FLAG_LOGO_UPDATE);
#if (DEBUG == 1)
Serial.println("Entering init state!");
#endif
} }
void on_init_run() void on_init_run()
@@ -435,13 +436,21 @@ void setup()
statemachine_register_state(&main_state, STATE_CAR_INFO); statemachine_register_state(&main_state, STATE_CAR_INFO);
statemachine_init(); statemachine_init();
display.clrScr();
display.setFont(BigFont); display.setFont(BigFont);
display.setColor(VGA_AQUA); display.setColor(VGA_AQUA);
display.print("Welcome Sem",(display.getDisplayXSize()/2) - ((11 * display.getFontXsize())/2),120); display.print("Welcome Sem", (display.getDisplayXSize() / 2) - ((11 * display.getFontXsize()) / 2), 120);
display.setColor(VGA_FUCHSIA); display.setColor(VGA_FUCHSIA);
display.print("Just a moment...",(display.getDisplayXSize()/2) - ((16 * display.getFontXsize())/2),140); display.print("Just a moment...", (display.getDisplayXSize() / 2) - ((16 * display.getFontXsize()) / 2), 140);
display.setColor(COLOR_ORANGE);
display.print("Waiting for ELM327", (display.getDisplayXSize() / 2) - ((18 * display.getFontXsize()) / 2), 160);
obd2_elm327_init(&elm327); obd2_elm327_init(&elm327);
#if (DEBUG == 1)
Serial.println("done with elm327 init");
Serial.println(elm327.elm327->connected);
#endif
display.clrScr(); display.clrScr();
@@ -452,7 +461,6 @@ void loop()
{ {
statemachine_loop(); statemachine_loop();
// Serial.println("Running");
// delay(10); // delay(10);
// while (Serial1.available()) // while (Serial1.available())
// { // {

View File

@@ -1,5 +1,5 @@
#include <string.h> #include <string.h>
#include "ELMduino.h"
#include "obd2_elm327.h" #include "obd2_elm327.h"
@@ -13,6 +13,7 @@ char obd2_elm327_init(obd2_elm327_t *elm327)
ELM327_SERIAL.begin(ELM327_BAUD); ELM327_SERIAL.begin(ELM327_BAUD);
elm327->bt_state = BT_INITIALISING; elm327->bt_state = BT_INITIALISING;
elm327->elm327 = &elm327_obj;
if (!elm327_obj.begin(ELM327_SERIAL, false, 5000)) if (!elm327_obj.begin(ELM327_SERIAL, false, 5000))
{ {

View File

@@ -33,6 +33,8 @@ Be sure to disconnect the TX and RX pins from the BT module when programming the
#ifndef OBD2_ELM327_H #ifndef OBD2_ELM327_H
#define OBD2_ELM327_H #define OBD2_ELM327_H
#include "ELMduino.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@@ -68,6 +70,7 @@ enum BluetoothState
*/ */
typedef struct obt2_elm327_tag typedef struct obt2_elm327_tag
{ {
ELM327 *elm327;
enum BluetoothState bt_state; enum BluetoothState bt_state;
void (*on_state_change)(); void (*on_state_change)();
} obd2_elm327_t; } obd2_elm327_t;

View File

@@ -3,6 +3,7 @@
int current_state_id = 0; int current_state_id = 0;
state_t states[STATE_AMOUNT] = {NULL, NULL}; state_t states[STATE_AMOUNT] = {NULL, NULL};
__UINT8_TYPE__ next = 0; __UINT8_TYPE__ next = 0;
static char has_init = 0;
__UINT8_TYPE__ statemachine_register_state(state_t *state, int index) __UINT8_TYPE__ statemachine_register_state(state_t *state, int index)
{ {
@@ -28,11 +29,17 @@ __UINT8_TYPE__ statemachine_init()
} }
} }
current_state_id = 0; current_state_id = 0;
states[current_state_id].on_enter();
} }
void statemachine_loop() void statemachine_loop()
{ {
if (has_init == 0)
{
has_init = 1;
states[current_state_id].on_enter();
}
if (states[current_state_id].next == current_state_id) if (states[current_state_id].next == current_state_id)
{ {
next = 0; next = 0;

View File

@@ -10,7 +10,7 @@ extern "C"
#define STATE_INIT 0 #define STATE_INIT 0
#define STATE_CAR_INFO 1 #define STATE_CAR_INFO 1
#define STATE_AMOUNT 2 /* change when more states are added */ #define STATE_AMOUNT 3 /* change when more states are added */
/* two displays: /* two displays:
- initialisation display, shows the state of booting up - initialisation display, shows the state of booting up