@@ -21,12 +21,12 @@ const bool vDev = 1;
21
21
#if ENABLE_DATE_RISESET // this probably doesn't work, per the above, but ¯\_(ツ)_/¯
22
22
#include < Dusk2Dawn.h> // DM Kishi - unlicensed - install in your Arduino IDE if needed - test without
23
23
#endif
24
- #include " storage.h" ; // for persistent storage - supports both AVR EEPROM and SAMD flash
24
+ #include " storage.h" // for persistent storage - supports both AVR EEPROM and SAMD flash
25
25
#include " dispNixie.h" // if DISP_NIXIE is defined in config - for a SN74141-multiplexed nixie array
26
26
#include " dispMAX7219.h" // if DISP_MAX7219 is defined in config - for a SPI MAX7219 8x8 LED array
27
27
#include " rtcDS3231.h" // if RTC_DS3231 is defined in config – for an I2C DS3231 RTC module
28
28
#include " rtcMillis.h" // if RTC_MILLIS is defined in config – for a fake RTC based on millis
29
- #include " input.h" ; // for Sel/Alt/Up/Dn - supports buttons, rotary control, and Nano 33 IoT IMU
29
+ #include " input.h" // for Sel/Alt/Up/Dn - supports buttons, rotary control, and Nano 33 IoT IMU
30
30
#include " network.h" // if not AVR – enables WiFi/web-based config/NTP sync on Nano 33 IoT WiFiNINA
31
31
32
32
@@ -155,10 +155,10 @@ unsigned int tempValDispLast = 0;
155
155
// //////// Main code control //////////
156
156
157
157
void setup (){
158
- // Serial.begin(9600);
159
- // #ifndef __AVR__ //SAMD only
160
- // while(!Serial);
161
- // #endif
158
+ Serial.begin (9600 );
159
+ #ifndef __AVR__ // SAMD only
160
+ while (!Serial);
161
+ #endif
162
162
rtcInit ();
163
163
initStorage (); // pulls persistent storage data into volatile vars - see storage.cpp
164
164
byte changed = initEEPROM (false ); // do a soft init to make sure vals in range
@@ -168,9 +168,7 @@ void setup(){
168
168
versionShowing = 1 ;
169
169
// skip network for now, since wifi connect hangs - we'll do it after version is done
170
170
} else {
171
- #ifdef NETWORK_SUPPORTED
172
- initNetwork ();
173
- #endif
171
+ if (networkSupported ()) initNetwork ();
174
172
}
175
173
176
174
// Some settings need to be set to a fixed value per the configuration.
@@ -221,9 +219,7 @@ void loop(){
221
219
checkRTC (false ); // if clock has ticked, decrement timer if running, and updateDisplay
222
220
millisApplyDrift ();
223
221
checkInputs (); // if inputs have changed, this will do things + updateDisplay as needed
224
- #ifdef NETWORK_SUPPORTED
225
- cycleNetwork ();
226
- #endif
222
+ if (networkSupported ()) cycleNetwork ();
227
223
cycleTimer ();
228
224
cycleDisplay (displayDim,fnSetPg); // keeps the display hardware multiplexing cycle going
229
225
cycleBacklight ();
@@ -246,9 +242,7 @@ void ctrlEvt(byte ctrl, byte evt, byte evtLast, bool velocity){
246
242
if (ctrl==CTRL_SEL && (evt==0 || evt==5 )){ // SEL release or superlong hold
247
243
if (evt==5 ){ initEEPROM (true ); commitEEPROM (); } // superlong hold: reset EEPROM
248
244
versionShowing = false ; inputStop (); updateDisplay ();
249
- #ifdef NETWORK_SUPPORTED
250
- initNetwork (); // we didn't do this earlier since the wifi connect makes the clock hang
251
- #endif
245
+ if (networkSupported ()) initNetwork (); // we didn't do this earlier since the wifi connect makes the clock hang
252
246
return ;
253
247
} else {
254
248
return ; // ignore other controls
@@ -308,18 +302,18 @@ void ctrlEvt(byte ctrl, byte evt, byte evtLast, bool velocity){
308
302
return ;
309
303
}
310
304
311
- #ifdef NETWORK_SUPPORTED
312
- // Short hold, Alt; or very long hold, Sel if no Alt: start admin
313
- if ((evt==2 && ctrl==CTRL_ALT)||(evt==4 && ctrl==CTRL_SEL && CTRL_ALT<=0 )) {
314
- networkStartAdmin ();
315
- return ;
316
- }
317
- // Super long hold, Alt, or Sel if no Alt: start AP (TODO would we rather it forget wifi?)
318
- if (evt==5 && (ctrl==CTRL_ALT || (ctrl==CTRL_SEL && CTRL_ALT<=0 ))) {
319
- networkStartAP ();
320
- return ;
305
+ if (networkSupported ()){
306
+ // Short hold, Alt; or very long hold, Sel if no Alt: start admin
307
+ if ((evt==2 && ctrl==CTRL_ALT)||(evt==4 && ctrl==CTRL_SEL && CTRL_ALT<=0 )) {
308
+ networkStartAdmin ();
309
+ return ;
310
+ }
311
+ // Super long hold, Alt, or Sel if no Alt: start AP (TODO would we rather it forget wifi?)
312
+ if (evt==5 && (ctrl==CTRL_ALT || (ctrl==CTRL_SEL && CTRL_ALT<=0 ))) {
313
+ networkStartAP ();
314
+ return ;
315
+ }
321
316
}
322
- #endif
323
317
324
318
if (fn < FN_OPTS) { // normal fn running/setting (not in settings menu)
325
319
@@ -410,15 +404,12 @@ void ctrlEvt(byte ctrl, byte evt, byte evtLast, bool velocity){
410
404
else if (CTRL_ALT>0 && ctrl==CTRL_ALT) {
411
405
// if soft power switch, we'll switch on release - but only if not held past activating settings page/AP
412
406
if (ENABLE_SOFT_POWER_SWITCH && SWITCH_PIN>=0 ) {
413
- if (evt==0
414
- #ifdef NETWORK_SUPPORTED
415
- && evtLast<2 // if holds are used to activate settings page/AP, do not switch. If not, switch no matter how long held.
416
- #endif
417
- ) switchPower (2 );
407
+ // If holds are used to activate network stuff, and we've passed those thresholds, do not switch.
408
+ // Otherwise, switch no matter how long held.
409
+ if (evt==0 && !(networkSupported () && evtLast<2 )) switchPower (2 );
418
410
}
419
- #ifndef NETWORK_SUPPORTED
420
411
// If neither soft power switch nor network support, this becomes our function preset
421
- else {
412
+ else if (! networkSupported ()) {
422
413
// On long hold, if this is not currently the preset, we'll set it, double beep, and inputStop.
423
414
// (Decided not to let this button set things, because then it steps on the toes of Sel's functionality.)
424
415
if (evt==2 ) {
@@ -441,7 +432,6 @@ void ctrlEvt(byte ctrl, byte evt, byte evtLast, bool velocity){
441
432
updateDisplay ();
442
433
}
443
434
}
444
- #endif
445
435
} // end alt
446
436
} // end fn running
447
437
@@ -460,9 +450,7 @@ void ctrlEvt(byte ctrl, byte evt, byte evtLast, bool velocity){
460
450
case FN_TOD: // save in RTC
461
451
if (fnSetValDid){ // but only if the value was actually changed
462
452
rtcSetTime (fnSetVal/60 ,fnSetVal%60 ,0 );
463
- #ifdef NETWORK_SUPPORTED
464
- ntpSyncLast = 0 ;
465
- #endif
453
+ if (networkSupported ()) clearNTPSyncLast ();
466
454
millisAtLastCheck = 0 ; // see ms()
467
455
calcSun ();
468
456
isDSTByHour (rtcGetYear (),rtcGetMonth (),rtcGetDate (),fnSetVal/60 ,true );
@@ -482,9 +470,7 @@ void ctrlEvt(byte ctrl, byte evt, byte evtLast, bool velocity){
482
470
case 3 : // write year/month/date to RTC
483
471
rtcSetDate (fnSetValDate[0 ],fnSetValDate[1 ],fnSetVal,
484
472
dayOfWeek (fnSetValDate[0 ],fnSetValDate[1 ],fnSetVal));
485
- #ifdef NETWORK_SUPPORTED
486
- ntpSyncLast = 0 ;
487
- #endif
473
+ if (networkSupported ()) clearNTPSyncLast ();
488
474
calcSun ();
489
475
isDSTByHour (fnSetValDate[0 ],fnSetValDate[1 ],fnSetVal,rtcGetHour (),true );
490
476
clearSet (); break ;
@@ -752,9 +738,7 @@ bool initEEPROM(bool hard){
752
738
if (hard) {
753
739
rtcSetDate (2021 ,1 ,1 ,dayOfWeek (2021 ,1 ,1 ));
754
740
rtcSetTime (0 ,0 ,0 );
755
- #ifdef NETWORK_SUPPORTED
756
- ntpSyncLast = 0 ;
757
- #endif
741
+ if (networkSupported ()) clearNTPSyncLast ();
758
742
}
759
743
// The vars outside the settings menu
760
744
if (hard || readEEPROM (0 ,true )>1439 ) changed += writeEEPROM (0 ,420 ,true ,false ); // 0-1: alarm at 7am
@@ -767,24 +751,24 @@ bool initEEPROM(bool hard){
767
751
// 8: TODO functions/pages enabled (bitmask)
768
752
if (hard || readEEPROM (9 ,false )>1 ) changed += writeEEPROM (9 ,1 ,false ,false ); // 9: NTP sync on
769
753
if (hard) changed += writeEEPROM (15 ,0 ,false ,false ); // 15: last known DST on flag - clear on hard reset (to match the reset RTC/auto DST/anti-poisoning settings to trigger midnight tubes as a tube test)
770
- # ifdef NETWORK_SUPPORTED
771
- if (hard){ // everything in here needs no range testing
772
- // 51-54 NTP server IP address (4 bytes) - e.g. from https://tf.nist.gov/tf-cgi/servers.cgi
773
- // Serial.println(F("setting IP address in eeprom"));
774
- // Serial.print(readEEPROM(51,false),DEC); Serial.print(F(".")); Serial.print(readEEPROM(52,false),DEC); Serial.print(F(".")); Serial.print(readEEPROM(53,false),DEC); Serial.print(F(".")); Serial.println(readEEPROM(54,false),DEC);
775
- changed += writeEEPROM (51 ,129 ,false ,false );
776
- changed += writeEEPROM (52 , 6 ,false ,false );
777
- changed += writeEEPROM (53 , 15 ,false ,false );
778
- changed += writeEEPROM (54 , 27 ,false ,false );
779
- // Serial.print(readEEPROM(51,false),DEC); Serial.print(F(".")); Serial.print(readEEPROM(52,false),DEC); Serial.print(F(".")); Serial.print(readEEPROM(53,false),DEC); Serial.print(F(".")); Serial.println(readEEPROM(54,false),DEC);
780
- // 55-86 Wi-Fi SSID (32 bytes)
781
- // TODO
782
- // 87-150 Wi-Fi WPA passphrase/key or WEP key (64 bytes)
783
- // TODO
784
- }
785
- // 151 Wi-Fi WEP key index
786
- if (hard || readEEPROM (151 ,false )>3 ) changed += writeEEPROM (151 ,0 ,false ,false );
787
- # endif
754
+ if ( networkSupported ()){
755
+ if (hard){ // everything in here needs no range testing
756
+ // 51-54 NTP server IP address (4 bytes) - e.g. from https://tf.nist.gov/tf-cgi/servers.cgi
757
+ // Serial.println(F("setting IP address in eeprom"));
758
+ // Serial.print(readEEPROM(51,false),DEC); Serial.print(F(".")); Serial.print(readEEPROM(52,false),DEC); Serial.print(F(".")); Serial.print(readEEPROM(53,false),DEC); Serial.print(F(".")); Serial.println(readEEPROM(54,false),DEC);
759
+ changed += writeEEPROM (51 ,129 ,false ,false );
760
+ changed += writeEEPROM (52 , 6 ,false ,false );
761
+ changed += writeEEPROM (53 , 15 ,false ,false );
762
+ changed += writeEEPROM (54 , 27 ,false ,false );
763
+ // Serial.print(readEEPROM(51,false),DEC); Serial.print(F(".")); Serial.print(readEEPROM(52,false),DEC); Serial.print(F(".")); Serial.print(readEEPROM(53,false),DEC); Serial.print(F(".")); Serial.println(readEEPROM(54,false),DEC);
764
+ // 55-86 Wi-Fi SSID (32 bytes)
765
+ // TODO
766
+ // 87-150 Wi-Fi WPA passphrase/key or WEP key (64 bytes)
767
+ // TODO
768
+ }
769
+ // 151 Wi-Fi WEP key index
770
+ if (hard || readEEPROM (151 ,false )>3 ) changed += writeEEPROM (151 ,0 ,false ,false );
771
+ } // end network supported
788
772
// The vars inside the settings menu
789
773
bool isInt = false ;
790
774
for (byte opt=0 ; opt<sizeof (optsLoc); opt++) {
@@ -945,13 +929,11 @@ void checkRTC(bool force){
945
929
// }
946
930
}
947
931
948
- #ifdef NETWORK_SUPPORTED
949
932
// NTP cue at :59:00
950
- if (readEEPROM ( 9 , false ) && rtcGetMinute ()== 59 ){
933
+ if (rtcGetMinute ()== 59 && networkSupported () && readEEPROM ( 9 , false ) ){
951
934
if (rtcGetSecond ()==0 ) cueNTP ();
952
935
if (rtcGetSecond ()==30 && ntpSyncAgo ()>=30000 ) cueNTP (); // if at first you don't succeed...
953
936
}
954
- #endif
955
937
956
938
// Strikes - only if fn=clock, not setting, not signaling/snoozing, not night/away. Setting 21 will be off if signal type is no good
957
939
// The six pips
@@ -1418,9 +1400,7 @@ void updateDisplay(){
1418
1400
editDisplay (hr, 0 , 1 , readEEPROM (19 ,false ), true );
1419
1401
editDisplay (rtcGetMinute (), 2 , 3 , true , true );
1420
1402
// Serial.print(millis(),DEC); Serial.println(F("show display per regular (hours/mins at least)"));
1421
- #ifdef NETWORK_SUPPORTED
1422
- if (readEEPROM (9 ,false ) && ntpSyncAgo ()>=86400000 ){ blankDisplay (4 ,5 ,true ); break ; }
1423
- #endif
1403
+ if (networkSupported () && readEEPROM (9 ,false ) && ntpSyncAgo ()>=86400000 ){ blankDisplay (4 ,5 ,true ); break ; }
1424
1404
if (readEEPROM (18 ,false )==1 ) editDisplay (rtcGetDate (), 4 , 5 , readEEPROM (19 ,false ), true ); // date
1425
1405
else editDisplay (rtcGetSecond (), 4 , 5 , true , true ); // seconds
1426
1406
break ;
0 commit comments