From cf24296f63c59503aaf39c906800ed0eecabeecf Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 12 Nov 2024 00:14:45 +0100 Subject: [PATCH] bars --- .vscode/c_cpp_properties.json | 3 +- due_obd2/bars.cpp | 23 +++++++++++ due_obd2/bars.h | 34 ++++++++++++++++ due_obd2/due_obd2.ino | 54 ++++++++++++++++---------- due_obd2/obd2_elm327.cpp | 73 ++++++++++++++++++++++++++--------- due_obd2/obd2_elm327.h | 21 +++++++++- 6 files changed, 168 insertions(+), 40 deletions(-) create mode 100644 due_obd2/bars.cpp create mode 100644 due_obd2/bars.h diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index e70bc12..2b6d75d 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -4,7 +4,8 @@ "name": "Linux", "includePath": [ "${workspaceFolder}/**", - "/home/sem/.arduino15/packages/arduino/hardware/sam/1.6.12/system/CMSIS/Device/ATMEL/sam3xa/include" + "F:/User Files/Sem/Documents/Arduino/libraries/ELMDuino/src", + "F:/User Files/Sem/Documents/Arduino/libraries/**" ], "defines": [], "compilerPath": "/usr/bin/gcc", diff --git a/due_obd2/bars.cpp b/due_obd2/bars.cpp new file mode 100644 index 0000000..46d9b4e --- /dev/null +++ b/due_obd2/bars.cpp @@ -0,0 +1,23 @@ +#include "bars.h" + +void draw_bar_horizontal(int start_x, int start_y, int width, int height, int value, int max_value, uint color, char fill_outline, UTFT *display) +{ + display->setColor(color); + int bar_width = (int)((float)value / (float)max_value * width); + if (fill_outline) + { + display->drawRect(start_x, start_y, start_x + width, start_y + height); + } + display->fillRect(start_x, start_y, start_x + bar_width, start_y + height); +} + +void draw_bar_vertical(int start_x, int start_y, int width, int height, int value, int max_value, uint color, char fill_outline, UTFT *display) +{ + display->setColor(color); + int bar_height = (int)((float)value / (float)max_value * height); + if (fill_outline) + { + display->drawRect(start_x, start_y, start_x + width, start_y + height); + } + display->fillRect(start_x, (start_y + height) - bar_height, start_x + width, start_y + height); +} \ No newline at end of file diff --git a/due_obd2/bars.h b/due_obd2/bars.h new file mode 100644 index 0000000..fb16a78 --- /dev/null +++ b/due_obd2/bars.h @@ -0,0 +1,34 @@ +#ifndef BARS_H +#define BARS_H + +#include + +/** + * @brief Draws a horizontal bar on the display + * @param start_x The start x position (top left) + * @param start_y The start y position (top left) + * @param width The width of the bar + * @param height The height of the bar + * @param value The value to be displayed + * @param max_value The maximum value of the bar + * @param color The color of the bar + * @param fill_outline Whether to fill the outline of the bar + * @param display The display object + */ +void draw_bar_horizontal(int start_x, int start_y, int width, int height, int value, int max_value, uint color, char fill_outline, UTFT *display); + +/** + * @brief Draws a vertical bar on the display + * @param start_x The start x position (top left) + * @param start_y The start y position (top left) + * @param width The width of the bar + * @param height The height of the bar + * @param value The value to be displayed + * @param max_value The maximum value of the bar + * @param color The color of the bar + * @param fill_outline Whether to fill the outline of the bar + * @param display The display object + */ +void draw_bar_vertical(int start_x, int start_y, int width, int height, int value, int max_value, uint color, char fill_outline, UTFT *display); + +#endif \ No newline at end of file diff --git a/due_obd2/due_obd2.ino b/due_obd2/due_obd2.ino index 38dfc97..9652965 100644 --- a/due_obd2/due_obd2.ino +++ b/due_obd2/due_obd2.ino @@ -4,6 +4,9 @@ Program to create a car monitor display using: - a TFT display (https://www.tinytronics.nl/shop/en/displays/tft/3.5-inch-tft-display-320*480-pixels-mega-compatible-ili9486) - ELM327 Bluetooth OBD2 scanner - FSC-DB004 (BT 836B) bluetooth module + + linux arduino library: + /home/sem/.arduino15/packages/arduino/hardware/sam/1.6.12/system/CMSIS/Device/ATMEL/sam3xa/include */ #include "Arduino.h" @@ -15,6 +18,7 @@ Program to create a car monitor display using: #include "statemachine.h" #include "obd2_util.h" #include "obd2_elm327.h" +#include "bars.h" #define DEBUG 1 // #define RTT_MR 0x400E1A30U @@ -90,6 +94,10 @@ char update_slow = 0; obd2_elm327_t elm327; +int bar_1_x = 0; +int bar_2_x = 10; +int bar_3_y = 50; + void on_init_enter(); void on_init_run(); void on_init_exit(); @@ -107,6 +115,7 @@ state_t init_state = .on_run = &on_init_run, .on_exit = &on_init_exit}; + state_t main_state = { .id = STATE_CAR_INFO, @@ -318,6 +327,7 @@ void on_main_run() if (update_slow) { update_slow = 0; + obd2_elm327_process_slow(&elm327); } else { obd2_elm327_process_fast(&elm327); @@ -507,7 +517,6 @@ void query_slow_obd2_values() } void setup() - { /* TODO change for TRNG (section 42 of datasheet)*/ @@ -567,24 +576,29 @@ void setup() void loop() { - statemachine_loop(); - // delay(10); - // while (Serial1.available()) - // { - // char rec = Serial1.read(); - // #if (DEBUG == 1) - // Serial.println(rec); - // #endif - // } + draw_bar_horizontal(0,0,200,10,bar_1_x,100,VGA_AQUA,1,&display); - // if (should_clear) - // { - // display.clrScr(); - // display.setColor(255, 0, 166); - // display.fillRect(LCD_W / 2 - 200, LCD_H / 2 - 100, LCD_W / 2 + 200, LCD_H / 2 + 100); - // display.setColor(0, 247, 255); - // display.print("OBD2 display yeet", CENTER, LCD_H / 2); - // delay(10); - // should_clear = 0; - // } + draw_bar_horizontal(0,20,200,11,bar_2_x,100,VGA_FUCHSIA,1,&display); + + draw_bar_vertical(0,50,20,100,bar_3_y,100,VGA_BLUE,1,&display); + + bar_1_x++; + if (bar_1_x > 100) + { + bar_1_x = 0; + } + + bar_2_x++; + if (bar_2_x > 100) + { + bar_2_x = 10; + } + bar_3_y++; + if (bar_3_y > 100) + { + bar_3_y = 50; + } + display.clrScr(); + delay(50); + //statemachine_loop(); } diff --git a/due_obd2/obd2_elm327.cpp b/due_obd2/obd2_elm327.cpp index db95525..b734434 100644 --- a/due_obd2/obd2_elm327.cpp +++ b/due_obd2/obd2_elm327.cpp @@ -3,6 +3,8 @@ #include "obd2_elm327.h" +#define DEBUG 1 + #define KM_IN_MILES 1.6093440 #define RPM_MAX 7000 #define THROTTLE_MAX 100 @@ -86,24 +88,14 @@ void obd2_elm327_process_fast(obd2_elm327_t *elm327) { switch (current_value_fast) { - case RPM_V: - { - float rpm = elm327->elm327->rpm(); - if (elm327->elm327->nb_rx_state == ELM_SUCCESS) - { - elm327->rpm = (uint16_t)rpm; - elm327->value_updates |= (1 << UPDATE_RPM_POS); - current_value_fast = ENGINE_LOAD_V; - } - else if (elm327->elm327->nb_rx_state != ELM_GETTING_MSG) - { - // elm327->elm327->printError(); - current_value_fast = ENGINE_LOAD_V; - } - break; - } case ENGINE_LOAD_V: { +#if (DEBUG == 1) + float engine_load = 50; + current_value_fast = FUEL_PRESSURE_V; + elm327->engine_load = (uint8_t)engine_load; + elm327->value_updates |= (1 << UPDATE_ENGINE_LOAD_POS); +#else float engine_load = elm327->elm327->engineLoad(); if (elm327->elm327->nb_rx_state == ELM_SUCCESS) { @@ -116,10 +108,17 @@ void obd2_elm327_process_fast(obd2_elm327_t *elm327) // elm327->elm327->printError(); current_value_fast = FUEL_PRESSURE_V; } +#endif break; } case FUEL_PRESSURE_V: { +#if (DEBUG == 1) + float fuel_pressure = 8; + elm327->fuel_pressure = (uint16_t)fuel_pressure; + elm327->value_updates |= (1 << UPDATE_FUEL_PRESSURE_POS); + current_value_fast = MANIFOLD_PRESSURE_V; +#else float fuel_pressure = elm327->elm327->fuelPressure(); if (elm327->elm327->nb_rx_state == ELM_SUCCESS) { @@ -132,22 +131,30 @@ void obd2_elm327_process_fast(obd2_elm327_t *elm327) // elm327->elm327->printError(); current_value_fast = MANIFOLD_PRESSURE_V; } +#endif break; } case MANIFOLD_PRESSURE_V: { +#if (DEBUG == 1) + float manifold_pressure = 8; + elm327->manifold_pressure = (uint8_t)manifold_pressure; + elm327->value_updates |= (1 << UPDATE_MANIFOLD_PRESSURE_POS); + current_value_fast = ENGINE_LOAD_V; +#else float manifold_pressure = elm327->elm327->manifoldPressure(); if (elm327->elm327->nb_rx_state == ELM_SUCCESS) { elm327->manifold_pressure = (uint8_t)manifold_pressure; elm327->value_updates |= (1 << UPDATE_MANIFOLD_PRESSURE_POS); - current_value_fast = RPM_V; + current_value_fast = ENGINE_LOAD_V; } else if (elm327->elm327->nb_rx_state != ELM_GETTING_MSG) { // elm327->elm327->printError(); - current_value_fast = RPM_V; + current_value_fast = ENGINE_LOAD_V; } +#endif break; } } @@ -160,6 +167,11 @@ void obd2_elm327_process_slow(obd2_elm327_t *elm327) { case COOLANT_TEMP_V: { +#if (DEBUG == 1) + elm327->engine_coolant_temp = 102; + elm327->value_updates |= (1 << UPDATE_COOLANT_TEMP_POS); + current_value_slow = INTAKE_AIR_TEMP_V; +#else float coolant_temp = elm327->elm327->engineCoolantTemp(); if (elm327->elm327->nb_rx_state == ELM_SUCCESS) { @@ -172,10 +184,16 @@ void obd2_elm327_process_slow(obd2_elm327_t *elm327) // elm327->elm327->printError(); current_value_slow = INTAKE_AIR_TEMP_V; } +#endif break; } case INTAKE_AIR_TEMP_V: { +#if (DEBUG == 1) + elm327->intake_air_temp = 40; + elm327->value_updates |= (1 << UPDATE_INTAKE_AIR_TEMP_POS); + current_value_slow = AMBIENT_AIR_TEMP_V; +#else float intake_air_temp = elm327->elm327->intakeAirTemp(); if (elm327->elm327->nb_rx_state == ELM_SUCCESS) { @@ -188,10 +206,16 @@ void obd2_elm327_process_slow(obd2_elm327_t *elm327) // elm327->elm327->printError(); current_value_slow = AMBIENT_AIR_TEMP_V; } +#endif break; } case AMBIENT_AIR_TEMP_V: { +#if (DEBUG == 1) + elm327->ambient_air_temp = 20; + elm327->value_updates |= (1 << UPDATE_AMBIENT_AIR_TEMP_POS); + current_value_slow = OIL_TEMP_V; +#else float ambient_air_temp = elm327->elm327->ambientAirTemp(); if (elm327->elm327->nb_rx_state == ELM_SUCCESS) { @@ -204,10 +228,16 @@ void obd2_elm327_process_slow(obd2_elm327_t *elm327) // elm327->elm327->printError(); current_value_slow = OIL_TEMP_V; } +#endif break; } case OIL_TEMP_V: { +#if (DEBUG == 1) + elm327->engine_oil_temp = 80; + elm327->value_updates |= (1 << UPDATE_OIL_TEMP_POS); + current_value_slow = FUEL_LEVEL_V; +#else float engine_oil_temp = elm327->elm327->oilTemp(); if (elm327->elm327->nb_rx_state == ELM_SUCCESS) { @@ -220,10 +250,16 @@ void obd2_elm327_process_slow(obd2_elm327_t *elm327) // elm327->elm327->printError(); current_value_slow = FUEL_LEVEL_V; } +#endif break; } case FUEL_LEVEL_V: { +#if (DEBUG == 1) + elm327->fuel_level = 50; + elm327->value_updates |= (1 << UPDATE_FUEL_LEVEL_POS); + current_value_slow = COOLANT_TEMP_V; +#else float fuel_level = elm327->elm327->fuelLevel(); if (elm327->elm327->nb_rx_state == ELM_SUCCESS) { @@ -236,6 +272,7 @@ void obd2_elm327_process_slow(obd2_elm327_t *elm327) // elm327->elm327->printError(); current_value_slow = COOLANT_TEMP_V; } +#endif break; } default: diff --git a/due_obd2/obd2_elm327.h b/due_obd2/obd2_elm327.h index 20447f3..ba373f6 100644 --- a/due_obd2/obd2_elm327.h +++ b/due_obd2/obd2_elm327.h @@ -68,6 +68,25 @@ Be sure to disconnect the TX and RX pins from the BT module when programming the #define UPDATE_MANIFOLD_PRESSURE_POS 0x0A #define UPDATE_FUEL_LEVEL_POS 0x0B +#define COOLANT_TEMP_MIN 0 +#define COOLANT_TEMP_MAX 120 +#define INTAKE_AIR_TEMP_MIN -40 +#define INTAKE_AIR_TEMP_MAX 120 +#define AMBIENT_AIR_TEMP_MIN -40 +#define AMBIENT_AIR_TEMP_MAX 120 +#define OIL_TEMP_MIN -40 +#define OIL_TEMP_MAX 120 +#define ENGINE_LOAD_MIN 0 +#define ENGINE_LOAD_MAX 100 +#define BATTERY_VOLTAGE_MIN 0 +#define BATTERY_VOLTAGE_MAX 16 +#define FUEL_PRESSURE_MIN 0 +#define FUEL_PRESSURE_MAX 765 +#define MANIFOLD_PRESSURE_MIN 0 +#define MANIFOLD_PRESSURE_MAX 255 +#define FUEL_LEVEL_MIN 0 +#define FUEL_LEVEL_MAX 100 + enum BluetoothState { BT_INITIALISING = 0, @@ -116,7 +135,7 @@ char obd2_elm327_init(obd2_elm327_t *elm327); void obd2_elm327_process_fast(obd2_elm327_t *elm327); /** - * @brief processes the bluetooth module communication with the OBD2 reader for variables that change slowly + * @brief processes the bluetooth module communication with the OBD2 reader for variables that change slowly (checks every 5 seconds) * * @param bt pointer to struct holding bluetooth driver data */