timekeeper use syncprovider by Time.h

This commit is contained in:
Klaus K Wilting 2019-02-25 00:26:46 +01:00
parent 6ab4a0bd9f
commit 5fb4c7bec2
8 changed files with 23 additions and 48 deletions

View File

@ -5,15 +5,12 @@
#include "senddata.h" #include "senddata.h"
#include "rcommand.h" #include "rcommand.h"
#include "spislave.h" #include "spislave.h"
#include "timekeeper.h"
#include <lmic.h> #include <lmic.h>
#ifdef HAS_BME #ifdef HAS_BME
#include "bme680mems.h" #include "bme680mems.h"
#endif #endif
void doHousekeeping(void); void doHousekeeping(void);
uint64_t uptime(void); uint64_t uptime(void);
void reset_counters(void); void reset_counters(void);

View File

@ -35,7 +35,4 @@ void lora_queuereset(void);
void lora_housekeeping(void); void lora_housekeeping(void);
void user_request_network_time_callback(void *pVoidUserUTCTime, void user_request_network_time_callback(void *pVoidUserUTCTime,
int flagSuccess); int flagSuccess);
time_t set_loratime(time_t t);
time_t get_loratime(void);
#endif #endif

View File

@ -18,9 +18,9 @@ extern const char timeSetSymbols[];
void IRAM_ATTR CLOCKIRQ(void); void IRAM_ATTR CLOCKIRQ(void);
void clock_init(void); void clock_init(void);
void clock_loop(void *pvParameters); void clock_loop(void *pvParameters);
void time_sync(void); time_t time_sync(void);
void timepulse_start(void); void timepulse_start(void);
uint8_t syncTime(getExternalTime getTimeFunction, timesource_t const caller); time_t syncTime(getExternalTime getTimeFunction, timesource_t const caller);
uint8_t timepulse_init(void); uint8_t timepulse_init(void);
uint8_t TimeIsValid(time_t const t); uint8_t TimeIsValid(time_t const t);
time_t syncProvider_CB(void); time_t syncProvider_CB(void);

View File

@ -19,7 +19,6 @@ void doHousekeeping() {
spi_housekeeping(); spi_housekeeping();
lora_housekeeping(); lora_housekeeping();
time_sync();
// task storage debugging // // task storage debugging //
ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d", ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d",

View File

@ -132,7 +132,7 @@ void refreshtheDisplay() {
uint8_t msgWaiting; uint8_t msgWaiting;
char timeState, buff[16]; char timeState, buff[16];
time_t t; time_t t = myTZ.toLocal(now()); // note: call now() here *before* locking mutex!
// block i2c bus access // block i2c bus access
if (I2C_MUTEX_LOCK()) { if (I2C_MUTEX_LOCK()) {
@ -216,8 +216,7 @@ void refreshtheDisplay() {
// update LoRa status display (line 6) // update LoRa status display (line 6)
u8x8.printf("%-16s", display_line6); u8x8.printf("%-16s", display_line6);
#else // we want a systime display instead LoRa status #else // we want a systime display instead LoRa status
t = myTZ.toLocal(now()); timeState = TimePulseTick ? ' ' : timeSetSymbols[timeSource];
timeState = TimePulseTick ? ' ' : timeSetSymbols[timeSource];
TimePulseTick = false; TimePulseTick = false;
u8x8.printf("%02d:%02d:%02d%c %2d.%3s", hour(t), minute(t), second(t), u8x8.printf("%02d:%02d:%02d%c %2d.%3s", hour(t), minute(t), second(t),
timeState, day(t), printmonth[month(t)]); timeState, day(t), printmonth[month(t)]);

View File

@ -472,19 +472,11 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
*pUserUTCTime += requestDelaySec; *pUserUTCTime += requestDelaySec;
// Update system time with time read from the network // Update system time with time read from the network
set_loratime(*pUserUTCTime); // store time in time sync provider function if (TimeIsValid(*pUserUTCTime)) {
if (syncTime(get_loratime, _lora)) setTime(*pUserUTCTime);
ESP_LOGI(TAG, "Received recent time from LoRa"); ESP_LOGI(TAG, "Received recent time from LoRa");
}
else else
ESP_LOGI(TAG, "Invalid time received from LoRa"); ESP_LOGI(TAG, "Invalid time received from LoRa");
#endif // HAS_LORA #endif // HAS_LORA
} // user_request_network_time_callback } // user_request_network_time_callback
time_t set_loratime(time_t t) {
static time_t loratime = 0; // stores time for retrieval
if (t > loratime)
loratime = t; // store time if it is recent
return loratime;
}
time_t get_loratime(void) { return set_loratime(0); }

View File

@ -363,9 +363,8 @@ void setup() {
timepulse_start(); timepulse_start();
#ifdef TIME_SYNC_INTERVAL #ifdef TIME_SYNC_INTERVAL
setSyncInterval(TIME_SYNC_INTERVAL * 60); // controls timeStatus() via Time.h setSyncInterval(TIME_SYNC_INTERVAL * 60); // controls timeStatus() via Time.h
setSyncProvider(syncProvider_CB); // is called by Time.h setSyncProvider(time_sync); // is called by Time.h
#endif #endif
time_sync(); // sync time
// start wifi in monitor mode and start channel rotation timer // start wifi in monitor mode and start channel rotation timer
ESP_LOGI(TAG, "Starting Wifi..."); ESP_LOGI(TAG, "Starting Wifi...");

View File

@ -8,22 +8,23 @@ const char timeSetSymbols[] = {'G', 'R', 'L', '?'};
getExternalTime TimeSourcePtr; // pointer to time source function getExternalTime TimeSourcePtr; // pointer to time source function
void time_sync() { time_t time_sync() {
// check synchonization of systime, called by cyclic.cpp // check synchonization of systime, called by cyclic.cpp
time_t t = 0;
#ifdef TIME_SYNC_INTERVAL #ifdef TIME_SYNC_INTERVAL
if (timeStatus() == timeSet) // timeStatus() is flipped in Time.h
return;
#ifdef HAS_GPS #ifdef HAS_GPS
if (syncTime(get_gpstime, _gps)) t = syncTime(get_gpstime, _gps);
return; // attempt sync with GPS time if (t)
return t; // attempt sync with GPS time
#endif #endif
// no GPS -> fallback to RTC time while trying lora sync // no GPS -> fallback to RTC time while trying lora sync
#ifdef HAS_RTC #ifdef HAS_RTC
if (!syncTime(get_rtctime, _rtc)) // sync with RTC time t = syncTime(get_rtctime, _rtc); // sync with RTC time
if (!t)
ESP_LOGW(TAG, "no confident RTC time"); ESP_LOGW(TAG, "no confident RTC time");
#endif #endif
@ -33,26 +34,25 @@ void time_sync() {
#endif #endif
#endif // TIME_SYNC_INTERVAL #endif // TIME_SYNC_INTERVAL
return t;
} // time_sync() } // time_sync()
// sync time on start of next second from GPS or RTC // sync time on start of next second from GPS or RTC
uint8_t syncTime(getExternalTime getTimeFunction, timesource_t const caller) { time_t syncTime(getExternalTime getTimeFunction, timesource_t const caller) {
TimeSourcePtr = getTimeFunction;
time_t t; time_t t;
TimeSourcePtr = getTimeFunction;
if (!TimeSourcePtr) if (!TimeSourcePtr)
goto error; goto error;
if ((caller == _gps || caller == _rtc)) // ticking timesource? xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1000)); // wait for pps
xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1000)); // then wait on pps
t = TimeSourcePtr(); // get time from given timesource t = TimeSourcePtr(); // get time from given timesource
if (TimeIsValid(t)) { if (TimeIsValid(t)) {
if (caller == _gps) // gps time concerns past second if (caller == _gps) // gps time concerns past second
t++; t++;
setTime(t); // flips timeStatus() in Time.h
timeSource = caller; timeSource = caller;
ESP_LOGD(TAG, "Time source %c set time to %02d:%02d:%02d", ESP_LOGD(TAG, "Time source %c set time to %02d:%02d:%02d",
timeSetSymbols[timeSource], hour(t), minute(t), second(t)); timeSetSymbols[timeSource], hour(t), minute(t), second(t));
@ -62,7 +62,7 @@ uint8_t syncTime(getExternalTime getTimeFunction, timesource_t const caller) {
set_rtctime(t); set_rtctime(t);
#endif #endif
return 1; // success return t; // success
} }
error: error:
@ -72,14 +72,6 @@ error:
} // syncTime() } // 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 setup a pulse per second for time synchronisation // helper function to setup a pulse per second for time synchronisation
uint8_t timepulse_init() { uint8_t timepulse_init() {