From 12fdf93377b2dda962b3124c3063548a851272bb Mon Sep 17 00:00:00 2001 From: Drath Date: Thu, 1 Jan 2026 19:24:41 +0100 Subject: [PATCH] clonified, should be faster too bc we're avoiding copying --- include/LCDDisplay.hpp | 10 ++++----- src/LCDDisplay.cpp | 47 +++++++++++++++++++++--------------------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/include/LCDDisplay.hpp b/include/LCDDisplay.hpp index 89af818..cb8004b 100644 --- a/include/LCDDisplay.hpp +++ b/include/LCDDisplay.hpp @@ -20,11 +20,11 @@ class LCDDisplay { void close(); // we avoid using these, use buffer (even if these are faster for small writes, perhaps a rewrite is in order) - void writeStr(std::string str); - void writeStr(uint8_t row, uint8_t col, std::string str); + void writeStr(const std::string& str); + void writeStr(uint8_t row, uint8_t col, const std::string& str); public: - explicit LCDDisplay(std::string dev, uint32_t baud = 0010002); // from termios-baud.h, B115200 + explicit LCDDisplay(const std::string& dev, uint32_t baud = 0010002); // from termios-baud.h, B115200 ~LCDDisplay(); void clear(); @@ -34,13 +34,13 @@ class LCDDisplay { void setCursor(uint8_t row, uint8_t col); void setCustomChar(uint8_t index, std::array cchar); - void writeBuf(uint8_t row, uint8_t col, std::string str); + void writeBuf(uint8_t row, uint8_t col, const std::string& str); void clearBuf(); void flushBuf(); char bar(uint8_t char_id, double value); // returns custom char no. c, small enough to define in header - static constexpr char cc(const uint8_t c) { return c % 8; } + static constexpr char cc(const uint8_t c) { return static_cast(c % 8); } }; #endif diff --git a/src/LCDDisplay.cpp b/src/LCDDisplay.cpp index ade1ca4..29b5c59 100644 --- a/src/LCDDisplay.cpp +++ b/src/LCDDisplay.cpp @@ -11,11 +11,11 @@ #include /* lcd size hardcoded to 4x40 idgaf */ -LCDDisplay::LCDDisplay(std::string dev, uint32_t baud) { +LCDDisplay::LCDDisplay(const std::string& dev, const uint32_t baud) { _dev = dev; this->open(); // calls this (LCDDisplay) open() function, LCDDisplay::open() - struct termios term; + struct termios term{}; // Setup the serial port 8 data bits, no parity, 1 stopbit, no flow control term.c_cflag = baud | CS8 | CSTOPB | CLOCAL | CREAD; term.c_iflag = 0; @@ -39,7 +39,7 @@ LCDDisplay::~LCDDisplay() { this->close(); } -void LCDDisplay::open(bool reopen) { +void LCDDisplay::open(const bool reopen) { applog(LOG::INF, "Opening serial port at \"{}\"", _dev); if (_lcd < 0 || reopen) _lcd = ::open(_dev.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); // ::open calls the global open() instead of LCDDisplay::open() @@ -57,9 +57,8 @@ void LCDDisplay::close() { _lcd = -1; } -void LCDDisplay::write(const uint8_t* cmdbuf, uint8_t len) { - int rc = ::write(_lcd, cmdbuf, len); - if(rc < 0) { +void LCDDisplay::write(const uint8_t* cmdbuf, const uint8_t len) { + if (::write(_lcd, cmdbuf, len) != len) { applog(LOG::ERR, "failed to execute ::write, lost access to port?"); this->open(true); } @@ -70,51 +69,51 @@ void LCDDisplay::clear() { this->write(cmdbuf, sizeof(cmdbuf)); } -void LCDDisplay::setContrast(uint8_t contrast) { +void LCDDisplay::setContrast(const uint8_t contrast) { applog(LOG::INF, "Set contrast to {}", contrast); const uint8_t cmdbuf[] = {254, 80, contrast}; this->write(cmdbuf, sizeof(cmdbuf)); } -void LCDDisplay::setBacklight(bool enabled) { +void LCDDisplay::setBacklight(const bool enabled) { applog(LOG::INF, "Set backlight to {}", enabled); - const uint8_t cmdbuf[] = {254, (uint8_t)(enabled ? 66 : 70), 255}; + const uint8_t cmdbuf[] = {254, static_cast(enabled ? 66 : 70), 255}; this->write(cmdbuf, enabled ? 3 : 2); } /* still doesnt work? */ -void LCDDisplay::setBacklight(uint8_t brightness) { +void LCDDisplay::setBacklight(const uint8_t brightness) { applog(LOG::INF, "Set backlight brightness to {}", brightness); - const uint8_t cmdbuf[] = {254, (uint8_t)(brightness ? 66 : 70), 255, brightness}; // 3rd char is timer, 4th is brightness + const uint8_t cmdbuf[] = {254, static_cast(brightness ? 66 : 70), 255, brightness}; // 3rd char is timer, 4th is brightness this->write(cmdbuf, brightness ? 4 : 2); } -void LCDDisplay::setCursor(uint8_t row, uint8_t col) { - const uint8_t cmdbuf[] = {254, 71, (uint8_t)(col + 1), (uint8_t)(row + 1)}; +void LCDDisplay::setCursor(const uint8_t row, const uint8_t col) { + const uint8_t cmdbuf[] = {254, 71, static_cast(col + 1), static_cast(row + 1)}; this->write(cmdbuf, sizeof(cmdbuf)); } /* reconsider so im not writing every time. prepend custom chars to the write buffer? */ -void LCDDisplay::setCustomChar(uint8_t index, std::array cchar) { +void LCDDisplay::setCustomChar(const uint8_t index, const std::array cchar) { uint8_t cmdbuf[] = {254, 78, index, 0, 0, 0, 0, 0, 0, 0, 0}; std::memcpy(&cmdbuf[3], &cchar, sizeof(cchar)); this->write(cmdbuf, sizeof(cmdbuf)); } /* immediately writes string to the lcd at current cursor pos */ -void LCDDisplay::writeStr(std::string str) { - this->write((uint8_t*)(str.c_str()), str.size()); +void LCDDisplay::writeStr(const std::string& str) { + this->write(reinterpret_cast(str.c_str()), str.size()); } /* immediately writes string to the lcd at given cursor pos */ -void LCDDisplay::writeStr(uint8_t row, uint8_t col, std::string str) { +void LCDDisplay::writeStr(const uint8_t row, const uint8_t col, const std::string& str) { this->setCursor(row, col); - this->writeStr(str); + this->write(reinterpret_cast(str.c_str()), str.size()); } /* 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(writebuf) - col); +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); 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); @@ -131,16 +130,16 @@ 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((uint8_t*)writebuf, sizeof(writebuf)); + this->write(reinterpret_cast(writebuf), sizeof(writebuf)); } // i have a whole flushbuf func to avoid too many writes, but im writing on every bar, need to rethink this. // we could have a separate pre-char buffer and put all the setCustomChar commands to that? -char LCDDisplay::bar(uint8_t char_id, double value) { - std::array b; +char LCDDisplay::bar(const uint8_t char_id, const double value) { + std::array b{}; b.fill(0b01110); for (uint8_t i = 0; i < (1.0 - value) * 8; i++) b[i] = 0; this->setCustomChar(char_id, b); - return this->cc(char_id); + return cc(char_id); }