From 804e477d50b7abaf486ba4e94ce4716b07285dc8 Mon Sep 17 00:00:00 2001 From: Drath Date: Sun, 14 Jun 2026 11:55:14 +0200 Subject: [PATCH] optimize sending custom chars, one write operation now --- include/lcd.h | 3 ++- src/lcd.c | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/lcd.h b/include/lcd.h index b3dde6c..806a456 100644 --- a/include/lcd.h +++ b/include/lcd.h @@ -10,7 +10,8 @@ struct lcd_t { int fd; uint32_t baud; - char writebuf[LCD_LINES * LCD_COLS + 8]; + char ccharbuf[8][11]; // 8 custom chars, each 11 bytes (3 byte command + 8 byte glyph) + char writebuf[LCD_LINES * LCD_COLS + 8]; // number of chars on the lcd + 2*4 byte set cursor commands char port[96]; }; diff --git a/src/lcd.c b/src/lcd.c index 0accaec..d132242 100644 --- a/src/lcd.c +++ b/src/lcd.c @@ -59,11 +59,10 @@ void lcd_set_cursor(struct lcd_t *lcd, const uint8_t row, const uint8_t col) { lcd_write(lcd, cmdbuf, sizeof(cmdbuf)); } -/* reconsider so im not writing every time. prepend custom chars to the write buffer? */ void lcd_set_custom_char(struct lcd_t *lcd, const uint8_t index, const lcd_glyph_t *glyph) { uint8_t cmdbuf[] = {254, 78, index, 0, 0, 0, 0, 0, 0, 0, 0}; memcpy(&cmdbuf[3], glyph, sizeof(lcd_glyph_t)); - lcd_write(lcd, cmdbuf, sizeof(cmdbuf)); + memcpy(lcd->ccharbuf[index], cmdbuf, sizeof(cmdbuf)); } /* writes string to write buffer at calculated pos, we trust it to be null terminated */ @@ -104,7 +103,8 @@ inline void lcd_flush_buf(struct lcd_t *lcd) { memcpy(&lcd->writebuf[0], set_disp, 4); memcpy(&lcd->writebuf[2 * LCD_COLS + 4], set_disp, 4); lcd->writebuf[2 * LCD_COLS + 7] = 3; // overwrite set cursor c1 l1 to c1 l3 - lcd_write(lcd, (const uint8_t *)lcd->writebuf, sizeof(lcd->writebuf)); + // hacky, relies on charbuf being before writebuf in the lcd struct. we start at the charbuf pointer and write both at the same time + lcd_write(lcd, (const uint8_t *)lcd->ccharbuf, sizeof(lcd->ccharbuf) + sizeof(lcd->writebuf)); } // i have a whole flushbuf func to avoid too many writes, but im writing on every bar, need to rethink this. @@ -131,6 +131,12 @@ int lcd_init(struct lcd_t *lcd) { tcflush(lcd->fd, TCIFLUSH); tcsetattr(lcd->fd, TCSANOW, &term); + // zero out custom chars + for (uint8_t i = 0; i < 8; i++) { + const uint8_t charbuf[] = {254, 78, i, 0, 0, 0, 0, 0, 0, 0, 0}; + memcpy(lcd->ccharbuf[i], charbuf, sizeof(charbuf)); + } + lcd_clear_buf(lcd); const uint8_t cmdbuf[] = {254, 75, 254, 84}; lcd_write(lcd, cmdbuf, sizeof(cmdbuf));