-
Notifications
You must be signed in to change notification settings - Fork 0
/
BufferedLcd.h
137 lines (105 loc) · 3.09 KB
/
BufferedLcd.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef BUFFEREDLCD_HEADER_GUARD
#define BUFFEREDLCD_HEADER_GUARD
#include <LiquidCrystal_I2C.h>
#include <Print.h>
#include <print_util.h>
#include "lcd_util.h"
#include "debug.h"
using namespace prnt;
using namespace lcdut;
// constexpr uint8_t nColsT = 16;
// constexpr uint8_t nRowsT = 2;
/*
Responsible for buffering the output to the LCD.
*/
template<uint8_t nColsT, uint8_t nRowsT>
class BufferedLcd : public LiquidCrystal_I2C {
public:
static constexpr uint8_t size = nRowsT * nColsT;
private:
char screenBuffer[size];
uint8_t row = 0;
uint8_t col = 0;
using super = LiquidCrystal_I2C;
size_t writeToScreen(const uint8_t* buffer, size_t size) {
DBG_Serial("Screen write called" << endl);
size_t n = 0;
while (size--) {
if (super::write(*buffer++))
n++;
else
break;
}
return n;
}
void flushNoCursorReset() {
DBG_Serial(DBG_HEADER() << "Flushing the screen buffer:" << endl);
for (uint8_t i = 0; i < nRowsT; i++) {
super::setCursor(0, i);
auto start = screenBuffer + i*nColsT;
writeToScreen((const uint8_t *)start, nColsT);
DBG_Serial("line " << i << " (string): " << start << endl);
DBG_Serial("line " << i << " (ints): ");
#if DEBUG_PRINTS
for (auto j = start; j < start + nColsT; j++)
DBG_Serial((uint8_t) *start << ' ');
#endif
DBG_Serial(endl);
}
}
public:
BufferedLcd(uint8_t lcdAddr): super(lcdAddr, nColsT, nRowsT) {
clear();
}
virtual void clear() {
memset(screenBuffer, ' ', size);
}
static constexpr uint8_t cols = nColsT;
static constexpr uint8_t rows = nRowsT;
void saveContents(char* dest) {
memcpy(dest, screenBuffer, size);
}
void restoreContents(const char* src) {
memcpy(screenBuffer, src, size);
flush();
}
void flush() {
flushNoCursorReset();
super::setCursor(col, row);
}
virtual void setCursor(uint8_t col, uint8_t row) {
DBG_Serial(DBG_HEADER() << "Cursor set to " << col << ", " << row << endl);
this->row = row;
this->col = col;
super::setCursor(col, row);
}
virtual size_t write(uint8_t val) {
if (row < nRowsT && col < nColsT) {
auto c = (char) val;
if (isPrintable(c))
DBG_Serial(c);
else
DBG_Serial(val);
// DBG_Serial(" at (" << row << ", " << col << ") ");
DBG_Serial(' ');
screenBuffer[row*nColsT + col] = val;
col++;
} else {
flushNoCursorReset();
super::write(val);
}
return 1;
}
virtual size_t write(const uint8_t* buffer, size_t size) {
DBG_Serial("Buffer write called" << endl);
size_t n = 0;
while (size--) {
if (write(*buffer++))
n++;
else
break;
}
return n;
}
};
#endif