From c20ef02ce839a0c2cc20f2b157fec374a40421cc Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sun, 24 Feb 2019 15:08:41 +0100 Subject: [PATCH] timekeeper fixes --- include/display.h | 2 +- include/globals.h | 1 + include/timekeeper.h | 5 +++-- src/display.cpp | 4 ++-- src/main.cpp | 5 +++-- src/paxcounter.conf | 4 ++-- src/timekeeper.cpp | 27 ++++++++++++++++----------- 7 files changed, 28 insertions(+), 20 deletions(-) diff --git a/include/display.h b/include/display.h index 8f3227d0..d4a873c2 100644 --- a/include/display.h +++ b/include/display.h @@ -5,7 +5,7 @@ #include "cyclic.h" extern uint8_t DisplayState; -extern char timeSource; +extern timesource_t timeSource; extern HAS_DISPLAY u8x8; diff --git a/include/globals.h b/include/globals.h index 349e61bd..5805af63 100644 --- a/include/globals.h +++ b/include/globals.h @@ -97,6 +97,7 @@ typedef struct { } bmeStatus_t; enum sendprio_t { prio_low, prio_normal, prio_high }; +enum timesource_t { _gps, _rtc, _lora, _unsynced }; extern std::set, Mallocator> macs; extern std::array::iterator it; diff --git a/include/timekeeper.h b/include/timekeeper.h index 176aec83..90441736 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -13,7 +13,7 @@ #include "dcf77.h" #endif -enum timesources { _gps, _rtc, _lora, _unsynced }; +extern const char timeSetSymbols[]; void IRAM_ATTR CLOCKIRQ(void); void clock_init(void); @@ -21,9 +21,10 @@ void clock_loop(void *pvParameters); void time_sync(void); void timepulse_start(void); uint8_t wait_for_pulse(void); -uint8_t syncTime(time_t const t, uint8_t const caller); +uint8_t syncTime(time_t const t, timesource_t const caller); uint8_t timepulse_init(void); uint8_t TimeIsValid(time_t const t); +time_t syncProvider_CB(void); time_t compiledUTC(void); time_t tmConvert(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh, uint8_t mm, uint8_t ss); diff --git a/src/display.cpp b/src/display.cpp index 54e6f1ad..f0c556d6 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -46,7 +46,7 @@ const char *printmonth[] = {"xxx", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; uint8_t DisplayState = 0; -char timeSource = '?'; +timesource_t timeSource = _unsynced; // helper function, prints a hex key on display void DisplayKey(const uint8_t *key, uint8_t len, bool lsb) { @@ -217,7 +217,7 @@ void refreshtheDisplay() { u8x8.printf("%-16s", display_line6); #else // we want a systime display instead LoRa status t = myTZ.toLocal(now()); - timeState = TimePulseTick ? ' ' : timeSource; + timeState = TimePulseTick ? ' ' : timeSetSymbols[timeSource]; TimePulseTick = false; u8x8.printf("%02d:%02d:%02d%c %2d.%3s", hour(t), minute(t), second(t), timeState, day(t), printmonth[month(t)]); diff --git a/src/main.cpp b/src/main.cpp index daadf5b4..27c6fa3c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -361,10 +361,11 @@ void setup() { ESP_LOGI(TAG, "Starting Timekeeper..."); assert(timepulse_init()); // setup timepulse timepulse_start(); - time_sync(); // sync time #ifdef TIME_SYNC_INTERVAL - setSyncInterval(TIME_SYNC_INTERVAL * 60); // controls timeStatus() + setSyncInterval(TIME_SYNC_INTERVAL * 60); // controls timeStatus() via Time.h + setSyncProvider(syncProvider_CB); // is called by Time.h #endif + time_sync(); // sync time // start wifi in monitor mode and start channel rotation timer ESP_LOGI(TAG, "Starting Wifi..."); diff --git a/src/paxcounter.conf b/src/paxcounter.conf index ac0a22b7..766688eb 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -66,8 +66,8 @@ #define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds] // settings for syncing time of node with external time source -#define TIME_SYNC_INTERVAL 2 // sync time attempt each .. minutes from time source (GPS/LORA), comment out means [default = 5] -#define TIME_SYNC_LORA 1 // use LORA network as time source, comment out means off [default = off] +#define TIME_SYNC_INTERVAL 60 // sync time attempt each .. minutes from time source (GPS/LORA) [default = 60], comment out means off +//#define TIME_SYNC_LORA 1 // use LORA network as time source, comment out means off [default = off] // time zone, see https://github.com/JChristensen/Timezone/blob/master/examples/WorldClock/WorldClock.ino #define DAYLIGHT_TIME {"CEST", Last, Sun, Mar, 2, 120} // Central European Summer Time diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 5bebbc57..a34ff6a3 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -3,6 +3,9 @@ // Local logging tag static const char TAG[] = "main"; +// symbol to display current time source +const char timeSetSymbols[] = {'G', 'R', 'L', '?'}; + void time_sync() { // synchonization of systime with external time source (GPS/LORA) // frequently called from cyclic.cpp @@ -31,20 +34,16 @@ void time_sync() { #endif // TIME_SYNC_INTERVAL } // time_sync() -// helper function to sync time on start of next second -uint8_t syncTime(time_t const t, uint8_t const caller) { - - // symbol to display current time source - const char timeSetSymbols[] = {'G', 'R', 'L', '?'}; - +// sync time on start of next second +uint8_t syncTime(time_t const t, timesource_t const caller) { if (TimeIsValid(t)) { uint8_t const TimeIsPulseSynced = wait_for_pulse(); // wait for next 1pps timepulse setTime(t); // sync time and reset timeStatus() to timeSet adjustTime(1); // forward time to next second - timeSource = timeSetSymbols[caller]; - ESP_LOGD(TAG, "Time source %c set time to %02d:%02d:%02d", timeSource, - hour(t), minute(t), second(t)); + timeSource = caller; + ESP_LOGD(TAG, "Time source %c set time to %02d:%02d:%02d", + timeSetSymbols[timeSource], hour(t), minute(t), second(t)); #ifdef HAS_RTC if ((TimeIsPulseSynced) && (caller != _rtc)) set_rtctime(now()); @@ -53,11 +52,17 @@ uint8_t syncTime(time_t const t, uint8_t const caller) { } else { ESP_LOGD(TAG, "Time source %c sync attempt failed", timeSetSymbols[caller]); - timeSource = timeSetSymbols[_unsynced]; + timeSource = _unsynced; return 0; // failure } } // syncTime() +// callback function called by Time.h in interval set in main.cpp +time_t syncProvider_CB(void) { + timeSource = _unsynced; + return 0; +} + // helper function to sync moment on timepulse uint8_t wait_for_pulse(void) { // sync on top of next second with 1pps timepulse @@ -204,7 +209,7 @@ void clock_loop(void *pvParameters) { // ClockTask xTaskNotifyWait(0x00, ULONG_MAX, &wakeTime, portMAX_DELAY); // wait for timepulse - if (timeStatus() != timeSet) // no confident time -> no output to clock + if (timeStatus() == timeNotSet) // no confident time -> no output to clock continue; t = now(); // payload to send to clock