From 25cee5f669731ac76b0a780817f01908fbd635a0 Mon Sep 17 00:00:00 2001 From: Drath Date: Wed, 29 Oct 2025 18:29:13 +0100 Subject: [PATCH] cleanup lcddisplay a lil --- include/LCDDisplay.hpp | 11 +++++----- main.cpp | 20 +++++++++++------ src/LCDDisplay.cpp | 49 +++++++++++++++++++++--------------------- 3 files changed, 44 insertions(+), 36 deletions(-) diff --git a/include/LCDDisplay.hpp b/include/LCDDisplay.hpp index ed997e6..e91ad5f 100644 --- a/include/LCDDisplay.hpp +++ b/include/LCDDisplay.hpp @@ -15,12 +15,15 @@ class LCDDisplay { char writebuf[LCD_LINES * LCD_COLS + 8]; // 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 writeLines(std::array lines); // do not use + // 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); public: LCDDisplay(std::string dev, uint32_t baud = 0010002); // from termios-baud.h, B115200 ~LCDDisplay(); - void _open(); - void _close(); + void open(); + void close(); void clear(); void setContrast(uint8_t contrast); @@ -29,8 +32,6 @@ class LCDDisplay { void setCursor(uint8_t row, uint8_t col); void setCustomChar(uint8_t index, std::array cchar); - void writeStr(std::string str); - void writeStr(uint8_t row, uint8_t col, std::string str); void writeBuf(uint8_t row, uint8_t col, std::string str); void clearBuf(); void flushBuf(); @@ -41,4 +42,4 @@ class LCDDisplay { char bar(uint8_t char_id, double value); }; -#endif \ No newline at end of file +#endif diff --git a/main.cpp b/main.cpp index df4bceb..981aa2b 100644 --- a/main.cpp +++ b/main.cpp @@ -92,17 +92,24 @@ void sensorsThread(std::stop_token st, Sensors* s) { void displayThread(std::stop_token st, Sensors* s) { log(LOG::INF, "Thread", "Starting display_thread, pid {}", gettid()); + // init internal variables of LCDDisplay class LCDDisplay lcd(LCD_PORT); + // basic lcd setup + lcd.open(); lcd.setContrast(LCD_CONTRAST); lcd.setBacklight(true); - lcd.writeStr(2, 0, center("Waiting for sensors...", 40)); - lcd.setCustomChar(0, {16, 6, 9, 8, 8, 9, 6, 0}); // celcius lcd.setCustomChar(1, {0, 0, 4, 10, 10, 17, 31, 0}); // delta lcd.setCustomChar(2, {0, 8, 21, 21, 2, 0, 0, 0}); // home (tilde) - lcd._close(); + lcd.close(); + // welcome msg + lcd.writeBuf(2, 0, center("Waiting for sensors...", 40)); + lcd.flushBuf(); + lcd.clearBuf(); + + // actual main display loop while (!st.stop_requested()) { sem::display.acquire(); // holds the loop until sensor data is ready { @@ -147,10 +154,9 @@ void displayThread(std::stop_token st, Sensors* s) { lcd.clearBuf(); } - lcd._open(); - lcd.clear(); - lcd.writeStr(2, 0, center("linuxSmartie closed", 40)); - lcd._close(); + // goodbye msg + lcd.writeBuf(2, 0, center("linuxSmartie closed", 40)); + lcd.flushBuf(); } int main(int argc, char* argv[]) { diff --git a/src/LCDDisplay.cpp b/src/LCDDisplay.cpp index 54c8ceb..78bed28 100644 --- a/src/LCDDisplay.cpp +++ b/src/LCDDisplay.cpp @@ -13,7 +13,7 @@ /* lcd size hardcoded to 4x40 idgaf */ LCDDisplay::LCDDisplay(std::string dev, uint32_t baud) { _dev = dev; - _open(); + this->open(); // calls this (LCDDisplay) open() function, LCDDisplay::open() struct termios term; // Setup the serial port 8 data bits, no parity, 1 stopbit, no flow control @@ -25,22 +25,23 @@ LCDDisplay::LCDDisplay(std::string dev, uint32_t baud) { tcsetattr(_lcd, TCSANOW, &term); // init linebuf to spaces - clearBuf(); + this->clearBuf(); // disable underline and block cursor uint8_t cmdbuf[] = {254, 75, 254, 84}; - write(_lcd, &cmdbuf, 4); + ::write(_lcd, &cmdbuf, 4); // clear display and set cursor home - clear(); + this->clear(); + this->close(); } LCDDisplay::~LCDDisplay() { - _close(); + this->close(); } -void LCDDisplay::_open() { - _lcd = open(_dev.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); +void LCDDisplay::open() { + _lcd = ::open(_dev.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); // ::open calls the global open() instead of LCDDisplay::open() if (_lcd < 0) { log(LOG::ERR, "LCDDisplay", "Error opening serial port \"{}\"", _dev); // err exit(_lcd); @@ -48,57 +49,57 @@ void LCDDisplay::_open() { // log(LOG::INF, "LCDDisplay", "Opened LCD at \"{}\"", _dev); } -void LCDDisplay::_close() { - if (_lcd >= 0) close(_lcd); +void LCDDisplay::close() { + if (_lcd >= 0) ::close(_lcd); _lcd = -1; // log(LOG::INF, "LCDDisplay", "LCD port closed"); } void LCDDisplay::clear() { uint8_t cmdbuf[] = {254, 88}; - write(_lcd, &cmdbuf, 2); + ::write(_lcd, &cmdbuf, 2); } void LCDDisplay::setContrast(uint8_t contrast) { log(LOG::INF, "LCDDisplay::setContrast", "Set contrast to {}", contrast); uint8_t cmdbuf[] = {254, 80, contrast}; - write(_lcd, &cmdbuf, 3); + ::write(_lcd, &cmdbuf, 3); } void LCDDisplay::setBacklight(bool enabled) { log(LOG::INF, "LCDDisplay::setBacklight", "Set backlight to {}", enabled); uint8_t cmdbuf[] = {254, (uint8_t)(enabled ? 66 : 70), 255}; - write(_lcd, &cmdbuf, enabled ? 3 : 2); + ::write(_lcd, &cmdbuf, enabled ? 3 : 2); } /* still doesnt work? */ void LCDDisplay::setBacklight(uint8_t brightness) { log(LOG::INF, "LCDDisplay::setBacklight", "Set backlight brightness to {}", brightness); uint8_t cmdbuf[] = {254, (uint8_t)(brightness ? 66 : 70), 255, brightness}; // 3rd char is timer, 4th is brightness - write(_lcd, &cmdbuf, brightness ? 4 : 2); + ::write(_lcd, &cmdbuf, brightness ? 4 : 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); + ::write(_lcd, &cmdbuf, 4); } void LCDDisplay::setCustomChar(uint8_t index, std::array cchar) { uint8_t cmdbuf[11] = {254, 78, index, 0, 0, 0, 0, 0, 0, 0, 0}; for (uint8_t i = 0; i < 8; i++) cmdbuf[i + 3] = cchar[i]; - write(_lcd, &cmdbuf, 11); + ::write(_lcd, &cmdbuf, 11); } /* immediately writes string to the lcd at current cursor pos */ void LCDDisplay::writeStr(std::string str) { - write(_lcd, str.c_str(), str.size()); + ::write(_lcd, 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) { - setCursor(row, col); - writeStr(str); + this->setCursor(row, col); + this->writeStr(str); } void LCDDisplay::writeLines(std::array lines) { @@ -110,8 +111,8 @@ void LCDDisplay::writeLines(std::array lines) { line.insert(line.end(), LCD_COLS - line.size(), ' '); } - writeStr(0, 0, lines[0] + lines[1]); - writeStr(2, 0, lines[2] + lines[3]); + this->writeStr(0, 0, lines[0] + lines[1]); + this->writeStr(2, 0, lines[2] + lines[3]); } /* writes string to write buffer at calculated pos */ @@ -135,9 +136,9 @@ void LCDDisplay::flushBuf() { std::memcpy(&writebuf[2 * LCD_COLS + 4], setDisp, 4); writebuf[2 * LCD_COLS + 7] = 3; // overwrite set cursor c1 l1 to c1 l3 - _open(); - write(_lcd, writebuf, sizeof(writebuf)); - _close(); + this->open(); + ::write(_lcd, writebuf, sizeof(writebuf)); + this->close(); } char LCDDisplay::bar(uint8_t char_id, double value) { @@ -145,6 +146,6 @@ char LCDDisplay::bar(uint8_t char_id, double value) { b.fill(0b01110); for (uint8_t i = 0; i < (1.0 - value) * 8; i++) b[i] = 0; - setCustomChar(char_id, b); + this->setCustomChar(char_id, b); return cc(char_id); }