-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDisplayMgmt.ino
1571 lines (1380 loc) · 54.2 KB
/
DisplayMgmt.ino
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 "globalInclude.h"
// This file contains most of the functions to handle the display
#include "modeMgmt.h"
#define HORIZ_DIV_POS 85
#define VERT_DIV_POS 125
#define LIGHT_SENSOR_IN_PIN 34
#define TFT_BACKLIGHT_OUT_PIN 15
#define SECONDS_IN_DAY 86400
//===================================================================
//======================== Globals ==================================
//===================================================================
extern alarmData alarm1;
extern alarmData alarm2;
extern alarmData alarm3;
//button objects
TFT_eSPI_Button hoursUp, hoursDown, minUp, minDown;
TFT_eSPI_Button roomLightButton, readLightButton, nightLightButton;
TFT_eSPI_Button hUpButton, hDownButton, sUpButton, sDownButton, bUpButton, bDownButton;
TFT_eSPI_Button daysButton[7];
TFT_eSPI_Button roomSubModeButton, readSubModeButton, nightSubModeButton;
//===================================================================
//================ Display Manager ==================================
//===================================================================
void dispMgr( void * parameter) {
extern displayMgr disp;
static int lastMode = 0;
// Set up PWM for screen backlight
ledcSetup(0, 5000, 8);
ledcAttachPin(TFT_BACKLIGHT_OUT_PIN, 0);
controlBacklight(); // set the screen brightness
if ( esp_task_wdt_add(NULL) != ESP_OK) { // add task to WDT
Serial.println("dispMgr: Unable to add displayMgr to taskWDT!");
}
uint32_t ts;
TickType_t xLastWakeTime;
static int minuteNow = 0, minutePrevious = 0;
Serial.println("dispMgr: Entering Screen Management task");
// Setup tft display
if (xSemaphoreTake(tftMutex, (TickType_t) 50 ) == pdTRUE ) {
tft.begin();
tft.setRotation(3);
// read diagnostics (optional but can help debug problems)
uint8_t x = tft.readcommand8(ILI9341_RDMODE);
Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDMADCTL);
Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDPIXFMT);
Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDIMGFMT);
Serial.print("Image Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDSELFDIAG);
Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
tft.fillScreen(TFT_BLACK);
xSemaphoreGive(tftMutex);
}
else {
Serial.print("dispMgr: Unable to set up Display. Restarting.");
ESP.restart();
}
drawTextString("Waiting for Time Sync.", tft.width() / 2, (tft.height() / 2) + 30, FSSB12, 320, MC_DATUM, TFT_WHITE, TFT_BLACK);
drawTextString("Data by OpenWeather", tft.width() / 2, (tft.height() / 2) - 30 , FSSB12, 320, MC_DATUM, TFT_WHITE, TFT_BLACK);
while (timeStatus() != timeSet) {
vTaskDelay(500 / portTICK_PERIOD_MS);
Serial.println("dispMgr: Delaying start of screen. Status: " + String(timeStatus()));
}
ts = now();
drawTime(ts, true);
drawWeatherDisplay(true);
if (esp_task_wdt_reset() != ESP_OK) {
Serial.println("dispMgr: Unable to reset displayMgr taskWDT!");
}
const TickType_t xFrequency = 75 / portTICK_PERIOD_MS; // run the master display loop at 20Hz
xLastWakeTime = xTaskGetTickCount();
// Master Display Loop
for (;;) {
ts = now();
minuteNow = minute(ts);
controlBacklight(); // set the screen brightness
if (disp.getFullReDraw() || lastMode != disp.getCurrentMode()) {
sprite1.delSprite();
sprite2.delSprite();
sprite3.delSprite();
if (disp.getFullReDraw()) {
drawTime(ts, true);
}
disp.setFullReDraw(false);
disp.setSpriteEnable(false);
lastMode = disp.getCurrentMode();
clearWorkingArea();
if (disp.getCurrentMode() == MAIN_MODE) {
drawWeatherDisplay(true);
}
else if (disp.getCurrentMode() == CURRENT_WX_MODE) {
drawCurrentWeatherDisplay(true);
}
else if (disp.getCurrentMode() == FORECAST_WX_MODE) {
drawForecastWeatherDisplay(true);
}
else if (disp.getCurrentMode() == HOURLY_WX_MODE) {
drawHourlyWeatherDisplay(true);
}
else if (disp.getCurrentMode() == ALARM_DISPLAY_MODE) {
drawAlarmDisplay(true);
}
else if (disp.getCurrentMode() == ALARM_SET_MODE) {
drawAlarmSetDisplay(true, disp.getAlarmEdit());
}
else if (disp.getCurrentMode() == GEN_LIGHT_CTRL_MODE) {
drawLightSetDisplay(true);
}
disp.setSpriteEnable(true);
continue;
}
if (minuteNow != minutePrevious) {
drawTime(ts, false);
minutePrevious = minuteNow;
}
if (disp.getDrawLowerScreen()) {
disp.setDrawLowerScreen(false);
disp.setSpriteEnable(false);
if (disp.getCurrentMode() == MAIN_MODE) {
drawWeatherDisplay(false);
}
else if (disp.getCurrentMode() == CURRENT_WX_MODE) {
drawCurrentWeatherDisplay(false);
}
else if (disp.getCurrentMode() == FORECAST_WX_MODE) {
drawForecastWeatherDisplay(false);
}
else if (disp.getCurrentMode() == HOURLY_WX_MODE) {
drawHourlyWeatherDisplay(false);
}
else if (disp.getCurrentMode() == ALARM_DISPLAY_MODE) {
drawAlarmDisplay(false);
}
else if (disp.getCurrentMode() == ALARM_SET_MODE) {
drawAlarmSetDisplay(false, disp.getAlarmEdit());
}
else if (disp.getCurrentMode() == GEN_LIGHT_CTRL_MODE) {
drawLightSetDisplay(false);
}
disp.setSpriteEnable(true);
}
if (esp_task_wdt_reset() != ESP_OK) {
Serial.println("Unable to reset displayMgr taskWDT!");
}
if (disp.getDrawTimeSection()) {
disp.setDrawTimeSection(false);
drawTime(now(), false);
}
vTaskDelayUntil( &xLastWakeTime, xFrequency );
}
}
//===================================================================
//==================== Control Backlight ============================
//===================================================================
void controlBacklight ( void ) {
uint16_t lightReading;
uint16_t backlightCtrl;
static uint16_t filteredBacklightCtrl = 128;
static uint16_t loopCount = 0;
lightReading = analogRead(LIGHT_SENSOR_IN_PIN);
if (lightReading < 127) {
backlightCtrl = 8;
}
else if (lightReading < 2500) {
backlightCtrl = 8 + (int)(lightReading / 10.46);
}
else {
backlightCtrl = 275; // overkill to allow the filter to saturate
}
filteredBacklightCtrl = (int)(((float)filteredBacklightCtrl * 0.95) + ((float)backlightCtrl * 0.05));
uint16_t blTemp;
if (disp.checkRecentTouch()) blTemp = filteredBacklightCtrl;
else blTemp = filteredBacklightCtrl >> 2;
if (blTemp > 255) blTemp = 255; // bounds check
if (blTemp < 3) blTemp = 3; // bounds check
ledcWrite(0, blTemp);
}
//===================================================================
//==================== Draw Time ====================================
//===================================================================
// Draw the "time" section of the screen
void drawTime(time_t ts, bool repaint)
{
int xpos = tft.width() / 2; // Half the screen width
int padding;
int16_t timeVertPos = 70, dateVertPos = 20;
String dateString = assembleDateStr(ts);
String hours = assembleTimeStr(ts);
static int wifiStatusPrevious = -1;
static int dayPrevious = 0;
static int dayNow = 0;
if (repaint) {
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
tft.fillRect(0, 0, 320, HORIZ_DIV_POS + 1, TFT_BLACK); // Clear the upper part of the screen
// Draw the divider
tft.drawFastHLine(0, HORIZ_DIV_POS - 1, tft.width(), TFT_BLUE);
tft.drawFastHLine(0, HORIZ_DIV_POS, tft.width(), TFT_BLUE);
tft.drawFastHLine(0, HORIZ_DIV_POS + 1, tft.width(), TFT_BLUE);
xSemaphoreGive(tftMutex);
}
else {
Serial.println("drawTime: Unable to run inital screen setup. Try again next time.");
}
}
Serial.println("drawTime: Time Redraw. - " + hours);
if (xSemaphoreTake(tftMutex, (TickType_t) 75) == pdTRUE ) {
tft.setFreeFont(FSSB24);
padding = tft.textWidth(" 99:99 PM ", GFXFF);
tft.setTextPadding(padding);
tft.setTextDatum(C_BASELINE); // Centre text on x,y position
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawString(hours, xpos, timeVertPos, GFXFF);
xSemaphoreGive(tftMutex);
}
dayNow = day(ts);
if (dayNow != dayPrevious || repaint) {
dayPrevious = dayNow;
Serial.println("drawTime: Date Redraw. - " + dateString);
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
tft.setFreeFont(FSS9);
tft.setTextDatum(C_BASELINE); // Centre text on x,y position
tft.setTextColor(TFT_GREEN, TFT_BLACK);
padding = tft.textWidth(" Saturday Dec. 99 9999 ", GFXFF);
tft.setTextPadding(padding);
tft.drawString(dateString, xpos, 20, GFXFF);
xSemaphoreGive(tftMutex);
}
else {
Serial.println("drawTime: Unable to draw date to display. unable to obtain Mutex");
}
}
// Draw WiFi Indicator
if (disp.getCurrWiFiStatus() != wifiStatusPrevious || repaint) {
wifiStatusPrevious = disp.getCurrWiFiStatus();
drawWiFiStatus(disp.getCurrWiFiStatus());
}
tft.setTextPadding(0);
drawAlarmIndicator(repaint);
drawReadingLightButton(repaint);
drawRoomLightButton(repaint);
drawNightLightButton(repaint);
}
//===================================================================
//============== Draw Alarm Indicator ===============================
//===================================================================
void drawAlarmIndicator(bool repaint) {
static bool lastAlarmAct;
static bool lastSnoozeAct;
static bool lastRingAct;
bool alarmAct = (alarm1.isActive() || alarm2.isActive() || alarm3.isActive());
bool snoozeAct = (alarm1.isSnoozed() || alarm2.isSnoozed() || alarm3.isSnoozed());
uint8_t ringAct = disp.getAlarmRinging();
if (alarmAct != lastAlarmAct || snoozeAct != lastSnoozeAct || ringAct != lastRingAct || repaint) {
if (snoozeAct || ringAct != 0) {
String alarmIcon = ("/alarmicon/alarm_red_sm.bmp");
char iconChar[31];
alarmIcon.toCharArray(iconChar, 31);
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
drawBmp(iconChar, 30, 2);
xSemaphoreGive(tftMutex);
}
else {
Serial.print("drawAlarmIndicator: Unable to draw alarm status to display. Unable to obtain Mutex");
}
}
else if (alarmAct) {
String alarmIcon = ("/alarmicon/alarm_sm.bmp");
char iconChar[31];
alarmIcon.toCharArray(iconChar, 31);
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
drawBmp(iconChar, 30, 2);
xSemaphoreGive(tftMutex);
}
else {
Serial.print("drawAlarmIndicator: Unable to draw alarm status to display. Unable to obtain Mutex");
}
}
else {
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
tft.fillRect(30, 2, 20, 20, TFT_BLACK);
xSemaphoreGive(tftMutex);
}
else {
Serial.print("drawAlarmIndicator: Unable to erase alarm status display. Unable to obtain Mutex.");
}
}
lastAlarmAct = alarmAct;
lastSnoozeAct = snoozeAct;
lastRingAct = ringAct;
}
}
//===================================================================
//============== Draw Reading Light Indicator =======================
//===================================================================
void drawReadingLightButton(bool repaint) {
static bool lastReadLightState;
String lightIcon;
readLightButton.initButton(&tft, 26, 55, 50, 50, TFT_WHITE, TFT_BLACK, TFT_RED, "", 1);
bool currentState = ledMaster.getReadLightState();
if (currentState != lastReadLightState || repaint) {
lastReadLightState = currentState;
if (ledMaster.getReadLightState()) {
lightIcon = "/lightIcon/book_on.bmp";
}
else {
lightIcon = "/lightIcon/book_off.bmp";
}
if (repaint) {
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
readLightButton.drawButton(false);
xSemaphoreGive(tftMutex);
}
else {
Serial.print("drawReadingLightButton: Unable to draw light status to display. Unable to obtain Mutex");
}
}
char iconChar[31];
lightIcon.toCharArray(iconChar, 31);
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
drawBmp(iconChar, 6, 35);
xSemaphoreGive(tftMutex);
}
else {
Serial.print("drawReadingLightButton: Unable to draw light status to display. Unable to obtain Mutex");
}
}
}
//===================================================================
//================= Draw Room Light Indicator =======================
//===================================================================
void drawRoomLightButton(bool repaint) {
static bool lastRoomLightState;
String lightIcon;
roomLightButton.initButton(&tft, 295, 20, 50, 41, TFT_WHITE, TFT_BLACK, TFT_RED, "", 1);
bool currentState = ledMaster.getRoomLightState();
if (currentState != lastRoomLightState || repaint) {
lastRoomLightState = currentState;
if (currentState) {
lightIcon = "/lightIcon/light_on30.bmp";
}
else {
lightIcon = "/lightIcon/light_off30.bmp";
}
if (repaint) {
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
roomLightButton.drawButton(false);
xSemaphoreGive(tftMutex);
}
else {
Serial.print("drawRoomLightButton: Unable to draw light status to display. Unable to obtain Mutex");
}
}
char iconChar[31];
lightIcon.toCharArray(iconChar, 31);
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
drawBmp(iconChar, 280, 5);
xSemaphoreGive(tftMutex);
}
else {
Serial.print("drawRoomLightButton: Unable to draw light status to display. Unable to obtain Mutex");
}
}
}
//===================================================================
//================= Draw Night Light Indicator =======================
//===================================================================
void drawNightLightButton(bool repaint) {
static bool lastNightLightState;
String lightIcon;
nightLightButton.initButton(&tft, 295, 61, 50, 41, TFT_WHITE, TFT_BLACK, TFT_RED, "", 1);
bool currentState = ledMaster.getNightLightState();
if (currentState != lastNightLightState || repaint) {
lastNightLightState = currentState;
if (currentState) {
lightIcon = "/lightIcon/night_on30.bmp";
}
else {
lightIcon = "/lightIcon/night_off30.bmp";
}
if (repaint) {
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
nightLightButton.drawButton(false);
xSemaphoreGive(tftMutex);
}
else {
Serial.print("drawNightLightButton: Unable to draw light status to display. Unable to obtain Mutex");
}
}
char iconChar[31];
lightIcon.toCharArray(iconChar, 31);
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
drawBmp(iconChar, 280, 47);
xSemaphoreGive(tftMutex);
}
else {
Serial.print("drawNightLightButton: Unable to draw light status to display. Unable to obtain Mutex");
}
}
}
//===================================================================
//============== Draw Weather Display ===============================
//===================================================================
void drawWeatherDisplay(bool repaint) {
int padding;
int showTomorrow;
int lineLength;
String msgTmp;
static int lastCurTemp;
static String lastHighTemp;
static String lastLowTemp;
static String lastHumid;
static String lastPercip;
static String lastCurrMsg;
static String lastMainPgMessage;
static String lastIcon = " ";
bool redrawCurrStat = false;
static uint32_t lastOwAPICalls;
if (hour() < 14 || hour() == 24) { // After 2pm show tomorrow's forcast
showTomorrow = 0;
}
else {
showTomorrow = 1;
}
if (repaint) {
if (xSemaphoreTake(tftMutex, (TickType_t) 100) == pdTRUE ) {
tft.drawFastVLine(VERT_DIV_POS - 1, HORIZ_DIV_POS, tft.height(), TFT_BLUE);
tft.drawFastVLine(VERT_DIV_POS, HORIZ_DIV_POS, tft.height(), TFT_BLUE);
tft.drawFastVLine(VERT_DIV_POS + 1, HORIZ_DIV_POS, tft.height(), TFT_BLUE);
xSemaphoreGive(tftMutex);
}
else {
Serial.println("drawWeatherDisplay: Unable to run inital screen setup. Try again next time.");
}
}
// Draw the weather icon and text
if (disp.getWeatherValid() == true) {
if (xSemaphoreTake(owMutex, (TickType_t) 200) != pdTRUE ) {
Serial.println("drawWeatherDisplay: Unable to get dfwMutex to update weather display!");
return;
}
String weatherIcon = "";
String currentSummary = current->main;
currentSummary.toLowerCase();
weatherIcon = getMeteoconIcon(current->id, true, 0);
if (weatherIcon != lastIcon || repaint) { // save some work drawing the weather icon
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
drawWeatherIcon(weatherIcon, 12, 105, true);
xSemaphoreGive(tftMutex);
lastIcon = weatherIcon;
redrawCurrStat = true;
}
else {
Serial.println("drawWeatherDisplay: Unable to push new weather icon.");
}
}
drawTextString("Currently", 62, 90, FSS9, 124, TC_DATUM, TFT_YELLOW, TFT_BLACK);
msgTmp = String(current->description) + ", " + String((int)round(current->temp)) + " F";
msgTmp[0] = msgTmp[0] - 32; // upper case 1st letter
lineLength = tft.textWidth(msgTmp, GFXFF);
if (msgTmp != lastCurrMsg || repaint || redrawCurrStat || (int)round(current->temp) != lastCurTemp) {
redrawCurrStat = false;
lastCurrMsg = msgTmp;
sprite1.delSprite();
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
tft.fillRect(4, 205, 120 , 20, TFT_BLACK);
xSemaphoreGive(tftMutex);
}
if (lineLength >= 115) {
sprite1.newSprite(msgTmp, 20, 115, 2, 20, TFT_YELLOW, 5, 205);
}
else {
drawTextString(msgTmp, 62, 205, FSS9, 124, TC_DATUM, TFT_YELLOW, TFT_BLACK);
}
}
if (showTomorrow == 1) {
drawTextString("Tomorrow's Forecast", 135, 90, FSS9, 194, TL_DATUM, TFT_YELLOW, TFT_BLACK);
}
else {
drawTextString("Today's Forecast", 135, 90, FSS9, 194, TL_DATUM, TFT_YELLOW, TFT_BLACK);
}
if (disp.getMainPgMessage() != lastMainPgMessage || repaint || lastOwAPICalls != OwAPICalls) {
drawTextString(disp.getMainPgMessage(), 135, 215, FSS9, 194, TL_DATUM, disp.getMainPgMessageColor(), TFT_BLACK);
lastMainPgMessage = disp.getMainPgMessage();
lastOwAPICalls = OwAPICalls;
}
msgTmp = String(daily->description[showTomorrow]);
msgTmp[0] = msgTmp[0] - 32; // upper case 1st letter
lineLength = tft.textWidth(msgTmp, GFXFF);
if (msgTmp != sprite2.currMsg() || repaint) {
sprite2.delSprite();
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
tft.fillRect(135, 115, 185 , 20, TFT_BLACK);
xSemaphoreGive(tftMutex);
}
if (lineLength >= 185) {
sprite2.newSprite(msgTmp, 20, 180, 2, 30, TFT_YELLOW, 135, 112);
}
else {
drawTextString(msgTmp, 135, 112, FSS9, 194, TL_DATUM, TFT_YELLOW, TFT_BLACK);
}
}
msgTmp = "Temp: " + String((int)round(daily->temp_morn[showTomorrow])) + "-" + String((int)round(daily->temp_day[showTomorrow])) + "-" + String((int)round(daily->temp_eve[showTomorrow])) + "-" + String((int)round(daily->temp_night[showTomorrow])) + " F";
if (lastHighTemp != msgTmp || repaint) {
drawTextString(msgTmp, 135, 135, FSS9, 194, TL_DATUM, TFT_YELLOW, TFT_BLACK);
lastHighTemp = msgTmp;
}
msgTmp = "Feels: " + String((int)round(daily->feels_like_morn[showTomorrow])) + "-" + String((int)round(daily->feels_like_day[showTomorrow])) + "-" + String((int)round(daily->feels_like_eve[showTomorrow])) + "-" + String((int)round(daily->feels_like_night[showTomorrow])) + " F";
if (lastLowTemp != msgTmp || repaint) {
drawTextString(msgTmp, 135, 155, FSS9, 194, TL_DATUM, TFT_YELLOW, TFT_BLACK);
lastLowTemp = msgTmp;
}
msgTmp = formatPrecipString(daily->pop[showTomorrow], daily->rain[showTomorrow], daily->snow[showTomorrow]);
if (lastPercip != msgTmp || repaint) {
drawTextString(msgTmp, 135, 175, FSS9, 194, TL_DATUM, TFT_YELLOW, TFT_BLACK);
lastPercip = msgTmp;
}
msgTmp = "Humidity: " + String(daily->humidity[showTomorrow]) + "%";
if (lastHumid != msgTmp || repaint) {
drawTextString(msgTmp, 135, 195, FSS9, 194, TL_DATUM, TFT_YELLOW, TFT_BLACK);
lastHumid = msgTmp;
}
xSemaphoreGive(owMutex);
}
else
{
drawTextString("Currently", 62, 90, FSS9, 124, TC_DATUM, TFT_GREEN, TFT_BLACK);
// turn off the sprites if the weather is invalid
sprite1.delSprite();
sprite2.delSprite();
sprite3.delSprite();
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {// clear the forecastSprite
tft.fillRect(130, 115, 190 , 20, TFT_BLACK);
xSemaphoreGive(tftMutex);
}
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) { // clear the currentSprite
tft.fillRect(4, 205, 120 , 20, TFT_BLACK);
xSemaphoreGive(tftMutex);
}
drawTextString("Wating for Weather...", 149, 90, FSS9, 194, TL_DATUM, TFT_YELLOW, TFT_BLACK);
if (disp.getMainPgMessage() != lastMainPgMessage || repaint) {
drawTextString(disp.getMainPgMessage(), 135, 215, FSS9, 194, TL_DATUM, disp.getMainPgMessageColor(), TFT_BLACK);
lastMainPgMessage = disp.getMainPgMessage();
}
}
}
//===================================================================
//================ Draw Current Weather =============================
//===================================================================
void drawCurrentWeatherDisplay(bool repaint) {
String msgTmp;
int lineLength;
static int lastCurTemp;
static String lastFeels;
static String lastCurrMsg;
static String lastHumid;
static String lastPress;
static String lastWind;
static String lastIcon = " ";
if (disp.getWeatherValid() == true) {
if (xSemaphoreTake(owMutex, (TickType_t) 100) != pdTRUE ) {
Serial.println("drawWeatherDisplay: Unable to get dfwMutex to update weather display!");
return;
}
if (repaint) {
drawTextString("Current Weather - 1/2", tft.width() / 2, 90, FSS9, 310, TC_DATUM, TFT_YELLOW, TFT_BLACK);
}
// draw weather text
msgTmp = String(current->description);
msgTmp[0] = msgTmp[0] - 32; // upper case 1st letter
msgTmp = "Summary: " + msgTmp;
// lineLength = tft.textWidth(msgTmp, GFXFF);
if (msgTmp != lastCurrMsg || repaint ) {
lastCurrMsg = msgTmp;
drawTextString(msgTmp, 5, 110, FSS9, 310, TL_DATUM, TFT_YELLOW, TFT_BLACK);
}
msgTmp = "Temprature: " + String(current->temp) + " F";
// lineLength = tft.textWidth(msgTmp, GFXFF);
if (msgTmp != lastCurrMsg || repaint ) {
lastCurrMsg = msgTmp;
drawTextString(msgTmp, 5, 130, FSS9, 310, TL_DATUM, TFT_YELLOW, TFT_BLACK);
}
msgTmp = "Feels Like: " + String(current->feels_like) + " F";
// lineLength = tft.textWidth(msgTmp, GFXFF);
if (msgTmp != lastFeels || repaint ) {
lastFeels = msgTmp;
drawTextString(msgTmp, 5, 150, FSS9, 310, TL_DATUM, TFT_YELLOW, TFT_BLACK);
}
msgTmp = "Humidity: " + String(current->humidity) + "% Clouds: " + String(current->clouds) + "%";
if (lastHumid != msgTmp || repaint) {
drawTextString(msgTmp, 5, 170, FSS9, 310, TL_DATUM, TFT_YELLOW, TFT_BLACK);
lastHumid = msgTmp;
}
msgTmp = "Presure: " + String(current->pressure) + " mBar";
if (lastPress != msgTmp || repaint) {
drawTextString(msgTmp, 5, 190, FSS9, 310, TL_DATUM, TFT_YELLOW, TFT_BLACK);
lastPress = msgTmp;
}
msgTmp = formatWindString(current->wind_speed, current->wind_gust, current->wind_deg);
//msgTmp = "I am the test case, and this is WAY too long to fit in the availible space.";
lineLength = tft.textWidth(msgTmp, GFXFF);
if (msgTmp != sprite1.currMsg() || repaint) {
sprite1.delSprite();
if (xSemaphoreTake(tftMutex, (TickType_t) 35) == pdTRUE ) {
tft.fillRect(5, 210, 310, 20, TFT_BLACK);
xSemaphoreGive(tftMutex);
}
if (lineLength >= 310) {
sprite1.newSprite(msgTmp, 20, 310, 2, 30, TFT_YELLOW, 5, 210);
}
else {
drawTextString(msgTmp, 5, 210, FSS9, 310, TL_DATUM, TFT_YELLOW, TFT_BLACK);
}
}
xSemaphoreGive(owMutex);
}
else {
drawTextString("Waiting for Weather Data...", tft.width() / 2, 90, FSS9, 310, TC_DATUM, TFT_YELLOW, TFT_BLACK);
}
}
//===================================================================
//================= Draw Hourly Weather =============================
//===================================================================
void drawHourlyWeatherDisplay(bool repaint) {
String msgTmp;
int lineLength;
int hourCounter;
static String lastTemp[3];
static String lastDesc[3];
static String lastWind[3];
static String lastIcon[3] = (" ", " ", " ");
uint8_t tmpHour;
static uint8_t lastHours[3] = {0};
time_t ts = now();
if (minute(ts) < 20) {
hourCounter = 1;
}
else {
hourCounter = 2;
}
if (disp.getWeatherValid() == true) {
if (xSemaphoreTake(owMutex, (TickType_t) 100) != pdTRUE ) {
Serial.println("drawWeatherDisplay: Unable to get dfwMutex to update weather display!");
return;
}
if (repaint) {
if (xSemaphoreTake(tftMutex, (TickType_t) 75) == pdTRUE ) {
tft.drawFastHLine(0, 110, tft.width(), TFT_WHITE);
tft.drawFastVLine(107, 110, tft.height(), TFT_WHITE);
tft.drawFastVLine(214, 110, tft.height(), TFT_WHITE);
xSemaphoreGive(tftMutex);
}
// draw weather text
msgTmp = "Hourly Forcast - 2/2";
drawTextString(msgTmp, tft.width() / 2, 90, FSS9, 320, TC_DATUM, TFT_YELLOW, TFT_BLACK);
}
for (int i = 0; i < 3; i++) {
tmpHour = hour(ts + (hourCounter * 3600));
if (lastHours[i] != tmpHour || repaint) {
msgTmp = assembleHourlyTimeStr(ts + (hourCounter * 3600));
drawTextString(msgTmp, 53 + (107 * i), 115, FSS9, 103, TC_DATUM, TFT_YELLOW, TFT_BLACK);
lastHours[i] = tmpHour;
}
String weatherIcon = "";
String dailySummary = hourly->main[hourCounter];
dailySummary.toLowerCase();
weatherIcon = getMeteoconIcon(hourly->id[hourCounter], false, hourCounter);
if (weatherIcon != lastIcon[i] || repaint) { // save some work drawing the weather icon
if (xSemaphoreTake(tftMutex, (TickType_t) 75) == pdTRUE ) {
drawWeatherIcon(weatherIcon, 28 + (107 * i), 135, false);
xSemaphoreGive(tftMutex);
lastIcon[i] = weatherIcon;
}
else {
Serial.println("drawWeatherDisplay: Unable to push new weather icon.");
}
}
msgTmp = String((int)round(hourly->temp[hourCounter])) + " F";
if (lastTemp[i] != msgTmp || repaint) {
drawTextString(msgTmp, 53 + (107 * i), 185, FSS9, 103, TC_DATUM, TFT_YELLOW, TFT_BLACK);
lastTemp[i] = msgTmp;
}
// Set workSprite to point to the right rolling sprite object should have used an array, but too late now
rollingSprite *workSprite;
if (i == 0) {
workSprite = &sprite1;
}
else if (i == 1) {
workSprite = &sprite2;
}
else if (i == 2) {
workSprite = &sprite3;
}
msgTmp = String(hourly->description[hourCounter]);
// msgTmp = "this is a test of the system";
msgTmp[0] = msgTmp[0] - 32; // upper case 1st letter
lineLength = tft.textWidth(msgTmp, GFXFF);
if (msgTmp != lastDesc[i] || repaint) {
workSprite->delSprite();
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
tft.fillRect((107 * i) + 1, 202, 106 , 222, TFT_BLACK);
xSemaphoreGive(tftMutex);
}
if (lineLength >= 100) {
workSprite->newSprite(msgTmp, 20, 95, 2, 30, TFT_YELLOW, (107 * i) + 6, 202);
}
else {
drawTextString(msgTmp, 53 + (107 * i), 202, FSS9, 103, TC_DATUM, TFT_YELLOW, TFT_BLACK);
}
lastDesc[i] = msgTmp;
}
msgTmp = String(int(round(hourly->wind_speed[hourCounter]))) + "/" + String(int(round(hourly->wind_gust[hourCounter]))) + "mph";
if (lastWind[i] != msgTmp || repaint) {
drawTextString(msgTmp, 53 + (107 * i), 222, FSS9, 103, TC_DATUM, TFT_YELLOW, TFT_BLACK);
lastWind[i] = msgTmp;
}
hourCounter++;
}
xSemaphoreGive(owMutex);
}
else {
drawTextString("Waiting for Weather Data...", tft.width() / 2, 90, FSS9, 320, TC_DATUM, TFT_YELLOW, TFT_BLACK);
}
}
//===================================================================
//================ Draw Forecast Weather =============================
//===================================================================
void drawForecastWeatherDisplay(bool repaint) {
String msgTmp;
int lineLength;
int dayCounter;
static String lastTemp[3];
static String lastDesc[3];
static String lastWind[3];
static String lastIcon[3] = (" ", " ", " ");
uint8_t tmpDay;
static uint8_t lastDays[3] = {0};
time_t ts = now();
if (hour() < 14 || hour() == 24) { // After 2pm show the following day's forcast
dayCounter = 1;
}
else {
dayCounter = 2;
}
if (disp.getWeatherValid() == true) {
if (xSemaphoreTake(owMutex, (TickType_t) 100) != pdTRUE ) {
Serial.println("drawWeatherDisplay: Unable to get dfwMutex to update weather display!");
return;
}
if (repaint) {
if (xSemaphoreTake(tftMutex, (TickType_t) 75) == pdTRUE ) {
tft.drawFastHLine(0, 110, tft.width(), TFT_WHITE);
tft.drawFastVLine(107, 110, tft.height(), TFT_WHITE);
tft.drawFastVLine(214, 110, tft.height(), TFT_WHITE);
xSemaphoreGive(tftMutex);
}
}
// draw weather text
msgTmp = "3-day Forecast";
drawTextString(msgTmp, tft.width() / 2, 90, FSS9, 320, TC_DATUM, TFT_YELLOW, TFT_BLACK);
for (int i = 0; i < 3; i++) {
tmpDay = weekday(ts + (dayCounter * SECONDS_IN_DAY));
if (lastDays[i] != tmpDay || repaint) {
msgTmp = getDayOfWeek(weekday(ts + (dayCounter * SECONDS_IN_DAY)));
drawTextString(msgTmp, 53 + (107 * i), 115, FSS9, 103, TC_DATUM, TFT_YELLOW, TFT_BLACK);
lastDays[i] = tmpDay;
}
String weatherIcon = "";
String dailySummary = daily->main[dayCounter];
dailySummary.toLowerCase();
weatherIcon = getMeteoconIcon(daily->id[dayCounter], false, 0);
if (weatherIcon != lastIcon[i] || repaint) { // save some work drawing the weather icon
if (xSemaphoreTake(tftMutex, (TickType_t) 75) == pdTRUE ) {
drawWeatherIcon(weatherIcon, 28 + (107 * i), 135, false);
xSemaphoreGive(tftMutex);
lastIcon[i] = weatherIcon;
}
else {
Serial.println("drawWeatherDisplay: Unable to push new weather icon.");
}
}
msgTmp = String((int)round(daily->temp_min[dayCounter])) + "-" + String((int)round(daily->temp_max[dayCounter])) + " F";
if (lastTemp[i] != msgTmp || repaint) {
drawTextString(msgTmp, 53 + (107 * i), 185, FSS9, 103, TC_DATUM, TFT_YELLOW, TFT_BLACK);
lastTemp[i] = msgTmp;
}
rollingSprite *workSprite;
if (i == 0) {
workSprite = &sprite1;
}
else if (i == 1) {
workSprite = &sprite2;
}
else if (i == 2) {
workSprite = &sprite3;
}
// needs work
msgTmp = String(daily->description[dayCounter]);
// msgTmp = "this is a test of the system";
msgTmp[0] = msgTmp[0] - 32; // upper case 1st letter
lineLength = tft.textWidth(msgTmp, GFXFF);
if (msgTmp != lastDesc[i] || repaint) {
workSprite->delSprite();
if (xSemaphoreTake(tftMutex, (TickType_t) 50) == pdTRUE ) {
tft.fillRect((107 * i) + 1, 202, 106 , 222, TFT_BLACK);
xSemaphoreGive(tftMutex);
}
if (lineLength >= 100) {
workSprite->newSprite(msgTmp, 20, 95, 2, 30, TFT_YELLOW, (107 * i) + 6, 202);
}
else {
drawTextString(msgTmp, 53 + (107 * i), 202, FSS9, 103, TC_DATUM, TFT_YELLOW, TFT_BLACK);
}
lastDesc[i] = msgTmp;
}
msgTmp = String(int(round(daily->wind_speed[dayCounter]))) + "/" + String(int(round(daily->wind_gust[dayCounter]))) + "mph";
if (lastWind[i] != msgTmp || repaint) {
drawTextString(msgTmp, 53 + (107 * i), 222, FSS9, 103, TC_DATUM, TFT_YELLOW, TFT_BLACK);
lastWind[i] = msgTmp;
}
dayCounter++;
}
xSemaphoreGive(owMutex);
}
else {
drawTextString("Waiting for Weather Data...", tft.width() / 2, 90, FSS9, 320, TC_DATUM, TFT_YELLOW, TFT_BLACK);
}
}
//===================================================================
//================== Draw Alarm Display =============================
//===================================================================
void drawAlarmDisplay(bool repaint) {
String msgTmp;
int lineLength;
String alarmIcon;
char iconChar[30];
const int alarmIndHoriz = 285;
static bool alarm1LastActive;
static bool alarm2LastActive;
static bool alarm3LastActive;
static bool alarm1LastSunrise;
static bool alarm2LastSunrise;
static bool alarm3LastSunrise;
if (repaint) {
drawTextString("Set Alarms", tft.width() / 2, 90, FSS9, 320, TC_DATUM, TFT_WHITE, TFT_BLACK);
}
if (alarm1.isActive() != alarm1LastActive || alarm1.isSunriseActive() != alarm1LastSunrise || repaint) {
drawAlarmDisplayElem(repaint, 1, 110);
alarm1LastActive = alarm1.isActive();
alarm1LastSunrise = alarm1.isSunriseActive();
drawAlarmIndicator(false);
}
if (alarm2.isActive() != alarm2LastActive || alarm2.isSunriseActive() != alarm2LastSunrise || repaint) {
drawAlarmDisplayElem(repaint, 2, 153);
alarm2LastActive = alarm2.isActive();
alarm2LastSunrise = alarm2.isSunriseActive();
drawAlarmIndicator(false);
}
if (alarm3.isActive() != alarm3LastActive || alarm3.isSunriseActive() != alarm3LastSunrise || repaint) {
drawAlarmDisplayElem(repaint, 3, 197);
alarm3LastActive = alarm3.isActive();
alarm3LastSunrise = alarm3.isSunriseActive();
drawAlarmIndicator(false);
}
}
//===================================================================
//============= Draw Alarm Display Element ==========================
//===================================================================
void drawAlarmDisplayElem(bool repaint, uint8_t alarmNumber, uint16_t yOff) {
int lineLength;
static String lastTime;
static String lastDays;
String tmpString;
String lightIcon;
String alarmIcon;