forked from chvvkumar/ESP32-P4-Allsky-Display
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdisplay_manager.cpp
More file actions
244 lines (195 loc) · 6.69 KB
/
Copy pathdisplay_manager.cpp
File metadata and controls
244 lines (195 loc) · 6.69 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
#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)
{
}
DisplayManager::~DisplayManager() {
cleanup();
}
bool DisplayManager::begin() {
Serial.println("Initializing display hardware...");
// Initialize display objects
dsipanel = new Arduino_ESP32DSIPanel(
display_cfg.hsync_pulse_width,
display_cfg.hsync_back_porch,
display_cfg.hsync_front_porch,
display_cfg.vsync_pulse_width,
display_cfg.vsync_back_porch,
display_cfg.vsync_front_porch,
display_cfg.prefer_speed,
display_cfg.lane_bit_rate);
if (!dsipanel) {
Serial.println("ERROR: Failed to create DSI panel!");
return false;
}
Serial.println("DSI panel created successfully");
gfx = new Arduino_DSI_Display(
display_cfg.width,
display_cfg.height,
dsipanel,
display_cfg.rotation,
true,
display_cfg.lcd_rst,
display_cfg.init_cmds,
display_cfg.init_cmds_size);
if (!gfx) {
Serial.println("ERROR: Failed to create display object!");
return false;
}
Serial.println("Display object created successfully");
#ifdef GFX_EXTRA_PRE_INIT
Serial.println("Running GFX_EXTRA_PRE_INIT");
GFX_EXTRA_PRE_INIT();
#endif
Serial.println("Starting display initialization...");
if (!gfx->begin()) {
Serial.println("ERROR: Display init failed!");
return false;
}
Serial.println("Display initialized successfully!");
displayWidth = gfx->width();
displayHeight = gfx->height();
Serial.printf("Display size: %dx%d\n", displayWidth, displayHeight);
// Clear screen and start debug output
clearScreen();
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;
debugPrint("Initializing brightness control...", COLOR_YELLOW);
// 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();
debugPrintf(COLOR_WHITE, "Using stored brightness: %d%%", displayBrightness);
// Set initial brightness
setBrightness(displayBrightness);
brightnessInitialized = true;
debugPrint("Brightness control initialized!", COLOR_GREEN);
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);
Serial.printf("Display brightness set to: %d%% (PWM duty: %d)\n", brightness, 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;
Serial.println(message); // Also print to serial
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
if (debugY > displayHeight - 50 || debugLineCount > MAX_DEBUG_LINES) {
// Clear screen and reset
clearScreen();
debugY = DEBUG_START_Y;
debugLineCount = 0;
// 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::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 (gfx && bitmap) {
gfx->draw16bitRGBBitmap(x, y, bitmap, w, h);
}
}
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");
}