-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SYSTEM.ino
812 lines (707 loc) · 24.8 KB
/
SYSTEM.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
#include <pgmspace.h>
void everyXsec() {
if (powerAlarm || tempAlarm || wetAlarm) { // for Logging Timing: normal operation - every X sec. In Alarm state - continuous
Logging();
} else if (everyXsecFlag) {
Logging();
}
if (everyXsecFlag) everyXsecFlag = false; // Code that runs every X ms - 1 SEC
if (millisElapsed - previousMs > everyXms) {
previousMs = millis();
everyXsecFlag = true;
Time(timeinfo); // update Time
WiFiRSSI = WiFi.RSSI();
CCS811err = myCCS811.checkForStatusError();
if (flag) putPersistentBool("loggingActive", loggingActive); flag = false;
if (deepSleepActive && !screenState) {
gotoDeepSleep();
}
if (Amps < -0.02) { // is device Charging
chargingActive = true;
} else {
chargingActive = false;
}
if (SOC < 15 || lowPowerMode) {
bklTimeout = 6; // TFT Backlight Off timer in Sec
everyXms = 2000;
} else {
bklTimeout = 30; // TFT Backlight Off timer in Sec
everyXms = 1000;
}
DTdevicecount = sensors.getDeviceCount();
t1 = sensors.getTempC(tempProbe1);
t2 = sensors.getTempC(tempProbe2);
t3 = sensors.getTempC(tempProbe3);
t4 = sensors.getTempC(tempProbe4);
t5 = sensors.getTempC(tempProbe5);
t6 = sensors.getTempC(tempProbe6);
t7 = sensors.getTempC(tempProbe7);
t8 = sensors.getTempC(tempProbe8);
ta1 = sensors.hasAlarm(tempProbe1);
ta2 = sensors.hasAlarm(tempProbe2);
ta3 = sensors.hasAlarm(tempProbe3);
ta4 = sensors.hasAlarm(tempProbe4);
ta5 = sensors.hasAlarm(tempProbe5);
ta6 = sensors.hasAlarm(tempProbe6);
ta7 = sensors.hasAlarm(tempProbe7);
ta8 = sensors.hasAlarm(tempProbe8);
sensors.requestTemperatures();
if (fanActive) {
DTdata[0] = t1;
DTdata[1] = t2;
DTdata[2] = t3;
DTdata[3] = t4;
DTdata[4] = t5;
DTdata[5] = t6;
DTdata[6] = t7;
DTdata[7] = t8;
max_probe_temp = DTdata[7];
for (idx = 1; idx < 7; idx++) {
if (DTdata[idx] > max_probe_temp); max_probe_temp = DTdata[idx]; // find biggest value
}
switch ( idx ) {
case 1: max_probe_idx = "Ambient LED"; break;
case 2: max_probe_idx = "Battery 1"; break;
case 3: max_probe_idx = "LED"; break;
case 4: max_probe_idx = "ESP32"; break;
case 5: max_probe_idx = "Power Mgmt 1"; break;
case 6: max_probe_idx = "Power Mgmt 2"; break;
case 7: max_probe_idx = "Battery 2"; break;
case 8: max_probe_idx = "Power Bus"; break;
}
high_temp_message = max_probe_idx + " is at " + max_probe_temp + " C";
}
if (loggingActive) {
tempProbes = "";
tempProbes += String(t1) + ", ";
tempProbes += String(t2) + ", ";
tempProbes += String(t3) + ", ";
tempProbes += String(t4) + ", ";
tempProbes += String(t5) + ", ";
tempProbes += String(t6) + ", ";
tempProbes += String(t7) + ", ";
tempProbes += String(t8) + ", ";
}
if (tempAlarm || powerAlarm) {
smlICON("C"); // "C"
} else if (chargingActive) {
smlCHRG();
} else {
switch ( pageCount ) {
case 0: smlCarousel(); break;
case 1: smlCarousel(); break; // smlICON("L"); break; // "L" settings icon
case 2: smlICON("B"); break; // SD icon
case 3: break; // is being handled by the page
case 4: smlPRNT2(String(co2SCD), String(co2SMPL), 0, 0); break;
case 5: smlCarousel(); break;
case 6: smlPRNT(String(co2SCD), "CO2", 0, 0); break; // "B"
case 7: smlPRNT(String(tvocCCS), "VOC", 0, 0); break;
case 8: break; // is being handled by the page
case 9: smlPRNT(String(beatAvg), "bpm", 0, 0); break;
}
}
everyX = millis() - previousMs;
}
}
void smlCarousel() {
switch ( count ) {
case 0 ... 2: smlPRNT(String(RTCprint), "", -65, -15); break; // Line 1, Line 2, Offset X1, Offset X2
case 3: smlPRNT(String(SOC) + "%", "BATTERY", -8, -20); break;
case 4: smlPRNT(String(tempSCD, 0), "TEMP", 3, -1); break;
case 5: smlPRNT(String(UVI), "UV INDEX", 16, -32); break;
case 6: smlPRNT(String(AirQI), "AirQI", 15, -7); break; // smlPRNT(String(steps), "STEPS", 16, -8); break;
case 7: smlPRNT2(String(co2SCD), String(co2CCS), -9, 2); count = 0; break;
default: count = 0; break;
}
count++;
}
/*
smlCarousel();
smlPRNT2(String(Volts), String(Amps), 0);
smlPRNT(String(beatAvg), "bpm", 0);
smlPRNT2(String(co2SCD, 0), String(co2SMPL), 0);
smlPRNT(String(co2SCD, 0), "CO2", 0);
smlPRNT(String(tvocCCS), "VOC", 0);
*/
void SYSTEM() {
millisElapsed = millis();
getTrackBall();
readSystemPower();
Warnings(); // updating Warnings & triggering Alarms
// readIMU(); // MPU9250 IMU Readings and Step Counter
if (alarmEnable && powerAlarm || tempAlarm) { // Alarm Page & low power down
cycleCount = 0;
tft.fillScreen(TFT_BLACK);
tft.setTextPadding(152);
MAX30105.shutDown();
TFTon();
tft.fillRoundRect(40, 30, 160, 180, 12, TFT_DARKGREY);
tft.drawRoundRect(43, 33, 154, 174, 12, TFT_WHITE);
while (alarmEnable && powerAlarm || tempAlarm) {
millisElapsed = millis();
getTrackBall();
everyXsec();
Warnings();
readSystemPower();
readEnvironmentData();
printStatusBar();
// panicLog();
TFTon();
cycleCount++;
if (cycleCount > 100) gotoDeepSleep(); // simulate powerOFF while critical alarm
if (cycleCount % 3 == 0) {
tft.fillCircle(TFT_WIDTH - 20, TFT_HEIGHT - 105, 10, TFT_BLACK);
tft.fillCircle(TFT_WIDTH - 20, TFT_HEIGHT - 75, 10, TFT_RED);
tft.fillCircle(TFT_WIDTH - 20, TFT_HEIGHT - 45, 10, TFT_BLACK);
ledB = 200;
ledcWrite(1, ledB);
tone(beepPin, 2600, 300);
} else {
tft.fillCircle(TFT_WIDTH - 20, TFT_HEIGHT - 105, 10, TFT_RED);
tft.fillCircle(TFT_WIDTH - 20, TFT_HEIGHT - 75, 10, TFT_BLACK);
tft.fillCircle(TFT_WIDTH - 20, TFT_HEIGHT - 45, 10, TFT_RED);
tone(beepPin, 2900, 300);
if (ledB > 10) ledB -= 10;
}
tft.setTextDatum(MC_DATUM);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.drawNumber(100 - cycleCount, TFT_WIDTH - 20, TFT_HEIGHT - 10, 4);
tft.setTextColor(TFT_WHITE, TFT_DARKGREY);
if (tempAlarm) { // High Temp Alarm Display
tft.setTextColor(TFT_RED, TFT_DARKGREY);
if (!silentMode) tone(beepPin, 2640, 70);
tft.drawString("TEMP ALARM", TFT_WIDTH / 2, 45, 2);
} else {
tft.drawString("TEMP OK", TFT_WIDTH / 2, 45, 2);
}
tft.setTextColor(TFT_WHITE, TFT_DARKGREY);
if (powerAlarm) {
tft.setTextColor(TFT_RED, TFT_DARKGREY);
if (!silentMode) tone(beepPin, 2340, 70);
tft.drawString("VOLTAGE ALARM", TFT_WIDTH / 2, 65, 2);
} else {
tft.drawString("VOLTAGE OK", TFT_WIDTH / 2, 65, 2);
}
tft.drawString(String(Volts), TFT_WIDTH / 2, 100, 7);
tft.setTextColor(TFT_WHITE, TFT_DARKGREY);
if (Amps > ampsFanTH) {
tft.setTextColor(TFT_RED, TFT_DARKGREY);
if (!silentMode) tone(beepPin, 2000, 70);
tft.drawString("CURRENT ALARM", TFT_WIDTH / 2, 190, 2);
} else {
tft.drawString("CURRENT OK", TFT_WIDTH / 2, 190, 2);
}
tft.drawString(String(Amps), TFT_WIDTH / 2, 155, 7);
tft.setCursor(0, 63);
tft.setTextSize(2);
printTemperature(tempProbe1); tft.println(" ");
printTemperature(tempProbe2); tft.println(" ");
printTemperature(tempProbe3); tft.println(" ");
printTemperature(tempProbe4); tft.println(" ");
printTemperature(tempProbe5); tft.println(" ");
printTemperature(tempProbe6); tft.println(" ");
printTemperature(tempProbe7); tft.println(" ");
printTemperature(tempProbe8); tft.println(" ");
tft.setTextSize(1);
tft.setTextColor(TFT_LIGHTGREY, TFT_BLACK);
tft.setCursor(197, 25);
tft.drawRoundRect(193, 23, 55, 12, 3, TFT_MIDDLEGREY); // (xO, yO, Length, Width, Radius, color)
FPS = millis() - millisElapsed;
tft.print(1000 / FPS); tft.print("fps ");
}
tft.fillScreen(TFT_BLACK);
cycleCount = 0;
switch ( pageCount ) {
case 0: page0();
case 1: page1();
case 2: page2();
case 3: page3();
case 4: page4();
case 5: page5();
case 6: page6();
case 7: page7();
case 8: page8();
case 9: page9();
}
}
}
void readSystemPower() {
// if (activeSOC) { // fuel gauge, no I2C data
// soc.quickStart();
// soc.setThreshold(15);
// socAlert = soc.getAlert();
// socVolts = soc.getVoltage();
// SOC = soc.getSOC();
// }
if (activeINA) {
if (ina260.conversionReady()) {
Amps = ina260.readCurrent(); // in mAh
Volts = ina260.readBusVoltage(); // in mV
Watts = ina260.readPower(); // in mW
if (powerAlarm) {
ina260.setMode(INA260_MODE_CONTINUOUS);
} else {
ina260.setMode(INA260_MODE_TRIGGERED);
}
if (batIDX >= 100) { // Battery Data Averaging
ampsAvg = batT / batIDX;
voltsAvg = volT / batIDX;
batT = 0;
volT = 0;
batIDX = 0;
} else {
batT += Amps;
volT += Volts;
batIDX++;
}
// temporary SOC calculation and projection
SOC = constrain(map(voltsAvg, 6400, 8380, 0, 100), 0, 100);
capacityLeft = constrain(map(voltsAvg, 6400, 8380, 0, BatteryCapacity), 0, BatteryCapacity);
batDayLeft = (capacityLeft / ampsAvg) / 24.00;
b1 = batDayLeft;
batHourLeft = (batDayLeft - b1) * 24.0;
// b2 = batHourLeft; // doesn't work
// batMinuteLeft = (batHourLeft - int(b2)) * 60;
BatteryTime = String(int(batDayLeft)) + "d" + String(int(batHourLeft)) + "h"; // + String(batMinuteLeft) + "m";
Volts = Volts / 1000;
Amps = Amps / 1000;
if (loggingActive) {
PowerLog = "";
PowerLog += String(SOC) + ", ";
PowerLog += String(Volts) + ", ";
PowerLog += String(Amps) + ", ";
PowerLog += String(Watts / 1000) + ", ";
}
}
}
}
void Warnings() {
if (t1 > tA1 || t2 > tA2 || t3 > tA3 || t4 > tA4 || t5 > tA5 || t6 > tA6 || t7 > tA7 || t8 > tA8 ||
Amps > ampsFanTH || // Fan Activation when Temp Warning or CurrentTH
chargingActive ||
tempBME > 45 ||
tempSCD > 45) {
fanActive = true;
digitalWrite(fan, HIGH);
} else {
fanActive = false;
digitalWrite(fan, LOW);
}
if (humidSCD > 95 || dewPoint > 50) {
wetAlarm = true;
} else {
wetAlarm = false;
}
if (ta1 || ta2 || ta3 || ta4 || ta5 || ta6 || ta7 || ta8) { // critical temp alarms
tempAlarm = true;
} else {
tempAlarm = false;
}
if (Volts < lowVoltAlarm || Volts > highVoltAlarm || Amps > 8 && Amps < 15) { // Critical Temp & overCurrent Alarm Enable
powerAlarm = true;
} else {
powerAlarm = false;
}
if (AirQI > 3) { // Critical Air Quality
airWarning = true;
} else {
airWarning = false;
}
if (ledBon) { // if you want to trigger ledB fade on, set ledB = 250
if (ledB < ledBmax) {
ledB += 15;
if (ledB > ledBmax - 20) ledB = ledBmax;
ledcWrite(1, ledB);
}
}
if (!ledBon) { // if you want to trigger ledB fade off, set ledB = 1
if (ledB > 0) {
ledB -= 15;
if (ledB < 15) ledB = 0;
ledcWrite(1, ledB);
}
}
}
void notification(String line1, String line2, bool notiFlag, byte alarmType, long int tS) { // notification function
if (notiFlag) {
tft.setTextDatum(TL_DATUM);
tft.setTextPadding(180);
tft.setTextSize(1);
TFTon();
if (notiY <= 19 && millisElapsed - tS < 3000) {
notiY = notiY + 4;
tft.fillRoundRect(10, notiY - 4, 220, 10, 8, TFT_BLACK);
tft.fillRoundRect(10, notiY, 220, 60, 8, TFT_DARKGREY);
tft.setTextColor(TFT_WHITE);
tft.drawString(line1, 20, notiY + 22, 2);
tft.drawString(line2, 20, notiY + 38, 2);
switch ( alarmType ) {
case 0: drawBmp(SPIFFS, "/inactive12.bmp", 18, notiY + 7); break;
case 1: drawBmp(SPIFFS, "/warnRoundW12.bmp", 19, notiY + 6); break;
case 2: drawBmp(SPIFFS, "/warnRoundY12.bmp", 19, notiY + 6); break;
case 3: drawBmp(SPIFFS, "/warnRoundR12.bmp", 19, notiY + 6); break;
}
} else if (notiY == 20 && millisElapsed - tS < 3000) {
tft.setTextColor(TFT_WHITE, TFT_DARKGREY);
tft.drawString(line1, 20, notiY + 22, 2);
tft.drawString(line2, 20, notiY + 38, 2);
switch ( alarmType ) {
case 0: drawBmp(SPIFFS, "/inactive12.bmp", 18, notiY + 7); break;
case 1: drawBmp(SPIFFS, "/warnRoundW12.bmp", 19, notiY + 6); break;
case 2: drawBmp(SPIFFS, "/warnRoundY12.bmp", 19, notiY + 6); break;
case 3: drawBmp(SPIFFS, "/warnRoundR12.bmp", 19, notiY + 6); break;
}
} else if (millisElapsed - tS > 3000) {
if (notiY >= -60) {
notiY = notiY - 4;
tft.fillRoundRect(10, notiY + 50, 220, 20, 8, TFT_BLACK);
tft.fillRoundRect(10, notiY, 220, 60, 8, TFT_DARKGREY);
tft.setTextColor(TFT_WHITE);
tft.drawString(line1, 20, notiY + 22, 2);
tft.drawString(line2, 20, notiY + 38, 2);
switch ( alarmType ) {
case 0: drawBmp(SPIFFS, "/inactive12.bmp", 18, notiY + 7); break;
case 1: drawBmp(SPIFFS, "/warnRoundW12.bmp", 19, notiY + 6); break;
case 2: drawBmp(SPIFFS, "/warnRoundY12.bmp", 19, notiY + 6); break;
case 3: drawBmp(SPIFFS, "/warnRoundR12.bmp", 19, notiY + 6); break;
}
if (notiY <= -60) {
if (nFanFlag) nFanFlag = false;
if (nAirFlag) nAirFlag = false;
if (nTempFlag) nTempFlag = false;
if (nLedFlag) nLedFlag = false;
if (nWetFlag) nWetFlag = false;
notiOn = false;
switch ( pageCount ) {
case 0: page0();
case 1: page1();
case 2: page2();
case 3: page3();
case 4: page4();
case 5: page5();
case 6: page6();
case 7: page7();
case 8: page8();
case 9: page9();
}
}
}
}
}
}
void notificationManager() { // Notification Organizer
if (fanActive && millisElapsed - notiTimestampF > 60010) { // Rising Edge
notiTimestampF = millisElapsed;
tone(beepPin, 1400, 3);
nFanFlag = true;
}
if (airWarning && millisElapsed - notiTimestampA > 20010) {
notiTimestampA = millisElapsed;
tone(beepPin, 1400, 3);
nAirFlag = true;
}
// if (tempAlarm && millisElapsed - notiTimestampT > 20010) {
// notiTimestampT = millisElapsed;
// tone(beepPin, 1400, 3);
// nTempFlag = true;
// }
if (ledBon && millisElapsed - notiTimestampL > 60010) {
notiTimestampL = millisElapsed;
tone(beepPin, 1400, 3);
nLedFlag = true;
}
if (wetAlarm && millisElapsed - notiTimestampW > 10010) {
notiTimestampW = millisElapsed;
tone(beepPin, 1400, 3);
nWetFlag = true;
}
if (nFanFlag || nAirFlag || nTempFlag || nLedFlag || nWetFlag) notiOn = true;
notification("High Temperature Detected", high_temp_message, nFanFlag, 1, notiTimestampF);
notification("Air Quality Warning", "Index: " + String(AirQI), nAirFlag, 2, notiTimestampA);
notification("LED is at " + String(map(ledB, 0, 255, 0, 100)) + "% ", "LED Temp: " + String(t7) + " C", nLedFlag, 0, notiTimestampL);
notification("Water Ingress Detected", "SHUT OFF POWER NOW", nWetFlag, 3, notiTimestampW);
// notification("Temperature", "", nTempFlag, 2, notiTimestampT);
// notification("Battery Low", "Battery is at" + String(SOC) + "%", nBatFlag, 2, notiTimestampB);
// notification("Battery Almost Empty", "Battery is at" + String(SOC) + "%", nBatFlag2, 2, notiTimestampB2);
}
void getTrackBall() {
LeftIn = extIO.getPin(0, A);
RightIn = extIO.getPin(1, A);
UpIn = extIO.getPin(2, A);
DownIn = extIO.getPin(3, A);
if (LeftIn != LeftTemp)
{
LeftTemp = LeftIn;
LeftT++;
if (LeftT > trckBallSens) {
Left = true;
LeftT = 0;
#ifdef SERIAL_DEBUG
Serial.println("Left");
#endif
}
}
if (RightIn != RightTemp)
{
RightTemp = RightIn;
RightT++;
if (RightT > trckBallSens) {
Right = true;
RightT = 0;
#ifdef SERIAL_DEBUG
Serial.println("Right");
#endif
}
}
if (UpIn != UpTemp)
{
UpTemp = UpIn;
UpT++;
if (UpT > trckBallSens) {
Up = true;
UpT = 0;
#ifdef SERIAL_DEBUG
Serial.println("Up");
#endif
}
}
if (DownIn != DownTemp)
{
DownTemp = DownIn;
DownT++;
if (DownT > trckBallSens) {
Down = true;
DownT = 0;
#ifdef SERIAL_DEBUG
Serial.println("Down");
#endif
}
}
}
void gotoDeepSleep() { // send Device to deep Sleep
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
putPersistentBool("loggingActive", loggingActive); // remember Logging Status
powerOffPeripherals();
// rtc_gpio_init(GPIO_NUM_17); //initialize the RTC GPIO port
// rtc_gpio_init(GPIO_NUM_27); //initialize the RTC GPIO port
// rtc_gpio_init(GPIO_NUM_26); //initialize the RTC GPIO port
// rtc_gpio_hold_dis(GPIO_NUM_17); //disable hold before setting the level
// rtc_gpio_hold_dis(GPIO_NUM_27); //disable hold before setting the level
// rtc_gpio_hold_dis(GPIO_NUM_26); // disable hold before setting the level
// rtc_gpio_set_level(GPIO_NUM_17, LOW); //set high/low
// rtc_gpio_set_level(GPIO_NUM_26, HIGH); //set high/low
// rtc_gpio_set_level(GPIO_NUM_27, HIGH); //set high/low
// rtc_gpio_set_direction(GPIO_NUM_17, RTC_GPIO_MODE_INPUT_ONLY);
// rtc_gpio_set_direction(GPIO_NUM_27, RTC_GPIO_MODE_INPUT_ONLY);
// rtc_gpio_set_direction(GPIO_NUM_26, RTC_GPIO_MODE_INPUT_ONLY);
rtc_gpio_hold_en(GPIO_NUM_17); // led
rtc_gpio_hold_en(GPIO_NUM_27); // tftBLK
rtc_gpio_hold_en(GPIO_NUM_26); // fan
// gpio_deep_sleep_hold_en();
esp_sleep_enable_ext0_wakeup(GPIO_NUM_2, 1); // 1 = High, 0 = Low, 2,4,35
// if (powerAlarm) sleepWakeupTime += (3600 * uS_TO_S_FACTOR);
esp_sleep_enable_timer_wakeup(sleepWakeupTime * uS_TO_S_FACTOR);
esp_deep_sleep_start();
}
void powerOffPeripherals() { // turning all peripherals off or into low power mode
ledcWrite(1, 0);
TFToff();
u8g2.clearBuffer();
u8g2.sleepOn();
Wire.beginTransmission(0x69); // AMG8833 Modes 0x00 - Normal, 0x10 - Sleep, 0x20 - 60sec, 0x21 - 10sec
Wire.write(0x00); // Register
Wire.write(0x10); // Value to Register
Wire.endTransmission();
ina260.setAveragingCount(INA260_COUNT_128); // 128 * 8.24ms = 1.05sec ?
ina260.setVoltageConversionTime(INA260_TIME_8_244_ms); // 140_us, 204_us, 332_us, 558_us, 1_1_ms,
ina260.setCurrentConversionTime(INA260_TIME_8_244_ms);
// ina260.setMode(INA260_MODE_SHUTDOWN);
myCCS811.setDriveMode(0); // IDLE
scd30.StopMeasurement();
MAX30105.shutDown();
myBME280.setMode(0);
VML.shutdown();
btStop();
esp_bt_controller_disable();
}
void TFToff() {
screenState = 0;
checkScreenState();
}
void TFTon() {
lastWake = millisElapsed;
screenState = 1;
checkScreenState();
}
void checkScreenState() {
sleepTimer = (millisElapsed - lastWake) / 1000;
if (sleepTimer > bklTimeout) screenState = 0;
if (screenState) {
if (tftBKL < tftBKLmax) {
tftBKL += 8;
if (tftBKL > tftBKLmax - 10) tftBKL = tftBKLmax;
analogWrite(tftPIN, tftBKLmax);
}
} else if (tftBKL > 1) {
tftBKL -= 6;
if (tftBKL < 12) tftBKL = 0;
analogWrite(tftPIN, tftBKL);
}
}
void printStatusBar() {
drawBmp(SPIFFS, "/batLine.bmp", batX, 2);
if (chargingActive) {
TFT_BATCOLOR = TFT_DARKGREEN;
} else if (lowPowerMode) {
TFT_BATCOLOR = TFT_YELLOW;
} else {
TFT_BATCOLOR = TFT_WHITE;
}
if (SOC > 90) {
tft.fillRect(batX + 1, 7, 9, 2, TFT_BATCOLOR);
} else if (SOC < 90 && SOC > 80) {
tft.fillRect(batX + 1, 6, 8, 4, TFT_BATCOLOR);
} else if (SOC < 80 && SOC > 70) {
tft.fillRect(batX + 1, 6, 7, 4, TFT_BATCOLOR);
} else if (SOC < 70 && SOC > 60) {
tft.fillRect(batX + 1, 6, 6, 4, TFT_BATCOLOR);
} else if (SOC < 60 && SOC > 50) {
tft.fillRect(batX + 1, 6, 5, 4, TFT_BATCOLOR);
} else if (SOC < 50 && SOC > 40) {
tft.fillRect(batX + 1, 6, 4, 4, TFT_BATCOLOR);
} else if (SOC < 40 && SOC > 30) {
tft.fillRect(batX + 1, 6, 3, 4, TFT_BATCOLOR);
} else if (SOC < 30 && SOC > 20) {
tft.fillRect(batX + 1, 6, 2, 4, TFT_BATCOLOR);
} else if (SOC < 20 && SOC > 10) {
if (!chargingActive) TFT_BATCOLOR = TFT_ORANGE;
tft.fillRect(batX + 1, 6, 2, 4, TFT_BATCOLOR);
} else if (SOC < 10 && SOC > 0) {
if (!chargingActive) TFT_BATCOLOR = TFT_RED;
tft.fillRect(batX + 1, 6, 1, 4, TFT_BATCOLOR);
}
if (pageCount == 0 && everyXsecFlag && !notiOn) {
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextDatum(TC_DATUM);
tft.setTextPadding(60);
tft.drawString(RTCprint, 120, 1, 2);
}
if (pageCount == 3) { // print minElapsed & Lamp Icon
tft.setTextColor(TFT_CYAN, TFT_BLACK);
tft.setTextPadding(30);
tft.setTextSize(1);
tft.setTextDatum(BL_DATUM);
tft.drawString(String(minElapsed) + "min", 0, TFT_HEIGHT, 2);
}
if (WiFistatus == 3) {
if (WiFiRSSI > -60) {
tft.pushImage(wifiX, 2, 12, 12, Wifi12);
} else if (WiFiRSSI < -60 && WiFiRSSI > -80) {
tft.pushImage(wifiX, 2, 12, 12, WifiLow12);
} else {
tft.pushImage(wifiX, 2, 12, 12, WifiLower12);
}
} else if (WiFistatus == 6 || WiFistatus == 255) {
tft.pushImage(wifiX, 2, 12, 12, WifiOff12);
} else if (WiFistatus == 0) {
if (WiFiRSSI > -60) {
tft.pushImage(wifiX, 2, 12, 12, Wifi12);
} else if (WiFiRSSI < -60 && WiFiRSSI > -80) {
tft.pushImage(wifiX, 2, 12, 12, WifiLow12);
} else {
tft.pushImage(wifiX, 2, 12, 12, WifiLower12);
}
} else {
tft.pushImage(wifiX, 2, 12, 12, WifiIdle12);
}
if (silentMode) {
drawBmp(SPIFFS, "/soundOff12.bmp", 2, 2);
// tft.pushImage(60, 2, 12, 12, sleep12);
// if (SDpresent) drawBmp("/UI/sleep12.bmp", 60, 2); // 24bit
} else {
drawBmp(SPIFFS, "/soundOn12.bmp", 2, 2);
// tft.fillRect(60, 2, 12, 12, TFT_BLACK);
}
if (SDpresent) {
drawBmp(SPIFFS, "/sd12.bmp", 20, 2);
// tft.pushImage(3, 2, 12, 12, sd13);
} else {
tft.fillRect(20, 2, 12, 12, TFT_BLACK);
}
if (loggingActive) {
drawBmp(SPIFFS, "/logging12.bmp", 38, 2);
// tft.pushImage(23, 2, 12, 12, logging12);
// if (SDpresent) drawBmp("/UI/logging12.bmp", 24, 2); // 24bit
} else {
tft.fillRect(38, 2, 12, 12, TFT_BLACK);
}
if (pageCount != 5) {
if (fanActive) {
drawBmp(SPIFFS, "/fan12.bmp", 56, 2);
// tft.pushImage(44, 2, 12, 12, fan12);
// if (SDpresent) drawBmp("/UI/fan12.bmp", 44, 2); // 24bit
} else {
tft.fillRect(56, 2, 12, 12, TFT_BLACK);
}
if (deepSleepActive) {
drawBmp(SPIFFS, "/sleep12.bmp", 75, 2);
// tft.pushImage(60, 2, 12, 12, sleep12);
// if (SDpresent) drawBmp("/UI/sleep12.bmp", 60, 2); // 24bit
} else {
tft.fillRect(75, 2, 12, 12, TFT_BLACK);
}
}
if (pageCount == 5) {
if (fanActive) {
drawBmp(SPIFFS, "/fan12.bmp", 114, 135);
// tft.pushImage(44, 2, 12, 12, fan12);
// if (SDpresent) drawBmp("/UI/fan12.bmp", 44, 2); // 24bit
} else {
tft.fillRect(114, 135, 12, 12, TFT_BLACK);
}
if (deepSleepActive) {
drawBmp(SPIFFS, "/sleep12.bmp", 114, 154);
// tft.pushImage(60, 2, 12, 12, sleep12);
// if (SDpresent) drawBmp("/UI/sleep12.bmp", 60, 2); // 24bit
} else {
tft.fillRect(114, 154, 12, 12, TFT_BLACK);
}
if (airWarning) {
drawBmp(SPIFFS, "/AIR12.bmp", 197, 115);
// tft.pushImage(3, 22, 12, 12, warn12);
// if (SDpresent) drawBmp(SD, "/UI/warn12.bmp", 3, 22);
} else {
tft.fillRect(197, 115, 12, 12, TFT_BLACK);
}
if (uvWarning) {
drawBmp(SPIFFS, "/UV12.bmp", 33, 115);
// tft.pushImage(3, 22, 12, 12, warn12);
// if (SDpresent) drawBmp(SD, "/UI/warn12.bmp", 3, 22);
} else {
tft.fillRect(33, 115, 12, 12, TFT_BLACK);
}
if (ledBon) {
drawBmp(SPIFFS, "/lampon.bmp", 213, 218); // drawBmp(SPIFFS, "/lamp.bmp", 111, 147);
} else {
tft.fillRect(213, 218, 21, 21, TFT_BLACK);
}
}
}
/*
byte fanSpeed() {
const byte maxMeasurements = 8;
int myMeasurements[maxMeasurements] = {t1, t2, t3, t4, t5, t6, t7, t8};
byte maxIndex = 0;
int maxValue = myMeasurements[maxIndex];
for (byte i = 1; i < maxMeasurements; i++)
{
if (myMeasurements[i] > maxValue) {
maxValue = myMeasurements[i];
maxIndex = i;
}
}
fanPWM = constrain(map(maxValue, 40, 80, 0, 255), 0 , 255); // mapping highest sensor temp from 40-80C to 0-255 for fan speed
return fanPWM;
} */