-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bedside_Atom.ino
814 lines (699 loc) · 24.5 KB
/
Bedside_Atom.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
/*!
* @file Bedside_Atom.ino
* Bedside Atom V1.2
* A WWVB syncronized tabletop clock
*
* Written by Zach Shiner, 2024
*
* MIT License
* See README.md and LICENSE.md for more information
*/
#include "ES100.h"
#include <Wire.h>
#include <TimeLib.h> // https://github.com/PaulStoffregen/Time
#include "Adafruit_LEDBackpack.h" // https://github.com/adafruit/Adafruit_LED_Backpack
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2
// Initialize library objects
ES100 es100;
Adafruit_7segment matrix = Adafruit_7segment();
Bounce2::Button hourButton = Bounce2::Button();
Bounce2::Button minuteButton = Bounce2::Button();
Bounce2::Button DSTswitch = Bounce2::Button();
// Compile debug/options
#define DEBUG ///< Print core functionality debug info
#define DEBUG_CLOCK ///< Print display-related debug info
//#define DEBUG_CONTINUOUS ///< Don't stop decoding for debug
//#define DISABLE_DISPLAY ///< Disable I2C display
//#define SYNC_LED ///< Keep builtin LED in sync with es100_EN
// Pin Assignments
const uint8_t es100_IRQ = 7;
const uint8_t es100_EN = 5; // LED_BUILTIN; // 13 on UNO
const uint8_t clockButtonPin_Hour = 10;
const uint8_t clockButtonPin_Minute = 9;
const uint8_t clockSwitchPin_TZ0 = A2;
const uint8_t clockSwitchPin_TZ1 = A1;
const uint8_t clockSwitchPin_DST = 11;
const uint8_t clockSwitchPin_24HR = 12;
const unsigned long baudrate = 115200;
// Variables for manipulating a time syncronization
volatile unsigned long atomicMillis = 0;
volatile uint8_t interruptCount = 0;
uint8_t lastInterruptCount = 0;
time_t lastGoodSyncTime = 0;
time_t lastSyncAttempt = 0;
unsigned long syncWatchdog = 0;
// Timestamps for faux multitasking
unsigned long secondsIndicatorMillis = 0;
unsigned long syncCheckIntervalMillis = 0;
unsigned long displayTimeMillis = 0;
unsigned long debugTimeMillis = 0;
unsigned long currentExecutionTime = 0;
unsigned long lastExecutionTime = 0;
// Switch interactions
uint8_t lastTZswitch = 0;
uint8_t heldLoops = 0;
const uint16_t holdThreshold = 800; //ms
// Cycle Times (seconds)
const unsigned long watchdogTimeout = SECS_PER_HOUR * 5;
const time_t staleTimeoutShort = SECS_PER_HOUR * 24;
const time_t staleTimeoutLong = SECS_PER_WEEK;
// Flags to control reception
bool timeSyncInProgress = false; // variable to determine if we are in receiving mode
bool triggerTimeSync = false; // variable to trigger the reception
// ES100 Data Structures
ES100IRQstatus lastReadIRQStatus = {}; // read every interrupt
ES100Status0 lastReadStatus0 = {}; // read every interrupt
ES100Data validES100Data = {}; // only gets updated when rx complete & rx okay are both true
// Display status indicators
bool indicatorSecondsSeparator = true; // start illuminated
bool indicatorPM = false;
bool indicatorAL1 = false;
bool indicatorAL2 = false;
bool useTwentyFourHourTime = false; // true to use 24-hour clock
// Local time settings
int8_t timezone = 0;
int8_t UTCoffset = 0;
time_t localTime = 0;
void atomic() {
// Called procedure when we receive an interrupt from the ES100
// Store milis of when the interrupt happened. This is the second (time) boundary.
// Full time frame reception happens in 134s, Tracking reception takes 24.5s
atomicMillis = millis();
interruptCount++;
#ifdef DEBUG
Serial.println("** INTERRUPT CALLED ** ");
#endif
}
void printES100DateTime(ES100DateTime dt) {
Serial.print("Received UTC time = 20");
Serial.print(dt.year);
Serial.print(":");
Serial.print(dt.month);
Serial.print(":");
Serial.print(dt.day);
Serial.print(" ");
Serial.print(dt.hour);
Serial.print(":");
Serial.print(dt.minute);
Serial.print(":");
Serial.println(dt.second);
}
inline uint8_t decodeTZswitch() {
uint8_t timeZoneSwitch; // position 0 through 3
timeZoneSwitch = !digitalRead(clockSwitchPin_TZ0);
timeZoneSwitch += !digitalRead(clockSwitchPin_TZ1) << 1;
return timeZoneSwitch;
}
void updateTime(ES100DateTime dt) {
// Align time update with the atomic offset
// Calculated number of ms needed to get to a multiple of 1000ms away from atomicMillis
unsigned long secondOffset = millis() - atomicMillis;
secondOffset -= 300; // my particular MCU takes about 300ms to think through this
secondOffset %= 1000;
delay(1000 - secondOffset);
// Calculate the whole number of second elapsed since the interrupt was called
unsigned long secondAdjust = secondOffset / 1000;
setTime(dt.hour,dt.minute,dt.second + 1 + secondAdjust,dt.day,dt.month,dt.year);
// This is also a good place to align blinking seconds indicator to actual seconds
secondsIndicatorMillis += (atomicMillis % 1000); //Offset to next whole second
}
void calculateUTCoffset(){
#ifdef DEBUG
Serial.println("Calculating UTC offset");
#endif
timezone = -5 - decodeTZswitch();
UTCoffset = timezone;
if (DSTswitch.isPressed()) {
// If DST begins today
if (validES100Data.Status0.dstState == DSTbegin){
if (hour() < 2 - timezone){
UTCoffset = timezone;
} else {
UTCoffset = timezone + 1;
}
} else
// If DST ends today
if (validES100Data.Status0.dstState == DSTend){
if (hour() < 2 - timezone - 1){
UTCoffset = timezone + 1;
} else {
UTCoffset = timezone;
}
} else
// If DST is in effect
if (validES100Data.Status0.dstState == DSTactive){
UTCoffset = timezone + 1;
}
}
}
void setup() {
Wire.begin();
#ifndef DISABLE_DISPLAY
// Begin display on i2c address 0x70
matrix.begin(0x70);
matrix.setBrightness(15); // 0 to 15
// Turn on all addressable digits
for (uint8_t i = 0; i < 5; i++) {
matrix.writeDigitRaw(i, 0b11111111);
}
matrix.writeDisplay();
delay(3000);
#if defined(DEBUG) || defined(DEBUG_CLOCK)
Serial.begin(baudrate);
#endif
// Encircle display startup animation
const uint8_t animateDelay = 80;
for (uint8_t i = 0; i < 6; i++) {
matrix.writeDigitRaw(0, 0b00000001);
matrix.writeDisplay();
delay(animateDelay);
matrix.clear();
matrix.writeDigitRaw(1, 0b00000001);
matrix.writeDisplay();
delay(animateDelay);
matrix.clear();
matrix.writeDigitRaw(3, 0b00000001);
matrix.writeDisplay();
delay(animateDelay);
matrix.clear();
matrix.writeDigitRaw(4, 0b00000001);
matrix.writeDisplay();
delay(animateDelay/2);
matrix.clear();
matrix.writeDigitRaw(4, 0b00000010);
matrix.writeDisplay();
delay(animateDelay/2);
matrix.clear();
matrix.writeDigitRaw(4, 0b00000100);
matrix.writeDisplay();
delay(animateDelay);
matrix.clear();
matrix.writeDigitRaw(4, 0b00001000);
matrix.writeDisplay();
delay(animateDelay);
matrix.clear();
matrix.writeDigitRaw(3, 0b00001000);
matrix.writeDisplay();
delay(animateDelay);
matrix.clear();
matrix.writeDigitRaw(1, 0b00001000);
matrix.writeDisplay();
delay(animateDelay);
matrix.clear();
matrix.writeDigitRaw(0, 0b00001000);
matrix.writeDisplay();
delay(animateDelay);
matrix.clear();
matrix.writeDigitRaw(0, 0b00010000);
matrix.writeDisplay();
delay(animateDelay/2);
matrix.clear();
matrix.writeDigitRaw(0, 0b00100000);
matrix.writeDisplay();
delay(animateDelay/2);
matrix.clear();
}
delay(animateDelay * 5);
matrix.writeDisplay();
#endif
#if defined(DEBUG) || defined(DEBUG_CLOCK)
while(!Serial && millis() < 5000) {
// Wait for serial to connect
}
Serial.println("\r\n\r\n*** INIT ***");
#endif
// Initialize WWVB reciever
es100.begin(es100_IRQ, es100_EN);
attachInterrupt(digitalPinToInterrupt(es100_IRQ), atomic, FALLING);
// Setup hardware buttons
const uint16_t debounceInterval = 5;
hourButton.attach(clockButtonPin_Hour, INPUT_PULLUP);
minuteButton.attach(clockButtonPin_Minute, INPUT_PULLUP);
DSTswitch.attach(clockSwitchPin_DST, INPUT_PULLUP);
pinMode(clockSwitchPin_24HR, INPUT_PULLUP);
pinMode(clockSwitchPin_TZ0, INPUT_PULLUP);
pinMode(clockSwitchPin_TZ1, INPUT_PULLUP);
hourButton.interval(debounceInterval);
minuteButton.interval(debounceInterval);
DSTswitch.interval(debounceInterval);
hourButton.setPressedState(LOW);
minuteButton.setPressedState(LOW);
DSTswitch.setPressedState(LOW);
#ifdef DEBUG
delay(1000);
es100.enable();
uint8_t deviceID = es100.getDeviceID();
Serial.print("Device ID: 0x");
Serial.print(deviceID, HEX);
Serial.print(";\t");
if (deviceID == 0x10) {
Serial.println("Success: Device ID match");
} else {
Serial.println("Error: Device ID mismatch");
}
Serial.println("Finished setup");
lastInterruptCount = 0;
interruptCount = 0;
#endif
lastTZswitch = decodeTZswitch();
}
void loop() {
uint8_t timeoutCounter; // limit how many times we retry talking to es100
const uint8_t timeoutLimit = 5;
// If we are not currently receiving a 1-minute frame and reception is triggered, enable ES100 and start reception
if (!timeSyncInProgress && triggerTimeSync) {
es100.enable();
timeoutCounter = 0;
while (es100.startRx(ANT_1, true) != EXIT_SUCCESS && timeoutCounter < timeoutLimit) {
#ifdef DEBUG
Serial.println("StartRx did not return EXIT_SUCCESS. Retry in 5s...");
#endif
delay(5000);
timeoutCounter++;
}
Serial.println("StartRx Antenna 1 only: EXIT_SUCCESS");
lastSyncAttempt = now();
syncWatchdog = millis();
timeSyncInProgress = true;
triggerTimeSync = false;
/* Important to set the interrupt counter AFTER the startRx because the es100
* confirms that the rx has started by triggering the interrupt.
* We can't disable interrupts because the wire library will stop working
* so we initialize the counters after we start so we can ignore the first false
* trigger
*/
lastInterruptCount = 0;
interruptCount = 0;
}
// If an interrupt was detected
if (lastInterruptCount < interruptCount) {
// Read registers once
lastReadIRQStatus = es100.getIRQStatus();
lastReadStatus0 = es100.getStatus0();
#ifdef DEBUG
Serial.print("Reading IRQ and Status0 registers. Received: ");
#endif
lastSyncAttempt = now();
// If we received a valid decode
if (lastReadIRQStatus.rxComplete && !lastReadIRQStatus.cycleComplete && lastReadStatus0.rxOk) { // IRQStatus = 0x01 & Status0 bit 0
// Read remaining data from es100 and calculate time zone & DST offsets
validES100Data.DateTimeUTC = es100.getUTCdateTime();
validES100Data.Status0 = lastReadStatus0;
#ifdef DEBUG
Serial.println("Successful Reception");
Serial.print("Atomic millis = ");
Serial.println(atomicMillis);
Serial.print("Time Library: ");
Serial.print(year());
Serial.print(":");
Serial.print(month());
Serial.print(":");
Serial.print(day());
Serial.print(" ");
if(hour() <10)
{
Serial.print("0");
}
Serial.print(hour());
Serial.print(":");
if(minute() <10)
{
Serial.print("0");
}
Serial.print(minute());
Serial.print(":");
if(second() <10)
{
Serial.print("0");
}
Serial.println(second());
// Serial Print out recieved time
printES100DateTime(validES100Data.DateTimeUTC);
#endif
updateTime(validES100Data.DateTimeUTC);
lastGoodSyncTime = now();
lastSyncAttempt = lastGoodSyncTime;
calculateUTCoffset();
// Flag that we aren't recieving anymore. Only happens if we get a good decode.
timeSyncInProgress = false;
#ifndef DEBUG_CONTINUOUS
// Stop reception after a good decode
timeoutCounter = 0;
while (false && es100.stopRx() != EXIT_SUCCESS && timeoutCounter < timeoutLimit) {
#ifdef DEBUG
Serial.println("StopRx did not return EXIT_SUCCESS. Retry in 5s...");
#endif
delay(5000);
timeoutCounter++;
}
#ifdef DEBUG
Serial.println("StopRx: EXIT_SUCCESS; Disabling es100 after good Rx");
#endif
es100.disable();
#endif
#ifdef DEBUG
Serial.print("status0.rxOk = 0b");
Serial.println(lastReadStatus0.rxOk, BIN);
Serial.print("status0.antenna = 0b");
Serial.println(lastReadStatus0.antenna, BIN);
Serial.print("status0.dstState = 0b");
Serial.println(lastReadStatus0.dstState, BIN);
Serial.print("status0.tracking = 0b");
Serial.println(lastReadStatus0.tracking, BIN);
#endif
#ifndef DISABLE_DISPLAY
matrix.clear();
matrix.println("SYNC");
matrix.writeDisplay();
delay(5000);
matrix.clear();
#endif
}
else if (!lastReadIRQStatus.rxComplete && lastReadIRQStatus.cycleComplete){ // IRQStatus = 0x04
#ifdef DEBUG
Serial.println("Unsuccessful Reception");
#endif
}
else {
#ifdef DEBUG
Serial.println("Bad Data");
#endif
}
lastInterruptCount = interruptCount;
}
// Very rarely ES100 and MCU get out of sync with each other. Let's reset the process every five hours of searching.
if (timeSyncInProgress && (millis() - syncWatchdog) > (watchdogTimeout * 1000)) {
timeoutCounter = 0;
while (false && es100.stopRx() != EXIT_SUCCESS && timeoutCounter < timeoutLimit) {
#ifdef DEBUG
Serial.println("StopRx did not return EXIT_SUCCESS. Retry in 5s...");
#endif
delay(5000);
timeoutCounter++;
}
#ifdef DEBUG
Serial.println("StopRx: EXIT_SUCCESS; Disabling es100 after good Rx");
#endif
es100.disable();
delay(1500);
triggerTimeSync = true;
timeSyncInProgress = false;
syncWatchdog = millis();
}
// Coarse Update Schedule
// - Receive the current time on an schedule
// - Evaluate if DST is changing
// - Set stale indicators
if (syncCheckIntervalMillis + 10000 < millis()) {
// Are we currently recieving the time?
if (!timeSyncInProgress) {
// If we aren't already syncing (outer loop)
// and the time isn't set, request a reception
if(timeStatus() == timeNotSet){
triggerTimeSync = true;
#ifdef DEBUG
Serial.println("Time not set, requesting sync");
#endif
}
// If we aren't already syncing (outer loop)
// and the time is set (else)
// and if we havn't had a successful sync this hour today
else if(!(hour(lastGoodSyncTime) == hour() && day(lastGoodSyncTime) == day()))
{
triggerTimeSync = true;
#ifdef DEBUG
Serial.println("New hour, requesting sync");
#endif
}
else {
// do nothing
#ifdef DEBUG
Serial.println("Sync not required");
#endif
}
}
// If we aren't already syncing (outer loop)
// and the time is set (else)
// and it's daytime or we had a good sync this hour today
else {
// Already recieving the time, do nothing
#ifdef DEBUG
Serial.println("Time sync in progress...");
#endif
}
#ifndef DEBUG_CLOCK
Serial.print(".");
#endif
if(timeStatus() != timeNotSet){
calculateUTCoffset();
}
syncCheckIntervalMillis = millis();
}
// Every 500ms, change the state of various status LEDs
if (secondsIndicatorMillis + 500 < millis()) {
// Blink display separator every second
indicatorSecondsSeparator = !indicatorSecondsSeparator;
// Indicate if time is unset or exceeds short stale timeout
if((timeStatus() == timeNotSet) || (now() - lastGoodSyncTime) > staleTimeoutShort){
indicatorAL1 = true;
// Indicate if time exceeds long stale timeout
if ((timeStatus() == timeNotSet) || (now() - lastGoodSyncTime) > staleTimeoutLong) {
indicatorAL2 = true;
}
}
// Otherwise turn them off
else {
indicatorAL1 = false;
indicatorAL2 = false;
}
secondsIndicatorMillis = millis();
}
hourButton.update();
minuteButton.update();
DSTswitch.update();
// Advance hour with a single button press less than the hold threshold
if (hourButton.released() && hourButton.previousDuration() < (holdThreshold / 2)){
#ifdef DEBUG
Serial.println("HOUR Pressed\t");
#endif
adjustTime(SECS_PER_HOUR);
lastGoodSyncTime = now();
}
// Advance minute with a single button press less than the hold threshold
if (minuteButton.released() && minuteButton.previousDuration() < (holdThreshold / 2)){
#ifdef DEBUG
Serial.println("MINUTE Pressed\t");
#endif
adjustTime(SECS_PER_MIN);
lastGoodSyncTime = now();
// Undo rollover of minute
if (minute(lastGoodSyncTime) == 0) {
adjustTime(SECS_PER_DAY - SECS_PER_HOUR);
lastGoodSyncTime = now();
}
}
// Advance minute with a button held longer than than the hold threshold
if (minuteButton.isPressed() && !hourButton.isPressed() && minuteButton.currentDuration() > holdThreshold + (125 * heldLoops) && hourButton.currentDuration() > holdThreshold) {
#ifdef DEBUG
Serial.println("MINUTE Held\t");
#endif
adjustTime(SECS_PER_MIN);
heldLoops++;
lastGoodSyncTime = now();
// Undo rollover of minute
if (minute(lastGoodSyncTime) == 0) {
adjustTime(SECS_PER_DAY - SECS_PER_HOUR);
lastGoodSyncTime = now();
}
}
// Advance hour with a button held longer than than the hold threshold
if (hourButton.isPressed() && !minuteButton.isPressed() && hourButton.currentDuration() > holdThreshold + (200 * heldLoops) && minuteButton.currentDuration() > holdThreshold) {
#ifdef DEBUG
Serial.println("HOUR Held\t");
#endif
adjustTime(SECS_PER_HOUR);
heldLoops++;
lastGoodSyncTime = now();
}
// Show sync status if both adjust buttons are held
if (minuteButton.isPressed() && hourButton.isPressed() && minuteButton.currentDuration() > 1000 && hourButton.currentDuration() > 1000 && heldLoops == 0) {
#ifndef DISABLE_DISPLAY
matrix.clear();
matrix.println("LASt");
matrix.writeDisplay();
delay(1000);
matrix.println("SYNC");
matrix.writeDisplay();
delay(1000);
matrix.clear();
time_t syncAge = now() - lastGoodSyncTime;
// display "----" if we have not yet synced (or adjusted)
if (lastGoodSyncTime == 0) {
matrix.println("----");
} else {
// display in days if over 72 hours old
if (elapsedDays(syncAge) > 2) {
matrix.println(elapsedDays(syncAge));
matrix.writeDisplay();
delay(1500);
matrix.println("DAYS");
} else {
// would have been nice if this macro was in TimeLib
#define elapsedHours(_time_) ((_time_) / SECS_PER_HOUR) // this is number of hours since Jan 1 1970
// display in hours if over 1 hr hold
if (elapsedHours(syncAge) > 0) {
matrix.writeDigitNum(0, elapsedHours(syncAge) / 10);
matrix.writeDigitNum(1, elapsedHours(syncAge) % 10);
matrix.writeDigitAscii(3, 'h');
matrix.writeDigitAscii(4, 'r');
} else {
// display in seconds because M can't be drawin on 7 segment display
// the character S also looks like a 5 which is not ideal
matrix.println(syncAge);
matrix.writeDisplay();
delay(1500);
matrix.println(" SEC");
}
}
}
matrix.writeDisplay();
delay(2000);
matrix.clear();
heldLoops++;
#endif
}
if (!minuteButton.isPressed() && !hourButton.isPressed()) {
heldLoops = 0;
}
//Send updates to the display driver
if(displayTimeMillis + 50 < millis()){
localTime = now();
localTime += (time_t)UTCoffset * SECS_PER_HOUR;
#ifndef DISABLE_DISPLAY
useTwentyFourHourTime = !digitalRead(clockSwitchPin_24HR);
if(useTwentyFourHourTime){
matrix.writeDigitNum(0, hour(localTime) / 10);
matrix.writeDigitNum(1, hour(localTime) % 10);
indicatorPM = false;
}
else {
if(hourFormat12(localTime) < 10){ // in 12-hour time first digit is 1 or blank
matrix.writeDigitRaw(0, 0b00000000);
} else {
matrix.writeDigitNum(0, 1);
}
matrix.writeDigitNum(1, hourFormat12(localTime) % 10);
indicatorPM = isPM(localTime);
}
matrix.writeDigitNum(3, minute(localTime) / 10);
matrix.writeDigitNum(4, minute(localTime) % 10);
uint8_t rawToWrite;
rawToWrite = 0b00000001 * indicatorAL1;
rawToWrite += 0b00000010 * indicatorAL2;
rawToWrite += 0b00001100 * indicatorSecondsSeparator;
rawToWrite += 0b00010000 * indicatorPM;
matrix.writeDigitRaw(2, rawToWrite);
matrix.writeDisplay();
#endif
#ifdef SYNC_LED
// Turn onboard led on and off with the enable status
digitalWrite(LED_BUILTIN, digitalRead(es100_EN));
#endif
displayTimeMillis = millis();
}
#ifdef DEBUG
// Keep track of execution time
currentExecutionTime = micros() - lastExecutionTime;
lastExecutionTime = micros();
#endif
#ifdef DEBUG_CLOCK
if(debugTimeMillis + 300 < millis()){
if(indicatorPM){
Serial.print("PM ");
} else{
Serial.print("AM ");
}
if(indicatorAL1){Serial.print("*");
}else{Serial.print(" ");}
if(indicatorAL2){Serial.print("*");
}else{Serial.print(" ");}
Serial.print("\t");
Serial.print(hour());
Serial.print(":");
Serial.print(minute());
Serial.print(":");
Serial.print(second());
Serial.print("\t");
Serial.print("Last attempt: ");
Serial.print(now() - lastSyncAttempt);
Serial.print("s\t");
Serial.print("Last sync: ");
if(lastGoodSyncTime == 0){
Serial.print("never\t");
} else {
Serial.print(now() - lastGoodSyncTime);
Serial.print("s\t");
}
if(timeSyncInProgress){
Serial.print("Watchdog: ");
Serial.print(watchdogTimeout - ((millis() - syncWatchdog) / 1000));
Serial.print("\t");
}
Serial.print("UTC Offset: ");
Serial.print(UTCoffset);
Serial.print("\tExec Time: ");
Serial.print(currentExecutionTime / 1000);
Serial.print(".");
Serial.print(currentExecutionTime % 1000);
if (currentExecutionTime % 1000 < 100) {
Serial.print(0);
}
Serial.print("ms\tTZ: ");
Serial.println(decodeTZswitch());
debugTimeMillis = millis();
}
#endif
// Force a recalculate of UTC offset if time zone switches change
uint8_t currentTZswitch = decodeTZswitch();
if (lastTZswitch != currentTZswitch) {
// delay 100ms, then re-read; Shorting-style switch can cause misreads
delay(100);
#ifdef DEBUG
currentTZswitch = decodeTZswitch();
Serial.print("TZ Switch Changed! Now: ");
Serial.println(currentTZswitch);
#endif
#ifndef DISABLE_DISPLAY
if(timeStatus() == timeNotSet) {
matrix.clear();
matrix.println(" -");
int8_t tzSwitchState = 5 + currentTZswitch;
matrix.writeDigitNum(3, tzSwitchState);
matrix.writeDisplay();
delay(1000);
matrix.clear();
}
#endif
if(timeStatus() != timeNotSet) {
calculateUTCoffset();
}
}
lastTZswitch = currentTZswitch;
// Force a recalculate of UTC offset if DST switch changes
if (DSTswitch.changed()) {
#ifdef DEBUG
Serial.println("DST Switch Changed!");
#endif
if(timeStatus() != timeNotSet) {
calculateUTCoffset();
}
#ifndef DISABLE_DISPLAY
matrix.clear();
matrix.println("dSt ");
matrix.writeDigitNum(4, DSTswitch.isPressed());
matrix.writeDisplay();
delay(1000);
matrix.clear();
#endif
}
}