add possibility to clear bars

This commit is contained in:
Sem van der Hoeven
2024-11-12 22:57:38 +01:00
parent cf24296f63
commit 21c166aadf
3 changed files with 88 additions and 20 deletions

View File

@@ -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);
}

View File

@@ -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

View File

@@ -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,
@@ -328,8 +327,9 @@ void on_main_run()
{ {
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);
} }
@@ -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();
} }