Implement battery icon function for bw screens

Add battery drawing function in graphics_bw.c, fix GD77 layout.
This commit is contained in:
Niccolò Izzo 2020-12-06 18:03:48 +01:00
parent d8c81dcabb
commit 6a0afd7df7
3 changed files with 94 additions and 42 deletions

View File

@ -19,7 +19,7 @@
/** /**
* This source file provides a black and white implementation for the graphics.h interface * This source file provides a black and white implementation for the graphics.h interface
* It is suitable for monochromatic displays with 1 bit per pixel, * It is suitable for monochromatic displays with 1 bit per pixel,
* it will have RGB and grayscale counterparts * it will have RGB and grayscale counterparts
*/ */
@ -46,7 +46,7 @@ void gfx_init()
// Calculate framebuffer size // Calculate framebuffer size
fbSize = (SCREEN_HEIGHT * SCREEN_WIDTH) / 8; fbSize = (SCREEN_HEIGHT * SCREEN_WIDTH) / 8;
/* Compensate for eventual truncation error in division */ /* Compensate for eventual truncation error in division */
if((fbSize * 8) < (SCREEN_HEIGHT * SCREEN_WIDTH)) fbSize += 1; if((fbSize * 8) < (SCREEN_HEIGHT * SCREEN_WIDTH)) fbSize += 1;
fbSize *= sizeof(uint8_t); fbSize *= sizeof(uint8_t);
initialized = 1; initialized = 1;
} }
@ -74,8 +74,8 @@ bool gfx_renderingInProgress()
bw_t _color2bw(color_t true_color) bw_t _color2bw(color_t true_color)
{ {
if(true_color.r == 0 && if(true_color.r == 0 &&
true_color.g == 0 && true_color.g == 0 &&
true_color.b == 0) true_color.b == 0)
return WHITE; return WHITE;
else else
@ -116,16 +116,16 @@ void gfx_setPixel(point_t pos, color_t color)
if (pos.x >= SCREEN_WIDTH || pos.y >= SCREEN_HEIGHT) if (pos.x >= SCREEN_WIDTH || pos.y >= SCREEN_HEIGHT)
return; // off the screen return; // off the screen
bw_t bw = _color2bw(color); bw_t bw = _color2bw(color);
_bw_setPixel(pos, bw); _bw_setPixel(pos, bw);
} }
void gfx_drawLine(point_t start, point_t end, color_t color) void gfx_drawLine(point_t start, point_t end, color_t color)
{ {
if(!initialized) return; if(!initialized) return;
bw_t bw = _color2bw(color); bw_t bw = _color2bw(color);
for(int y = start.y; y < end.y; y++) for(int y = start.y; y <= end.y; y++)
{ {
for(int x = start.x; x < end.x; x++) for(int x = start.x; x <= end.x; x++)
{ {
point_t pos = {x, y}; point_t pos = {x, y};
_bw_setPixel(pos, bw); _bw_setPixel(pos, bw);
@ -160,14 +160,14 @@ void gfx_drawRect(point_t start, uint16_t width, uint16_t height, color_t color,
void gfx_drawHLine(uint16_t y, uint16_t height, color_t color) void gfx_drawHLine(uint16_t y, uint16_t height, color_t color)
{ {
point_t start = {0, y}; point_t start = {0, y};
gfx_drawRect(start, SCREEN_WIDTH, height, color, 1); gfx_drawRect(start, SCREEN_WIDTH, height, color, 1);
} }
void gfx_drawVLine(uint16_t x, uint16_t width, color_t color) void gfx_drawVLine(uint16_t x, uint16_t width, color_t color)
{ {
point_t start = {x, 0}; point_t start = {x, 0};
gfx_drawRect(start, width, SCREEN_HEIGHT, color, 1); gfx_drawRect(start, width, SCREEN_HEIGHT, color, 1);
} }
/** /**
@ -271,3 +271,54 @@ void gfx_print(point_t start, const char *text, fontSize_t size, textAlign_t ali
start.x += glyph.xAdvance; start.x += glyph.xAdvance;
} }
} }
/*
* Function to draw battery of arbitrary size
* starting coordinates are relative to the top left point.
*
* **************** |
* * * |
* * ******* * |
* * ******* ** |
* * ******* ** | <-- Height (px)
* * ******* * |
* * * |
* **************** |
*
* __________________
*
* ^
* |
*
* Width (px)
*
*/
void gfx_drawBattery(point_t start, uint16_t width, uint16_t height, float percentage) {
color_t white = {255, 255, 255};
color_t black = {0, 0, 0 };
// Cap percentage to 1
percentage = (percentage > 1.0f) ? 1.0f : percentage;
// Draw the battery outline
gfx_drawRect(start, width, height, white, false);
// Draw the battery fill
point_t fill_start = {start.x + 2, start.y + 2};
gfx_drawRect(fill_start, (int)(((float)(width - 4)) * percentage), height - 4, white, true);
// Round corners
point_t top_left = start;
point_t top_right = {start.x + width - 1, start.y};
point_t bottom_left = {start.x, start.y + height - 1};
point_t bottom_right = {start.x + width - 1, start.y + height - 1};
gfx_setPixel(top_left, black);
gfx_setPixel(top_right, black);
gfx_setPixel(bottom_left, black);
gfx_setPixel(bottom_right, black);
// Draw the button
point_t button_start = {start.x + width, start.y + height / 2 - 1};
point_t button_end = {start.x + width, start.y + height / 2 + 1};
gfx_drawLine(button_start, button_end, white);
}

View File

@ -270,27 +270,27 @@ void gfx_print(point_t start, const char *text, fontSize_t size, textAlign_t ali
} }
} }
/* /*
* Function to draw battery of arbitrary size * Function to draw battery of arbitrary size
* starting coordinates are relative to the top left point. * starting coordinates are relative to the top left point.
* *
* **************** | * **************** |
* * * | * * * |
* * ******* * | * * ******* * |
* * ******* ** | * * ******* ** |
* * ******* ** | <-- Height (px) * * ******* ** | <-- Height (px)
* * ******* * | * * ******* * |
* * * | * * * |
* **************** | * **************** |
* *
* __________________ * __________________
* *
* ^ * ^
* | * |
* *
* Width (px) * Width (px)
* *
*/ */
void gfx_drawBattery(point_t start, uint16_t width, uint16_t height, float percentage) { void gfx_drawBattery(point_t start, uint16_t width, uint16_t height, float percentage) {
color_t white = {255, 255, 255}; color_t white = {255, 255, 255};
color_t green = {0, 255, 0 }; color_t green = {0, 255, 0 };
@ -326,7 +326,7 @@ void gfx_drawBattery(point_t start, uint16_t width, uint16_t height, float perce
gfx_setPixel(bottom_right, black); gfx_setPixel(bottom_right, black);
// Draw the button // Draw the button
point_t button_start = {start.x + width, start.y + height / 2 - 2}; point_t button_start = {start.x + width, start.y + height / 2 - 2 + (height % 2)};
point_t button_end = {start.x + width, start.y + height / 2 + 1}; point_t button_end = {start.x + width, start.y + height / 2 + 1};
gfx_drawLine(button_start, button_end, white); gfx_drawLine(button_start, button_end, white);
} }

View File

@ -58,8 +58,8 @@
* *
* top_status_bar (8 px) 8 pt font without vertical padding * top_status_bar (8 px) 8 pt font without vertical padding
* *
* Line 1 (20px) 16 pt font with 2 px vertical padding * Line 1 (15px) 16 pt font with 2 px vertical padding
* Line 2 (20px) 16 pt font with 2 px vertical padding * Line 2 (15px) 16 pt font with 2 px vertical padding
* *
*/ */
@ -127,11 +127,11 @@ layout_t _ui_calculateLayout()
// Height and padding shown in diagram at beginning of file // Height and padding shown in diagram at beginning of file
const uint16_t top_h = 13; const uint16_t top_h = 13;
const uint16_t bottom_h = top_h; const uint16_t bottom_h = 18;
const uint16_t line1_h = 15; const uint16_t line1_h = 0;
const uint16_t line2_h = 15; const uint16_t line2_h = 15;
const uint16_t line3_h = 15; const uint16_t line3_h = 15;
const uint16_t line_pad = 15; const uint16_t line_pad = 2;
const uint16_t vertical_pad = 4; const uint16_t vertical_pad = 4;
const uint16_t horizontal_pad = 4; const uint16_t horizontal_pad = 4;
@ -181,7 +181,7 @@ layout_t _ui_calculateLayout()
top_h, top_h,
bottom_h, bottom_h,
vertical_pad, vertical_pad,
horizontal_pad horizontal_pad,
top_pos, top_pos,
line1_pos, line1_pos,
line2_pos, line2_pos,
@ -191,7 +191,7 @@ layout_t _ui_calculateLayout()
line1_font, line1_font,
line2_font, line2_font,
line3_font, line3_font,
bottom_font, bottom_font
}; };
return new_layout; return new_layout;
} }
@ -216,8 +216,9 @@ void _ui_drawTopBar(state_t* state)
// Print battery icon on top bar, use 4 px padding // Print battery icon on top bar, use 4 px padding
float percentage = state->v_bat / MAX_VBAT; float percentage = state->v_bat / MAX_VBAT;
uint16_t bat_width = SCREEN_WIDTH / 9; uint16_t bat_width = SCREEN_WIDTH / 9;
uint16_t bat_height = layout.top_h - layout.vertical_pad / 2; uint16_t bat_height = layout.top_h - layout.vertical_pad;
point_t bat_pos = {SCREEN_WIDTH - bat_width - layout.horizontal_pad, 1}; point_t bat_pos = {SCREEN_WIDTH - bat_width - layout.horizontal_pad,
layout.vertical_pad / 2};
gfx_drawBattery(bat_pos, bat_width, bat_height, percentage); gfx_drawBattery(bat_pos, bat_width, bat_height, percentage);
} }