From aa45690833a56639bc12c5d37a56dc612cf01005 Mon Sep 17 00:00:00 2001 From: Chris Giard Date: Wed, 3 Jan 2024 16:23:15 -1000 Subject: [PATCH 1/2] Optimize lcd_write_char by using lcd_draw_image (indirectly using i2c_burst_transfer). Fix an off-by-one error in lcd_fill_rectangle. Remove an unnecessary REG_SYNC from lcd_write_string. --- hardware/st7735/st7735.c | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/hardware/st7735/st7735.c b/hardware/st7735/st7735.c index 519b905..e829d30 100644 --- a/hardware/st7735/st7735.c +++ b/hardware/st7735/st7735.c @@ -2,6 +2,7 @@ #include "st7735.h" #include "time.h" #include +#include #include #include #include @@ -40,25 +41,28 @@ void lcd_set_address_window(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1) */ void lcd_write_char(uint16_t x, uint16_t y, char ch, FontDef font, uint16_t color, uint16_t bgcolor) { - uint32_t i, b, j; + size_t fbsize = font.height * font.width * sizeof(uint16_t); + uint16_t *buff; + uint32_t i, b, j, offset; - lcd_set_address_window(x, y, x + font.width - 1, y + font.height - 1); + /* Allocate a buffer to hold the screen data */ + buff = malloc(fbsize); + if (!buff) return; + /* Render the character to the buffer */ for (i = 0; i < font.height; i++) { + offset = i * font.width; b = font.data[(ch - 32) * font.height + i]; for (j = 0; j < font.width; j++) { - if ((b << j) & 0x8000) - { - i2c_write_data(color >> 8, color & 0xFF); - } - else - { - i2c_write_data(bgcolor >> 8, bgcolor & 0xFF); - } + buff[offset + j] = ((b << j) & 0x8000) ? color : bgcolor; } } + + /* Push the image to the screen */ + lcd_draw_image(x, y, font.width, font.height, (uint8_t*)buff); + free(buff); } void lcd_write_ch(uint16_t x, uint16_t y, char ch, FontType font, uint16_t color, uint16_t bgcolor) @@ -106,7 +110,6 @@ void lcd_write_string(uint16_t x, uint16_t y, char *str, FontDef font, uint16_t } lcd_write_char(x, y, *str, font, color, bgcolor); - i2c_write_command(SYNC_REG, 0x00, 0x01); x += font.width; str++; } @@ -141,9 +144,9 @@ void lcd_fill_rectangle(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t // clipping if ((x >= ST7735_WIDTH) || (y >= ST7735_HEIGHT)) return; - if ((x + w - 1) >= ST7735_WIDTH) + if ((x + w) >= ST7735_WIDTH) w = ST7735_WIDTH - x; - if ((y + h - 1) >= ST7735_HEIGHT) + if ((y + h) >= ST7735_HEIGHT) h = ST7735_HEIGHT - y; lcd_set_address_window(x, y, x + w - 1, y + h - 1); From 388c6ca7b1801085cc73895e27a7a1c42a4e6737 Mon Sep 17 00:00:00 2001 From: Chris Giard Date: Thu, 4 Jan 2024 10:57:13 -1000 Subject: [PATCH 2/2] Fix bug in lcd_draw_image --- hardware/st7735/st7735.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hardware/st7735/st7735.c b/hardware/st7735/st7735.c index e829d30..34d1efe 100644 --- a/hardware/st7735/st7735.c +++ b/hardware/st7735/st7735.c @@ -173,10 +173,8 @@ void lcd_fill_screen(uint16_t color) void lcd_draw_image(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint8_t *data) { - uint16_t col = h - y; - uint16_t row = w - x; lcd_set_address_window(x, y, x + w - 1, y + h - 1); - i2c_burst_transfer(data, sizeof(uint16_t) * col * row); + i2c_burst_transfer(data, sizeof(uint16_t) * w * h); } uint8_t lcd_begin(void)