-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunctions.ino
1774 lines (1637 loc) · 57.7 KB
/
functions.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
/***************************************/
/* ------------ FUNCTIONS ------------ */
/***************************************/
//print final air quality (set color & LED)
void Print_Level(int data1)
{
if (data1 < 600)
{
tft.setTextColor(GREEN,BLACK);
tft.println("Excellent Air");
controlLED('G');
setOutput('G');
controlLogo(GREEN);
}
else if ((data1 >= 600) && (data1 < 800))
{
tft.setTextColor(GREEN,BLACK);
tft.println(" Good Air ");
controlLED('G');
setOutput('G');
controlLogo(GREEN);
}
else if ((data1 >= 800) && (data1 < 1000))
{
tft.setTextColor(BLUE,BLACK);
tft.println(" Fair Air ");
controlLED('B');
setOutput('Y');
controlLogo(BLUE);
}
else if ((data1 >= 1000) && (data1 < 1500))
{
tft.setTextColor(YELLOW,BLACK);
tft.println("Mediocre Air ");
controlLED('Y');
setOutput('Y');
controlLogo(YELLOW);
}
else if (data1 >= 1500)
{
tft.setTextColor(RED,BLACK);
tft.println(" Bad Air ");
controlLED('R');
setOutput('R');
controlLogo(RED);
}
}
//printDriverError decodes the CCS811Core::status type and prints the
//type of error to the serial terminal.
//
//Save the return value of any function of type CCS811Core::status, then pass
//to this function to see what the output was.
void printDriverError( CCS811Core::status errorCode )
{
tft.setCursor(15, 105);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("CCS811 status: ");
switch ( errorCode )
{
case CCS811Core::SENSOR_SUCCESS:
Serial.print("SUCCESS");
tft.setTextColor(GREEN);
tft.print("DETECTED");
break;
case CCS811Core::SENSOR_ID_ERROR:
Serial.print("ID_ERROR");
tft.setTextColor(RED);
tft.print("ID_ERROR");
break;
case CCS811Core::SENSOR_I2C_ERROR:
Serial.print("I2C_ERROR");
tft.setTextColor(RED);
tft.print("I2C_ERROR");
break;
case CCS811Core::SENSOR_INTERNAL_ERROR:
Serial.print("INTERNAL_ERROR");
tft.setTextColor(RED);
tft.print("INTERNAL_ERROR");
break;
case CCS811Core::SENSOR_GENERIC_ERROR:
Serial.print("GENERIC_ERROR");
tft.setTextColor(RED);
tft.print("GENERIC_ERROR");
break;
default:
Serial.print("Unspecified error.");
tft.setTextColor(RED);
tft.print("Unspecified error.");
}
}
//printSensorError gets, clears, then prints the errors
//saved within the error register.
void printSensorError()
{
Serial.println("Error with CCS811 sensor!");
uint8_t error = myCCS811.getErrorRegister();
tft.setCursor(15, 105);
tft.setTextColor(RED); tft.setTextSize(2);
tft.println("Error with CCS811 sensor!");
if ( error == 0xFF ) //comm error
{
Serial.println("Failed to get ERROR_ID register.");
tft.println("Failed to get ERROR_ID register!:");
}
else
{
Serial.print("Error: ");
if (error & 1 << 5) Serial.print("HeaterSupply"); tft.println(" Error: HeaterSupply");
if (error & 1 << 4) Serial.print("HeaterFault"); tft.println(" Error: HeaterFault");
if (error & 1 << 3) Serial.print("MaxResistance"); tft.println(" Error: MaxResistance");
if (error & 1 << 2) Serial.print("MeasModeInvalid"); tft.println(" Error: MeasModeInvalid");
if (error & 1 << 1) Serial.print("ReadRegInvalid"); tft.println(" Error: ReadRegInvalid");
if (error & 1 << 0) Serial.print("MsgInvalid"); tft.println(" Error: MsgInvalid");
Serial.println();
}
}
//function to show red, green and blue pattern on screen
//returns time it took to run the function
unsigned long testFillScreen()
{
unsigned long start = micros();
tft.fillScreen(BLACK);
tft.fillScreen(RED);
tft.fillScreen(GREEN);
tft.fillScreen(BLUE);
tft.fillScreen(BLACK);
return micros() - start;
}
//update the vars in the CCS811 sensor with temp & humi data from BME280 sensor
void updateCCS811vars(float TEMP, float HUMIDITY)
{
//Serial.println("Updating CCS811 sensor with environment values...");
myCCS811.setEnvironmentalData( HUMIDITY, TEMP);
//Serial.print("New humidity= "); Serial.print(HUMIDITY, 2); Serial.println("%");
//Serial.print("New temp= "); Serial.print(TEMP, 2); Serial.println("°C");
}
//interrupt from AS3935 sensor, set var so loop handles this first
void interruptFunction()
{
AS3935IrqTriggered = 1;
}
//change the LED color according to status
void controlLED(char COLOR)
{
if(LEDenabled)
{
switch (COLOR)
{
case 'R': //red
digitalWrite(LEDg, LOW);
digitalWrite(LEDr, HIGH);
digitalWrite(LEDb, LOW);
break;
case 'G': //green
digitalWrite(LEDg, HIGH);
digitalWrite(LEDr, LOW);
digitalWrite(LEDb, LOW);
break;
case 'Y': //yellow
digitalWrite(LEDg, HIGH);
digitalWrite(LEDr, HIGH);
digitalWrite(LEDb, LOW);
break;
case 'B': //blue
digitalWrite(LEDg, LOW);
digitalWrite(LEDr, LOW);
digitalWrite(LEDb, HIGH);
break;
case 'W': //white
digitalWrite(LEDg, HIGH);
digitalWrite(LEDr, HIGH);
digitalWrite(LEDb, HIGH);
break;
case 'O': //off
digitalWrite(LEDg, LOW);
digitalWrite(LEDr, LOW);
digitalWrite(LEDb, LOW);
break;
default:
digitalWrite(LEDg, LOW);
digitalWrite(LEDr, LOW);
digitalWrite(LEDb, LOW);
}
}
}
//set output channels to indicate air quality
void setOutput(char COLOR)
{
switch (COLOR)
{
case 'G': //green
digitalWrite(GreenOutPin, HIGH);
digitalWrite(YellowOutPin, LOW);
digitalWrite(RedOutPin, LOW);
break;
case 'Y': //yellow
digitalWrite(GreenOutPin, LOW);
digitalWrite(YellowOutPin, HIGH);
digitalWrite(RedOutPin, LOW);
break;
case 'R': //red
digitalWrite(GreenOutPin, LOW);
digitalWrite(YellowOutPin, LOW);
digitalWrite(RedOutPin, HIGH);
break;
default:
digitalWrite(GreenOutPin, LOW);
digitalWrite(YellowOutPin, LOW);
digitalWrite(RedOutPin, LOW);
}
}
//change the Velleman logo's color
void controlLogo(uint16_t logocolor)
{
showbgd(103, 20, Velleman_114x36, 114, 36, logocolor, BLACK);
}
//convert the time since boot (in milliseconds) to readable format
void getTimeSinceBoot()
{
allSeconds= millis() / 1000;
runDays= allSeconds / 86400;
secsRemaining= allSeconds % 86400;
runHours= secsRemaining / 3600;
secsRemaining= secsRemaining % 3600;
runMinutes= secsRemaining / 60;
runSeconds= secsRemaining % 60;
}
//convert a time in millis to minutes
int returnMinutes(unsigned long Millis)
{
unsigned long conSeconds = Millis/1000;
int conMinutes = conSeconds / 60;
return conMinutes;
}
//return the day as a string
String dayAsString(const Time::Day day) {
switch (day) {
case Time::kSunday: return "Sunday";
case Time::kMonday: return "Monday";
case Time::kTuesday: return "Tuesday";
case Time::kWednesday: return "Wednesday";
case Time::kThursday: return "Thursday";
case Time::kFriday: return "Friday";
case Time::kSaturday: return "Saturday";
}
return "(unknown day)";
}
// return the current time in a nice format
String returnTime(Time currentTime)
{
// Get the current time and date from the chip.
Time t_now = currentTime;
// Name the day of the week.
const String day = dayAsString(t_now.day);
// Format the time and date and insert into the temporary buffer.
char buf[50];
snprintf(buf, sizeof(buf), "%s %04d-%02d-%02d %02d:%02d:%02d",
day.c_str(),
t_now.yr, t_now.mon, t_now.date,
t_now.hr, t_now.min, t_now.sec);
// return formatted string so we can see the time.
return(buf);
}
//check if the touchscreen has been touched & update Xpos & Ypos global vars
boolean touchedMe()
{
digitalWrite(touchPin, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(touchPin, LOW);
// if sharing pins, you'll need to fix the directions of the touchscreen pins
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
// we have some minimum pressure we consider 'valid'
// pressure of 0 means no pressing!
if (p.z > MINPRESSURE && p.z < MAXPRESSURE)
{
// scale from 0->1023 to tft.width
Xpos = map(p.x, TS_MINX, TS_MAXX, tft.width(), 0);
Ypos = (tft.height()-map(p.y, TS_MINY, TS_MAXY, tft.height(), 0));
/*
Serial.print("X = "); Serial.print(Xpos);
Serial.print("\tY = "); Serial.print(Ypos);
Serial.print("\tPressure = "); Serial.println(p.z);
*/
return 1; //we have been touched (yeey!)
}
else
{
return 0; //we haven't been touched (shame...)
}
}
//function to print info to tft screen
void showScreen(int screenNr)
{
currentScreenNr = screenNr; //set currentScreenVal
//local vars for plotting the indicator onto the scales/graph
int Xindic;
float calcVal;
//local vars for plotting the scales/graphs
int startXimg = 10; int startYimg = 175;
int widthImg = 300; int heightImg = 1;
//if a screen has been changed, clear the part below the header & update menu buttons
if(currentScreenNr != previousScreenNr)
{
//tft.fillScreen(BLACK); //clear screen
//we only have to clear part of the screen (startX, startY, width, height, color)
tft.fillRect(10,60,310,180,BLACK);
//print menu buttons
printMenuBtn();
}
//according to each screen, print some info
switch (screenNr)
{
//Boot screen
case 0:
//print logo Velleman
controlLogo(GREYY);
//print logo EarthListener
showbgd(35, 65, EarthListener_200x150, 200, 150, WHITE, BLACK);
//print SW version
tft.setFont(&FreeSans9pt7b);
tft.setCursor(235, 207);
tft.setTextColor(GREYY);
tft.setTextSize(1);
tft.print(SWversion);
//set font to standard system font
tft.setFont();
//test LED
controlLED('W');
delay(1500);
//cycle logo & LED color
showbgd(35, 65, EarthListener_200x150, 200, 150, RED, BLACK);
controlLED('R');
delay(500);
showbgd(35, 65, EarthListener_200x150, 200, 150, GREEN, BLACK);
controlLED('G');
delay(500);
showbgd(35, 65, EarthListener_200x150, 200, 150, BLUE, BLACK);
controlLED('B');
delay(500);
//clear logo & version for sensor info
tft.fillRect(0,65,320,175,BLACK);
controlLED('0');
break;
//Info screen
case 1:
//print icons
showbgd(40, 72, eCO2_65x50, 65, 50, WHITE, BLACK);
showbgd(127, 72, temperature_65x50, 65, 50, WHITE, BLACK);
showbgd(214, 72, pressure_65x50, 65, 50, WHITE, BLACK);
showbgd(40, 148, tvoc_65x50, 65, 50, WHITE, BLACK);
showbgd(127, 148, humidity_65x50, 65, 50, WHITE, BLACK);
showbgd(214, 148, lightning_65x50, 65, 50, WHITE, BLACK);
//print values from sensors
printValues();
break;
//Setup screen
case 2:
//global font & color
tft.setFont(); //standard system font
tft.setTextSize(1);
tft.setTextColor(GREYY,BLACK);
//buzzer
tft.setCursor(50, 125);
if(BuzzerEnabled)
{
showbgd(40, 72, buzzer_on_65x50, 65, 50, GREYY, BLACK);
tft.print("Buzzer ON ");
}
else
{
showbgd(40, 72, buzzer_off_65x50, 65, 50, GREYY, BLACK);
tft.print("Buzzer OFF");
}
//metric/imperial
tft.setCursor(140, 125);
if(MetricON)
{
showbgd(127, 72, metrical_65x50, 65, 50, GREYY, BLACK);
tft.print("Metrical");
}
else
{
showbgd(127, 72, imperial_65x50, 65, 50, GREYY, BLACK);
tft.print("Imperial");
}
//slideshow timer
showbgd(214, 72, slideshow_65x50, 65, 50, GREYY, BLACK);
tft.setCursor(215, 125);
tft.print("Timer: "); tft.print(slideshowTimer); if(slideshowTimer < 10){tft.print(" ");}
//lightning sensitivity
tft.setCursor(40, 200);
if(globalSense == 1)
{
showbgd(40, 148, lightning_low_65x50, 65, 50, GREYY, BLACK);
tft.print("Sense Low ");
}
else if(globalSense == 2)
{
showbgd(40, 148, lightning_65x50, 65, 50, GREYY, BLACK);
tft.print("Sense Medium");
}
else if(globalSense == 3)
{
showbgd(40, 148, lightning_high_65x50, 65, 50, GREYY, BLACK);
tft.print("Sense High ");
}
else
{
showbgd(40, 148, lightning_65x50, 65, 50, RED, BLACK);
tft.print("Sense Error!");
}
//indoor/outdoor mode
tft.setCursor(135, 200);
if(AS3935_OUTDOORS)
{
showbgd(127, 148, outdoor_65x50, 65, 50, GREYY, BLACK);
tft.print("Outdoor");
}
else
{
showbgd(127, 148, indoor_65x50, 65, 50, GREYY, BLACK);
tft.print(" Indoor");
}
//chip interface AS3935
tft.setCursor(215, 200);
if(AS3935_SPI)
{
showbgd(214, 148, SPI_65x50, 65, 50, GREYY, BLACK);
tft.print("Interface");
}
else
{
showbgd(214, 148, IIC_65x50, 65, 50, GREYY, BLACK);
tft.print("Interface");
}
//control LED
controlLED('0'); //off
//control Logo
controlLogo(GREYY);
break;
//eCO2 screen
case 3:
//print value, icon & update LED
tft.setFont(); //standard system font
tft.setTextSize(3);
tft.setCursor(140, 120);
if (CO2 < 800)
{
tft.setTextColor(GREEN,BLACK);
showbgd(10, 75, eCO2_100x77, 100, 77, GREEN, BLACK);
controlLED('G');
controlLogo(GREEN);
}
else if ((CO2 >=800) && (CO2 < 1000))
{
tft.setTextColor(BLUE,BLACK);
showbgd(10, 75, eCO2_100x77, 100, 77, BLUE, BLACK);
controlLED('B');
controlLogo(BLUE);
}
else if ((CO2 >= 1000) && (CO2 < 1500))
{
tft.setTextColor(YELLOW,BLACK);
showbgd(10, 75, eCO2_100x77, 100, 77, YELLOW, BLACK);
controlLED('Y');
controlLogo(YELLOW);
}
else if (CO2 >= 1500)
{
tft.setTextColor(RED,BLACK);
showbgd(10, 75, eCO2_100x77, 100, 77, RED, BLACK);
controlLED('R');
controlLogo(RED);
}
if(CO2 < 10) { tft.print(" "); } //add leading spaces
else if(CO2 < 100) { tft.print(" "); }
else if(CO2 < 1000) { tft.print(" "); }
tft.print(CO2);
tft.println(" ppm");
/*
//print scale with fillRect(startX, startY, width, height, color)
tft.fillRect( 10, 175, 46, 18, GREEN);
tft.fillRect( 57, 175, 22, 18, BLUE);
tft.fillRect( 80, 175, 56, 18, YELLOW);
tft.fillRect( 137,175,173, 18, RED);
*/
//print scale from bitmap file - file is 1 line, so print it 18 times
for( int zz = 0; zz < 18; zz++)
{
tft.setAddrWindow(startXimg, startYimg + zz, startXimg + widthImg - 1, startYimg + zz + heightImg - 1);
tft.pushColors((const uint8_t*)eCO2_graph_300x1, widthImg * heightImg, 1, false);
}
tft.fillRect( 10,193,300, 6, BLACK); //erase the bottom under the scale (from previous indicator)
//draw indicator with drawLine(startX, startY, endX, endY, color)
//calcVal = ( ( (CO2-400) / (3000-400) ) * 300 ) + 10;
calcVal = CO2-400;
calcVal = calcVal / (3000-400);
calcVal = calcVal * 300;
calcVal = calcVal + 10;
Xindic = (int) calcVal;
if(Xindic < 10){Xindic = 10;}
else if(Xindic > 300){Xindic = 300;}
tft.drawLine(Xindic, 175, Xindic, 198, WHITE);
//print values of scale
tft.setTextSize(1);
tft.setCursor(10, 165); tft.setTextColor(GREEN,BLACK); tft.print("400");
tft.setCursor(48, 165); tft.setTextColor(BLUE,BLACK); tft.print("801");
tft.setCursor(81, 165); tft.setTextColor(YELLOW,BLACK); tft.print("1001");
tft.setCursor(138, 165); tft.setTextColor(RED,BLACK); tft.print("1501");
tft.setCursor(285, 165); tft.setTextColor(RED,BLACK); tft.print("3000");
break;
//Temperature screen
case 4:
//print value, icon & update LED
tft.setFont(); //standard system font
tft.setTextSize(3);
tft.setCursor(140, 120);
if(TEMP_BME280 < 0)
{
tft.setTextColor(GREY,BLACK);
showbgd(10, 75, temperature_100x77, 100, 77, GREY, BLACK);
controlLED('W');
controlLogo(GREY);
}
else if (TEMP_BME280 <= 18)
{
tft.setTextColor(BLUE,BLACK);
showbgd(10, 75, temperature_100x77, 100, 77, BLUE, BLACK);
controlLED('B');
controlLogo(BLUE);
}
else if ((TEMP_BME280 > 18) && (TEMP_BME280 <= 24))
{
tft.setTextColor(GREEN,BLACK);
showbgd(10, 75, temperature_100x77, 100, 77, GREEN, BLACK);
controlLED('G');
controlLogo(GREEN);
}
else if ((TEMP_BME280 > 24) && (TEMP_BME280 <= 30))
{
tft.setTextColor(YELLOW,BLACK);
showbgd(10, 75, temperature_100x77, 100, 77, YELLOW, BLACK);
controlLED('Y');
controlLogo(YELLOW);
}
else if (TEMP_BME280 > 30)
{
tft.setTextColor(RED,BLACK);
showbgd(10, 75, temperature_100x77, 100, 77, RED, BLACK);
controlLED('R');
controlLogo(RED);
}
if(MetricON)
{
if(TEMP_BME280 < 10){ tft.print(" "); } //add leading spaces
tft.print(TEMP_BME280,1);
tft.print(" ");
tft.print((char)247); //to print the ° symbol
tft.println("C");
}
else
{
float TEMP_BME280_F = ((TEMP_BME280 * 9)/5 + 32);
if(TEMP_BME280_F < 10){ tft.print(" "); } //add leading spaces
tft.print(TEMP_BME280_F,1);
tft.print(" ");
tft.print((char)247); //to print the ° symbol
tft.println("F");
}
/*
//print scale with fillRect(startX, startY, width, height, color)
tft.fillRect( 10, 175, 33, 18, GREY);
tft.fillRect( 44, 175,122, 18, BLUE);
tft.fillRect(167, 175, 39, 18, GREEN);
tft.fillRect(207, 175, 39, 18, YELLOW);
tft.fillRect(247, 175, 63, 18, RED);
*/
//print scale from bitmap file - file is 1 line, so print it 18 times
for( int zz = 0; zz < 18; zz++)
{
tft.setAddrWindow(startXimg, startYimg + zz, startXimg + widthImg - 1, startYimg + zz + heightImg - 1);
tft.pushColors((const uint8_t*)temp_graph_300x1, widthImg * heightImg, 1, false);
}
tft.fillRect( 10,193,300, 6, BLACK); //erase the bottom under the scale (from previous indicator)
//draw indicator with drawLine(startX, startY, endX, endY, color)
//calcVal = ( ( (TEMP_BME280+5) / (40+5) ) * 300 ) + 10;
calcVal = TEMP_BME280 + 5;
calcVal = calcVal / (40+5);
calcVal = calcVal * 300;
calcVal = calcVal + 10;
Xindic = (int) calcVal;
if(Xindic < 10){Xindic = 10;}
else if(Xindic > 300){Xindic = 300;}
tft.drawLine(Xindic, 175, Xindic, 198, WHITE);
//print values of scale
tft.setTextSize(1);
if(MetricON)
{
tft.setCursor(10, 165); tft.setTextColor(GREY,BLACK); tft.print("-5");
tft.setCursor(45, 165); tft.setTextColor(BLUE,BLACK); tft.print("0");
tft.setCursor(167, 165); tft.setTextColor(GREEN,BLACK); tft.print("19");
tft.setCursor(207, 165); tft.setTextColor(YELLOW,BLACK); tft.print("25");
tft.setCursor(248, 165); tft.setTextColor(RED,BLACK); tft.print("31");
tft.setCursor(295, 165); tft.setTextColor(RED,BLACK); tft.print("40");
}
else
{
tft.setCursor(10, 165); tft.setTextColor(GREY,BLACK); tft.print("23");
tft.setCursor(45, 165); tft.setTextColor(BLUE,BLACK); tft.print("32");
tft.setCursor(167, 165); tft.setTextColor(GREEN,BLACK); tft.print("66");
tft.setCursor(207, 165); tft.setTextColor(YELLOW,BLACK); tft.print("77");
tft.setCursor(248, 165); tft.setTextColor(RED,BLACK); tft.print("88");
tft.setCursor(295, 165); tft.setTextColor(RED,BLACK); tft.print("104");
}
break;
//Pressure screen
case 5:
//print value, icon & update LED
tft.setFont(); //standard system font
tft.setTextSize(3);
tft.setCursor(120, 110);
tft.setTextColor(GREY,BLACK);
if(AMBIENTPRESSURE_BME280_c < 1000)
{
tft.print(" "); //add leading spaces
}
tft.print(AMBIENTPRESSURE_BME280_c,1);
tft.println(" mBar");
showbgd(10, 85, pressure_100x77, 100, 77, WHITE, BLACK);
/*
//plot pressure val @ sea level for comparison -> needs to be set for proper altitude value
//(see note at case 9)
tft.setTextSize(1);
tft.setCursor(145, 140);
tft.setTextColor(BLUE,BLACK);
float convertedSeaLevelPressure;
convertedSeaLevelPressure = seaLevelPressure / 100;
tft.print(convertedSeaLevelPressure,1); tft.print(" mBar @ sea level");
*/
controlLED('W');
controlLogo(WHITE);
break;
//TVOC screen
case 6:
//print value, icon & update LED
tft.setFont(); //standard system font
tft.setTextSize(3);
tft.setCursor(140, 120);
if (TVOC <= 50)
{
tft.setTextColor(GREEN,BLACK);
showbgd(10, 75, tvoc_100x77, 100, 77, GREEN, BLACK);
controlLED('G');
controlLogo(GREEN);
}
else if ((TVOC > 50) && (TVOC <= 150))
{
tft.setTextColor(YELLOW,BLACK);
showbgd(10, 75, tvoc_100x77, 100, 77, YELLOW, BLACK);
controlLED('Y');
controlLogo(YELLOW);
}
else if (TVOC > 150)
{
tft.setTextColor(RED,BLACK);
showbgd(10, 75, tvoc_100x77, 100, 77, RED, BLACK);
controlLED('R');
controlLogo(RED);
}
if(TVOC < 10){ tft.print(" "); } //add leading spaces
else if(TVOC < 100){ tft.print(" "); }
else if(TVOC < 1000){ tft.print(" "); }
tft.print(TVOC);
tft.println(" ppb");
/*
//print scale with fillRect(startX, startY, width, height, color)
tft.fillRect( 10, 175, 50, 18, GREEN);
tft.fillRect( 61, 175, 99, 18, YELLOW);
tft.fillRect( 161,175,149, 18, RED);
*/
//print scale from bitmap file - file is 1 line, so print it 18 times
for( int zz = 0; zz < 18; zz++)
{
tft.setAddrWindow(startXimg, startYimg + zz, startXimg + widthImg - 1, startYimg + zz + heightImg - 1);
tft.pushColors((const uint8_t*)tvoc_graph_300x1, widthImg * heightImg, 1, false);
}
tft.fillRect( 10,193,300, 6, BLACK); //erase the bottom under the scale (from previous indicator)
//draw indicator with drawLine(startX, startY, endX, endY, color)
//calcVal = TVOC + 10;
calcVal = TVOC + 10;
Xindic = (int) calcVal;
if(Xindic < 10){Xindic = 10;}
else if(Xindic > 300){Xindic = 300;}
tft.drawLine(Xindic, 175, Xindic, 198, WHITE);
//print values of scale
tft.setTextSize(1);
tft.setCursor(10, 165); tft.setTextColor(GREEN,BLACK); tft.print("0");
tft.setCursor(60, 165); tft.setTextColor(YELLOW,BLACK); tft.print("51");
tft.setCursor(160, 165); tft.setTextColor(RED,BLACK); tft.print("151");
tft.setCursor(290, 165); tft.setTextColor(RED,BLACK); tft.print("300");
break;
//Humidity screen
case 7:
//print value, icon & update LED
tft.setFont(); //standard system font
tft.setTextSize(3);
tft.setCursor(140, 120);
if (HUMIDITY_BME280 < 30)
{
tft.setTextColor(RED,BLACK);
showbgd(10, 75, humidity_100x77, 100, 77, RED, BLACK);
controlLED('R');
controlLogo(RED);
}
else if ((HUMIDITY_BME280 >= 30) && (HUMIDITY_BME280 <= 50))
{
tft.setTextColor(GREEN,BLACK);
showbgd(10, 75, humidity_100x77, 100, 77, GREEN, BLACK);
controlLED('G');
controlLogo(GREEN);
}
else if (HUMIDITY_BME280 > 50)
{
tft.setTextColor(BLUE,BLACK);
showbgd(10, 75, humidity_100x77, 100, 77, BLUE, BLACK);
controlLED('B');
controlLogo(BLUE);
}
if(HUMIDITY_BME280 < 10) { tft.print(" "); } //add leading spaces
tft.print(HUMIDITY_BME280,1);
tft.println(" %");
/*
//print scale with fillRect(startX, startY, width, height, color)
tft.fillRect( 10, 175, 91, 18, RED);
tft.fillRect( 102,175, 59, 18, GREEN);
tft.fillRect( 162,175,148, 18, BLUE);
*/
//print scale from bitmap file - file is 1 line, so print it 18 times
for( int zz = 0; zz < 18; zz++)
{
tft.setAddrWindow(startXimg, startYimg + zz, startXimg + widthImg - 1, startYimg + zz + heightImg - 1);
tft.pushColors((const uint8_t*)humi_graph_300x1, widthImg * heightImg, 1, false);
}
tft.fillRect( 10,193,300, 6, BLACK); //erase the bottom under the scale (from previous indicator)
//draw indicator with drawLine(startX, startY, endX, endY, color)
//calcVal = ( (HUMIDITY_BME280 / 100) * 300) + 10;
calcVal = HUMIDITY_BME280 * 3;
calcVal = calcVal + 10;
Xindic = (int) calcVal;
if(Xindic < 10){Xindic = 10;}
else if(Xindic > 300){Xindic = 300;}
tft.drawLine(Xindic, 175, Xindic, 198, WHITE);
//print values of scale
tft.setTextSize(1);
tft.setCursor(10, 165); tft.setTextColor(RED,BLACK); tft.print("0");
tft.setCursor(103, 165); tft.setTextColor(GREEN,BLACK); tft.print("31");
tft.setCursor(163, 165); tft.setTextColor(BLUE,BLACK); tft.print("51");
tft.setCursor(290, 165); tft.setTextColor(BLUE,BLACK); tft.print("100");
break;
//lightning screen
case 8:
//print lightning information from previous strike
tft.setFont(); //standard system font
tft.setTextSize(2);
tft.setCursor(35, 78);
tft.setTextColor(RED,BLACK);
tft.println(lastErrorLine1);
tft.setCursor(35, 98);
tft.println(lastErrorLine2);
//print icon
showbgd(210, 125, lightning_100x77, 100, 77, YELLOW, BLACK);
//print last detection
tft.setCursor(35, 145);
tft.setTextColor(YELLOW,BLACK);
tft.println("Last detection:");
tft.setCursor(35, 165);
printLastDetectionTimeAS3935();
break;
//lightning screen after interrupt from sensor
case 81:
//print lightning information
lightningscreen();
int tempScreenNr;
tempScreenNr = previousScreenNr; //store previous screen to show later
previousScreenNr = 81;
showScreen(tempScreenNr); //after lightning pop-up, show previous screen
break;
default:
//Serial.println("Nothing to see here folks!");
break;
}
//store our current screen so we can track if we changed screens later
previousScreenNr = currentScreenNr;
}
//print the screen with lightning info after interrupt
//also stores error messages so we can show them later
void lightningscreen()
{
tft.setFont(); //standard system font
tft.setTextSize(2);
tft.setCursor(35, 78);
tft.setTextColor(RED,BLACK);
//interpret interrupt source
if (int_src == LIGHTNING_INT)
{
uint8_t lightning_dist_km;
if(AS3935_SPI)
{
lightning_dist_km = lightningSPI.distanceToStorm();
}else
{
lightning_dist_km = lightningIIC.distanceToStorm();
}
Serial.print("Lightning detected! Distance to strike: ");
lastErrorLine1 = "Lightning detected! ";
lastErrorLine2 = "Distance to strike:";
if(MetricON)
{
Serial.print(lightning_dist_km);
Serial.println("km");
lastErrorLine2 += lightning_dist_km;
lastErrorLine2 += "km ";
}
else
{
uint8_t lightning_dist_miles = lightning_dist_km / 1.609344;
Serial.print(lightning_dist_miles);
Serial.println("mi");
lastErrorLine2 += lightning_dist_miles;
lastErrorLine2 += "mi ";
}
tft.println(lastErrorLine1);
tft.setCursor(35, 98);
tft.println(lastErrorLine2);
//report lightning energy, a pure number that does not have any physical meaning
long lightEnergy;
if(AS3935_SPI)
{
lightEnergy = lightningSPI.lightningEnergy();
}else
{
lightEnergy = lightningIIC.lightningEnergy();
}
Serial.print("Lightning Energy: ");
Serial.println(lightEnergy);
}
else if (int_src == DISTURBER_INT)
{
Serial.println("Disturber detected!");
lastErrorLine1 = "Disturber detected! ";
lastErrorLine2 = " ";
tft.println(lastErrorLine1);
tft.setCursor(35, 98);
tft.println(lastErrorLine2);
}
else if (int_src == NOISE_INT)
{
Serial.println("Noise detected! Move the sensor.");
lastErrorLine1 = "Noise detected! ";
lastErrorLine2 = "Please move the sensor.";
tft.println(lastErrorLine1);
tft.setCursor(35, 98);
tft.println(lastErrorLine2);
}
else
{
Serial.print("IRQ source result "); Serial.print(int_src); Serial.println(" not expected...");
lastErrorLine1 = "Error:IRQ source result";
lastErrorLine2 = " ";
lastErrorLine2 += int_src;
lastErrorLine2 += " not expected... ";
tft.println(lastErrorLine1);
tft.setCursor(35, 98);
tft.println(lastErrorLine2);
}
//print icon
showbgd(210, 125, lightning_100x77, 100, 77, YELLOW, BLACK);
//print last detection
tft.setCursor(35, 145);
tft.setTextColor(YELLOW,BLACK);
tft.println("Last detection:");
tft.setCursor(35, 165);
printLastDetectionTimeAS3935();
AS3935IrqTriggeredTime = millis(); //set previous detection to now
//buzz the buzzer for a short period + control the LED (if they are turned on)
if(BuzzerEnabled){digitalWrite(BuzzerPin, HIGH);}
controlLED('R');
controlLogo(RED);
delay(100);
if(BuzzerEnabled){digitalWrite(BuzzerPin, LOW);}
controlLED('Y');
controlLogo(YELLOW);
delay(200);
if(BuzzerEnabled){digitalWrite(BuzzerPin, HIGH);}
controlLED('R');
controlLogo(RED);
delay(100);
if(BuzzerEnabled){digitalWrite(BuzzerPin, LOW);}
controlLED('Y');
controlLogo(YELLOW);
delay(3000); //keep this info on screen for 3 seconds
}
//setup lighting sensor
void setupAS3935()
{
// Set everything to default values
if(AS3935_SPI)
{
lightningSPI.resetSettings();
}else
{
lightningIIC.resetSettings();
}
delay(500);