-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.cpp
More file actions
234 lines (182 loc) · 4.72 KB
/
Copy pathdisplay.cpp
File metadata and controls
234 lines (182 loc) · 4.72 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "display.h"
#include "config.h"
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Fonts/FreeSans9pt7b.h>
#include <GxEPD2_BW.h>
#include <vector>
// Create the one global display object
Display display;
// Create the e-paper driver
GxEPD2_BW<GxEPD2_290_T94_V2, GxEPD2_290_T94_V2::HEIGHT> epd(
GxEPD2_290_T94_V2(
PIN_CS,
PIN_DC,
PIN_RST,
PIN_BUSY
)
);
uint16_t getTextWidth(const String& text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
epd.getTextBounds(text.c_str(), 0, 0, &x1, &y1, &width, &height);
return width;
}
uint16_t getTextHeight(const String& text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
epd.getTextBounds(text.c_str(), 0, 0, &x1, &y1, &width, &height);
return height;
}
int getTextTopOffset(const String& text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
epd.getTextBounds(text.c_str(), 0, 0, &x1, &y1, &width, &height);
return y1;
}
uint16_t getLineHeight() {
return getTextHeight("Ag") + 4;
}
void configureTextSize(uint8_t textSize) {
if (textSize == DISPLAY_TEXT_MEDIUM) {
epd.setFont(&FreeSans9pt7b);
epd.setTextSize(1);
}
else {
epd.setFont();
epd.setTextSize(textSize);
}
}
std::vector<String> wrapText(int x, const char* text) {
std::vector<String> lines;
const int maxWidth = epd.width() - x;
String line;
String word;
auto pushLine = [&]() {
lines.push_back(line);
line = "";
};
auto addWord = [&]() {
if (word.length() == 0) {
return;
}
String candidate = line.length() == 0 ? word : line + " " + word;
if (line.length() == 0 || getTextWidth(candidate) <= maxWidth) {
line = candidate;
}
else {
pushLine();
line = word;
}
word = "";
};
for (const char* cursor = text; *cursor != '\0'; cursor++) {
const char c = *cursor;
if (c == '\n') {
addWord();
pushLine();
}
else if (c == ' ' || c == '\t' || c == '\r') {
addWord();
}
else {
word += c;
}
}
addWord();
if (line.length() > 0 || lines.empty()) {
pushLine();
}
return lines;
}
void drawWrappedText(int x, int y, const char* text) {
const std::vector<String> lines = wrapText(x, text);
const int lineHeight = getLineHeight();
const int visibleHeight = epd.height() - y;
const int totalHeight = lines.size() * lineHeight;
int startY = y;
if (totalHeight > visibleHeight) {
startY = epd.height() - totalHeight;
}
for (size_t i = 0; i < lines.size(); i++) {
const int lineTop = startY + (i * lineHeight);
if (lineTop + lineHeight < y) {
continue;
}
if (lineTop >= epd.height()) {
break;
}
if (lines[i].length() == 0) {
continue;
}
epd.setCursor(x, lineTop - getTextTopOffset(lines[i]));
epd.print(lines[i]);
}
}
void Display::begin() {
Serial.begin(115200);
Serial.println("Initializing display...");
#if defined(ESP32)
SPI.begin(
PIN_CLK, // SCK
-1, // MISO (unused)
PIN_DIN, // MOSI
PIN_CS // SS
);
#else
SPI.begin();
#endif
epd.init();
epd.setRotation(-1);
Serial.println("Display ready!");
}
void Display::clear() {
epd.setFullWindow();
epd.firstPage();
do
{
epd.fillScreen(GxEPD_WHITE);
}
while (epd.nextPage());
}
void Display::printText(int x, int y, const char* text, uint8_t textSize) {
epd.setFullWindow();
epd.firstPage();
do {
epd.fillScreen(GxEPD_WHITE);
epd.setTextColor(GxEPD_BLACK);
configureTextSize(textSize);
epd.setTextWrap(false);
drawWrappedText(x, y, text);
}
while (epd.nextPage());
}
void Display::printTextPartial(int x, int y, const char* text, uint8_t textSize) {
epd.setPartialWindow(0, 0, epd.width(), epd.height());
epd.firstPage();
do {
epd.fillScreen(GxEPD_WHITE);
epd.setTextColor(GxEPD_BLACK);
configureTextSize(textSize);
epd.setTextWrap(false);
drawWrappedText(x, y, text);
}
while (epd.nextPage());
}
void Display::printCentered(const char* text) {
int16_t x1;
int16_t y1;
uint16_t width;
uint16_t height;
configureTextSize(DISPLAY_TEXT_LARGE);
epd.getTextBounds(text, 0, 0, &x1, &y1, &width, &height);
const int x = ((epd.width() - width) / 2) - x1;
const int y = ((epd.height() - height) / 2) - y1;
printText(x, y, text);
}