slightly cursed but we save 160 bytes
This commit is contained in:
@@ -9,7 +9,7 @@
|
|||||||
class LCDDisplay {
|
class LCDDisplay {
|
||||||
private:
|
private:
|
||||||
uint8_t _lcd;
|
uint8_t _lcd;
|
||||||
char linebuf[4][40];
|
char writebuf[168]; // 4x40 chars + 2x4 setcursor commands
|
||||||
void writeStr(std::string str); // do not use
|
void writeStr(std::string str); // do not use
|
||||||
void writeLines(std::array<std::string, 4> lines); // do not use
|
void writeLines(std::array<std::string, 4> lines); // do not use
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,6 @@ void LCDDisplay::setBacklight(uint8_t brightness) {
|
|||||||
write(_lcd, &cmdbuf, brightness ? 3 : 2);
|
write(_lcd, &cmdbuf, brightness ? 3 : 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void LCDDisplay::setCursor(uint8_t row, uint8_t col) {
|
void LCDDisplay::setCursor(uint8_t row, uint8_t col) {
|
||||||
uint8_t cmdbuf[] = {254, 71, (uint8_t)(col + 1), (uint8_t)(row + 1)};
|
uint8_t cmdbuf[] = {254, 71, (uint8_t)(col + 1), (uint8_t)(row + 1)};
|
||||||
write(_lcd, &cmdbuf, 4);
|
write(_lcd, &cmdbuf, 4);
|
||||||
@@ -92,28 +91,25 @@ void LCDDisplay::writeLines(std::array<std::string, 4> lines) {
|
|||||||
writeStr(lines[2] + lines[3]);
|
writeStr(lines[2] + lines[3]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* writes string to line buffer at given pos*/
|
/* writes string to write buffer at calculated pos */
|
||||||
void LCDDisplay::writeBuf(uint8_t row, uint8_t col, std::string str) {
|
void LCDDisplay::writeBuf(uint8_t row, uint8_t col, std::string str) {
|
||||||
size_t len = std::min(str.size(), sizeof(linebuf[row]) - col);
|
size_t len = std::min(str.size(), sizeof(writebuf) - col);
|
||||||
std::memcpy(&linebuf[row][col], str.c_str(), len); // will overflow
|
uint8_t pos = 4 + col + (40 * (row % 2)); // skip 4 bytes to leave room for set cursor command
|
||||||
|
if (row >= 2) pos += 84; // 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LCDDisplay::clearBuf() {
|
void LCDDisplay::clearBuf() {
|
||||||
std::memset(linebuf, ' ', sizeof(linebuf));
|
std::memset(writebuf, ' ', sizeof(writebuf));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* hyper optimized buffer flush command, writes whole disp with a single write */
|
/* hyper optimized buffer flush command, writes whole disp with a single write */
|
||||||
void LCDDisplay::flushBuf() {
|
void LCDDisplay::flushBuf() {
|
||||||
uint8_t writebuf[168]; // 4x40 chars + 2x4 setcursor commands
|
// we could skip all of this and set it in constructor if we were extra sure the lines wont ever exceed 40 chars
|
||||||
uint8_t setDisp[] = {254, 71, 1, 1}; // set cursor to char 1, line 1
|
uint8_t setDisp[] = {254, 71, 1, 1}; // set cursor to char 1, line 1
|
||||||
|
std::memcpy(&writebuf[0], setDisp, 4);
|
||||||
std::memcpy(&writebuf[0], setDisp, 4); // copy set cursor command
|
std::memcpy(&writebuf[84], setDisp, 4);
|
||||||
std::memcpy(&writebuf[4], linebuf[0], 40); // copy 1st line
|
writebuf[87] = 3; // overwrite set cursor c1 l1 to c1 l3
|
||||||
std::memcpy(&writebuf[44], linebuf[1], 40); // copy 2nd line
|
|
||||||
std::memcpy(&writebuf[84], setDisp, 3); // copy set cursor command but skip the line byte
|
|
||||||
writebuf[87] = 3; // set cursor to 3rd line
|
|
||||||
std::memcpy(&writebuf[88], linebuf[2], 40); // copy 3rd line
|
|
||||||
std::memcpy(&writebuf[128], linebuf[3], 40); // copy 4th line
|
|
||||||
|
|
||||||
write(_lcd, writebuf, 168);
|
write(_lcd, writebuf, 168);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user