mirror of
https://github.com/SemvdH/OBD2-car-display.git
synced 2025-12-16 12:41:05 +00:00
add possibility to clear bars
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
#include "bars.h"
|
#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);
|
display->setColor(color);
|
||||||
int bar_width = (int)((float)value / (float)max_value * width);
|
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);
|
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);
|
display->setColor(color);
|
||||||
int bar_height = (int)((float)value / (float)max_value * height);
|
int bar_height = (int)((float)value / (float)max_value * height);
|
||||||
@@ -21,3 +21,41 @@ void draw_bar_vertical(int start_x, int start_y, int width, int height, int valu
|
|||||||
}
|
}
|
||||||
display->fillRect(start_x, (start_y + height) - bar_height, 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);
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@
|
|||||||
* @param fill_outline Whether to fill the outline of the bar
|
* @param fill_outline Whether to fill the outline of the bar
|
||||||
* @param display The display object
|
* @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
|
* @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 fill_outline Whether to fill the outline of the bar
|
||||||
* @param display The display object
|
* @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
|
#endif
|
||||||
@@ -115,7 +115,6 @@ state_t init_state =
|
|||||||
.on_run = &on_init_run,
|
.on_run = &on_init_run,
|
||||||
.on_exit = &on_init_exit};
|
.on_exit = &on_init_exit};
|
||||||
|
|
||||||
|
|
||||||
state_t main_state =
|
state_t main_state =
|
||||||
{
|
{
|
||||||
.id = STATE_CAR_INFO,
|
.id = STATE_CAR_INFO,
|
||||||
@@ -326,18 +325,19 @@ void on_main_run()
|
|||||||
{
|
{
|
||||||
if (update_slow)
|
if (update_slow)
|
||||||
{
|
{
|
||||||
update_slow = 0;
|
update_slow = 0;
|
||||||
obd2_elm327_process_slow(&elm327);
|
obd2_elm327_process_slow(&elm327);
|
||||||
|
}
|
||||||
} else {
|
else
|
||||||
obd2_elm327_process_fast(&elm327);
|
{
|
||||||
|
obd2_elm327_process_fast(&elm327);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (elm327.value_updates & (1 << UPDATE_RPM_POS))
|
if (elm327.value_updates & (1 << UPDATE_RPM_POS))
|
||||||
{
|
{
|
||||||
elm327.value_updates &= ~(1 << UPDATE_RPM_POS);
|
elm327.value_updates &= ~(1 << UPDATE_RPM_POS);
|
||||||
int width = (int)(display.getDisplayXSize() * ((float)elm327.rpm / 7000.0));
|
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.setColor(VGA_FUCHSIA);
|
||||||
display.print("rpm", 0, 20);
|
display.print("rpm", 0, 20);
|
||||||
display.printNumI(elm327.engine_load, 30, 20, 4, '0');
|
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);
|
elm327.value_updates &= ~(1 << UPDATE_ENGINE_LOAD_POS);
|
||||||
int width = (int)(display.getDisplayXSize() * ((float)elm327.engine_load / 100.0));
|
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.setColor(VGA_FUCHSIA);
|
||||||
display.print("el", 0, 50);
|
display.print("el", 0, 50);
|
||||||
display.printNumI(elm327.engine_load, 20, 50, 4, '0');
|
display.printNumI(elm327.engine_load, 20, 50, 4, '0');
|
||||||
@@ -576,29 +576,31 @@ void setup()
|
|||||||
void loop()
|
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++;
|
bar_1_x++;
|
||||||
if (bar_1_x > 100)
|
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++;
|
bar_2_x++;
|
||||||
if (bar_2_x > 100)
|
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++;
|
bar_3_y++;
|
||||||
if (bar_3_y > 100)
|
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);
|
delay(50);
|
||||||
//statemachine_loop();
|
// statemachine_loop();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user