diff --git a/include/LCDDisplay.hpp b/include/LCDDisplay.hpp index 457e751..2b2c872 100644 --- a/include/LCDDisplay.hpp +++ b/include/LCDDisplay.hpp @@ -18,14 +18,18 @@ class LCDDisplay { private: - uint8_t _lcd; + uint8_t _lcd = -1; + std::string _dev; 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 - + + public: LCDDisplay(std::string dev, uint32_t baud = 0010002); // from termios-baud.h, B115200 ~LCDDisplay(); + void _open(); + void _close(); void clear(); void setContrast(uint8_t contrast); diff --git a/src/LCDDisplay.cpp b/src/LCDDisplay.cpp index a3c732f..6d7e0fc 100644 --- a/src/LCDDisplay.cpp +++ b/src/LCDDisplay.cpp @@ -2,23 +2,17 @@ /* lcd size hardcoded to 4x40 idgaf */ LCDDisplay::LCDDisplay(std::string dev, uint32_t baud) { - log(LOG::INF, "LCDDisplay", "Opening LCD at \"{}\"", dev); + _dev = dev; + _open(); + struct termios term; - int lcd = open(dev.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); - - if (lcd < 0) { - log(LOG::ERR, "LCDDisplay", "Error opening serial port \"{}\"", dev); // err - exit(lcd); - } - // 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; term.c_oflag = 0; term.c_lflag = 0; - tcflush(lcd, TCIFLUSH); - tcsetattr(lcd, TCSANOW, &term); - _lcd = lcd; + tcflush(_lcd, TCIFLUSH); + tcsetattr(_lcd, TCSANOW, &term); // init linebuf to spaces clearBuf(); @@ -32,7 +26,21 @@ LCDDisplay::LCDDisplay(std::string dev, uint32_t baud) { } LCDDisplay::~LCDDisplay() { - close(_lcd); + _close(); +} + +void LCDDisplay::_open() { + _lcd = open(_dev.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); + if (_lcd < 0) { + log(LOG::ERR, "LCDDisplay", "Error opening serial port \"{}\"", dev); // err + exit(_lcd); + } + log(LOG::INF, "LCDDisplay", "Opened LCD at \"{}\"", _dev); +} + +void LCDDisplay::_close() { + if (_lcd >= 0) close(_lcd); + _lcd = -1; log(LOG::INF, "LCDDisplay", "LCD port closed"); } @@ -117,7 +125,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(); } char LCDDisplay::bar(uint8_t char_id, double value) {