From 53d8acc671e410522eecc0202ce3862eed06d47a Mon Sep 17 00:00:00 2001 From: Drath Date: Fri, 2 Jan 2026 13:55:21 +0100 Subject: [PATCH] use std::array instead of c-style bufs --- include/LCDDisplay.hpp | 2 +- src/LCDDisplay.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/LCDDisplay.hpp b/include/LCDDisplay.hpp index cb8004b..8e8f195 100644 --- a/include/LCDDisplay.hpp +++ b/include/LCDDisplay.hpp @@ -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 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); diff --git a/src/LCDDisplay.cpp b/src/LCDDisplay.cpp index 29b5c59..1532c7b 100644 --- a/src/LCDDisplay.cpp +++ b/src/LCDDisplay.cpp @@ -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(writebuf), sizeof(writebuf)); + this->write(reinterpret_cast(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.