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.
replace/1fc4943755e5037647d7270fecaf3fbaf1ab4b63
Niccolò Izzo 2020-12-06 16:16:42 +01:00
rodzic f1400ac528
commit 1ee4744cb7
5 zmienionych plików z 40 dodań i 23 usunięć

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

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

Wyświetl plik

@ -85,6 +85,6 @@
#define FLASH_SDI GPIOB,5
/* Maximum battery voltage */
#define MAX_VBAT 8.2f
#define MAX_VBAT 8.4f
#endif

Wyświetl plik

@ -50,4 +50,4 @@
#define PTT_SW "PTT_SW",11
/* Maximum battery voltage */
#define MAX_VBAT 8.5f
#define MAX_VBAT 8.4f