slightly cursed but we save 160 bytes

This commit is contained in:
Drath
2025-09-18 23:58:47 +02:00
parent 057b2ccfe8
commit e54befda99
2 changed files with 11 additions and 15 deletions

View File

@@ -60,7 +60,6 @@ void LCDDisplay::setBacklight(uint8_t brightness) {
write(_lcd, &cmdbuf, brightness ? 3 : 2);
}
void LCDDisplay::setCursor(uint8_t row, uint8_t col) {
uint8_t cmdbuf[] = {254, 71, (uint8_t)(col + 1), (uint8_t)(row + 1)};
write(_lcd, &cmdbuf, 4);
@@ -92,28 +91,25 @@ void LCDDisplay::writeLines(std::array<std::string, 4> lines) {
writeStr(lines[2] + lines[3]);
}
/* writes string to line buffer at given pos*/
/* writes string to write buffer at calculated pos */
void LCDDisplay::writeBuf(uint8_t row, uint8_t col, std::string str) {
size_t len = std::min(str.size(), sizeof(linebuf[row]) - col);
std::memcpy(&linebuf[row][col], str.c_str(), len); // will overflow
size_t len = std::min(str.size(), sizeof(writebuf) - col);
uint8_t pos = 4 + col + (40 * (row % 2)); // skip 4 bytes to leave room for set cursor command
if (row >= 2) pos += 84; // if we're writing to disp2, skip another 4 bytes for set cursor command and 2*40 bytes of disp 1
std::memcpy(&writebuf[pos], str.c_str(), len);
}
void LCDDisplay::clearBuf() {
std::memset(linebuf, ' ', sizeof(linebuf));
std::memset(writebuf, ' ', sizeof(writebuf));
}
/* hyper optimized buffer flush command, writes whole disp with a single write */
void LCDDisplay::flushBuf() {
uint8_t writebuf[168]; // 4x40 chars + 2x4 setcursor commands
// we could skip all of this and set it in constructor if we were extra sure the lines wont ever exceed 40 chars
uint8_t setDisp[] = {254, 71, 1, 1}; // set cursor to char 1, line 1
std::memcpy(&writebuf[0], setDisp, 4); // copy set cursor command
std::memcpy(&writebuf[4], linebuf[0], 40); // copy 1st line
std::memcpy(&writebuf[44], linebuf[1], 40); // copy 2nd line
std::memcpy(&writebuf[84], setDisp, 3); // copy set cursor command but skip the line byte
writebuf[87] = 3; // set cursor to 3rd line
std::memcpy(&writebuf[88], linebuf[2], 40); // copy 3rd line
std::memcpy(&writebuf[128], linebuf[3], 40); // copy 4th line
std::memcpy(&writebuf[0], setDisp, 4);
std::memcpy(&writebuf[84], setDisp, 4);
writebuf[87] = 3; // overwrite set cursor c1 l1 to c1 l3
write(_lcd, writebuf, 168);
}