-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathdisplayTFT.cpp
More file actions
1233 lines (1102 loc) · 43.4 KB
/
Copy pathdisplayTFT.cpp
File metadata and controls
1233 lines (1102 loc) · 43.4 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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <displayTFT.h>
#include <dtuInterface.h>
#ifdef ARDUINO_ARCH_ESP8266
ADC_MODE(ADC_VCC); // Read the supply voltage
#endif
TFT_eSPI tft = TFT_eSPI();
setup_t user;
// Constants for gauge angles and radii
const int32_t GAUGE_MIN_ANGLE = 30;
const int32_t GAUGE_MAX_ANGLE = 330;
const int32_t POWER_MIN_ANGLE = 55;
const int32_t POWER_MAX_ANGLE = 305;
const uint8_t GAUGE_RADIUS = 119;
const uint8_t GAUGE_INNER_RADIUS = 104; // r - 15 for arcs
// Constants for SOC ring
const int32_t SOC_START_LEFT = 50;
const int32_t SOC_START_RIGHT = 310;
DisplayTFT::DisplayTFT() {}
void DisplayTFT::setup()
{
tft.init();
uint16_t orientation = userConfig.displayOrientation;
if (orientation >= 0 && orientation < 90)
orientation = 0;
else if (orientation >= 90 && orientation < 180)
orientation = 1;
else if (orientation >= 180 && orientation < 270)
orientation = 2;
else if (orientation >= 270 && orientation < 360)
orientation = 3;
tft.setRotation(orientation);
tft.fillScreen(TFT_BLACK);
// Swap the colour byte order when rendering
tft.setSwapBytes(true);
pinMode(BACKLIIGHT_PIN, OUTPUT);
#ifndef DGC9A01_MOUNTED
brightness = userConfig.displayBrightnessDay;
// set brightness to max - workaround for unconfigured brightness and no backlight control
if (brightness == 0)
brightness = 255;
analogWrite(BACKLIIGHT_PIN, brightness);
#else
digitalWrite(BACKLIIGHT_PIN, 1);
#endif
Serial.println(F("TFT display initialized"));
}
void DisplayTFT::setRemoteDisplayMode(bool remoteDisplayActive, bool remoteDisplay_SolarMonitor, bool remoteDisplay_BatteryMonitor)
{
lastDisplayData.remoteDisplayActive = remoteDisplayActive;
lastDisplayData.remoteDisplay_SolarMonitor = remoteDisplay_SolarMonitor;
lastDisplayData.remoteDisplay_BatteryMonitor = remoteDisplay_BatteryMonitor;
}
// function has to be called every 50 milliseconds
void DisplayTFT::renderScreen(String time, String version)
{
displayTicks++;
if (displayTicks > 1200)
displayTicks = 0; // after 1 minute restart
lastDisplayData.version = version.c_str();
lastDisplayData.formattedTime = time.c_str();
// every 50 milliseconds
checkChangedValues();
// every 0.1 second
if (displayTicks % 2 == 0)
setBrightnessAuto();
// every 0.5 second
if (displayTicks % 10 == 0)
{
if (lastDisplayData.remoteDisplay_SolarMonitor && lastDisplayData.remoteDisplay_BatteryMonitor)
drawScreen_monitorSolarBattery(version, time);
else if (lastDisplayData.remoteDisplay_SolarMonitor)
drawScreen_monitorSolar(version, time);
else if (lastDisplayData.remoteDisplay_BatteryMonitor)
drawScreen_monitorBattery(version, time);
else
drawScreen(version, time); // draw every 0.5 second
// Serial.println("Displaying screen");
}
// every 5 seconds
if (displayTicks % 100 == 0)
// if (displayTicks % 40 == 0)
{
checkNightMode();
// Serial.println("current brightness: " + String(brightness));
}
// if (displayTicks % 5 == 0)
// {
// lastDisplayData.totalPower = lastDisplayData.totalPower + 20;
// if (lastDisplayData.totalPower > 800)
// lastDisplayData.totalPower = 0;
// lastDisplayData.powerLimit = lastDisplayData.powerLimit + 5;
// if (lastDisplayData.powerLimit > 100)
// lastDisplayData.powerLimit = 0;
// }
}
void DisplayTFT::drawScreen(String version, String time)
{
// store last shown value
lastDisplayData.totalYieldDay = dtuGlobalData.grid.dailyEnergy;
lastDisplayData.totalYieldTotal = dtuGlobalData.grid.totalEnergy;
lastDisplayData.rssiGW = dtuGlobalData.wifi_rssi_gateway;
lastDisplayData.rssiDTU = dtuGlobalData.dtuRssi;
if (dtuGlobalData.grid.power.getValue() > 0)
lastDisplayData.totalPower = round(dtuGlobalData.grid.power.getValue());
lastDisplayData.powerLimit = dtuGlobalData.powerLimit;
drawHeader(version);
// main screen
if (!isNight)
{
// EXTERNAL CONTROL FIX: Detect inverter state changes (rare: Hoymile app control)
// Force display refresh even if DTU is temporarily offline
static bool lastInverterState = dtuGlobalData.inverterControl.stateOn;
bool inverterStateChanged = (lastInverterState != dtuGlobalData.inverterControl.stateOn);
if (inverterStateChanged)
lastInverterState = dtuGlobalData.inverterControl.stateOn;
if (dtuConnection.dtuConnectState == DTU_STATE_CONNECTED)
{
drawMainDTUOnline();
displayState = 0;
}
else if (dtuConnection.dtuConnectState == DTU_STATE_CLOUD_PAUSE)
{
drawMainDTUOnline(true);
displayState = 0;
}
else if (inverterStateChanged)
{
// Rare case: Inverter controlled externally, show state even if DTU offline
drawMainDTUOnline(false);
displayState = 0;
Serial.println("DisplayTFT:\t >> external inverter state change detected");
}
else if (dtuConnection.dtuConnectionOnline == false)
{
drawMainDTUOffline();
displayState = 3;
}
}
else
{
displayState = 4;
}
drawFooter(time);
if (displayState != displayStateOld)
{
Serial.println("DisplayTFT:\t >> display state changed - state: " + String(displayState) + " - old state: " + String(displayStateOld));
displayStateOld = displayState;
tft.fillScreen(TFT_BLACK);
}
}
void DisplayTFT::drawScreen_monitorSolar(String version, String time)
{
// store last shown value
lastDisplayData.totalYieldDay = dtuGlobalData.grid.dailyEnergy;
if (dtuGlobalData.grid.power.getValue() > 0)
lastDisplayData.totalPower = round(dtuGlobalData.grid.power.getValue());
drawHeaderSummary(version);
if (!isNight)
{
drawText_Value_Large(120, 85, lastDisplayData.totalPower, "W");
displayState = 0;
}
else
{
displayState = 4;
}
drawFooter(time);
drawGauge_SolarMonitor(time);
if (displayState != displayStateOld)
{
Serial.println("DisplayTFT:\t >> display state changed - state: " + String(displayState) + " - old state: " + String(displayStateOld));
displayStateOld = displayState;
tft.fillScreen(TFT_BLACK);
}
}
void DisplayTFT::drawScreen_monitorBattery(String version, String time)
{
// store last shown value
if (dtuGlobalData.batteryStoredEnergy >= 0 || dtuGlobalData.batteryStoredEnergy == -1234)
lastDisplayData.battery_StoredEnergy = dtuGlobalData.batteryStoredEnergy;
if (dtuGlobalData.grid.dailyEnergy > 0)
lastDisplayData.totalYieldDay = dtuGlobalData.grid.dailyEnergy;
if (dtuGlobalData.batterySOC >= 0)
lastDisplayData.battery_SOC = dtuGlobalData.batterySOC;
if (lastDisplayData.battery_SOC > 100)
lastDisplayData.battery_SOC = 100;
drawHeaderSummary(version);
if (!isNight)
{
drawText_Value_Large(120, 85, round(lastDisplayData.battery_SOC), "%");
displayState = 0;
}
else
{
displayState = 4;
}
drawFooter(time);
drawGauge_BatteryMonitor(time);
if (displayState != displayStateOld)
{
Serial.println("DisplayTFT:\t >> display state changed - state: " + String(displayState) + " - old state: " + String(displayStateOld));
displayStateOld = displayState;
tft.fillScreen(TFT_BLACK);
}
}
void DisplayTFT::drawScreen_monitorSolarBattery(String version, String time)
{
// store last shown value
lastDisplayData.totalYieldDay = dtuGlobalData.grid.dailyEnergy;
if (dtuGlobalData.grid.power.getValue() > 0)
lastDisplayData.totalPower = round(dtuGlobalData.grid.power.getValue());
if (dtuGlobalData.batteryStoredEnergy >= 0)
lastDisplayData.battery_StoredEnergy = dtuGlobalData.batteryStoredEnergy;
if (dtuGlobalData.batterySOC >= 0)
lastDisplayData.battery_SOC = dtuGlobalData.batterySOC;
drawHeaderSummary(version);
if (!isNight)
{
drawText_Value_Large(120, 85, lastDisplayData.totalPower, "W");
displayState = 0;
}
else
{
displayState = 4;
}
drawFooter(time);
if (!isNight)
drawGauge_SolarBatteryMonitor(time);
if (displayState != displayStateOld)
{
Serial.println("DisplayTFT:\t >> display state changed - state: " + String(displayState) + " - old state: " + String(displayStateOld));
displayStateOld = displayState;
tft.fillScreen(TFT_BLACK);
}
}
void DisplayTFT::drawMainDTUOnline(bool pause)
{
// show a cloud upload symbol if cloud pause active
if (pause)
tft.pushImage(195, 70, cloudWidth, cloudHeight, cloud);
else
tft.fillRect(195, 70, cloudWidth, cloudHeight, TFT_BLACK); // clear icon
if (dtuGlobalData.warningsActive > 0)
tft.pushImage(12, 70, warningWidth, warningHeight, warning);
else
tft.fillRect(12, 70, warningWidth, warningHeight, TFT_BLACK); // clear icon
// main screen
// clear main space if inverterControl.stateOn changed
static bool previousStateOn = dtuGlobalData.inverterControl.stateOn;
if (previousStateOn != dtuGlobalData.inverterControl.stateOn)
{
tft.drawSmoothArc(119, 119, 75, 0, 47, 313, TFT_BLACK, TFT_BLACK);
tft.fillRect(64, 140, 109, 169, TFT_BLACK);
previousStateOn = dtuGlobalData.inverterControl.stateOn;
Serial.println("DisplayTFT:\t >> inverter state changed - clear main screen");
}
if (!dtuGlobalData.inverterControl.stateOn)
{
tft.pushImage(70, 60, power_offWidth, power_offHeight, power_off);
tft.setTextColor(TFT_DARKCYAN, TFT_BLACK);
tft.drawCentreString("inverter switched off", 120, 150, 2);
}
else
{
// --- base window for wattage ----------------------------------
uint32_t wattRingColor = TFT_CYAN;
if (lastDisplayData.remoteDisplayActive)
wattRingColor = TFT_DARKGREEN;
tft.drawSmoothArc(119, 119, 75, 75 - 1, 47, 313, wattRingColor, TFT_BLACK);
tft.drawWideLine(64, 169, 64 + 109, 169, 2, wattRingColor, TFT_BLACK);
int padding = tft.textWidth("999.9", 6);
// current power
tft.setTextColor(TFT_DARKCYAN, TFT_BLACK);
tft.drawCentreString("W", 120, 57, 4);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextDatum(TC_DATUM); // centered datum
tft.setTextPadding(padding);
tft.drawNumber(lastDisplayData.totalPower, 120, 84, 6);
// power limit
tft.setTextColor(TFT_GREENYELLOW, TFT_BLACK);
tft.setTextDatum(TC_DATUM); // centered datum
padding = tft.textWidth("999", 6);
tft.setTextPadding(padding);
// tft.drawCentreString(String(lastDisplayData.powerLimit) + " %", 120, 130, 4);
tft.drawNumber(lastDisplayData.powerLimit, 120, 130, 4);
tft.setTextPadding(0); // reset padding
tft.drawString("%", 148, 135, 2);
tft.setTextColor(TFT_DARKCYAN, TFT_BLACK);
tft.drawCentreString("Power Limit", 120, 150, 2);
}
// tft.fillRoundRect(80, 120, 100, 26, 1, TFT_BLACK);
}
void DisplayTFT::drawMainDTUOffline()
{
// main screen
tft.setTextColor(TFT_VIOLET, TFT_BLACK);
tft.drawCentreString("DTU", 120, 85, 4);
tft.drawCentreString("offline", 120, 120, 4);
}
void DisplayTFT::drawFactoryMode(String version, String apName, String ip)
{
Serial.println(F("TFT display - showing factory mode"));
// header
tft.setTextColor(TFT_GOLD);
tft.drawCentreString("dtuGateway", 120, 15, 1);
tft.setTextColor(TFT_DARKCYAN);
tft.drawCentreString(version, 120, 28, 1);
// main screen
tft.setTextColor(TFT_VIOLET, TFT_BLACK);
tft.drawCentreString("first start", 120, 43, 4);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawCentreString("connect with wifi", 120, 75, 2); // font2 16 + 3
tft.drawCentreString("and open", 120, 123, 2);
tft.drawCentreString("in your browser", 120, 171, 2);
tft.setTextColor(TFT_GREENYELLOW, TFT_BLACK);
tft.drawCentreString(apName, 120, 94, 2); // font4 26 + 3
tft.drawCentreString("http://" + ip, 120, 145, 4);
}
void DisplayTFT::drawUpdateMode(String text, String text2, boolean blank)
{
uint8_t y1 = 110;
// Serial.println("TFT display:\t update mode");
if (blank)
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
if (text2 != "")
{
// Serial.println("TFT display:\t update mode done");
y1 = 90;
tft.drawCentreString(text2, 120, 130, 4);
}
tft.drawCentreString(text, 120, y1, 4);
}
void DisplayTFT::drawHeader(String version)
{
// header
// show header info only if it is not night or it is night and night clock is enabled
if (!isNight || (isNight && userConfig.displayNightClock))
{
tft.setTextSize(1);
// header - content center
String headline = "dtuGateway";
if (lastDisplayData.remoteDisplayActive)
headline = "dtuMonitor";
else if (lastDisplayData.remoteDisplay_SolarMonitor && !lastDisplayData.remoteDisplay_BatteryMonitor)
headline = "Solar Monitor";
else if (lastDisplayData.remoteDisplay_BatteryMonitor && !lastDisplayData.remoteDisplay_SolarMonitor)
headline = "Battery Monitor";
tft.setTextColor(isNight ? TFT_MAROON : TFT_GOLD);
tft.drawCentreString(headline, 120, 15, 1);
tft.setTextColor(isNight ? TFT_MAROON : TFT_DARKCYAN);
tft.drawCentreString(version, 120, 28, 1);
}
if (!isNight && !lastDisplayData.remoteDisplay_SolarMonitor && !lastDisplayData.remoteDisplay_BatteryMonitor)
{
tft.setTextColor(TFT_DARKCYAN, TFT_BLACK);
tft.drawCentreString("gw", 52, 37, 1);
tft.drawCentreString("dtu", 184, 37, 1);
tft.setTextColor(TFT_CYAN, TFT_BLACK);
char rssi[10];
sprintf(rssi, "%3d %%", lastDisplayData.rssiGW);
tft.drawCentreString(rssi, 46, 48, 2);
sprintf(rssi, "%3d %%", lastDisplayData.rssiDTU);
tft.drawCentreString(rssi, 192, 48, 2);
}
}
void DisplayTFT::drawFooter(String time)
{
// monitor display modes - draw mode-specific values
if (lastDisplayData.remoteDisplay_SolarMonitor || lastDisplayData.remoteDisplay_BatteryMonitor)
{
if (lastDisplayData.remoteDisplay_SolarMonitor && lastDisplayData.remoteDisplay_BatteryMonitor)
{
// solar+battery: yield day at top with 3 decimals, battery SOC at bottom
drawText_Value_Small(120, 42, lastDisplayData.totalYieldDay, 3, "kWh", 201, 134, 0);
drawText_Value_Small(120, 180, lastDisplayData.battery_SOC, 1, "%", 0, 151, 65);
}
else if (lastDisplayData.remoteDisplay_SolarMonitor)
{
drawText_Value_Small(120, 180, lastDisplayData.totalYieldDay, 3, "kWh", 201, 134, 0);
}
else if (lastDisplayData.remoteDisplay_BatteryMonitor)
{
// special case: battery_StoredEnergy == -1234 means show yield day instead
float footerVal = (lastDisplayData.battery_StoredEnergy == -1234)
? lastDisplayData.totalYieldDay
: lastDisplayData.battery_StoredEnergy;
uint8_t footerDec = (lastDisplayData.battery_StoredEnergy == -1234) ? 3 : 1;
drawText_Value_Small(120, 180, footerVal, footerDec, "kWh", 0, 151, 65);
}
if (isNight)
{
tft.setTextSize(1);
tft.setTextColor(TFT_MAROON, TFT_BLACK);
tft.drawCentreString(time, 120, 100, 6);
}
}
// normal mode - draw yield day and total if is not night
else if (!isNight)
{
tft.setTextSize(1);
tft.setTextColor(SPECIAL_BLUE, TFT_BLACK);
tft.drawCentreString(time, 120, 174, 4);
tft.setTextColor(TFT_DARKCYAN, TFT_BLACK);
tft.drawCentreString("day", 85, 215, 1);
tft.drawCentreString("kWh", 120, 215, 1);
tft.drawCentreString("total", 155, 215, 1);
tft.drawCentreString("yield", 120, 225, 1);
tft.setTextColor(TFT_CYAN, TFT_BLACK);
tft.drawCentreString(String(lastDisplayData.totalYieldDay, 3), 85, 198, 2);
tft.drawCentreString(String(lastDisplayData.totalYieldTotal, 1), 155, 198, 2);
}
// normal mode - draw current power if it is night then show the clock
else if (userConfig.displayNightClock)
{
tft.setTextSize(1);
tft.setTextColor(TFT_MAROON, TFT_BLACK);
tft.drawCentreString(time, 120, 100, 6);
// show current power if greater than 0
if (lastDisplayData.totalPower > 0)
{
tft.setTextColor(SPECIAL_BLUE, TFT_BLACK);
tft.drawCentreString(" " + String(lastDisplayData.totalPower) + " W ", 120, 174, 4);
}
else
{
tft.fillRect(60, 170, 120, 37, TFT_BLACK); // clear power display
}
// // debug brightness
// tft.setTextColor(SPECIAL_BLUE, TFT_BLACK);
// tft.drawCentreString(String(brightness), 120, 174, 4);
}
else
{
tft.fillScreen(TFT_BLACK);
}
// show second clock ring only if generally enabled and if it is not night or it is night and night clock is enabled
if ((!isNight && !userConfig.remoteDisplay_SolarMonitor && !userConfig.remoteDisplay_BatteryMonitor) || (isNight && userConfig.displayNightClock))
{
drawSecondsRing(time);
}
}
void DisplayTFT::drawText_Value_Large(uint16_t x, uint16_t y, uint16_t value, String unit)
{
String displayValue = "--";
String displayUnit = unit;
int padding = tft.textWidth("999", 8);
if (value > 9999)
{
displayValue = String(value / 1000.0, 1);
padding = tft.textWidth("99.9", 8);
displayUnit = unit == "W" ? "kW" : "%";
}
else if (value > 999)
{
displayValue = String(value / 1000.0, 1);
padding = tft.textWidth(" 9.9 ", 8);
displayUnit = unit == "W" ? "kW" : "%";
}
else if (value >= 0)
{
displayValue = String(value);
padding = tft.textWidth(".999", 8);
displayUnit = unit == "W" ? " W " : "%";
}
else
{
displayValue = "--";
padding = tft.textWidth("99", 8);
displayUnit = " ";
}
tft.setTextColor(TFT_LIGHTGREY, TFT_BLACK);
tft.setTextDatum(TL_DATUM);
tft.setTextPadding(tft.textWidth("%", 2));
tft.drawCentreString(displayUnit, 185, 165, 2);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextDatum(TC_DATUM);
tft.setTextPadding(padding);
tft.drawCentreString(displayValue, x, y, 8);
}
void DisplayTFT::drawHeaderSummary(String version)
{
if (!isNight || (isNight && userConfig.displayNightClock))
{
tft.setTextSize(1);
if (lastDisplayData.remoteDisplay_SolarMonitor && lastDisplayData.remoteDisplay_BatteryMonitor)
{
// combined mode: no headline and no version - space reserved for yield display
return;
}
String headline = "Solar Monitor";
if (lastDisplayData.remoteDisplay_BatteryMonitor)
{
headline = "Battery Monitor";
tft.setTextColor(isNight ? TFT_MAROON : TFT_DARKGREEN);
}
else
{
tft.setTextColor(isNight ? TFT_MAROON : TFT_GOLD);
}
tft.drawCentreString(headline, 120, 45, 1);
tft.setTextColor(isNight ? TFT_MAROON : TFT_DARKCYAN);
tft.drawCentreString(version, 120, 58, 1);
}
}
uint8_t last_timeSS = 0;
int lastValueWidth_Small_y42 = 0; // Track max width for position y=42 (top)
int lastValueWidth_Small_y180 = 0; // Track max width for position y=180 (bottom)
void DisplayTFT::drawText_Value_Small(uint8_t x, uint8_t y, float value, uint8_t decimals, String unit, uint8_t colorRed, uint8_t colorGreen, uint8_t colorBlue)
{
if (x == 0 && y == 0)
{
x = 120;
y = 180;
}
uint32_t textColor = isNight ? tft.color565(185, 0, 0) : tft.color565(colorRed, colorGreen, colorBlue);
if (value < 0)
value = 0;
String shownValue = String(value, (uint)decimals);
int currentWidth = tft.textWidth(shownValue, 4);
// Select tracker based on y position
int &maxWidthRef = (y == 42) ? lastValueWidth_Small_y42 : lastValueWidth_Small_y180;
// Track maximum width for this position to ensure full clearing
if (currentWidth > maxWidthRef)
maxWidthRef = currentWidth;
tft.setTextColor(textColor, TFT_BLACK);
tft.setTextDatum(TC_DATUM);
// Use textPadding to clear the full area based on max width seen at this position
tft.setTextPadding(maxWidthRef + 10); // Add extra padding for safety
tft.drawCentreString(shownValue, x, y, 4);
tft.setTextPadding(0); // Reset padding
// Position unit based on current value width, with fixed gap
int unit_x_pos = x + (currentWidth / 2) + 6;
tft.setTextColor(textColor, TFT_BLACK);
tft.setTextDatum(TL_DATUM);
tft.setTextPadding(0);
tft.drawString(unit, unit_x_pos, y + 11, 1);
}
void DisplayTFT::drawClockWithBlinkingColon(String time)
{
tft.setTextSize(1);
tft.setTextColor(TFT_DARKCYAN, TFT_BLACK);
uint8_t timeSS = time.substring(time.lastIndexOf(":") + 1).toInt();
static bool blinkColon = true;
if (timeSS != last_timeSS)
{
last_timeSS = timeSS;
blinkColon = !blinkColon;
}
String hours = time.substring(0, 2);
String minutes = time.substring(3, 5);
String colon = blinkColon ? ":" : " ";
int xHours = 101, xColon = 120, xMinutes = 140, yPosition = 215;
tft.setTextDatum(TC_DATUM);
int padding = tft.textWidth("00", 4);
tft.setTextPadding(padding);
tft.drawString(hours, xHours, yPosition, 4);
tft.drawString(minutes, xMinutes, yPosition, 4);
padding = tft.textWidth(":", 4);
tft.setTextPadding(padding);
tft.drawString(colon, xColon, yPosition, 4);
}
void DisplayTFT::drawSecondsRing(String time)
{
uint8_t x = GAUGE_RADIUS, y = GAUGE_RADIUS, r = GAUGE_RADIUS;
int sec = time.substring(time.lastIndexOf(":") + 1).toInt();
int secpoint = (sec * 6) + 180;
uint32_t secActiveRingColor = isNight ? TFT_MAROON : TFT_RED;
uint32_t secEmptyRingColor = isNight ? TFT_BLACK : TFT_SILVER;
if (userConfig.displayTFTsecondsRing == false)
sec = 0;
if (sec < 31)
{
tft.drawSmoothArc(x, y, r, r - 1, 0, 180, secEmptyRingColor, TFT_BLACK);
if (sec != 0)
tft.drawSmoothArc(x, y, r, r - 1, 180, secpoint, secActiveRingColor, TFT_BLACK);
if (sec != 30)
tft.drawSmoothArc(x, y, r, r - 1, secpoint, 360, secEmptyRingColor, TFT_BLACK);
}
else
{
tft.drawSmoothArc(x, y, r, r - 1, secpoint - 360, 180, secEmptyRingColor, TFT_BLACK);
tft.drawSmoothArc(x, y, r, r - 1, 180, 360, secActiveRingColor, TFT_BLACK);
tft.drawSmoothArc(x, y, r, r - 1, 0, secpoint - 360, secActiveRingColor, TFT_BLACK);
}
}
void DisplayTFT::drawGaugeRing(int32_t value, int32_t maxValue, uint32_t color, uint32_t darkColor, uint32_t edgeColor, int32_t minAngle, int32_t maxAngle)
{
const uint32_t POWER_RING_COLOR = tft.color565(255, 165, 0);
const uint32_t POWER_RING_DARK = tft.color565(80, 53, 0);
const uint32_t POWER_RING_DARK_LIGHT = tft.color565(125, 83, 0);
(void)POWER_RING_COLOR;
(void)POWER_RING_DARK;
(void)POWER_RING_DARK_LIGHT;
int32_t angle = map(value, 0, maxValue, minAngle + 1, maxAngle);
angle = constrain(angle, minAngle + 1, maxAngle);
if (angle != maxAngle && (maxAngle - angle) > 1 && angle < (maxAngle - 2))
{
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, GAUGE_RADIUS - 2, GAUGE_INNER_RADIUS + 1, angle + 1, maxAngle - 1, darkColor, TFT_BLACK);
if (angle <= (minAngle + 1))
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, GAUGE_RADIUS, GAUGE_INNER_RADIUS, minAngle, minAngle + 1, edgeColor, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, GAUGE_RADIUS, GAUGE_INNER_RADIUS, maxAngle - 1, maxAngle, edgeColor, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, GAUGE_INNER_RADIUS - 1, GAUGE_INNER_RADIUS, angle, maxAngle, edgeColor, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, GAUGE_RADIUS, GAUGE_RADIUS - 1, angle, maxAngle, edgeColor, TFT_BLACK);
}
if (minAngle < angle && (angle - minAngle) > 1 && minAngle > 0)
{
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, GAUGE_RADIUS - 1, GAUGE_INNER_RADIUS, minAngle, angle, color, TFT_BLACK);
}
}
void DisplayTFT::drawGauge_SolarMonitor(String time)
{
const uint32_t POWER_RING_COLOR = tft.color565(255, 165, 0);
const uint32_t POWER_RING_DARK = tft.color565(80, 53, 0);
const uint32_t POWER_RING_DARK_LIGHT = tft.color565(125, 83, 0);
if (!isNight)
{
drawClockWithBlinkingColon(time);
uint16_t display_wattage = (lastDisplayData.totalPower > 0) ? lastDisplayData.totalPower : 0;
if (display_wattage > lastDisplayData.totalPowerMax)
{
lastDisplayData.totalPowerMax = display_wattage;
Serial.println("\nDisplayTFT:\t >>>>>>>> new max power: " + String(lastDisplayData.totalPowerMax) + "\n");
lastDisplayData.totalPowerMaxTimestamp = platformData.currentNTPtime;
}
if (lastDisplayData.totalPowerMax > 0)
{
bool isMidnight = (((platformData.currentNTPtime / 60) % 1440) == 0);
if (isMidnight || ((platformData.currentNTPtime - lastDisplayData.totalPowerMaxTimestamp) > (3600 * 12)))
{
lastDisplayData.totalPowerMax = 1;
lastDisplayData.totalPowerMaxTimestamp = platformData.currentNTPtime;
Serial.println("\nDisplayTFT:\t >> reset max power to 1 - midnight: " + String(isMidnight) + "\n");
}
}
if (lastDisplayData.totalPowerMax <= 1)
lastDisplayData.totalPowerMax = 1;
drawGaugeRing(display_wattage, lastDisplayData.totalPowerMax, POWER_RING_COLOR, POWER_RING_DARK, POWER_RING_DARK_LIGHT, GAUGE_MIN_ANGLE, GAUGE_MAX_ANGLE);
}
}
void DisplayTFT::drawGauge_BatteryMonitor(String time)
{
const uint32_t SOC_RING_COLOR = tft.color565(0, 252, 105);
const uint32_t SOC_RING_DARK = tft.color565(0, 55, 20);
const uint32_t SOC_RING_DARK_LIGHT = tft.color565(0, 105, 46);
if (!isNight)
{
drawClockWithBlinkingColon(time);
uint16_t display_soc = (lastDisplayData.battery_SOC > 0) ? (uint16_t)lastDisplayData.battery_SOC : 0;
drawGaugeRing(display_soc, 100, SOC_RING_COLOR, SOC_RING_DARK, SOC_RING_DARK_LIGHT, GAUGE_MIN_ANGLE, GAUGE_MAX_ANGLE);
}
}
void DisplayTFT::drawGauge_SolarBatteryMonitor(String time)
{
const uint32_t POWER_RING_COLOR = tft.color565(255, 165, 0);
const uint32_t POWER_RING_DARK = tft.color565(80, 53, 0);
const uint32_t POWER_RING_DARK_LIGHT = tft.color565(125, 83, 0);
uint16_t display_wattage = (lastDisplayData.totalPower > 0) ? lastDisplayData.totalPower : 0;
if (display_wattage > lastDisplayData.totalPowerMax)
{
lastDisplayData.totalPowerMax = display_wattage;
Serial.println("\nDisplayTFT:\t >>>>>>>> new max power: " + String(lastDisplayData.totalPowerMax) + "\n");
lastDisplayData.totalPowerMaxTimestamp = platformData.currentNTPtime;
}
if (lastDisplayData.totalPowerMax > 0)
{
bool isMidnight = (((platformData.currentNTPtime / 60) % 1440) == 0);
if (isMidnight || ((platformData.currentNTPtime - lastDisplayData.totalPowerMaxTimestamp) > (3600 * 12)))
{
lastDisplayData.totalPowerMax = 1;
lastDisplayData.totalPowerMaxTimestamp = platformData.currentNTPtime;
Serial.println("\nDisplayTFT:\t >> reset max power to 1 - midnight: " + String(isMidnight) + "\n");
}
}
if (lastDisplayData.totalPowerMax <= 1)
lastDisplayData.totalPowerMax = 1;
drawGaugeRing(display_wattage, lastDisplayData.totalPowerMax, POWER_RING_COLOR, POWER_RING_DARK, POWER_RING_DARK_LIGHT, POWER_MIN_ANGLE, POWER_MAX_ANGLE);
// battery SOC ring (two quarter-arcs at bottom: 50°→0° left side, 310°→360° right side)
const uint32_t SOC_COLOR = tft.color565(0, 252, 105);
const uint32_t SOC_DARK = tft.color565(1, 66, 25);
const uint32_t SOC_DARK_LIGHT = tft.color565(0, 105, 46);
static uint8_t soc_r_outer = GAUGE_RADIUS;
uint8_t soc_r_inner = soc_r_outer - 15;
uint16_t display_soc = (lastDisplayData.battery_SOC > 0) ? (uint16_t)lastDisplayData.battery_SOC : 0;
if (display_soc < 100)
{
if (display_soc <= 50)
{
uint16_t left_angle = map(display_soc, 0, 50, SOC_START_LEFT, 0);
if (left_angle != 0)
{
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer - 2, soc_r_inner + 2, 0, left_angle, SOC_DARK, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_inner, soc_r_inner + 1, 0, left_angle, SOC_DARK_LIGHT, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer, soc_r_outer - 1, 0, left_angle, SOC_DARK_LIGHT, TFT_BLACK);
}
if (left_angle >= SOC_START_LEFT)
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer, soc_r_inner, SOC_START_LEFT - 1, SOC_START_LEFT, SOC_DARK_LIGHT, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer - 2, soc_r_inner + 2, SOC_START_RIGHT + 1, 360, SOC_DARK, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer, soc_r_inner, SOC_START_RIGHT, SOC_START_RIGHT + 1, SOC_DARK_LIGHT, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_inner, soc_r_inner + 1, SOC_START_RIGHT, 360, SOC_DARK_LIGHT, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer, soc_r_outer - 1, SOC_START_RIGHT, 360, SOC_DARK_LIGHT, TFT_BLACK);
}
else
{
uint16_t right_angle = map(display_soc, 50, 100, 360, SOC_START_RIGHT);
if (right_angle != SOC_START_RIGHT)
{
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer - 2, soc_r_inner + 2, SOC_START_RIGHT, right_angle, SOC_DARK, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer, soc_r_inner, SOC_START_RIGHT, SOC_START_RIGHT + 1, SOC_DARK_LIGHT, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_inner, soc_r_inner + 1, SOC_START_RIGHT, right_angle, SOC_DARK_LIGHT, TFT_BLACK);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer, soc_r_outer - 1, SOC_START_RIGHT, right_angle, SOC_DARK_LIGHT, TFT_BLACK);
}
}
}
if (display_soc > 0 && display_soc <= 50)
{
uint16_t left_angle = map(display_soc, 0, 50, SOC_START_LEFT, 0);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer, soc_r_inner, left_angle, SOC_START_LEFT, SOC_COLOR, TFT_BLACK);
}
else if (display_soc > 50)
{
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer, soc_r_inner, 0, SOC_START_LEFT, SOC_COLOR, TFT_BLACK);
uint16_t right_angle = map(display_soc, 50, 100, 360, SOC_START_RIGHT);
tft.drawSmoothArc(GAUGE_RADIUS, GAUGE_RADIUS, soc_r_outer, soc_r_inner, right_angle, 360, SOC_COLOR, TFT_BLACK);
}
}
void DisplayTFT::checkChangedValues()
{
valueChanged = false;
if (lastDisplayData.totalPower != round(dtuGlobalData.grid.power.getValue()))
valueChanged = true;
}
void DisplayTFT::setBrightnessAuto()
{
#ifdef DGC9A01_MOUNTED
digitalWrite(BACKLIIGHT_PIN, 1);
#else
// do not change brightness if it is set to 0
if (userConfig.displayBrightnessDay == 0)
return;
uint8_t brigtnessNorm = isNight ? userConfig.displayBrightnessNight : userConfig.displayBrightnessDay;
if (isNight && !userConfig.displayNightClock)
brigtnessNorm = 0;
if (valueChanged && !isNight) // set brightness to max if value changed
{
brightness = isNight ? userConfig.displayBrightnessDay : BRIGHTNESS_TFT_MAX;
analogWrite(BACKLIIGHT_PIN, brightness);
}
else if (brightness > brigtnessNorm) // decrease brightness slowly
{
brightness = brightness - 1;
analogWrite(BACKLIIGHT_PIN, brightness);
}
else if (brightness < brigtnessNorm) // increase brightness slowly
{
brightness = brightness + 1;
analogWrite(BACKLIIGHT_PIN, brightness);
}
#endif
}
void DisplayTFT::checkNightMode()
{
boolean isNightBySchedule = false;
boolean isNightByOffline = false;
// get currentTime in minutes to 00:00 of current day from current time in minutes to 1.1.1970 00:00
uint16_t currentTime = (platformData.currentNTPtime / 60) % 1440;
// Serial.println("current time in minutes today: " + String(currentTime) + " - start: " + String(userConfig.displayNightmodeStart) + " - end: " + String(userConfig.displayNightmodeEnd) + " - current brightness: " + String(brightness) + " - dtuState: " + String(dtuConnection.dtuConnectState) + " night: " + String(isNight));
if (userConfig.displayNightMode)
{
// schedule trigger
// check if night mode can be activated - start time is smaller than end time
if (
(userConfig.displayNightmodeStart < userConfig.displayNightmodeEnd && currentTime >= userConfig.displayNightmodeStart && currentTime < userConfig.displayNightmodeEnd) ||
(userConfig.displayNightmodeStart > userConfig.displayNightmodeEnd && (currentTime >= userConfig.displayNightmodeStart || currentTime < userConfig.displayNightmodeEnd)))
{
isNightBySchedule = true;
// Serial.println("DisplayTFT:\t >> night mode activated by schedule");
}
else
{
isNightBySchedule = false;
// Serial.println("DisplayTFT:\t >> day mode activated by schedule");
}
// offline trigger
if (dtuConnection.dtuConnectionOnline == true)
{
isNightByOffline = false;
// Serial.println("DisplayTFT:\t >> night mode activated by offline trigger");
}
else if (dtuConnection.dtuConnectionOnline == false)
{
isNightByOffline = true;
// Serial.println("DisplayTFT:\t >> day mode activated by offline trigger");
}
// summary
// start night mode if schedule or offline trigger (when enabled) is active
if (isNightBySchedule || (userConfig.displayNightModeOfflineTrigger && isNightByOffline))
{
if (!isNight)
{
isNight = true;
Serial.println("DisplayTFT:\t >> night mode activated - schedule: " + String(isNightBySchedule) + " - offline: " + String(isNightByOffline));
}
}
// start day mode if
// offline trigger is enabled and schedule is not active and offline is not active OR
// offline trigger is dsiabled and schedule is not active
else if ((userConfig.displayNightModeOfflineTrigger && !isNightBySchedule && !isNightByOffline) || (!userConfig.displayNightModeOfflineTrigger && !isNightBySchedule))
{
if (isNight)
{
isNight = false;
Serial.println("DisplayTFT:\t >> day mode activated - schedule: " + String(isNightBySchedule) + " - offline: " + String(isNightByOffline));
}
}
}
}
void DisplayTFT::showDebug()
{
tft.getSetup(user); //
Serial.print("\n\n\n[code]\n");
Serial.print("TFT_eSPI ver = ");
Serial.println(user.version);
printProcessorName();
#if defined(ESP32) || defined(ARDUINO_ARCH_ESP8266)
if (user.esp < 0x32F000 || user.esp > 0x32FFFF)
{
Serial.print("Frequency = ");
Serial.print(ESP.getCpuFreqMHz());
Serial.println("MHz");
}
#endif
#ifdef ARDUINO_ARCH_ESP8266
Serial.print("Voltage = ");
Serial.print(ESP.getVcc() / 918.0);
Serial.println("V"); // 918 empirically determined
#endif
Serial.print("Transactions = ");
Serial.println((user.trans == 1) ? "Yes" : "No");
Serial.print("Interface = ");
Serial.println((user.serial == 1) ? "SPI" : "Parallel");
#ifdef ARDUINO_ARCH_ESP8266
if (user.serial == 1)
{
Serial.print("SPI overlap = ");
Serial.println((user.overlap == 1) ? "Yes\n" : "No\n");
}
#endif
if (user.tft_driver != 0xE9D) // For ePaper displays the size is defined in the sketch
{
Serial.print("Display driver = ");
Serial.println(user.tft_driver, HEX); // Hexadecimal code
Serial.print("Display width = ");
Serial.println(user.tft_width); // Rotation 0 width and height
Serial.print("Display height = ");
Serial.println(user.tft_height);
Serial.println();
}
else if (user.tft_driver == 0xE9D)
Serial.println("Display driver = ePaper\n");
if (user.r0_x_offset != 0)
{
Serial.print("R0 x offset = ");
Serial.println(user.r0_x_offset);
} // Offsets, not all used yet
if (user.r0_y_offset != 0)
{
Serial.print("R0 y offset = ");
Serial.println(user.r0_y_offset);
}
if (user.r1_x_offset != 0)
{
Serial.print("R1 x offset = ");
Serial.println(user.r1_x_offset);
}
if (user.r1_y_offset != 0)
{
Serial.print("R1 y offset = ");
Serial.println(user.r1_y_offset);
}
if (user.r2_x_offset != 0)
{
Serial.print("R2 x offset = ");
Serial.println(user.r2_x_offset);
}
if (user.r2_y_offset != 0)
{