From 88c00aacbdbcb1123fb7812cf45fcca85776242b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Grennerat?= Date: Mon, 15 Sep 2025 18:47:41 +0200 Subject: [PATCH] Power displaying --- Core/Src/dashboard.c | 90 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 73 insertions(+), 17 deletions(-) diff --git a/Core/Src/dashboard.c b/Core/Src/dashboard.c index 5c76e0d..6f124c8 100644 --- a/Core/Src/dashboard.c +++ b/Core/Src/dashboard.c @@ -14,7 +14,9 @@ void run_dashboard_loop() { draw_battery(654, i * 100 / 4, 1321, 343); draw_speed(i * 1000 / 4); - draw_power_bars(1000 - (int16_t) i * 1000 / 200); + int16_t duty = 1000 - (int16_t) i * 1000 / 200; + draw_power_bars(duty); + draw_power(100 * i / 20 * (duty < 0 ? -1 : 1), duty, 60 * 10); HAL_Delay(50); if (i == 400) { @@ -52,6 +54,9 @@ void LCD_DrawHollowRoundRect( uint16_t fill_color, uint8_t do_fill); +#define LEFT_CENTER_COL1 38 +#define LEFT_CENTER_COL2 116 +#define RIGHT_CENTER 400 void draw_init() { LCD_Fill_Screen(COLOR_OFF, 1); LCD_Fill_Screen(COLOR_BG, 0); @@ -97,8 +102,8 @@ void draw_battery( int32_t trip_dist, uint32_t life_dist) { - if (voltage == last_voltage && percent/100 == last_percent/100 - && trip_dist/10 == last_trip_dist/10) { + if (voltage == last_voltage && percent / 100 == last_percent / 100 + && trip_dist / 10 == last_trip_dist / 10) { return; } last_voltage = voltage; @@ -112,7 +117,7 @@ void draw_battery( filled_bar_width = 6;// Must be at least the size of the border radius. } uint16_t filled_bar_end_x = 4 + 2 + filled_bar_width; - uint16_t text_y_12 = LCD_HEIGHT - 6 - (22 - 16) / 2; +//uint16_t text_y_12 = LCD_HEIGHT - 6 - (22 - 16) / 2; uint16_t text_y_14 = LCD_HEIGHT - 6 - (22 - 18) / 2; char voltage_text[8]; @@ -180,30 +185,31 @@ void draw_power_bars(int16_t dutyy) { 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% + duty *= 1 / 0.8;// 80% duty means the bar is at 100% uint16_t width = ((float) duty / 1000.0) * max_width; 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) { + 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) { + } else if (width != last_width) { + if (regen) { if (last_origin < origin) { LCD_Draw_Rectangle(last_origin, 0, origin - last_origin, 20, COLOR_BG); - }else { + } else { LCD_Draw_Rectangle(origin, 0, last_origin - origin, 20, color); } - }else { + } else { if (last_width < width) { - LCD_Draw_Rectangle(origin + last_width, 0, width - last_width, 20, color); - }else { + 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); } } @@ -214,14 +220,14 @@ void draw_power_bars(int16_t dutyy) { // Displays the huge speed counter with avg and max values. // Speed from COMM_GET_VALUES_SETUP, scale 1000 -uint32_t last_speed = 0; +uint32_t last_speed = 30; uint32_t max_speed = 0; -uint32_t last_avg_speed = 0; +uint32_t last_avg_speed = 30; uint32_t avg_speed_tot = 0;// You need to ride super fast for a super long time for it to overflow ;) uint32_t avg_speed_count = 0; void draw_speed(int32_t speedd) { - uint16_t erase_width = 90;// width to erase from center - uint32_t speed = speedd < 0 ? -speedd/1000 : speedd/1000; + + uint32_t speed = speedd < 0 ? -speedd / 1000 : speedd / 1000; if (speed >= 100) { speed = 99; } @@ -244,6 +250,7 @@ void draw_speed(int32_t speedd) { // Draw if (last_speed != speed) { last_speed = speed; + uint16_t erase_width = 85;// width to erase from center char speed_text[4]; sprintf(speed_text, "%lu", speed); LCD_Draw_Rectangle(LCD_WIDTH / 2 - erase_width, 22, 2 * erase_width, 110, @@ -254,6 +261,7 @@ void draw_speed(int32_t speedd) { if (update_stats) { char stats_text[20]; + uint16_t erase_width = 90;// width to erase from center sprintf(stats_text, "avg: %lu max: %lu", avg_speed, max_speed); LCD_Draw_Rectangle(LCD_WIDTH / 2 - erase_width, 140, 2 * erase_width, 25, COLOR_BG); @@ -262,11 +270,59 @@ void draw_speed(int32_t speedd) { } } +int16_t last_current = 99; +uint16_t last_duty = 99; +int32_t last_power = 1000; // Displays Current, Duty, Watts // Current from COMM_GET_VALUES, scale 100 // Duty from COMM_GET_VALUES, scale 1000 // Input voltage from COMM_GET_VALUES, scale 10 -void draw_power(int32_t amps, int16_t duty, int16_t voltage) { +void draw_power(int32_t current_i, int16_t duty_i, int16_t voltage_i) { + + int16_t current = current_i / 100; + uint16_t duty = duty_i < 0 ? -duty_i / 10 : duty_i / 10; + int32_t power = ((int32_t) voltage_i) * ((int32_t) current_i) / 10000 * 10; + + if (current != last_current) { + last_current = current; + if(current >= 100) { + current = 99; + }else if(current <= -100){ + current = -99; + } + + char text[8]; + sprintf(text, "%dA", current); + LCD_Draw_Rectangle(LEFT_CENTER_COL1 - 38, 30, 2 * 39, 35, COLOR_BG); + GFX_DrawText(LEFT_CENTER_COL1, 60, text, &monomaniacone20pt, + COLOR_PRIMARY, COLOR_BG, 1, -2); + } + if (duty != last_duty) { + last_duty = duty; + if (duty >= 100) { + duty = 99; + } + + char text[8]; + sprintf(text, "%u%%", duty); + LCD_Draw_Rectangle(LEFT_CENTER_COL2 - 39, 30, 2 * 39, 35, COLOR_BG); + GFX_DrawText(LEFT_CENTER_COL2, 60, text, &monomaniacone20pt, + COLOR_PRIMARY, COLOR_BG, 1, -2); + } + if (power != last_power) { + last_power = power; + if (power >= 10000) { + power = 9999; + }else if(power <= -10000) { + power = -9999; + } + + char text[10]; + sprintf(text, "%dW", power); + LCD_Draw_Rectangle(RIGHT_CENTER - 70, 30, 2 * 70, 35, COLOR_BG); + GFX_DrawText(RIGHT_CENTER, 60, text, &monomaniacone20pt, + COLOR_PRIMARY, COLOR_BG, 1, -2); + } }