From 1ee4744cb7b4cd39e3320bc735ae884b4e5d39a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niccol=C3=B2=20Izzo?= Date: Sun, 6 Dec 2020 16:16:42 +0100 Subject: [PATCH] Improve battery icon drawing implementation Fix a bug where the signature of the battery draw function was not present, causing a build error. Now the battery size and position is screen size independent and should work for all layouts. --- openrtx/include/interfaces/graphics.h | 10 ++++++++ openrtx/src/graphics/graphics_rgb565.c | 16 +++++++------ openrtx/src/ui.c | 33 +++++++++++++++----------- platform/targets/MD-390/hwconfig.h | 2 +- platform/targets/linux/hwconfig.h | 2 +- 5 files changed, 40 insertions(+), 23 deletions(-) diff --git a/openrtx/include/interfaces/graphics.h b/openrtx/include/interfaces/graphics.h index a64e032b..4686b0a4 100644 --- a/openrtx/include/interfaces/graphics.h +++ b/openrtx/include/interfaces/graphics.h @@ -219,4 +219,14 @@ void gfx_drawRect(point_t start, uint16_t width, uint16_t height, color_t color, */ void gfx_print(point_t start, const char *text, fontSize_t size, textAlign_t alignment, color_t color); +/** + * Function to draw battery of arbitrary size. + * Starting coordinates are relative to the top left point. + * @param start: battery icon start point, in pixel coordinates. + * @param width: battery icon width + * @param height: battery icon height + * @param percentage: battery charge percentage + */ +void gfx_drawBattery(point_t start, uint16_t width, uint16_t height, float percentage); + #endif /* GRAPHICS_H */ diff --git a/openrtx/src/graphics/graphics_rgb565.c b/openrtx/src/graphics/graphics_rgb565.c index 8e850226..b319a823 100644 --- a/openrtx/src/graphics/graphics_rgb565.c +++ b/openrtx/src/graphics/graphics_rgb565.c @@ -274,14 +274,14 @@ void gfx_print(point_t start, const char *text, fontSize_t size, textAlign_t ali * Function to draw battery of arbitrary size * starting coordinates are relative to the top left point. * - * ****************** | + * **************** | * * * | * * ******* * | * * ******* ** | * * ******* ** | <-- Height (px) * * ******* * | * * * | - * ****************** | + * **************** | * * __________________ * @@ -292,18 +292,20 @@ void gfx_print(point_t start, const char *text, fontSize_t size, textAlign_t ali * */ void gfx_drawBattery(point_t start, uint16_t width, uint16_t height, float percentage) { - printf("AFTER: %f\n", percentage); color_t white = {255, 255, 255}; color_t green = {0, 255, 0 }; color_t yellow = {250, 180, 19 }; color_t red = {255, 0, 0 }; color_t black = {0, 0, 0 }; + // Cap percentage to 1 + percentage = (percentage > 1.0f) ? 1.0f : percentage; + // Select color according to percentage color_t bat_color = yellow; - if (percentage < 0.2) + if (percentage < 0.3) bat_color = red; - else if (percentage > 0.8) + else if (percentage > 0.7) bat_color = green; // Draw the battery outline @@ -324,7 +326,7 @@ void gfx_drawBattery(point_t start, uint16_t width, uint16_t height, float perce gfx_setPixel(bottom_right, black); // Draw the button - point_t button_start = {start.x + width, start.y + (height / 2) - 2}; - point_t button_end = {start.x + width, start.y + (height / 2) + 2}; + point_t button_start = {start.x + width, start.y + height / 2 - 2}; + point_t button_end = {start.x + width, start.y + height / 2 + 1}; gfx_drawLine(button_start, button_end, white); } diff --git a/openrtx/src/ui.c b/openrtx/src/ui.c index 1e704369..2455dbe5 100644 --- a/openrtx/src/ui.c +++ b/openrtx/src/ui.c @@ -76,6 +76,8 @@ typedef struct layout_t { uint16_t top_h; uint16_t bottom_h; + uint16_t vertical_pad; + uint16_t horizontal_pad; point_t top_pos; point_t line1_pos; point_t line2_pos; @@ -103,13 +105,13 @@ layout_t _ui_calculateLayout() // Height and padding shown in diagram at beginning of file const uint16_t top_h = 16; - const uint16_t top_pad = 4; + const uint16_t bottom_h = top_h; const uint16_t line1_h = 32; const uint16_t line2_h = 32; const uint16_t line3_h = 32; const uint16_t line_pad = 8; - const uint16_t bottom_h = top_h; - const uint16_t bottom_pad = 4; + const uint16_t vertical_pad = 4; + const uint16_t horizontal_pad = 4; // Top bar font: 8 pt const fontSize_t top_font = FONT_SIZE_8PT; @@ -125,13 +127,13 @@ layout_t _ui_calculateLayout() // Height and padding shown in diagram at beginning of file const uint16_t top_h = 13; - const uint16_t top_pad = 4; + const uint16_t bottom_h = top_h; const uint16_t line1_h = 15; const uint16_t line2_h = 15; const uint16_t line3_h = 15; const uint16_t line_pad = 15; - const uint16_t bottom_h = top_h; - const uint16_t bottom_pad = 11; + const uint16_t vertical_pad = 4; + const uint16_t horizontal_pad = 4; // Top bar font: 8 pt const fontSize_t top_font = FONT_SIZE_6PT; @@ -146,13 +148,13 @@ layout_t _ui_calculateLayout() // Height and padding shown in diagram at beginning of file const uint16_t top_h = 8; - const uint16_t top_pad = 0; + const uint16_t bottom_h = 0; const uint16_t line1_h = 20; const uint16_t line2_h = 20; const uint16_t line3_h = 0; const uint16_t line_pad = 2; - const uint16_t bottom_h = 0; - const uint16_t bottom_pad = 0; + const uint16_t vertical_pad = 0; + const uint16_t horizontal_pad = 0; // Top bar font: 8 pt const fontSize_t top_font = FONT_SIZE_1; @@ -168,16 +170,18 @@ layout_t _ui_calculateLayout() #endif // Calculate printing positions - point_t top_pos = {0, top_h - top_pad}; + point_t top_pos = {0, top_h - vertical_pad}; point_t line1_pos = {0, top_h + line1_h - line_pad}; point_t line2_pos = {0, top_h + line1_h + line2_h - line_pad}; point_t line3_pos = {0, top_h + line1_h + line2_h + line3_h - line_pad}; - point_t bottom_pos = {0, top_h + line1_h + line2_h + line3_h + bottom_h - bottom_pad}; + point_t bottom_pos = {0, top_h + line1_h + line2_h + line3_h + bottom_h - vertical_pad}; layout_t new_layout = { top_h, bottom_h, + vertical_pad, + horizontal_pad top_pos, line1_pos, line2_pos, @@ -211,9 +215,10 @@ void _ui_drawTopBar(state_t* state) // Print battery icon on top bar, use 4 px padding float percentage = state->v_bat / MAX_VBAT; - printf("BEFORE: %f\n", percentage); - point_t bat_pos = {SCREEN_WIDTH - 24, layout.top_pos.y - 10}; - gfx_drawBattery(bat_pos, 19, 12, 0.5f); + uint16_t bat_width = SCREEN_WIDTH / 9; + uint16_t bat_height = layout.top_h - layout.vertical_pad / 2; + point_t bat_pos = {SCREEN_WIDTH - bat_width - layout.horizontal_pad, 1}; + gfx_drawBattery(bat_pos, bat_width, bat_height, percentage); } void _ui_drawVFO(state_t* state) diff --git a/platform/targets/MD-390/hwconfig.h b/platform/targets/MD-390/hwconfig.h index e7d6e463..391bb8bc 100644 --- a/platform/targets/MD-390/hwconfig.h +++ b/platform/targets/MD-390/hwconfig.h @@ -85,6 +85,6 @@ #define FLASH_SDI GPIOB,5 /* Maximum battery voltage */ -#define MAX_VBAT 8.2f +#define MAX_VBAT 8.4f #endif diff --git a/platform/targets/linux/hwconfig.h b/platform/targets/linux/hwconfig.h index 9dadeb49..84370f0e 100644 --- a/platform/targets/linux/hwconfig.h +++ b/platform/targets/linux/hwconfig.h @@ -50,4 +50,4 @@ #define PTT_SW "PTT_SW",11 /* Maximum battery voltage */ -#define MAX_VBAT 8.5f +#define MAX_VBAT 8.4f