From 38d8a6eaa1d6ac2c367a4bf6f10d64d48a20add2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Grennerat?= Date: Mon, 15 Sep 2025 17:40:53 +0200 Subject: [PATCH] Duty cycle bar displaying --- Core/Src/dashboard.c | 43 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/Core/Src/dashboard.c b/Core/Src/dashboard.c index 988d039..5c76e0d 100644 --- a/Core/Src/dashboard.c +++ b/Core/Src/dashboard.c @@ -13,10 +13,11 @@ void run_dashboard_loop() { while (1) { draw_battery(654, i * 100 / 4, 1321, 343); - draw_speed(i * 1000 / 2); + draw_speed(i * 1000 / 4); + draw_power_bars(1000 - (int16_t) i * 1000 / 200); - HAL_Delay(100); - if (i == 200) { + HAL_Delay(50); + if (i == 400) { i = 0; } else { i++; @@ -172,11 +173,43 @@ void draw_battery( // Displays the power bars at the top // Duty from COMM_GET_VALUES, scale 1000 -void draw_power_bars(int16_t duty) { +uint16_t last_width = 0; +uint8_t last_regen = 0; +void draw_power_bars(int16_t dutyy) { uint16_t offset = LCD_WIDTH / 2; uint16_t max_width = LCD_WIDTH / 2; + uint8_t regen = dutyy < 0; + uint16_t duty = regen ? -dutyy : dutyy; + duty *= 1/0.8; // 80% duty means the bar is at 100% + uint16_t width = ((float) duty / 1000.0) * max_width; - uint16_t width = (duty / 100) * max_width / 10; + uint16_t last_origin = last_regen ? offset - last_width : offset; + uint16_t origin = regen ? offset - width : offset; + uint16_t color = regen ? COLOR_ERROR : COLOR_SUCCESS; + + if(regen != last_regen) { + LCD_Draw_Rectangle(last_origin, 0, last_width, 20, COLOR_BG); + LCD_Draw_Rectangle(origin, 0, width, 20, color); + last_regen = regen; + last_width = width; + + }else if (width != last_width){ + if(regen) { + if (last_origin < origin) { + LCD_Draw_Rectangle(last_origin, 0, origin - last_origin, 20, COLOR_BG); + }else { + LCD_Draw_Rectangle(origin, 0, last_origin - origin, 20, color); + } + }else { + if (last_width < width) { + LCD_Draw_Rectangle(origin + last_width, 0, width - last_width, 20, color); + }else { + LCD_Draw_Rectangle(origin + width, 0, last_width - width, 20, COLOR_BG); + } + } + + last_width = width; + } } // Displays the huge speed counter with avg and max values.