From fbe9e4ac10001f931b2f3b9898e3292d36e8cc7c Mon Sep 17 00:00:00 2001 From: Aidan MacDonald Date: Sat, 19 Mar 2022 13:54:25 +0000 Subject: x1000: bootloader: refactor splash/splash2 Allow the use of printf formatting and multiple lines of text using newlines in the string. Change-Id: I65919bf29c16c34c38cf3995e02d2ddbbaa4bdf3 --- bootloader/x1000/gui.c | 69 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 12 deletions(-) (limited to 'bootloader/x1000/gui.c') diff --git a/bootloader/x1000/gui.c b/bootloader/x1000/gui.c index a672c30380..513a3cb9cb 100644 --- a/bootloader/x1000/gui.c +++ b/bootloader/x1000/gui.c @@ -29,6 +29,8 @@ #include "button.h" #include "version.h" #include +#include +#include static bool lcd_inited = false; extern bool is_usb_connected; @@ -53,27 +55,70 @@ void putcenter_y(int y, const char* msg) lcd_putsxy(x, y, msg); } -void putcenter_line(int line, const char* msg) +static void get_splash_size(const char* str, int* width, int* height) { - int y = LCD_HEIGHT/2 + (line - 1)*SYSFONT_HEIGHT; - putcenter_y(y, msg); + *width = 0; + *height = 0; + + while(str) { + const char* np = strchr(str, '\n'); + int len; + if(np) { + len = np - str; + np++; + } else { + len = strlen(str); + } + + *width = MAX(len, *width); + *height += 1; + str = np; + } } -void splash2(long delay, const char* msg, const char* msg2) +void splashf(long delay, const char* msg, ...) { + static char buf[512]; + + va_list ap; + va_start(ap, msg); + int len = vsnprintf(buf, sizeof(buf), msg, ap); + va_end(ap); + + if(len < 0) + return; + + int xpos, ypos; + int width, height; + get_splash_size(buf, &width, &height); + + width *= SYSFONT_WIDTH; + height *= SYSFONT_HEIGHT; + xpos = (LCD_WIDTH - width) / 2; + ypos = (LCD_HEIGHT - height) / 2; + + const int padding = 10; clearscreen(); - putcenter_line(0, msg); - if(msg2) - putcenter_line(1, msg2); + lcd_drawrect(xpos-padding, ypos-padding, + width+2*padding, height+2*padding); + + char* str = buf; + do { + char* np = strchr(str, '\n'); + if(np) { + *np = '\0'; + np++; + } + + lcd_putsxyf(xpos, ypos, "%s", str); + ypos += SYSFONT_HEIGHT; + str = np; + } while(str); + lcd_update(); sleep(delay); } -void splash(long delay, const char* msg) -{ - splash2(delay, msg, NULL); -} - int get_button(int timeout) { int btn = button_get_w_tmo(timeout); -- cgit v1.2.3