-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay.h
More file actions
264 lines (225 loc) · 6.25 KB
/
display.h
File metadata and controls
264 lines (225 loc) · 6.25 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include "fonts.h"
#include "SSD1306Wire.h" // https://github.com/ThingPulse/esp8266-oled-ssd1306
SSD1306Wire* display = NULL;
QueueHandle_t displayQueue;
enum FontSize {FONT_SIZE_12, FONT_SIZE_36};
enum ProgressDirection {PROGRESS_DIRECTION_LEFT_TO_RIGHT, PROGRESS_DIRECTION_RIGHT_TO_LEFT};
enum DisplayItemType {DI_TYPE_TEXT, DI_TYPE_TEXT_PROGRESS, DI_TYPE_PROGRESS, DI_TYPE_PROGRESS_CANCEL, DI_TYPE_TEXT_PROGRESS_CANCEL};
class DisplayItem {
public:
DisplayItem(
DisplayItemType _type,
String _text = "",
FontSize _fontSize = FONT_SIZE_12,
int32_t _progressStartTime = 0,
int32_t _progressEndTime = 0,
ProgressDirection _progressDirection = PROGRESS_DIRECTION_LEFT_TO_RIGHT
): type(_type),
fontSize(_fontSize),
progressStartTime(_progressStartTime),
progressEndTime(_progressEndTime),
progressDirection(_progressDirection)
{
strcpy(text, _text.c_str());
}
DisplayItem():
type(DI_TYPE_TEXT),
text(""),
fontSize(FONT_SIZE_12),
progressStartTime(0),
progressEndTime(0),
progressDirection(PROGRESS_DIRECTION_LEFT_TO_RIGHT)
{
}
DisplayItemType type;
char text[255];
FontSize fontSize;
int64_t progressStartTime;
int64_t progressEndTime;
ProgressDirection progressDirection;
};
DisplayItem progressItem;
DisplayItem lastItem;
bool progressShown = false;
bool isDisplayOff = false;
String lastMessage = "";
int lastColor = 0;
bool isScreenSaverActive() {
if (idleFromTime == 0) {
return false;
}
uint32_t now = millis();
uint32_t diff = now - idleFromTime;
return diff > SCREEN_SAVER_TIMEOUT_SECONDS * 1000;
}
void initDisplay() {
if (display != NULL) {
return;
}
#ifdef DEBUG
Serial.println("Initializing display...");
#endif
display = new SSD1306Wire(0x3c, SDA, SCL, GEOMETRY_128_32);
display->init();
display->flipScreenVertically();
display->setBrightness(128);
}
void drawItem(DisplayItem& item) {
switch (item.type) {
case DI_TYPE_TEXT_PROGRESS:
#ifdef DEBUG
//Serial.println("TEXT PROGRESS");
#endif
lastItem = item;
case DI_TYPE_PROGRESS:
#ifdef DEBUG
//Serial.println("PROGRESS");
#endif
progressItem = item;
break;
case DI_TYPE_TEXT_PROGRESS_CANCEL:
#ifdef DEBUG
//Serial.println("TEXT_PROGRESS_CANCEL");
#endif
lastItem = item;
case DI_TYPE_PROGRESS_CANCEL:
#ifdef DEBUG
//Serial.println("PROGRESS_CANCEL");
#endif
progressItem = item;
break;
case DI_TYPE_TEXT:
#ifdef DEBUG
//Serial.println("TEXT");
#endif
lastItem = item;
break;
}
}
void refreshDisplay() {
if (isScreenSaverActive()) {
display->displayOff();
isDisplayOff = true;
return;
}
if (isDisplayOff) {
display->displayOn();
isDisplayOff = false;
}
String text = lastItem.text;
bool updateText = false;
if (lastMessage != text) {
#ifdef DEBUG
Serial.println(text);
#endif
lastMessage = text;
updateText = true;
}
int color = timeClient.getHours() % 2 != 0;
bool updateColor = lastColor != color;
lastColor = color;
uint32_t diff = progressItem.progressEndTime - progressItem.progressStartTime;
uint32_t now = millis();
bool updateProgress = diff > 0 && progressItem.progressEndTime > now;
if (!updateText && !updateColor && !updateProgress && !progressShown) {
return;
}
display->clear();
if (color != 0) {
display->invertDisplay();
} else {
display->normalDisplay();
}
display->setColor(INVERSE);
if (updateProgress) {
progressShown = true;
uint32_t elapsed = now - progressItem.progressStartTime;
int16_t width = 128 * elapsed / diff;
if (progressItem.progressDirection == PROGRESS_DIRECTION_RIGHT_TO_LEFT) {
display->fillRect(128 - width, 0, 128, 32);
} else {
display->fillRect(0, 0, width, 32);
}
} else {
progressShown = false;
}
if (lastItem.fontSize == FONT_SIZE_12) {
display->setFont(Roboto_Condensed_12);
} else {
display->setFont(Roboto_Condensed_36);
}
int lineBreakIndex = text.indexOf('\n');
if (lineBreakIndex > 0) {
String line1 = text.substring(0, lineBreakIndex);
String line2 = text.substring(lineBreakIndex, text.length());
display->setTextAlignment(TEXT_ALIGN_LEFT);
display->drawStringMaxWidth(0, 0, 128, line1);
display->drawStringMaxWidth(0, 16, 128, line2);
} else {
display->setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
int yOffset = 16;
if (display->getStringWidth(text) > 128) {
yOffset = 8;
}
display->drawStringMaxWidth(64, yOffset, 128, text);
}
display->display();
}
void updateDisplay(void * parameter) {
initDisplay();
while (true) {
DisplayItem di;
unsigned short lastBank = 0;
while(xQueueReceive(displayQueue, &di, 0) == pdTRUE) {
drawItem(di);
}
refreshDisplay();
delay(10);
}
}
void initDisplayQueue() {
displayQueue = xQueueCreate(10, sizeof(DisplayItem));
if (errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY == xTaskCreatePinnedToCore(updateDisplay, "display", 11000, NULL, 10, NULL, 0)) {
Serial.println("Could not create display task, sleeping forever...");
while(true) {
delay(10000);
}
}
}
void displayText(String text, FontSize fontSize = FONT_SIZE_12) {
DisplayItem item(
DI_TYPE_TEXT,
text,
fontSize
);
xQueueSend(displayQueue, &item, portMAX_DELAY);
}
void displayProgress(uint32_t duration, String text = "", FontSize fontSize = FONT_SIZE_12) {
DisplayItem item(
text == "" ? DI_TYPE_PROGRESS : DI_TYPE_TEXT_PROGRESS,
text,
fontSize,
millis(),
millis() + duration
);
xQueueSend(displayQueue, &item, portMAX_DELAY);
}
void displayReverseProgress(uint32_t duration, String text = "", FontSize fontSize = FONT_SIZE_12) {
DisplayItem item(
text == "" ? DI_TYPE_PROGRESS : DI_TYPE_TEXT_PROGRESS,
text,
fontSize,
millis(),
millis() + duration,
PROGRESS_DIRECTION_RIGHT_TO_LEFT
);
xQueueSend(displayQueue, &item, portMAX_DELAY);
}
void cancelProgress(String text = "", FontSize fontSize = FONT_SIZE_12) {
DisplayItem item(
text == "" ? DI_TYPE_PROGRESS_CANCEL : DI_TYPE_TEXT_PROGRESS_CANCEL,
text,
fontSize
);
xQueueSend(displayQueue, &item, portMAX_DELAY);
}