open and close funcs

This commit is contained in:
Drath
2025-10-23 13:02:45 +02:00
parent 54bbc8c117
commit 69b0291205
2 changed files with 28 additions and 14 deletions

View File

@@ -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<std::string, 4> 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);

View File

@@ -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) {