From 21c166aadf261f274d4a5a95e0e135c26fa9e3f1 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Tue, 12 Nov 2024 22:57:38 +0100 Subject: [PATCH] add possibility to clear bars --- due_obd2/bars.cpp | 42 ++++++++++++++++++++++++++++++++++++++++-- due_obd2/bars.h | 32 ++++++++++++++++++++++++++++++-- due_obd2/due_obd2.ino | 34 ++++++++++++++++++---------------- 3 files changed, 88 insertions(+), 20 deletions(-) diff --git a/due_obd2/bars.cpp b/due_obd2/bars.cpp index 46d9b4e..308ebcd 100644 --- a/due_obd2/bars.cpp +++ b/due_obd2/bars.cpp @@ -1,6 +1,6 @@ #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) +void bar_draw_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); @@ -11,7 +11,7 @@ void draw_bar_horizontal(int start_x, int start_y, int width, int height, int va 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) +void bar_draw_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); @@ -20,4 +20,42 @@ void draw_bar_vertical(int start_x, int start_y, int width, int height, int valu 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); +} + +void bar_clear_part_horizontal(int start_x, int start_y, int width, int height, int value_from, int max_value, char clear_color, char clear_outline, UTFT *display) +{ + display->setColor(clear_color); + if (!clear_outline) + { + /** + * If we want to clear the outline, the part we need to clear becomes 1px smaller on each side. + * (that's not part of the bar to be filled, the "empty space" inside the bar) + * Because the start_y gets incremented, the end of the bar on the y side would be 2px too big. + * That's why we need to decrement the height by 2. + */ + start_y++; + height -= 2; + width--; + } + int bar_width = (int)((float)value_from / (float)max_value * width); + display->fillRect(start_x + bar_width, start_y, start_x + width, start_y + height); +} + +void bar_clear_part_vertical(int start_x, int start_y, int width, int height, int value_from, int max_value, char clear_color, char clear_outline, UTFT *display) +{ + display->setColor(clear_color); + if (!clear_outline) + { + /** + * If we want to clear the outline, the part we need to clear becomes 1px smaller on each side. + * (that's not part of the bar to be filled, the "empty space" inside the bar) + * Because the start_x gets incremented, the end of the bar on the x side would be 2px too big. + * That's why we need to decrement the width by 2. + */ + start_x++; + start_y++; + width-= 2; + } + int bar_height = (int)((float)value_from / (float)max_value * height); + display->fillRect(start_x, start_y, start_x + width, start_y + bar_height); } \ No newline at end of file diff --git a/due_obd2/bars.h b/due_obd2/bars.h index fb16a78..8caebb7 100644 --- a/due_obd2/bars.h +++ b/due_obd2/bars.h @@ -15,7 +15,7 @@ * @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); +void bar_draw_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 @@ -29,6 +29,34 @@ void draw_bar_horizontal(int start_x, int start_y, int width, int height, int va * @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); +void bar_draw_vertical(int start_x, int start_y, int width, int height, int value, int max_value, uint color, char fill_outline, UTFT *display); + +/** + * @brief Clears a part of a horizontal bar + * @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_from The value to start clearing from + * @param max_value The maximum value of the bar + * @param clear_color The color to clear the bar with + * @param clear_outline Whether to clear the outline of the bar + * @param display The display object + */ +void bar_clear_part_horizontal(int start_x, int start_y, int width, int height, int value_from, int max_value, char clear_color, char clear_outline, UTFT *display); + +/** + * @brief Clears a part of a vertical bar + * @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_from The value to start clearing from + * @param max_value The maximum value of the bar + * @param clear_color The color to clear the bar with + * @param clear_outline Whether to clear the outline of the bar + * @param display The display object + */ +void bar_clear_part_vertical(int start_x, int start_y, int width, int height, int value_from, int max_value, char clear_color, char clear_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 9652965..a910622 100644 --- a/due_obd2/due_obd2.ino +++ b/due_obd2/due_obd2.ino @@ -115,7 +115,6 @@ state_t init_state = .on_run = &on_init_run, .on_exit = &on_init_exit}; - state_t main_state = { .id = STATE_CAR_INFO, @@ -326,18 +325,19 @@ void on_main_run() { if (update_slow) { - update_slow = 0; - obd2_elm327_process_slow(&elm327); - - } else { - obd2_elm327_process_fast(&elm327); + update_slow = 0; + obd2_elm327_process_slow(&elm327); + } + else + { + obd2_elm327_process_fast(&elm327); } if (elm327.value_updates & (1 << UPDATE_RPM_POS)) { elm327.value_updates &= ~(1 << UPDATE_RPM_POS); int width = (int)(display.getDisplayXSize() * ((float)elm327.rpm / 7000.0)); - display.fillRect(0,20,width,20); + display.fillRect(0, 20, width, 20); display.setColor(VGA_FUCHSIA); display.print("rpm", 0, 20); display.printNumI(elm327.engine_load, 30, 20, 4, '0'); @@ -390,7 +390,7 @@ void on_main_run() { elm327.value_updates &= ~(1 << UPDATE_ENGINE_LOAD_POS); int width = (int)(display.getDisplayXSize() * ((float)elm327.engine_load / 100.0)); - display.fillRect(0,50,width,50); + display.fillRect(0, 50, width, 50); display.setColor(VGA_FUCHSIA); display.print("el", 0, 50); display.printNumI(elm327.engine_load, 20, 50, 4, '0'); @@ -576,29 +576,31 @@ void setup() void loop() { - draw_bar_horizontal(0,0,200,10,bar_1_x,100,VGA_AQUA,1,&display); + bar_draw_horizontal(0, 0, 200, 10, bar_1_x, 100, VGA_AQUA, 1, &display); - draw_bar_horizontal(0,20,200,11,bar_2_x,100,VGA_FUCHSIA,1,&display); + bar_draw_horizontal(0, 20, 200, 11, bar_2_x, 100, VGA_FUCHSIA, 0, &display); - draw_bar_vertical(0,50,20,100,bar_3_y,100,VGA_BLUE,1,&display); + bar_draw_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_1_x = 0; + bar_clear_part_horizontal(0,0,200,10,bar_1_x,100,VGA_BLACK,0,&display); } bar_2_x++; if (bar_2_x > 100) { - bar_2_x = 10; + bar_2_x = 10; + bar_clear_part_horizontal(0,20,200,11,bar_2_x,100,VGA_BLACK,1,&display); } bar_3_y++; if (bar_3_y > 100) { - bar_3_y = 50; + bar_3_y = 50; + bar_clear_part_vertical(0,50,20,100,bar_3_y,100,VGA_BLACK,0,&display); } - display.clrScr(); delay(50); - //statemachine_loop(); + // statemachine_loop(); }