tested ELM327 connection and refactored bt driver to include elm327

This commit is contained in:
Sem van der Hoeven
2024-01-18 23:29:16 +01:00
parent 249f22c053
commit 6ffb01b535
4 changed files with 41 additions and 35 deletions

View File

@@ -14,7 +14,7 @@ Program to create a car monitor display using:
#include "obd2_display.h" #include "obd2_display.h"
#include "statemachine.h" #include "statemachine.h"
#include "obd2_util.h" #include "obd2_util.h"
#include "obd2_bt.h" #include "obd2_elm327.h"
#define DEBUG 1 #define DEBUG 1
// #define RTT_MR 0x400E1A30U // #define RTT_MR 0x400E1A30U
@@ -86,7 +86,7 @@ char device_label_i = -1; /* counter in the current device label */
char bt_state = BT_INITIALISING; char bt_state = BT_INITIALISING;
obd2_bt_t bt; obd2_elm327_t elm327;
void on_init_enter(); void on_init_enter();
void on_init_run(); void on_init_run();
@@ -191,7 +191,7 @@ void on_init_run()
display.print("Bluetooth ", (display.getDisplayXSize() / 2) - (11 * display.getFontXsize()), initialization_y + 50); display.print("Bluetooth ", (display.getDisplayXSize() / 2) - (11 * display.getFontXsize()), initialization_y + 50);
char current_bt_state[BT_STATE_LENGTH]; 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.print(current_bt_state, (display.getDisplayXSize() / 2) + display.getFontXsize(), initialization_y + 50);
display.setBackColor(VGA_BLACK); display.setBackColor(VGA_BLACK);
@@ -437,7 +437,7 @@ void setup()
/* Serial1 is bluetooth module, pin 18 and 19. */ /* Serial1 is bluetooth module, pin 18 and 19. */
Serial1.begin(BT_BAUD); Serial1.begin(BT_BAUD);
obd2_bt_init(&bt); obd2_elm327_init(&elm327);
} }
void loop() void loop()

View File

@@ -1,13 +1,16 @@
#include <string.h> #include <string.h>
#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[3][13] = {"Initializing", "Connecting ", "Connected "};
// char bt_states; // 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 */ /* result string must be of size 13 */
return; return;
} }
switch (bt->state) switch (elm327->bt_state)
{ {
case BT_INITIALISING: case BT_INITIALISING:
strcpy(state, "Initializing"); 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); __UINT32_TYPE__ bt_state = digitalRead(BT_STATE_PIN);
if (bt_state != bt->state) if (bt_state != elm327->bt_state)
{ {
if (bt_state) if (bt_state)
{ {
bt->state = BT_CONNECTED; elm327->bt_state = BT_CONNECTED;
} }
else 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 */
} }

View File

@@ -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. Be sure to disconnect the TX and RX pins from the BT module when programming the arduino.
*/ */
#ifndef OBD2_BT_H #ifndef OBD2_ELM327_H
#define OBD2_BT_H #define OBD2_ELM327_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { 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_STATE_PIN 3 /* state connection pin from bluetooth module -> level high on connected */
#define BT_BAUD 115200 #define BT_BAUD 115200
#define ELM327_BAUD 115200
#define BT_STATE_LENGTH 13 /* length of the string identifier used for the states */ #define BT_STATE_LENGTH 13 /* length of the string identifier used for the states */
#define ELM327_SERIAL Serial2
enum BluetoothState enum BluetoothState
{ {
BT_INITIALISING = 0, BT_INITIALISING = 0,
@@ -59,36 +62,36 @@ enum BluetoothState
* @param state the current state of the bluetooth module * @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 * @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)(); void (*on_state_change)();
} obd2_bt_t; } obd2_elm327_t;
/** /**
* @brief initializes the bluetooth module driver * @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 * @brief processes the bluetooth module communication with the OBD2 reader
* *
* @param bt pointer to struct holding bluetooth driver data * @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 * @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 * @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 #ifdef __cplusplus
} }
#endif #endif
#endif // OBD2_BT_H #endif // OBD2_ELM327_H

View File

@@ -21,14 +21,14 @@ void setup()
ELM_PORT.begin(115200); ELM_PORT.begin(115200);
int bt_state = digitalRead(11); // int bt_state = digitalRead(11);
while (bt_state == 0) // while (bt_state == 0)
{ // {
bt_state = digitalRead(11); // bt_state = digitalRead(11);
Serial.println(bt_state); // Serial.println(bt_state);
Serial.println("bt not connected"); // Serial.println("bt not connected");
delay(100); // delay(500);
} // }
Serial.println("bt connected"); Serial.println("bt connected");
Serial.println("Attempting to connect to ELM327..."); Serial.println("Attempting to connect to ELM327...");