Speed full displaying with refresh control

This commit is contained in:
Clément Grennerat 2025-09-15 16:46:05 +02:00
parent d4d9f44605
commit fda9a1add3
2 changed files with 699 additions and 961 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
#include "dashboard.h"
#include "font.h"
#include "monomaniacone12pt.h"
//#include "monomaniacone12pt.h"
#include "monomaniacone14pt.h"
#include "monomaniacone20pt.h"
#include "monomaniacone72pt.h"
@ -12,11 +12,11 @@ void run_dashboard_loop() {
uint32_t i = 0;
while (1) {
draw_battery(654, i * 100, 1321, 343);
draw_speed(i * 1000);
draw_battery(654, i * 100 / 4, 1321, 343);
draw_speed(i * 1000 / 2);
HAL_Delay(100);
if (i == 100) {
if (i == 200) {
i = 0;
} else {
i++;
@ -87,17 +87,28 @@ void draw_init() {
// battery percent (level) from COMM_GET_VALUES_SETUP, scale 100
// trip distance from COMM_GET_VALUES_SETUP, scale 100
// life distance (odometer) from COMM_GET_VALUES_SETUP
int16_t last_voltage = 0;
int32_t last_percent = 0;
int32_t last_trip_dist = 0;
void draw_battery(
int16_t voltage,
int32_t percent,
int32_t trip_dist,
uint32_t life_dist) {
if (voltage == last_voltage && percent/100 == last_percent/100
&& trip_dist/10 == last_trip_dist/10) {
return;
}
last_voltage = voltage;
last_percent = percent;
last_trip_dist = trip_dist;
uint16_t bar_width = LCD_WIDTH - 12;
uint16_t bar_height = 22;
uint16_t filled_bar_width = (((float) (percent / 100)) / 100.0) * bar_width;
if(filled_bar_width < 6) {
filled_bar_width = 6; // Must be at least the size of the border radius.
if (filled_bar_width < 6) {
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;
@ -125,12 +136,12 @@ void draw_battery(
if (percent > 20 * 100) {
// Drawing the voltage to the left
left_x = 12
left_x = 10
+ GFX_DrawText(left_x, text_y_14, voltage_text, &monomaniacone14pt,
COLOR_BG, COLOR_SUCCESS, 0, -2);
} else {
// Drawing the voltage to the right
right_x = -12
right_x = -10
+ GFX_DrawText(right_x, text_y_14, voltage_text, &monomaniacone14pt,
COLOR_FG, COLOR_BG, 2, -2);
}
@ -138,11 +149,11 @@ void draw_battery(
if (percent > 50 * 100) {
// Drawing the distances on the left
GFX_DrawText(left_x, text_y_14, distances_text, &monomaniacone14pt,
COLOR_BG, COLOR_SUCCESS, 0, -4);
COLOR_BG, COLOR_SUCCESS, 0, -3);
} else {
// Drawing the distances on the right
GFX_DrawText(right_x, text_y_14, distances_text, &monomaniacone14pt,
COLOR_FG, COLOR_BG, 2, -4);
COLOR_FG, COLOR_BG, 2, -3);
}
if (percent > 80 * 100) {
@ -162,23 +173,60 @@ 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 offset = LCD_WIDTH/2;
uint16_t max_width = LCD_WIDTH/2;
uint16_t offset = LCD_WIDTH / 2;
uint16_t max_width = LCD_WIDTH / 2;
uint16_t width = (duty/100) * max_width / 10;
uint16_t width = (duty / 100) * max_width / 10;
}
// Displays the huge speed counter with avg and max values.
// Speed from COMM_GET_VALUES_SETUP, scale 1000
void draw_speed(int32_t speed) {
uint16_t erase_width = 90; // width to erase from center
uint32_t last_speed = 0;
uint32_t max_speed = 0;
uint32_t last_avg_speed = 0;
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;
if (speed >= 100) {
speed = 99;
}
char speed_text[6];
sprintf(speed_text, "%lu", abs(speed / 1000));
// update max speed
uint8_t update_stats = 0;
if (speed > max_speed) {
max_speed = speed;
update_stats = 1;
}
// update avg speed
avg_speed_tot += speed;
avg_speed_count += 1;
uint32_t avg_speed = avg_speed_tot / avg_speed_count;
if (last_avg_speed != avg_speed) {
last_avg_speed = avg_speed;
update_stats = 1;
}
LCD_Draw_Rectangle(LCD_WIDTH/2 - erase_width, 22, 2*erase_width, 110, COLOR_BG);
GFX_DrawText(LCD_WIDTH / 2, 32 + 95, speed_text, &monomaniacone72pt, COLOR_FG,
COLOR_BG, 1, -5);
// Draw
if (last_speed != speed) {
last_speed = speed;
char speed_text[4];
sprintf(speed_text, "%lu", speed);
LCD_Draw_Rectangle(LCD_WIDTH / 2 - erase_width, 22, 2 * erase_width, 110,
COLOR_BG);
GFX_DrawText(LCD_WIDTH / 2, 32 + 95, speed_text, &monomaniacone72pt,
COLOR_FG, COLOR_BG, 1, -5);
}
if (update_stats) {
char stats_text[20];
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);
GFX_DrawText(LCD_WIDTH / 2, 159, stats_text, &monomaniacone14pt,
COLOR_SECONDARY, COLOR_BG, 1, -3);
}
}
// Displays Current, Duty, Watts