timekeeper code sanitizations

This commit is contained in:
Klaus K Wilting 2019-02-24 13:47:18 +01:00
parent c02b8bc751
commit 171207af22
6 changed files with 17 additions and 20 deletions

View File

@ -8,9 +8,8 @@
extern RtcDS3231<TwoWire> Rtc; // make RTC instance globally available
int rtc_init(void);
int set_rtctime(uint32_t t);
int set_rtctime(time_t t);
uint8_t rtc_init(void);
uint8_t set_rtctime(time_t t);
void sync_rtctime(void);
time_t get_rtctime(void);
float get_rtctemp(void);

View File

@ -13,7 +13,7 @@
#include "dcf77.h"
#endif
enum timesources { pps, rtc, lora, unsynced };
enum timesources { _gps, _rtc, _lora, _unsynced };
void IRAM_ATTR CLOCKIRQ(void);
void clock_init(void);

View File

@ -472,7 +472,7 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
*pUserUTCTime += requestDelaySec;
// Update system time with time read from the network
if (syncTime(*pUserUTCTime, lora)) { // have we got a valid time?
if (syncTime(*pUserUTCTime, _lora)) { // have we got a valid time?
ESP_LOGI(TAG, "LORA has set the system time");
} else
ESP_LOGI(TAG, "Unable to sync system time with LORA");

View File

@ -362,7 +362,9 @@ void setup() {
assert(timepulse_init()); // setup timepulse
timepulse_start();
time_sync(); // sync time
#ifdef TIME_SYNC_INTERVAL
setSyncInterval(TIME_SYNC_INTERVAL * 60); // controls timeStatus()
#endif
// start wifi in monitor mode and start channel rotation timer
ESP_LOGI(TAG, "Starting Wifi...");

View File

@ -8,7 +8,7 @@ static const char TAG[] = "main";
RtcDS3231<TwoWire> Rtc(Wire); // RTC hardware i2c interface
// initialize RTC
int rtc_init(void) {
uint8_t rtc_init(void) {
if (I2C_MUTEX_LOCK()) { // block i2c bus access
@ -24,6 +24,7 @@ int rtc_init(void) {
Rtc.SetIsRunning(true);
}
// If you want to initialize a fresh RTC to compiled time, use this code
/*
RtcDateTime tt = Rtc.GetDateTime();
time_t t = tt.Epoch32Time(); // sec2000 -> epoch
@ -45,7 +46,7 @@ int rtc_init(void) {
} // rtc_init()
int set_rtctime(time_t t) { // t is UTC in seconds epoch time
uint8_t set_rtctime(time_t t) { // t is UTC in seconds epoch time
if (I2C_MUTEX_LOCK()) {
Rtc.SetDateTime(RtcDateTime(t - SECS_YR_2000)); // epoch -> sec2000
I2C_MUTEX_UNLOCK();
@ -57,11 +58,6 @@ int set_rtctime(time_t t) { // t is UTC in seconds epoch time
} // failure
} // set_rtctime()
int set_rtctime(uint32_t t) { // t is UTC in seconds epoch time
return set_rtctime(static_cast<time_t>(t));
// set_rtctime()
}
time_t get_rtctime(void) {
// !! never call now() or delay in this function, this would break this
// function to be used as SyncProvider for Time.h

View File

@ -13,13 +13,13 @@ void time_sync() {
return;
#ifdef HAS_GPS
if (syncTime(get_gpstime(), pps))
if (syncTime(get_gpstime(), _gps))
return; // attempt sync with GPS time
#endif
// no GPS -> fallback to RTC time
// no GPS -> fallback to RTC time while trying lora sync
#ifdef HAS_RTC
if (!syncTime(get_rtctime(), rtc)) // sync with RTC time
if (!syncTime(get_rtctime(), _rtc)) // sync with RTC time
ESP_LOGW(TAG, "no confident RTC time");
#endif
@ -40,20 +40,20 @@ uint8_t syncTime(time_t const t, uint8_t const caller) {
if (TimeIsValid(t)) {
uint8_t const TimeIsPulseSynced =
wait_for_pulse(); // wait for next 1pps timepulse
setTime(t);
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));
#ifdef HAS_RTC
if ((TimeIsPulseSynced) && (caller != rtc))
if ((TimeIsPulseSynced) && (caller != _rtc))
set_rtctime(now());
#endif
return 1; // success
} else {
ESP_LOGD(TAG, "Time source %c sync attempt failed", timeSetSymbols[caller]);
timeSource = timeSetSymbols[unsynced];
timeSource = timeSetSymbols[_unsynced];
return 0; // failure
}
} // syncTime()