-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathdisplay_manager.cpp
More file actions
419 lines (339 loc) · 12.3 KB
/
display_manager.cpp
File metadata and controls
419 lines (339 loc) · 12.3 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include "display_manager.h"
#include "system_monitor.h"
#include "config_storage.h"
// Global instance
DisplayManager displayManager;
DisplayManager::DisplayManager() :
dsipanel(nullptr),
gfx(nullptr),
displayWidth(0),
displayHeight(0),
displayBrightness(DEFAULT_BRIGHTNESS),
brightnessInitialized(false),
debugY(DEBUG_START_Y),
debugLineCount(0),
firstImageLoaded(false),
otaScreenInitialized(false),
lastOTAPercent(255),
_paused(false)
{}
DisplayManager::~DisplayManager() {
cleanup();
}
bool DisplayManager::begin() {
// Get runtime display configuration from config storage
extern ConfigStorage configStorage;
int displayType = configStorage.getDisplayType();
// Select the appropriate display config (1 = 3.4", 2 = 4.0")
const DisplayConfig& activeConfig = (displayType == SCREEN_4INCH_DSI)
? SCREEN_4_INCH_CONFIG
: SCREEN_3_4_INCH_CONFIG;
Serial.printf("Display type selected: %d (%s)\n", displayType, activeConfig.name);
// Initialize display objects with selected config
dsipanel = new Arduino_ESP32DSIPanel(
activeConfig.hsync_pulse_width,
activeConfig.hsync_back_porch,
activeConfig.hsync_front_porch,
activeConfig.vsync_pulse_width,
activeConfig.vsync_back_porch,
activeConfig.vsync_front_porch,
activeConfig.prefer_speed,
activeConfig.lane_bit_rate);
if (!dsipanel) {
Serial.println("ERROR: Failed to create DSI panel!");
return false;
}
gfx = new Arduino_DSI_Display(
activeConfig.width,
activeConfig.height,
dsipanel,
activeConfig.rotation,
true,
activeConfig.lcd_rst,
activeConfig.init_cmds,
activeConfig.init_cmds_size);
if (!gfx) {
Serial.println("ERROR: Failed to create display object!");
return false;
}
#ifdef GFX_EXTRA_PRE_INIT
GFX_EXTRA_PRE_INIT();
#endif
if (!gfx->begin()) {
Serial.println("ERROR: Display init failed!");
return false;
}
displayWidth = gfx->width();
displayHeight = gfx->height();
// Clear screen and start debug output
clearScreen();
gfx->flush(); // Ensure screen is actually cleared to hardware
debugY = DEBUG_START_Y;
debugLineCount = 0;
return true;
}
void DisplayManager::cleanup() {
if (gfx) {
delete gfx;
gfx = nullptr;
}
if (dsipanel) {
delete dsipanel;
dsipanel = nullptr;
}
}
Arduino_DSI_Display* DisplayManager::getGFX() const {
return gfx;
}
int16_t DisplayManager::getWidth() const {
return displayWidth;
}
int16_t DisplayManager::getHeight() const {
return displayHeight;
}
bool DisplayManager::initBrightness() {
if (brightnessInitialized) return true;
// Configure LEDC for backlight control using newer API
if (ledcAttach(BACKLIGHT_PIN, BACKLIGHT_FREQ, BACKLIGHT_RESOLUTION) == 0) {
debugPrint("ERROR: LEDC attach failed!", COLOR_RED);
return false;
}
// Get the default brightness from configuration storage
displayBrightness = configStorage.getDefaultBrightness();
// Set initial brightness
setBrightness(displayBrightness);
brightnessInitialized = true;
return true;
}
void DisplayManager::setBrightness(int brightness) {
if (!brightnessInitialized) return;
brightness = constrain(brightness, 0, 100);
displayBrightness = brightness;
// Convert percentage to inverted 10-bit duty cycle (0-1023)
// Backlight uses inverted logic: low PWM = high brightness
uint32_t duty = 1023 - ((1023 * brightness) / 100);
ledcWrite(BACKLIGHT_PIN, duty);
}
int DisplayManager::getBrightness() const {
return displayBrightness;
}
void DisplayManager::debugPrint(const char* message, uint16_t color) {
if (firstImageLoaded) return; // Don't show debug after first image loads
if (!gfx) return;
gfx->setTextSize(DEBUG_TEXT_SIZE);
gfx->setTextColor(color);
// Calculate text width for centering
int16_t x1, y1;
uint16_t textWidth, textHeight;
gfx->getTextBounds(message, 0, 0, &x1, &y1, &textWidth, &textHeight);
// Center the text horizontally
int16_t centerX = (displayWidth - textWidth) / 2;
if (centerX < 10) centerX = 10; // Minimum margin
gfx->setCursor(centerX, debugY);
gfx->println(message);
debugY += DEBUG_LINE_HEIGHT;
debugLineCount++;
// Scroll if we reach the bottom (unless auto-scroll is disabled for WiFi setup)
if (!disableAutoScroll && (debugY > displayHeight - 50 || debugLineCount > MAX_DEBUG_LINES)) {
// Clear screen and reset
clearScreen();
debugY = DEBUG_START_Y;
debugLineCount = 0;
// Don't show the debug log header during initial boot
// Center the scroll header
// gfx->setTextSize(DEBUG_TEXT_SIZE);
// gfx->setTextColor(COLOR_YELLOW);
// const char* scrollMsg = "=== DEBUG LOG ===";
// gfx->getTextBounds(scrollMsg, 0, 0, &x1, &y1, &textWidth, &textHeight);
// centerX = (displayWidth - textWidth) / 2;
// gfx->setCursor(centerX, debugY);
// gfx->println(scrollMsg);
// debugY += DEBUG_LINE_HEIGHT * 2;
}
}
void DisplayManager::debugPrintf(uint16_t color, const char* format, ...) {
if (firstImageLoaded) return;
char buffer[256];
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
debugPrint(buffer, color);
}
void DisplayManager::clearDebugArea() {
debugY = DEBUG_START_Y;
debugLineCount = 0;
}
void DisplayManager::setFirstImageLoaded(bool loaded) {
firstImageLoaded = loaded;
}
void DisplayManager::setDebugY(int y) {
debugY = y;
debugLineCount = 0;
}
void DisplayManager::setDisableAutoScroll(bool disable) {
disableAutoScroll = disable;
}
void DisplayManager::clearScreen(uint16_t color) {
if (gfx) {
gfx->fillScreen(color);
}
}
void DisplayManager::drawBitmap(int16_t x, int16_t y, uint16_t* bitmap, int16_t w, int16_t h) {
if (_paused) return;
if (gfx && bitmap) {
gfx->draw16bitRGBBitmap(x, y, bitmap, w, h);
}
}
void DisplayManager::pauseDisplay() {
// Pause display rendering to prevent memory bandwidth conflicts during heavy PSRAM operations
_paused = true;
}
void DisplayManager::resumeDisplay() {
// Resume normal display rendering after heavy operations complete
_paused = false;
}
void DisplayManager::showSystemStatus() {
if (!gfx) return;
clearScreen();
gfx->setTextSize(2);
gfx->setTextColor(COLOR_WHITE);
gfx->setCursor(10, 10);
gfx->println("ESP32-P4 AllSky Display");
gfx->setTextSize(1);
gfx->setTextColor(COLOR_CYAN);
gfx->setCursor(10, 40);
gfx->printf("Display: %dx%d pixels\n", displayWidth, displayHeight);
gfx->printf("Brightness: %d%%\n", displayBrightness);
gfx->printf("Free Heap: %d bytes\n", systemMonitor.getCurrentFreeHeap());
gfx->printf("Free PSRAM: %d bytes\n", systemMonitor.getCurrentFreePsram());
gfx->printf("System Health: %s\n", systemMonitor.isSystemHealthy() ? "HEALTHY" : "CRITICAL");
}
void DisplayManager::drawStatusOverlay(const char* message, uint16_t color, int yOffset) {
if (_paused) return;
if (!gfx || !message) return;
// Draw semi-transparent background box with rounded corners effect
// Calculate text dimensions for the background
int16_t x1, y1;
uint16_t textWidth, textHeight;
gfx->setTextSize(1);
gfx->getTextBounds(message, 0, 0, &x1, &y1, &textWidth, &textHeight);
// Create overlay box with padding
int boxPadding = 8;
int16_t boxX = (displayWidth - textWidth) / 2 - boxPadding;
int16_t boxY = yOffset - 5;
int16_t boxWidth = textWidth + (boxPadding * 2);
int16_t boxHeight = textHeight + 10;
// Clamp to screen bounds
if (boxX < 0) boxX = 0;
if (boxY < 0) boxY = 0;
if (boxX + boxWidth > displayWidth) boxWidth = displayWidth - boxX;
if (boxY + boxHeight > displayHeight) boxHeight = displayHeight - boxY;
// Draw semi-transparent dark background (multiple overlays to create transparency effect)
uint16_t bgColor = 0x0000; // Black
gfx->fillRect(boxX, boxY, boxWidth, boxHeight, bgColor);
// Draw text on the overlay
gfx->setTextSize(1);
gfx->setTextColor(color);
int16_t centerX = (displayWidth - textWidth) / 2;
gfx->setCursor(centerX, yOffset);
gfx->println(message);
}
void DisplayManager::drawOverlayMessage(const char* message, int x, int y, uint16_t color, int backgroundColor) {
if (_paused) return;
if (!gfx || !message) return;
// Calculate text dimensions
int16_t x1, y1;
uint16_t textWidth, textHeight;
gfx->setTextSize(1);
gfx->getTextBounds(message, 0, 0, &x1, &y1, &textWidth, &textHeight);
// Create background box with padding
int boxPadding = 6;
int16_t boxX = x - boxPadding;
int16_t boxY = y - boxPadding;
int16_t boxWidth = textWidth + (boxPadding * 2);
int16_t boxHeight = textHeight + (boxPadding * 2);
// Clamp to screen bounds
if (boxX < 0) boxX = 0;
if (boxY < 0) boxY = 0;
if (boxX + boxWidth > displayWidth) boxWidth = displayWidth - boxX;
if (boxY + boxHeight > displayHeight) boxHeight = displayHeight - boxY;
// Draw dark background for readability
gfx->fillRect(boxX, boxY, boxWidth, boxHeight, backgroundColor);
// Draw text
gfx->setTextSize(1);
gfx->setTextColor(color);
gfx->setCursor(x, y);
gfx->println(message);
}
void DisplayManager::clearStatusOverlay() {
// Overlay is cleared by redrawing the last image
// This is done by calling renderFullImage() from the main loop
}
void DisplayManager::showOTAProgress(const char* title, uint8_t percent, const char* message) {
if (!gfx) return;
// Only draw the screen once at the start
if (!otaScreenInitialized) {
// Calculate center positions
int centerX = displayWidth / 2;
int centerY = displayHeight / 2;
int16_t x1, y1;
uint16_t w, h;
// Clear screen with dark background
clearScreen(0x1082); // Dark blue background
// Draw title
gfx->setTextSize(4);
gfx->setTextColor(COLOR_WHITE);
const char* mainMsg = "OTA Update";
gfx->getTextBounds(mainMsg, 0, 0, &x1, &y1, &w, &h);
gfx->setCursor(centerX - w/2, centerY - 60);
gfx->println(mainMsg);
// Draw in progress message
gfx->setTextSize(3);
gfx->setTextColor(COLOR_CYAN);
const char* statusMsg = "In Progress...";
gfx->getTextBounds(statusMsg, 0, 0, &x1, &y1, &w, &h);
gfx->setCursor(centerX - w/2, centerY + 20);
gfx->println(statusMsg);
// Draw warning message
gfx->setTextSize(2);
gfx->setTextColor(COLOR_RED);
const char* warning = "Do not power off device";
gfx->getTextBounds(warning, 0, 0, &x1, &y1, &w, &h);
gfx->setCursor(centerX - w/2, centerY + 100);
gfx->println(warning);
// Flush all drawing operations to screen at once
if (gfx) {
gfx->flush();
}
otaScreenInitialized = true;
}
// Special handling for completion
if (percent == 100) {
int centerX = displayWidth / 2;
int centerY = displayHeight / 2;
int16_t x1, y1;
uint16_t w, h;
clearScreen(0x1082);
gfx->setTextSize(4);
gfx->setTextColor(COLOR_GREEN);
const char* completeMsg = "OTA Complete!";
gfx->getTextBounds(completeMsg, 0, 0, &x1, &y1, &w, &h);
gfx->setCursor(centerX - w/2, centerY - 30);
gfx->println(completeMsg);
gfx->setTextSize(3);
gfx->setTextColor(COLOR_WHITE);
const char* rebootMsg = "Rebooting...";
gfx->getTextBounds(rebootMsg, 0, 0, &x1, &y1, &w, &h);
gfx->setCursor(centerX - w/2, centerY + 30);
gfx->println(rebootMsg);
// Flush completion screen to display
if (gfx) {
gfx->flush();
}
}
// Reset screen initialized flag after showing completion so next OTA starts fresh
if (percent == 100) {
otaScreenInitialized = false;
}
}