From dc01db6e8aad5926e25acd20e0698e49ddbde269 Mon Sep 17 00:00:00 2001 From: DiSlord Live Date: Sat, 3 Dec 2022 19:52:11 +0300 Subject: [PATCH] Add lcd_set_font function Allow lcd_printf print by selected font Fix text position in browser --- ili9341.c | 18 ++++++++++-------- main.c | 2 +- nanovna.h | 4 ++++ ui.c | 17 ++++++----------- vna_browser.c | 8 +++++--- 5 files changed, 26 insertions(+), 23 deletions(-) diff --git a/ili9341.c b/ili9341.c index 5ec1c55..552f371 100644 --- a/ili9341.c +++ b/ili9341.c @@ -827,7 +827,7 @@ typedef struct { int16_t y; } lcdPrintStream; -static msg_t lcd_put(void *ip, uint8_t ch) { +static msg_t lcd_put5x7(void *ip, uint8_t ch) { lcdPrintStream *ps = ip; if (ch == '\n') {ps->x = ps->start_x; ps->y+=FONT_STR_HEIGHT; return MSG_OK;} uint16_t w = FONT_GET_WIDTH(ch); @@ -836,23 +836,25 @@ static msg_t lcd_put(void *ip, uint8_t ch) { return MSG_OK; } -#if 0 -static msg_t lcd_put_7x13(void *ip, uint8_t ch) { +static msg_t lcd_put_7x11b(void *ip, uint8_t ch) { lcdPrintStream *ps = ip; - if (ch == '\n') {ps->x = ps->start_x; ps->y+=FONT_STR_HEIGHT; return MSG_OK;} - uint16_t w = FONT_GET_WIDTH(ch); - ili9341_blitBitmap(ps->x, ps->y, w, FONT_GET_HEIGHT, FONT_GET_DATA(ch)); + if (ch == '\n') {ps->x = ps->start_x; ps->y+=bFONT_STR_HEIGHT; return MSG_OK;} + uint16_t w = bFONT_GET_WIDTH(ch); + ili9341_blitBitmap(ps->x, ps->y, w, bFONT_GET_HEIGHT, bFONT_GET_DATA(ch)); ps->x+= w; return MSG_OK; } -#endif + +typedef msg_t (*font_put_t)(void *ps, uint8_t ch); +static font_put_t put_char = lcd_put5x7; +void lcd_set_font(int type) {put_char = type == FONT_SMALL ? lcd_put5x7 : lcd_put_7x11b;} // Simple print in buffer function int lcd_printf(int16_t x, int16_t y, const char *fmt, ...) { // Init small lcd print stream struct lcd_printStreamVMT { _base_sequential_stream_methods - } lcd_vmt = {NULL, NULL, lcd_put, NULL}; + } lcd_vmt = {NULL, NULL, put_char, NULL}; lcdPrintStream ps = {&lcd_vmt, x, y, x, y}; // Performing the print operation using the common code. va_list ap; diff --git a/main.c b/main.c index 55f5c21..4fb0f43 100644 --- a/main.c +++ b/main.c @@ -2649,7 +2649,7 @@ int main(void) #ifdef TINYSA4 ili9341_set_foreground(LCD_FG_COLOR); PULSE - ili9341_drawstring("Starting...", 0,0); + ili9341_drawstring_7x13(0, 0, "Starting..."); PULSE #ifdef __DISABLE_HOT_INSERT__ sd_card_inserted_at_boot = SD_Inserted(); diff --git a/nanovna.h b/nanovna.h index 5fd9170..d6d3a9d 100644 --- a/nanovna.h +++ b/nanovna.h @@ -622,6 +622,9 @@ extern const uint8_t x7x11b_bits []; extern const uint8_t x10x14_bits[]; extern const uint8_t numfont16x22[]; +#define FONT_SMALL 0 +#define FONT_NORMAL 1 + #define FONT_START_CHAR 0x16 #define FONT_MAX_WIDTH 7 #define FONT_WIDTH 5 @@ -1110,6 +1113,7 @@ void ili9341_drawchar(uint8_t ch, int x, int y); void ili9341_drawstring(const char *str, int x, int y); void ili9341_drawstring_7x13(const char *str, int x, int y); void ili9341_drawstring_10x14(const char *str, int x, int y); +void lcd_set_font(int type); int lcd_printf(int16_t x, int16_t y, const char *fmt, ...); void ili9341_drawstringV(const char *str, int x, int y); int ili9341_drawchar_size(uint8_t ch, int x, int y, uint8_t size, int x_max); diff --git a/ui.c b/ui.c index 2fcf970..3644d7e 100644 --- a/ui.c +++ b/ui.c @@ -543,9 +543,8 @@ show_version(void) do {shift>>=1; y+=5;} while (shift&1); ili9341_drawstring_7x13(info_about[i++], x, y+=bFONT_STR_HEIGHT+2-5); } - char buf[96]; - plot_printf(buf, sizeof(buf), "HW Version:%s", get_hw_version_text()); - ili9341_drawstring_7x13(buf, x, y+=bFONT_STR_HEIGHT); + lcd_set_font(FONT_NORMAL); + lcd_printf(x, y+=bFONT_STR_HEIGHT, "HW Version:%s", get_hw_version_text()); extern const char *states[]; #define ENABLE_THREADS_COMMAND @@ -563,12 +562,10 @@ extern const char *states[]; #else uint32_t stklimit = 0U; #endif - char buf[96]; - plot_printf(buf, sizeof(buf), "%08x|%08x|%08x|%08x|%4u|%4u|%9s|%12s", + lcd_printf(x, y+=bFONT_STR_HEIGHT, "%08x|%08x|%08x|%08x|%4u|%4u|%9s|%12s", stklimit, (uint32_t)tp->ctx.sp, max_stack_use, (uint32_t)tp, (uint32_t)tp->refs - 1, (uint32_t)tp->prio, states[tp->state], tp->name == NULL ? "" : tp->name); - ili9341_drawstring_7x13(buf, x, y+=bFONT_STR_HEIGHT); tp = chRegNextThread(tp); } while (tp != NULL); #endif @@ -587,8 +584,7 @@ extern const char *states[]; #ifdef __USE_RTC__ uint32_t tr = rtc_get_tr_bin(); // TR read first uint32_t dr = rtc_get_dr_bin(); // DR read second - char buf[96]; - plot_printf(buf, sizeof(buf), "Time: 20%02d/%02d/%02d %02d:%02d:%02d" " (LS%c)", + lcd_printf(x, y, "Time: 20%02d/%02d/%02d %02d:%02d:%02d" " (LS%c)", RTC_DR_YEAR(dr), RTC_DR_MONTH(dr), RTC_DR_DAY(dr), @@ -596,15 +592,14 @@ extern const char *states[]; RTC_TR_MIN(dr), RTC_TR_SEC(dr), (RCC->BDCR & STM32_RTCSEL_MASK) == STM32_RTCSEL_LSE ? 'E' : 'I'); - ili9341_drawstring_7x13(buf, x, y); #endif #if 0 uint32_t vbat=adc_vbat_read(); - plot_printf(buf, sizeof(buf), "Batt: %d.%03dV", vbat/1000, vbat%1000); - ili9341_drawstring_7x13(buf, x, y + bFONT_STR_HEIGHT + 1); + lcd_printf(x, y + bFONT_STR_HEIGHT + 1, "Batt: %d.%03dV", vbat/1000, vbat%1000); #endif #endif // TINYSA4 } + lcd_set_font(FONT_SMALL); } #ifndef TINYSA4 diff --git a/vna_browser.c b/vna_browser.c index 48cc809..2183f2d 100644 --- a/vna_browser.c +++ b/vna_browser.c @@ -51,7 +51,7 @@ static void browser_draw_button(int idx, const char *txt) { b.border = (idx == selection) ? BROWSER_BUTTON_BORDER|BUTTON_BORDER_FALLING : BROWSER_BUTTON_BORDER|BUTTON_BORDER_RISE; if (txt == NULL) b.border|= BUTTON_BORDER_NO_FILL; draw_button(btn.x, btn.y, btn.w, btn.h, &b); - if (txt) ili9341_drawstring_7x13(txt, btn.x + btn.ofs, btn.y + (btn.h - FONT_STR_HEIGHT) / 2); + if (txt) ili9341_drawstring_7x13(txt, btn.x + btn.ofs, btn.y + (btn.h - bFONT_STR_HEIGHT) / 2); } static char to_lower(char c) {return (c >='A' && c <= 'Z') ? c - 'A' + 'a' : c;} @@ -148,7 +148,7 @@ repeat: swap_bytes(buf_16, LCD_WIDTH); ili9341_bulk(0, y, LCD_WIDTH, 1); } - ili9341_drawstring_7x13(fno.fname, 0, LCD_HEIGHT - 3*FONT_STR_HEIGHT); + ili9341_drawstring_7x13(fno.fname, 0, LCD_HEIGHT - 3*bFONT_STR_HEIGHT); } break; /* @@ -245,6 +245,7 @@ static void browser_draw_page(int page) { uint16_t start_file = (page - 1) * FILES_PER_PAGE; ili9341_set_background(LCD_MENU_COLOR); ili9341_clear_screen(); + lcd_set_font(FONT_NORMAL); while (sd_findnext(&dj, &fno) == FR_OK) { if (cnt >= start_file && cnt < (start_file + FILES_PER_PAGE)) { //uint16_t sec = ((fno.ftime<<1) & 0x3F); @@ -266,7 +267,8 @@ static void browser_draw_page(int page) { page_count = cnt == 0 ? 1 : (file_count + FILES_PER_PAGE - 1) / FILES_PER_PAGE; } browser_draw_buttons(); - lcd_printf(LCD_WIDTH / 2 - 3 * FONT_WIDTH, LCD_HEIGHT - (FILE_BOTTOM_HEIGHT + FONT_STR_HEIGHT) / 2, "- %u | %u -", page, page_count); + lcd_printf(LCD_WIDTH / 2 - 3 * bFONT_WIDTH, LCD_HEIGHT - (FILE_BOTTOM_HEIGHT + bFONT_STR_HEIGHT) / 2, "- %u | %u -", page, page_count); + lcd_set_font(FONT_SMALL); return; }