From 6ffb01b535bbc1f0203bdd62dafa4769515e0f4b Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Thu, 18 Jan 2024 23:29:16 +0100 Subject: [PATCH] tested ELM327 connection and refactored bt driver to include elm327 --- due_obd2/due_obd2.ino | 8 +++--- due_obd2/{obd2_bt.c => obd2_elm327.cpp} | 27 ++++++++++--------- due_obd2/{obd2_bt.h => obd2_elm327.h} | 25 +++++++++-------- .../test_elm327.ino | 16 +++++------ 4 files changed, 41 insertions(+), 35 deletions(-) rename due_obd2/{obd2_bt.c => obd2_elm327.cpp} (58%) rename due_obd2/{obd2_bt.h => obd2_elm327.h} (80%) rename test_elm327.ino => test_elm327/test_elm327.ino (78%) diff --git a/due_obd2/due_obd2.ino b/due_obd2/due_obd2.ino index ec50807..0f612a1 100644 --- a/due_obd2/due_obd2.ino +++ b/due_obd2/due_obd2.ino @@ -14,7 +14,7 @@ Program to create a car monitor display using: #include "obd2_display.h" #include "statemachine.h" #include "obd2_util.h" -#include "obd2_bt.h" +#include "obd2_elm327.h" #define DEBUG 1 // #define RTT_MR 0x400E1A30U @@ -86,7 +86,7 @@ char device_label_i = -1; /* counter in the current device label */ char bt_state = BT_INITIALISING; -obd2_bt_t bt; +obd2_elm327_t elm327; void on_init_enter(); void on_init_run(); @@ -191,7 +191,7 @@ void on_init_run() display.print("Bluetooth ", (display.getDisplayXSize() / 2) - (11 * display.getFontXsize()), initialization_y + 50); char current_bt_state[BT_STATE_LENGTH]; - obd2_bt_get_state(&bt, (char *)current_bt_state); + obd2_elm327_get_state(&elm327, (char *)current_bt_state); display.print(current_bt_state, (display.getDisplayXSize() / 2) + display.getFontXsize(), initialization_y + 50); display.setBackColor(VGA_BLACK); @@ -437,7 +437,7 @@ void setup() /* Serial1 is bluetooth module, pin 18 and 19. */ Serial1.begin(BT_BAUD); - obd2_bt_init(&bt); + obd2_elm327_init(&elm327); } void loop() diff --git a/due_obd2/obd2_bt.c b/due_obd2/obd2_elm327.cpp similarity index 58% rename from due_obd2/obd2_bt.c rename to due_obd2/obd2_elm327.cpp index 5cd0184..51ac0e1 100644 --- a/due_obd2/obd2_bt.c +++ b/due_obd2/obd2_elm327.cpp @@ -1,13 +1,16 @@ #include +#include "ELMduino.h" -#include "obd2_bt.h" +#include "obd2_elm327.h" -void obd2_bt_init(obd2_bt_t *bt) +ELM327 myELM327; + +void obd2_elm327_init(obd2_elm327_t *elm327) { - bt->state = BT_INITIALISING; + elm327->bt_state = BT_INITIALISING; } -void obd2_bt_get_state(obd2_bt_t *bt, char *state) +void obd2_elm327_get_state(obd2_elm327_t *elm327, char *state) { // char bt_states[3][13] = {"Initializing", "Connecting ", "Connected "}; // char bt_states; @@ -18,7 +21,7 @@ void obd2_bt_get_state(obd2_bt_t *bt, char *state) /* result string must be of size 13 */ return; } - switch (bt->state) + switch (elm327->bt_state) { case BT_INITIALISING: strcpy(state, "Initializing"); @@ -35,25 +38,25 @@ void obd2_bt_get_state(obd2_bt_t *bt, char *state) } } -void obd2_bt_process(obd2_bt_t *bt) +void obd2_elm327_process(obd2_elm327_t *elm327) { - if (bt->state != BT_CONNECTED) + if (elm327->bt_state != BT_CONNECTED) { __UINT32_TYPE__ bt_state = digitalRead(BT_STATE_PIN); - if (bt_state != bt->state) + if (bt_state != elm327->bt_state) { if (bt_state) { - bt->state = BT_CONNECTED; + elm327->bt_state = BT_CONNECTED; } else { - bt->state = BT_INITIALISING; + elm327->bt_state = BT_INITIALISING; } - bt->on_state_change(); + elm327->on_state_change(); } } - /* TODO read from Serial1 to read OBD2 data */ + /* TODO read from Serial2 to read OBD2 data */ } \ No newline at end of file diff --git a/due_obd2/obd2_bt.h b/due_obd2/obd2_elm327.h similarity index 80% rename from due_obd2/obd2_bt.h rename to due_obd2/obd2_elm327.h index 3c1e19c..4f4b957 100644 --- a/due_obd2/obd2_bt.h +++ b/due_obd2/obd2_elm327.h @@ -26,8 +26,8 @@ AT Commands to connect permanently to the OBD2 module: Be sure to disconnect the TX and RX pins from the BT module when programming the arduino. */ -#ifndef OBD2_BT_H -#define OBD2_BT_H +#ifndef OBD2_ELM327_H +#define OBD2_ELM327_H #ifdef __cplusplus extern "C" { @@ -43,9 +43,12 @@ Be sure to disconnect the TX and RX pins from the BT module when programming the #define BT_STATE_PIN 3 /* state connection pin from bluetooth module -> level high on connected */ #define BT_BAUD 115200 +#define ELM327_BAUD 115200 #define BT_STATE_LENGTH 13 /* length of the string identifier used for the states */ +#define ELM327_SERIAL Serial2 + enum BluetoothState { BT_INITIALISING = 0, @@ -59,36 +62,36 @@ enum BluetoothState * @param state the current state of the bluetooth module * @param on_state_change function pointer to the function to be called when the state changes */ -typedef struct obt2_bt_tag +typedef struct obt2_elm327_tag { - enum BluetoothState state; + enum BluetoothState bt_state; void (*on_state_change)(); -} obd2_bt_t; +} obd2_elm327_t; /** * @brief initializes the bluetooth module driver * - * @param bt pointer to struct holding bluetooth driver data + * @param elm327 pointer to struct holding bluetooth driver data */ -void obd2_bt_init(obd2_bt_t *bt); +void obd2_elm327_init(obd2_elm327_t *elm327); /** * @brief processes the bluetooth module communication with the OBD2 reader * * @param bt pointer to struct holding bluetooth driver data */ -void obd2_bt_process(obd2_bt_t *bt); +void obd2_elm327_process(obd2_elm327_t *elm327); /** * @brief copies the current state of the bluetooth module to the state string * - * @param bt pointer to struct holding bluetooth driver data + * @param elm327 pointer to struct holding bluetooth driver data * @param state resulting string to hold the text representation of the state */ -void obd2_bt_get_state(obd2_bt_t *bt, char *state); +void obd2_elm327_get_state(obd2_elm327_t *elm327, char *state); #ifdef __cplusplus } #endif -#endif // OBD2_BT_H \ No newline at end of file +#endif // OBD2_ELM327_H \ No newline at end of file diff --git a/test_elm327.ino b/test_elm327/test_elm327.ino similarity index 78% rename from test_elm327.ino rename to test_elm327/test_elm327.ino index 2ff3326..8f5ba3e 100644 --- a/test_elm327.ino +++ b/test_elm327/test_elm327.ino @@ -21,14 +21,14 @@ void setup() ELM_PORT.begin(115200); - int bt_state = digitalRead(11); - while (bt_state == 0) - { - bt_state = digitalRead(11); - Serial.println(bt_state); - Serial.println("bt not connected"); - delay(100); - } + // int bt_state = digitalRead(11); + // while (bt_state == 0) + // { + // bt_state = digitalRead(11); + // Serial.println(bt_state); + // Serial.println("bt not connected"); + // delay(500); + // } Serial.println("bt connected"); Serial.println("Attempting to connect to ELM327...");