use std::array instead of c-style bufs

This commit is contained in:
2026-01-02 13:55:21 +01:00
parent 0eb401cdcc
commit 53d8acc671
2 changed files with 4 additions and 4 deletions

View File

@@ -12,7 +12,7 @@ class LCDDisplay {
private:
int _lcd = -1;
std::string _dev;
char writebuf[LCD_LINES * LCD_COLS + 8]{}; // 4x40 chars + 2x4 setcursor commands
std::array<char, LCD_LINES * LCD_COLS + 8> writebuf{}; // 4x40 chars + 2x4 setcursor commands
static constexpr bool dualDisp = (LCD_LINES * LCD_COLS > 80); // is it dual display device or not, evauluated from the size at compile time
void open(bool reopen = false);

View File

@@ -113,7 +113,7 @@ void LCDDisplay::writeStr(const uint8_t row, const uint8_t col, const std::strin
/* writes string to write buffer at calculated pos */
void LCDDisplay::writeBuf(const uint8_t row, const uint8_t col, const std::string& str) {
const size_t len = std::min(str.size(), sizeof(writebuf) - col);
const size_t len = std::min(str.size(), writebuf.size() - col);
uint8_t pos = 4 + col + (LCD_COLS * (row % 2)); // skip 4 bytes to leave room for set cursor command
if (row >= 2) pos += 2 * LCD_COLS + 4; // 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);
@@ -121,7 +121,7 @@ void LCDDisplay::writeBuf(const uint8_t row, const uint8_t col, const std::strin
/* initializes the write buffer to spaces */
void LCDDisplay::clearBuf() {
std::memset(writebuf, ' ', sizeof(writebuf));
std::memset(writebuf.data(), ' ', writebuf.size());
}
/* flushes writebuffer to lcd, hyperoptmized, flushes buffer and commands in a single write operation */
@@ -130,7 +130,7 @@ void LCDDisplay::flushBuf() {
std::memcpy(&writebuf[0], setDisp, 4);
std::memcpy(&writebuf[2 * LCD_COLS + 4], setDisp, 4);
writebuf[2 * LCD_COLS + 7] = 3; // overwrite set cursor c1 l1 to c1 l3
this->write(reinterpret_cast<uint8_t*>(writebuf), sizeof(writebuf));
this->write(reinterpret_cast<uint8_t*>(writebuf.data()), writebuf.size());
}
// i have a whole flushbuf func to avoid too many writes, but im writing on every bar, need to rethink this.