cleanup lcddisplay a lil

This commit is contained in:
Drath
2025-10-29 18:29:13 +01:00
parent 871edb642e
commit 25cee5f669
3 changed files with 44 additions and 36 deletions

View File

@@ -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<uint8_t, 8> 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<std::string, 4> lines) {
@@ -110,8 +111,8 @@ void LCDDisplay::writeLines(std::array<std::string, 4> 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);
}