forked from hidaba/PylontechMonitoring
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpy_display.cpp
More file actions
320 lines (249 loc) · 7.7 KB
/
Copy pathpy_display.cpp
File metadata and controls
320 lines (249 loc) · 7.7 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
#include <Arduino.h>
#include "driver/ledc.h"
#include "py_display.h"
#include "config.h"
extern HealthStatus health;
// Display-Pins
#define TFT_CS 23
#define TFT_DC 27
#define TFT_RST 26
#define TFT_SCK 14
#define TFT_MOSI 13
#define TFT_MISO 12
// Backlight-Pin (PWM)
#define TFT_BL 33
#define TFT_BL_CHANNEL LEDC_CHANNEL_0
#define TFT_BL_TIMER LEDC_TIMER_0
extern float stackVoltAvg;
extern float stackCurrSum;
extern float stackTempMax;
extern float stackSocAvg;
extern HealthStatus health;
void PyDisplay::syncFromGlobals() {
// Stack-Werte aus Parser übernehmen
int v = (int)(stackVoltAvg * 1000.0f); // V → mV
int c = (int)(stackCurrSum * 1000.0f); // A → mA
int t = (int)(stackTempMax * 1000.0f); // °C → mC
int s = (int)(stackSocAvg + 0.5f); // % → int
updatePwr(v, c, t, s);
}
void PyDisplay::syncHealth() {
// Health wird direkt aus dem globalen health-Objekt gelesen
needsRedraw = true;
}
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
PyDisplay display;
bool wifiAPMode = false;
String formatCurrent(float amps) {
char buf[10];
float a = fabs(amps);
if (a < 10.0f) {
// 1.23 oder -1.23
sprintf(buf, "%s%1.2f",
(amps < 0 ? "-" : " "),
a
);
}
else if (a < 100.0f) {
// 12.3 oder -12.3
sprintf(buf, "%s%2.1f",
(amps < 0 ? "-" : " "),
a
);
}
else {
// 123 oder -123
sprintf(buf, "%s%3.0f",
(amps < 0 ? "-" : " "),
a
);
}
return String(buf);
}
void PyDisplay::begin() {
// LEDC-Timer konfigurieren
ledc_timer_config_t timer = {};
timer.speed_mode = LEDC_HIGH_SPEED_MODE;
timer.duty_resolution = LEDC_TIMER_8_BIT;
timer.timer_num = TFT_BL_TIMER;
timer.freq_hz = 5000;
timer.clk_cfg = LEDC_AUTO_CLK;
ledc_timer_config(&timer);
// LEDC-Channel konfigurieren
ledc_channel_config_t ch = {};
ch.gpio_num = TFT_BL;
ch.speed_mode = LEDC_HIGH_SPEED_MODE;
ch.channel = TFT_BL_CHANNEL;
ch.intr_type = LEDC_INTR_DISABLE;
ch.timer_sel = TFT_BL_TIMER;
ch.duty = config.displayBrightness;
ch.hpoint = 0;
ledc_channel_config(&ch);
setBrightness(config.displayBrightness);
// SPI starten
SPI.begin(TFT_SCK, TFT_MISO, TFT_MOSI);
// Display starten
tft.initR(INITR_BLACKTAB);
tft.setRotation(1);
tft.fillScreen(ST77XX_BLACK);
drawStaticUI();
}
void PyDisplay::setBrightness(uint8_t value) {
ledc_set_duty(LEDC_HIGH_SPEED_MODE, TFT_BL_CHANNEL, value);
ledc_update_duty(LEDC_HIGH_SPEED_MODE, TFT_BL_CHANNEL);
}
void PyDisplay::drawStaticUI() {
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(ST77XX_WHITE);
tft.setTextSize(1);
// 2×2 Raster Labels
tft.setCursor(1, 10);
tft.print("U");
tft.setCursor(1, 35);
tft.print("I");
tft.setCursor(90, 10);
tft.print("T");
tft.setCursor(90, 35);
tft.print("SOC");
// Status unten
tft.setCursor(5, 110);
tft.print("WiFi:");
tft.setCursor(5, 120);
tft.print("MQTT:");
}
void PyDisplay::updatePwr(int volt_mV, int curr_mA, int temp_mC, int socVal) {
if (volt == volt_mV &&
curr == curr_mA &&
temp == temp_mC &&
soc == socVal) {
return;
}
volt = volt_mV;
curr = curr_mA;
temp = temp_mC;
soc = socVal;
needsRedraw = true;
}
void PyDisplay::updateWifi(bool connected, const String &ip, int rssi, bool apMode) {
wifiConnected = connected;
wifiIP = ip;
wifiRSSI = rssi;
wifiAPMode = apMode;
needsRedraw = true;
}
void PyDisplay::updateMqtt(bool connected, const String &server) {
mqttConnected = connected;
mqttServer = server;
needsRedraw = true;
}
void PyDisplay::drawValues() {
tft.setTextColor(ST77XX_YELLOW, ST77XX_BLACK);
tft.setTextSize(2);
// --- 2×2 Raster ---
// U (links oben)
tft.setCursor(15, 10);
tft.printf("%4.2fV", volt / 1000.0f);
// I (rechts oben)
tft.setCursor(100, 10);
//tft.printf("%5.2fA", curr / 1000.0f);
tft.printf("%4.1fC", temp / 1000.0f);
float amps = curr / 1000.0f;
String iStr = formatCurrent(amps);
tft.setCursor(15, 35);
tft.print(iStr);
tft.print("A");
// SOC (rechts unten)
tft.setCursor(110, 35);
tft.printf("%3d%%", soc);
// ---------------------------------------------------------
// HEALTH BLOCK (Mitte des Displays)
// ---------------------------------------------------------
extern HealthStatus health;
// Strings nur aus gültigen Einträgen aufbauen
String okStr;
okStr.reserve(64);
for (int i = 0; i < health.okCount; i++) {
okStr += String(health.okModules[i]);
okStr += " ";
}
String warnStr;
warnStr.reserve(32);
for (int i = 0; i < health.warnCount; i++) {
warnStr += String(health.warnModules[i]);
warnStr += " ";
}
String errStr;
errStr.reserve(32);
for (int i = 0; i < health.errorCount; i++) {
errStr += String(health.errorModules[i]);
errStr += " ";
}
bool changed =
(lastHealthColor != String(health.color)) ||
(lastHealthOK != okStr) ||
(lastHealthWarn != warnStr) ||
(lastHealthErr != errStr) ||
(lastHealthMsg != String(health.strongestMessage));
if (changed) {
// Hintergrundfarbe je nach Health-Farbe
uint16_t bg =
(strcmp(health.color, "green") == 0) ? ST77XX_BLACK :
(strcmp(health.color, "yellow") == 0) ? ST77XX_YELLOW :
ST77XX_RED;
tft.fillRect(0, 60, 160, 45, bg);
// Textfarbe
uint16_t fg =
(strcmp(health.color, "red") == 0 || strcmp(health.color, "yellow") == 0)
? ST77XX_BLACK
: ST77XX_GREEN;
tft.setTextColor(fg);
tft.setTextSize(1);
// Zeile 1: OK
tft.setCursor(2, 62);
tft.print("OK: ");
tft.print(okStr);
// Zeile 2: Warn
tft.setCursor(2, 74);
tft.print("Warn: ");
tft.print(warnStr);
// Zeile 3: Err
tft.setCursor(2, 86);
tft.print("Err: ");
tft.print(errStr);
// Zeile 4: stärkste Meldung
tft.setCursor(2, 98);
tft.print(health.strongestMessage);
// Cache aktualisieren
lastHealthColor = String(health.color);
lastHealthOK = okStr;
lastHealthWarn = warnStr;
lastHealthErr = errStr;
lastHealthMsg = String(health.strongestMessage);
}
// --- Status unten ---
tft.setTextSize(1);
// WiFi
tft.setCursor(50, 110);
tft.setTextColor(wifiConnected ? ST77XX_GREEN : ST77XX_RED, ST77XX_BLACK);
if (wifiAPMode) {
tft.printf("AP 192.168.4.1");
} else {
tft.printf("%s ", wifiIP.c_str());
}
// MQTT
tft.setCursor(50, 120);
tft.setTextColor(mqttConnected ? ST77XX_GREEN : ST77XX_RED, ST77XX_BLACK);
tft.printf("%s", mqttServer.c_str());
}
void PyDisplay::loop() {
if (!needsRedraw) return;
drawValues();
needsRedraw = false;
}
void PyDisplay::reset() {
tft.initR(INITR_BLACKTAB);
tft.setRotation(1);
tft.fillScreen(ST77XX_BLACK);
drawStaticUI();
needsRedraw = true;
}