forked from lmirel/MorphingClockRemix
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MorphingClockRemix.ino
1018 lines (941 loc) · 32.3 KB
/
MorphingClockRemix.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
/*
remix from HarryFun's great Morphing Digital Clock idea https://github.com/hwiguna/HariFun_166_Morphing_Clock
follow the great tutorial there and eventually use this code as alternative
provided 'AS IS', use at your own risk
*/
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <ESP8266WiFi.h>
#define double_buffer
#include <PxMatrix.h>
//#include <Adafruit_GFX.h> // Core graphics library
//#include <Fonts/FreeMono9pt7b.h>
//=== WIFI MANAGER ===
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
char wifiManagerAPName[] = "MorphClk";
char wifiManagerAPPassword[] = "MorphClk";
//== DOUBLE-RESET DETECTOR ==
#include <DoubleResetDetector.h>
#define DRD_TIMEOUT 10 // Second-reset must happen within 10 seconds of first reset to be considered a double-reset
#define DRD_ADDRESS 0 // RTC Memory Address for the DoubleResetDetector to use
DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS);
//== SAVING CONFIG ==
#include "FS.h"
#include <ArduinoJson.h>
bool shouldSaveConfig = false; // flag for saving data
//callback notifying us of the need to save config
void saveConfigCallback ()
{
Serial.println("Should save config");
shouldSaveConfig = true;
}
#ifdef ESP8266
#include <Ticker.h>
Ticker display_ticker;
#define P_LAT 16
#define P_A 5
#define P_B 4
#define P_C 15
#define P_D 12
#define P_E 0
#define P_OE 2
#endif
// Pins for LED MATRIX
PxMATRIX display(64, 32, P_LAT, P_OE, P_A, P_B, P_C, P_D, P_E);
//=== SEGMENTS ===
int cin = 25; //color intensity
#include "Digit.h"
Digit digit0(&display, 0, 63 - 1 - 9*1, 8, display.color565(0, 0, 255));
Digit digit1(&display, 0, 63 - 1 - 9*2, 8, display.color565(0, 0, 255));
Digit digit2(&display, 0, 63 - 4 - 9*3, 8, display.color565(0, 0, 255));
Digit digit3(&display, 0, 63 - 4 - 9*4, 8, display.color565(0, 0, 255));
Digit digit4(&display, 0, 63 - 7 - 9*5, 8, display.color565(0, 0, 255));
Digit digit5(&display, 0, 63 - 7 - 9*6, 8, display.color565(0, 0, 255));
#ifdef ESP8266
// ISR for display refresh
void display_updater ()
{
//display.displayTestPattern(70);
display.display (70);
}
#endif
void getWeather ();
void configModeCallback (WiFiManager *myWiFiManager)
{
Serial.println ("Entered config mode");
Serial.println (WiFi.softAPIP());
// You could indicate on your screen or by an LED you are in config mode here
// We don't want the next time the boar resets to be considered a double reset
// so we remove the flag
drd.stop ();
}
char timezone[5] = "0";
char military[3] = "Y"; // 24 hour mode? Y/N
char u_metric[3] = "Y"; // use metric for units? Y/N
char date_fmt[7] = "D.M.Y"; // date format: D.M.Y or M.D.Y or M.D or D.M or D/M/Y.. looking for trouble
bool loadConfig ()
{
File configFile = SPIFFS.open ("/config.json", "r");
if (!configFile)
{
Serial.println("Failed to open config file");
return false;
}
size_t size = configFile.size ();
if (size > 1024)
{
Serial.println("Config file size is too large");
return false;
}
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes (buf.get(), size);
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
if (!json.success ())
{
Serial.println("Failed to parse config file");
return false;
}
strcpy (timezone, json["timezone"]);
strcpy (military, json["military"]);
//avoid reboot loop on systems where this is not set
if (json.get<const char*>("metric"))
strcpy (u_metric, json["metric"]);
else
{
Serial.println ("metric units not set, using default: Y");
}
if (json.get<const char*>("date-format"))
strcpy (date_fmt, json["date-format"]);
else
{
Serial.println ("date format not set, using default: D.M.Y");
}
return true;
}
bool saveConfig ()
{
StaticJsonBuffer<200> jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["timezone"] = timezone;
json["military"] = military;
json["metric"] = u_metric;
json["date-format"] = date_fmt;
File configFile = SPIFFS.open ("/config.json", "w");
if (!configFile)
{
Serial.println ("Failed to open config file for writing");
return false;
}
Serial.println ("Saving configuration to file:");
Serial.print ("timezone=");
Serial.println (timezone);
Serial.print ("military=");
Serial.println (military);
Serial.print ("metric=");
Serial.println (u_metric);
Serial.print ("date-format=");
Serial.println (date_fmt);
json.printTo (configFile);
return true;
}
#include "TinyFont.h"
const byte row0 = 2+0*10;
const byte row1 = 2+1*10;
const byte row2 = 2+2*10;
void wifi_setup ()
{
//-- Config --
if (!SPIFFS.begin ())
{
Serial.println ("Failed to mount FS");
return;
}
loadConfig ();
//-- Display --
display.fillScreen (display.color565 (0, 0, 0));
display.setTextColor (display.color565 (0, 0, 255));
//-- WiFiManager --
//Local intialization. Once its business is done, there is no need to keep it around
WiFiManager wifiManager;
wifiManager.setSaveConfigCallback (saveConfigCallback);
WiFiManagerParameter timeZoneParameter ("timeZone", "Time Zone", timezone, 5);
wifiManager.addParameter (&timeZoneParameter);
WiFiManagerParameter militaryParameter ("military", "24Hr (Y/N)", military, 3);
wifiManager.addParameter (&militaryParameter);
WiFiManagerParameter metricParameter ("metric", "Metric Units (Y/N)", u_metric, 3);
wifiManager.addParameter (&metricParameter);
WiFiManagerParameter dmydateParameter ("date_fmt", "Date Format (D.M.Y)", date_fmt, 6);
wifiManager.addParameter (&dmydateParameter);
//-- Double-Reset --
if (drd.detectDoubleReset ())
{
Serial.println ("Double Reset Detected");
display.setCursor (0, row0);
display.print ("AP:");
display.print (wifiManagerAPName);
display.setCursor (0, row1);
display.print ("Pw:");
display.print (wifiManagerAPPassword);
display.setCursor (0, row2);
display.print ("192.168.4.1");
wifiManager.startConfigPortal (wifiManagerAPName, wifiManagerAPPassword);
display.fillScreen (display.color565(0, 0, 0));
}
else
{
Serial.println ("No Double Reset Detected");
//display.setCursor (2, row1);
//display.print ("connecting");
TFDrawText (&display, String(" CONNECTING "), 0, 13, display.color565(0, 0, 255));
//fetches ssid and pass from eeprom and tries to connect
//if it does not connect it starts an access point with the specified name wifiManagerAPName
//and goes into a blocking loop awaiting configuration
wifiManager.autoConnect (wifiManagerAPName);
}
//-- Status --
//display.fillScreen (0);
//display.setCursor (2, row1);
TFDrawText (&display, String(" ONLINE "), 0, 13, display.color565(0, 0, 255));
Serial.print ("WiFi connected, IP address: ");
Serial.println (WiFi.localIP ());
//
Serial.print ("timezone=");
Serial.println (timezone);
Serial.print ("military=");
Serial.println (military);
Serial.print ("metric=");
Serial.println (u_metric);
Serial.print ("date-format=");
Serial.println (date_fmt);
//timezone
strcpy (timezone, timeZoneParameter.getValue ());
//military time
strcpy (military, militaryParameter.getValue ());
//metric units
strcpy (u_metric, metricParameter.getValue ());
//date format
strcpy (date_fmt, dmydateParameter.getValue ());
//start NTP
NTP.begin ("pool.ntp.org", String(timezone).toInt(), false);
NTP.setInterval (10);//force rapid sync in 10sec
if (shouldSaveConfig)
{
saveConfig ();
}
drd.stop ();
//delay (1500);
getWeather ();
}
byte hh;
byte mm;
byte ss;
byte ntpsync = 1;
//
void setup()
{
Serial.begin (115200);
//display setup
display.begin (16);
#ifdef ESP8266
display_ticker.attach (0.002, display_updater);
#endif
//
wifi_setup ();
//
NTP.onNTPSyncEvent ([](NTPSyncEvent_t ntpEvent)
{
if (ntpEvent)
{
Serial.print ("Time Sync error: ");
if (ntpEvent == noResponse)
Serial.println ("NTP server not reachable");
else if (ntpEvent == invalidAddress)
Serial.println ("Invalid NTP server address");
}
else
{
Serial.print ("Got NTP time: ");
Serial.println (NTP.getTimeDateString (NTP.getLastNTPSync ()));
ntpsync = 1;
}
});
//prep screen for clock display
display.fillScreen (0);
int cc_blu = display.color565 (0, 0, cin);
digit1.DrawColon (cc_blu);
digit3.DrawColon (cc_blu);
//
Serial.print ("display color range [");
Serial.print (display.color565 (0, 0, 0));
Serial.print (" .. ");
Serial.print (display.color565 (255, 255, 255));
Serial.println ("]");
//
}
//open weather map api key
String apiKey = ""; //e.g a hex string like "abcdef0123456789abcdef0123456789"
//the city you want the weather for
String location = "Paris,FR"; //e.g. "Paris,FR"
char server[] = "api.openweathermap.org";
WiFiClient client;
int tempM = -10000;
int presM = -10000;
int humiM = -10000;
int condM = 0; //0 - unk, 1 - sunny, 2 - cloudy, 3 - overcast, 4 - rainy, 5 - thunders, 6 - snow
void getWeather ()
{
if (!apiKey.length ())
{
Serial.println ("!missing API KEY for weather data, skipping");
return;
}
Serial.print ("connecting to weather server.. ");
// if you get a connection, report back via serial:
if (client.connect (server, 80))
{
Serial.println ("connected.");
// Make a HTTP request:
client.print ("GET /data/2.5/weather?");
client.print ("q="+location);
client.print ("&appid="+apiKey);
client.print ("&cnt=3");
(*u_metric=='Y')?client.println ("&units=metric"):client.println ("&units=imperial");
client.println ("Host: api.openweathermap.org");
client.println ("Connection: close");
client.println ();
}
else
{
Serial.println ("!unable to connect");
return;
}
delay (1000);
String line = "";
String sval = "";
int bT, bT2;
while (client.connected ())
{
line = client.readStringUntil ('\n');
Serial.print ("weather:");
Serial.println (line);
//weather conditions - "main":"Clear",
bT = line.indexOf ("\"main\":\"");
if (bT > 0)
{
bT2 = line.indexOf ("\",\"", bT + 8);
sval = line.substring (bT + 8, bT2);
Serial.print ("cond ");
Serial.println (sval);
//0 - unk, 1 - sunny, 2 - cloudy, 3 - overcast, 4 - rainy, 5 - thunders, 6 - snow
if (sval.equals("Clear"))
condM = 1;
else if (sval.equals("Clouds"))
condM = 2;
else if (sval.equals("Overcast"))
condM = 3;
else if (sval.equals("Rain"))
condM = 4;
else if (sval.equals("Thunderstorm"))
condM = 5;
else if (sval.equals("Snow"))
condM = 6;
//tempM = sval.toInt();
}
//tempM
bT = line.indexOf ("\"temp\":");
if (bT > 0)
{
bT2 = line.indexOf (",\"", bT + 7);
sval = line.substring (bT + 7, bT2);
Serial.print ("temp ");
Serial.println (sval);
tempM = sval.toInt ();
}
else
Serial.println ("temp NOT found!");
//pressM
bT = line.indexOf ("\"pressure\":");
if (bT > 0)
{
bT2 = line.indexOf (",\"", bT + 11);
sval = line.substring (bT + 11, bT2);
Serial.print ("press ");
Serial.println (sval);
presM = sval.toInt();
}
else
Serial.println ("pressure NOT found!");
//humiM
bT = line.indexOf ("\"humidity\":");
if (bT > 0)
{
bT2 = line.indexOf (",\"", bT + 11);
sval = line.substring (bT + 11, bT2);
Serial.print ("humi ");
Serial.println (sval);
humiM = sval.toInt();
}
else
Serial.println ("humidity NOT found!");
}//connected
}
#include "TinyIcons.h"
//icons 10x5: 10 cols, 5 rows
int moony_ico [50] = {
//3 nuances: 0x18c3 < 0x3186 < 0x4a49
0x0000, 0x4a49, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x18c3,
0x0000, 0x0000, 0x0000, 0x4a49, 0x3186, 0x3186, 0x18c3, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4a49, 0x4a49, 0x3186, 0x3186, 0x3186, 0x18c3, 0x0000, 0x0000,
0x0000, 0x0000, 0x4a49, 0x3186, 0x3186, 0x3186, 0x3186, 0x18c3, 0x0000, 0x0000,
};
int moony1_ico [50] = {
//3 nuances: 0x18c3 < 0x3186 < 0x4a49
0x0000, 0x18c3, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x4a49,
0x0000, 0x0000, 0x0000, 0x4a49, 0x3186, 0x3186, 0x18c3, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4a49, 0x4a49, 0x3186, 0x3186, 0x3186, 0x18c3, 0x0000, 0x0000,
0x0000, 0x0000, 0x4a49, 0x3186, 0x3186, 0x3186, 0x3186, 0x18c3, 0x0000, 0x0000,
};
int moony2_ico [50] = {
//3 nuances: 0x18c3 < 0x3186 < 0x4a49
0x0000, 0x3186, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x3186,
0x0000, 0x0000, 0x0000, 0x4a49, 0x3186, 0x3186, 0x18c3, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x4a49, 0x4a49, 0x3186, 0x3186, 0x3186, 0x18c3, 0x0000, 0x0000,
0x0000, 0x0000, 0x4a49, 0x3186, 0x3186, 0x3186, 0x3186, 0x18c3, 0x0000, 0x0000,
};
int sunny_ico [50] = {
0x0000, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0x0000,
0x0000, 0xffe0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffe0, 0x0000,
0x0000, 0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0x0000, 0x0000,
0xffe0, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0xffe0,
0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0x0000,
};
int sunny1_ico [50] = {
0x0000, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000,
0x0000, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffe0, 0x0000,
0x0000, 0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0x0000, 0x0000,
0xffe0, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0xffff,
0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0x0000,
};
int sunny2_ico [50] = {
0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0x0000,
0x0000, 0xffe0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xffff, 0x0000,
0x0000, 0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0x0000, 0x0000,
0xffff, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0xffe0,
0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0x0000, 0x0000,
};
int cloudy_ico [50] = {
0x0000, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0x0000,
0x0000, 0xffe0, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0xc618, 0xffe0, 0x0000,
0x0000, 0x0000, 0x0000, 0xffe0, 0xffe0, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000,
0xffe0, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618,
0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xc618, 0xc618, 0xc618, 0xc618,
};
int cloudy1_ico [50] = {
0x0000, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0x0000,
0x0000, 0xffe0, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0xc618, 0xffff, 0x0000,
0x0000, 0x0000, 0x0000, 0xffe0, 0xffe0, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000,
0xffff, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618,
0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xc618, 0xc618, 0xc618, 0xc618,
};
int cloudy2_ico [50] = {
0x0000, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0xffff, 0x0000, 0x0000, 0x0000,
0x0000, 0xffff, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0xc618, 0xffe0, 0x0000,
0x0000, 0x0000, 0x0000, 0xffe0, 0xffe0, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000,
0xffe0, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618,
0x0000, 0x0000, 0xffe0, 0xffe0, 0xffe0, 0xffe0, 0xc618, 0xc618, 0xc618, 0xc618,
};
int ovrcst_ico [50] = {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0xc618, 0x0000, 0x0000,
0x0000, 0x0000, 0xc618, 0xc618, 0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000,
0x0000, 0xc618, 0xffff, 0xffff, 0xc618, 0xffff, 0xffff, 0xffff, 0xc618, 0x0000,
0x0000, 0xc618, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xc618, 0x0000,
0x0000, 0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000, 0x0000,
};
int ovrcst1_ico [50] = {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0xc618, 0x0000, 0x0000,
0x0000, 0x0000, 0xc618, 0xc618, 0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000,
0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0xffff, 0xffff, 0xffff, 0xc618, 0x0000,
0x0000, 0xc618, 0xc618, 0xc618, 0xffff, 0xffff, 0xffff, 0xffff, 0xc618, 0x0000,
0x0000, 0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000, 0x0000,
};
int ovrcst2_ico [50] = {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0xc618, 0x0000, 0x0000,
0x0000, 0x0000, 0xc618, 0xc618, 0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000,
0x0000, 0xc618, 0xffff, 0xffff, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000,
0x0000, 0xc618, 0xffff, 0xffff, 0xffff, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000,
0x0000, 0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000, 0x0000,
};
int thndr_ico [50] = {
0x041f, 0xc618, 0x041f, 0xc618, 0xc618, 0xc618, 0x041f, 0xc618, 0xc618, 0x041f,
0xc618, 0xc618, 0xc618, 0xc618, 0x041f, 0xc618, 0xc618, 0x041f, 0xc618, 0xc618,
0xc618, 0x041f, 0xc618, 0xc618, 0xc618, 0x041f, 0xc618, 0xc618, 0xc618, 0xc618,
0xc618, 0xc618, 0xc618, 0x041f, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0x041f,
0xc618, 0x041f, 0xc618, 0xc618, 0xc618, 0xc618, 0x041f, 0xc618, 0x041f, 0xc618,
};
int rain_ico [50] = {
0x041f, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x041f,
0x0000, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000,
0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x041f,
0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x041f, 0x0000,
};
int rain1_ico [50] = {
0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x041f, 0x0000,
0x041f, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x041f,
0x0000, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000,
0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x041f,
};
int rain2_ico [50] = {
0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x041f,
0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x041f, 0x0000,
0x041f, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x041f,
0x0000, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000,
0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000,
};
int rain3_ico [50] = {
0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x041f,
0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x041f, 0x0000,
0x041f, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x041f,
0x0000, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000,
};
int rain4_ico [50] = {
0x0000, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000,
0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x041f,
0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x041f, 0x0000,
0x041f, 0x0000, 0x041f, 0x0000, 0x0000, 0x0000, 0x041f, 0x0000, 0x0000, 0x041f,
};
int snow_ico [50] = {
0xc618, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0xc618,
0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000,
0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618,
0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0xc618, 0x0000,
};
int snow1_ico [50] = {
0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0xc618, 0x0000,
0xc618, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0xc618,
0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000,
0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618,
};
int snow2_ico [50] = {
0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618,
0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0xc618, 0x0000,
0xc618, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0xc618,
0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000,
0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000,
};
int snow3_ico [50] = {
0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618,
0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0xc618, 0x0000,
0xc618, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0xc618,
0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000,
};
int snow4_ico [50] = {
0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000,
0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618,
0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0xc618, 0x0000,
0xc618, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0xc618,
};
int *suny_ani[] = {sunny_ico, sunny1_ico, sunny2_ico, sunny1_ico, sunny2_ico};
int *clod_ani[] = {cloudy_ico, cloudy1_ico, cloudy2_ico, cloudy1_ico, cloudy2_ico};
int *ovct_ani[] = {ovrcst_ico, ovrcst1_ico, ovrcst2_ico, ovrcst1_ico, ovrcst2_ico};
int *rain_ani[] = {rain_ico, rain1_ico, rain2_ico, rain3_ico, rain4_ico};
int *thun_ani[] = {thndr_ico, rain1_ico, rain2_ico, rain3_ico, rain4_ico};
int *snow_ani[] = {snow_ico, snow1_ico, snow2_ico, snow3_ico, snow4_ico};
int *mony_ani[] = {moony_ico, moony1_ico, moony2_ico, moony1_ico, moony2_ico};
/*
*
int ovrcst_ico [50] = {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0xc618, 0xc618, 0xffff, 0xc618, 0x0000, 0x0000, 0x0000,
0x0000, 0xc618, 0xffff, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0xc618, 0x0000,
0xc618, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xc618,
0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000,
};
int ovrcst_ico [50] = {
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0xc618, 0xc618, 0x0000, 0x0000,
0x0000, 0x0000, 0xc618, 0xc618, 0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000,
0x0000, 0xc618, 0xffff, 0xffff, 0xc618, 0xffff, 0xffff, 0xffff, 0xc618, 0x0000,
0x0000, 0xc618, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xc618, 0x0000,
0x0000, 0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000, 0x0000,
};
int cloudy_ico [50] = {
0x0000, 0x0000, 0x0000, 0xffe0, 0x0000, 0x0000, 0xffe0, 0xc618, 0x0000, 0x0000,
0x0000, 0xffe0, 0xc618, 0xc618, 0x0000, 0xc618, 0xc618, 0xc618, 0xffe0, 0x0000,
0x0000, 0xc618, 0xc618, 0xc618, 0xffe0, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000,
0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xffe0,
0x0000, 0x0000, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0xc618, 0x0000, 0x0000,
};
*/
int xo = 1, yo = 26;
char use_ani = 0;
char daytime = 1;
void draw_weather_conditions ()
{
//0 - unk, 1 - sunny, 2 - cloudy, 3 - overcast, 4 - rainy, 5 - thunders, 6 - snow
Serial.print ("weather conditions ");
Serial.println (condM);
xo = 4*TF_COLS;
switch (condM)
{
case 0://unk
break;
case 1://sunny
DrawIcon (&display, sunny_ico, xo, yo, 10, 5);
//DrawIcon (&display, cloudy_ico, xo, yo, 10, 5);
//DrawIcon (&display, ovrcst_ico, xo, yo, 10, 5);
//DrawIcon (&display, rain_ico, xo, yo, 10, 5);
use_ani = 1;
break;
case 2://cloudy
DrawIcon (&display, cloudy_ico, xo, yo, 10, 5);
use_ani = 1;
break;
case 3://overcast
DrawIcon (&display, ovrcst_ico, xo, yo, 10, 5);
use_ani = 1;
break;
case 4://rainy
DrawIcon (&display, rain_ico, xo, yo, 10, 5);
use_ani = 1;
break;
case 5://thunders
DrawIcon (&display, thndr_ico, xo, yo, 10, 5);
use_ani = 1;
break;
case 6://snow
DrawIcon (&display, snow_ico, xo, yo, 10, 5);
use_ani = 1;
break;
}
}
void draw_weather ()
{
int cc_wht = display.color565 (cin, cin, cin);
int cc_red = display.color565 (cin, 0, 0);
int cc_grn = display.color565 (0, cin, 0);
int cc_blu = display.color565 (0, 0, cin);
int cc_ylw = display.color565 (cin, cin, 0);
int cc_gry = display.color565 (128, 128, 128);
int cc_dgr = display.color565 (30, 30, 30);
Serial.println ("showing the weather");
xo = 1; yo = 1;
if (tempM == -10000 || humiM == -10000 || presM == -10000)
{
//TFDrawText (&display, String("NO WEATHER DATA"), xo, yo, cc_dgr);
Serial.println ("!no weather data available");
}
else
{
//weather below the clock
//-temperature
int lcc = cc_red;
if (*u_metric == 'Y')
{
//C
if (tempM < 26)
lcc = cc_grn;
if (tempM < 18)
lcc = cc_blu;
if (tempM < 6)
lcc = cc_wht;
}
else
{
//F
if (tempM < 79)
lcc = cc_grn;
if (tempM < 64)
lcc = cc_blu;
if (tempM < 43)
lcc = cc_wht;
}
//
String lstr = String (tempM) + String((*u_metric=='Y')?"C":"F");
Serial.print ("temperature: ");
Serial.println (lstr);
TFDrawText (&display, lstr, xo, yo, lcc);
//-humidity
lcc = cc_red;
if (humiM < 65)
lcc = cc_grn;
if (humiM < 35)
lcc = cc_blu;
if (humiM < 15)
lcc = cc_wht;
lstr = String (humiM) + "%";
xo = 7*TF_COLS; yo = 1;
TFDrawText (&display, lstr, xo, yo, lcc);
//-pressure
lstr = String (presM);
xo = 10*TF_COLS; yo = 1;
TFDrawText (&display, lstr, xo, yo, cc_blu);
//
draw_weather_conditions ();
}
}
void draw_love ()
{
Serial.println ("showing some love");
use_ani = 0;
//love*you,boo
yo = 1;
int cc = random (255, 65535);
xo = 0; TFDrawChar (&display, 'L', xo, yo, cc); cc = random (255, 65535);
xo += 5; TFDrawChar (&display, 'O', xo, yo, cc); cc = random (255, 65535);
xo += 5; TFDrawChar (&display, 'V', xo, yo, cc); cc = random (255, 65535);
xo += 5; TFDrawChar (&display, 'E', xo, yo, cc); cc = random (255, 65535);
xo += 5; TFDrawChar (&display, 'h', xo, yo, display.color565 (255, 0, 0));
xo += 5; TFDrawChar (&display, 'Y', xo, yo, cc); cc = random (255, 65535);
xo += 5; TFDrawChar (&display, 'O', xo, yo, cc); cc = random (255, 65535);
xo += 5; TFDrawChar (&display, 'U', xo, yo, cc); cc = random (255, 65535);
xo += 5; TFDrawChar (&display, ',', xo, yo, cc); cc = random (255, 65535);
xo += 5; TFDrawChar (&display, 'B', xo, yo, cc); cc = random (255, 65535);
xo += 5; TFDrawChar (&display, 'O', xo, yo, cc); cc = random (255, 65535);
xo += 5; TFDrawChar (&display, 'O', xo, yo, cc); cc = random (255, 65535);
}
//
void draw_date ()
{
int cc_grn = display.color565 (0, cin, 0);
Serial.println ("showing the date");
//for (int i = 0 ; i < 12; i++)
//TFDrawChar (&display, '0' + i%10, xo + i * 5, yo, display.color565 (0, 255, 0));
//date below the clock
long tnow = now();
String lstr = "";
for (int i = 0; i < 5; i += 2)
{
switch (date_fmt[i])
{
case 'D':
lstr += (day(tnow) < 10 ? "0" + String(day(tnow)) : String(day(tnow)));
if (i < 4)
lstr += date_fmt[i + 1];
break;
case 'M':
lstr += (month(tnow) < 10 ? "0" + String(month(tnow)) : String(month(tnow)));
if (i < 4)
lstr += date_fmt[i + 1];
break;
case 'Y':
lstr += String(year(tnow));
if (i < 4)
lstr += date_fmt[i + 1];
break;
}
}
//
if (lstr.length())
{
//
xo = 3*TF_COLS; yo = 26;
TFDrawText (&display, lstr, xo, yo, cc_grn);
}
}
void draw_animations (int stp)
{
//weather icon animation
int xo = 4*TF_COLS;
int yo = 1;
//0 - unk, 1 - sunny, 2 - cloudy, 3 - overcast, 4 - rainy, 5 - thunders, 6 - snow
if (use_ani)
{
int *af = NULL;
//weather/night icon
if (!daytime)
af = mony_ani[stp%5];
else
{
switch (condM)
{
case 1:
af = suny_ani[stp%5];
break;
case 2:
af = clod_ani[stp%5];
break;
case 3:
af = ovct_ani[stp%5];
break;
case 4:
af = rain_ani[stp%5];
break;
case 5:
af = thun_ani[stp%5];
break;
case 6:
af = snow_ani[stp%5];
break;
}
}
//draw animation
if (af)
DrawIcon (&display, af, xo, yo, 10, 5);
}
//cin = 25 + (i%22) * 10;
//Serial.print ("draw brightness ");
//Serial.println (cin);
//ntpsync = 1;
//condM = (i%7);
//draw_weather ();
//
}
byte prevhh = 0;
byte prevmm = 0;
byte prevss = 0;
void loop()
{
static int i = 0;
static int last = 0;
static int cm;
//animations?
cm = millis ();
if ((cm - last) > 150)
{
//Serial.println(millis() - last);
last = cm;
i++;
//
draw_animations (i);
//
}
//time changes every miliseconds, we only want to draw when digits actually change.
hh = NTP.getHour ();
mm = NTP.getMinute ();
ss = NTP.getSecond ();
//
if (ntpsync)
{
ntpsync = 0;
//
prevss = ss;
prevmm = mm;
prevhh = hh;
//brightness control: dimmed during the night(25), bright during the day(150)
if (hh >= 20 && cin == 150)
{
cin = 25;
Serial.println ("night mode brightness");
daytime = 0;
}
if (hh < 8 && cin == 150)
{
cin = 25;
Serial.println ("night mode brightness");
daytime = 0;
}
//during the day, bright
if (hh >= 8 && hh < 20 && cin == 25)
{
cin = 150;
Serial.println ("day mode brightness");
daytime = 1;
}
//we had a sync so draw without morphing
//dark blue is little visible on a dimmed screen
int cc_blu = display.color565 (0, 0, cin);
if (cin == 25)
cc_blu = display.color565 (0, 0, 60);
//reset digits color
digit0.SetColor (cc_blu);
digit1.SetColor (cc_blu);
digit2.SetColor (cc_blu);
digit3.SetColor (cc_blu);
digit4.SetColor (cc_blu);
digit5.SetColor (cc_blu);
//clear screen
display.fillScreen (0);
//date and weather
draw_weather ();
draw_date ();
//
digit1.DrawColon (cc_blu);
digit3.DrawColon (cc_blu);
//military time?
if (hh > 12 && military[0] == 'N')
hh -= 12;
//
digit0.Draw (ss % 10);
digit1.Draw (ss / 10);
digit2.Draw (mm % 10);
digit3.Draw (mm / 10);
digit4.Draw (hh % 10);
digit5.Draw (hh / 10);
}
else
{
//seconds
if (ss != prevss)
{
int s0 = ss % 10;
int s1 = ss / 10;
if (s0 != digit0.Value ()) digit0.Morph (s0);
if (s1 != digit1.Value ()) digit1.Morph (s1);
//ntpClient.PrintTime();
prevss = ss;
//refresh weather every 5mins at 30sec in the minute
if (ss == 30 && ((mm % 5) == 0))
getWeather ();
}
//minutes
if (mm != prevmm)
{
int m0 = mm % 10;
int m1 = mm / 10;
if (m0 != digit2.Value ()) digit2.Morph (m0);
if (m1 != digit3.Value ()) digit3.Morph (m1);
prevmm = mm;
//
#ifdef SHOW_SOME_LOVE
if (mm == 0)
draw_love ();
else
#endif
draw_weather ();
}
//hours
if (hh != prevhh)
{
prevhh = hh;
//
draw_date ();
//brightness control: dimmed during the night(25), bright during the day(150)
if (hh == 20 || hh == 8)
{
ntpsync = 1;
//bri change is taken care of due to the sync
}
//military time?
if (hh > 12 && military[0] == 'N')