timesource display
This commit is contained in:
parent
d0de4ae3bc
commit
60467ca195
@ -4,7 +4,9 @@
|
||||
#include <U8x8lib.h>
|
||||
#include "cyclic.h"
|
||||
|
||||
extern uint8_t volatile DisplayState;
|
||||
extern uint8_t DisplayState;
|
||||
extern char timeSource;
|
||||
|
||||
extern HAS_DISPLAY u8x8;
|
||||
|
||||
void init_display(const char *Productname, const char *Version);
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define _RTCTIME_H
|
||||
|
||||
#include "globals.h"
|
||||
#include "timemanager.h"
|
||||
#include <Wire.h> // must be included here so that Arduino library object file references work
|
||||
#include <RtcDS3231.h>
|
||||
|
||||
|
@ -13,18 +13,20 @@
|
||||
#include "dcf77.h"
|
||||
#endif
|
||||
|
||||
time_t tmConvert_t(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh,
|
||||
uint8_t mm, uint8_t ss);
|
||||
enum timesources { pps, rtc, lora, unsynced };
|
||||
|
||||
void clock_init(void);
|
||||
void clock_loop(void *pvParameters);
|
||||
void time_sync(void);
|
||||
int wait_for_pulse(void);
|
||||
int syncTime(time_t);
|
||||
int syncTime(uint32_t t);
|
||||
int syncTime(time_t const t, uint8_t const timesource);
|
||||
int syncTime(uint32_t const t, uint8_t const timesource);
|
||||
void IRAM_ATTR CLOCKIRQ(void);
|
||||
int timepulse_init(void);
|
||||
void timepulse_start(void);
|
||||
int TimeIsValid(time_t t);
|
||||
int TimeIsValid(time_t const t);
|
||||
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);
|
||||
|
||||
#endif // _timemanager_H
|
@ -45,7 +45,8 @@ const char lora_datarate[] = {"121110090807FSNA"};
|
||||
const char *printmonth[] = {"xxx", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
||||
|
||||
uint8_t volatile DisplayState = 0;
|
||||
uint8_t DisplayState = 0;
|
||||
char timeSource = '?';
|
||||
|
||||
// helper function, prints a hex key on display
|
||||
void DisplayKey(const uint8_t *key, uint8_t len, bool lsb) {
|
||||
@ -129,16 +130,6 @@ void init_display(const char *Productname, const char *Version) {
|
||||
|
||||
void refreshtheDisplay() {
|
||||
|
||||
// time display symbols
|
||||
const char timeNotSetSymbol = '?';
|
||||
#if defined HAS_IF482
|
||||
const char timeIsSetSymbol = '+';
|
||||
#elif defined HAS_DCF77
|
||||
const char timeIsSetSymbol = '*';
|
||||
#else
|
||||
const char timeIsSetSymbol = '#';
|
||||
#endif
|
||||
|
||||
uint8_t msgWaiting;
|
||||
char timeIsSet, timeState;
|
||||
char buff[16]; // 16 chars line buffer
|
||||
@ -227,8 +218,7 @@ void refreshtheDisplay() {
|
||||
u8x8.printf("%-16s", display_line6);
|
||||
#else // we want a systime display instead LoRa status
|
||||
t = myTZ.toLocal(now());
|
||||
timeIsSet =
|
||||
(timeStatus() == timeNotSet) ? timeNotSetSymbol : timeIsSetSymbol;
|
||||
timeIsSet = (timeStatus() == timeNotSet) ? '#' : timeSource;
|
||||
timeState = TimePulseTick ? ' ' : timeIsSet;
|
||||
TimePulseTick = false;
|
||||
u8x8.printf("%02d:%02d:%02d%c %2d.%3s", hour(t), minute(t), second(t),
|
||||
|
@ -86,7 +86,7 @@ time_t get_gpstime(void) {
|
||||
gps.time.isValid() ? "yes" : "no");
|
||||
|
||||
// use recent gps time
|
||||
t = tmConvert_t(gps.date.year(), gps.date.month(), gps.date.day(),
|
||||
t = tmConvert(gps.date.year(), gps.date.month(), gps.date.day(),
|
||||
gps.time.hour(), gps.time.minute(), gps.time.second());
|
||||
|
||||
ESP_LOGD(TAG, "GPS time: %02d.%02d.%04d %02d:%02d:%02d", gps.date.day(),
|
||||
|
@ -460,7 +460,7 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
|
||||
|
||||
// Update userUTCTime, considering the difference between the GPS and UTC
|
||||
// time, and the leap seconds
|
||||
// !!! DANGER !!! This code will expire in the year when next leap second happenes
|
||||
// !!! DANGER !!! This code will expire in next year with leap second
|
||||
*pUserUTCTime = lmicTimeReference.tNetwork + 315964800;
|
||||
// Current time, in ticks
|
||||
ostime_t ticksNow = os_getTime();
|
||||
@ -472,12 +472,11 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
|
||||
*pUserUTCTime += requestDelaySec;
|
||||
|
||||
// Update system time with time read from the network
|
||||
if (syncTime(*pUserUTCTime)) { // do we have a valid time?
|
||||
if (syncTime(*pUserUTCTime, lora)) { // do we have a valid time?
|
||||
#ifdef HAS_RTC
|
||||
if (TimeIsSynced)
|
||||
set_rtctime(now()); // UTC time
|
||||
#endif
|
||||
LastSyncTime = now(); // remember time of this sync event
|
||||
ESP_LOGI(TAG, "LORA has set the system time");
|
||||
} else
|
||||
ESP_LOGI(TAG, "Unable to sync system time with LORA");
|
||||
|
@ -84,7 +84,7 @@
|
||||
// settings for syncing time of node and external time sources
|
||||
#define TIME_SYNC_INTERVAL 10 // sync time each .. minutes from external time source (GPS/LORA) [default = 10], comment out means off
|
||||
#define TIME_SYNC_TIMEOUT 30 // fallback to rtc for timesync after .. minutes no sync with external time source
|
||||
//#define TIME_SYNC_LORA 1 // use LORA network for timesync, comment out means off [default = off]
|
||||
#define TIME_SYNC_LORA 1 // use LORA network for timesync, 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
|
||||
|
@ -14,11 +14,12 @@ void time_sync() {
|
||||
if ((lastTimeSync >= (TIME_SYNC_INTERVAL * 60000)) || !LastSyncTime) {
|
||||
// is it time to sync with external source?
|
||||
#ifdef HAS_GPS
|
||||
syncTime(get_gpstime()); // attempt sync with GPS time
|
||||
if (syncTime(get_gpstime(), pps)) // attempt sync with GPS time
|
||||
#endif
|
||||
#if defined HAS_LORA && defined TIME_SYNC_LORA
|
||||
if (!TimeIsSynced) // no GPS sync -> try lora sync
|
||||
LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime);
|
||||
LMIC_requestNetworkTime(user_request_network_time_callback,
|
||||
&userUTCTime);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -28,7 +29,7 @@ void time_sync() {
|
||||
} else { // we switch to fallback time after a while
|
||||
if ((lastTimeSync >= (TIME_SYNC_TIMEOUT * 60000)) ||
|
||||
!LastSyncTime) { // sync is still due -> use RTC as fallback source
|
||||
syncTime(get_rtctime()); // sync with RTC time
|
||||
if (syncTime(get_rtctime(), rtc)) // sync with RTC time
|
||||
TimeIsSynced = false;
|
||||
}
|
||||
}
|
||||
@ -38,25 +39,31 @@ void time_sync() {
|
||||
} // time_sync()
|
||||
|
||||
// helper function to sync time on start of next second
|
||||
int syncTime(time_t t) {
|
||||
int syncTime(time_t const t, uint8_t const timesource) {
|
||||
|
||||
// symbol to display current time source
|
||||
const char timeSetSymbols[] = {'G', 'R', 'L', '~' };
|
||||
|
||||
if (TimeIsValid(t)) {
|
||||
TimeIsSynced = wait_for_pulse(); // wait for next 1pps timepulse
|
||||
setTime(t);
|
||||
adjustTime(1); // forward time to next second
|
||||
LastSyncTime = now(); // store time of this sync
|
||||
timeSource = timeSetSymbols[timesource];
|
||||
ESP_LOGD(TAG, "Time was set to %02d:%02d:%02d", hour(t), minute(t),
|
||||
second(t));
|
||||
return 1; // success
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Time sync attempt failed");
|
||||
timeSource = timeSetSymbols[unsynced];
|
||||
TimeIsSynced = false;
|
||||
return 0;
|
||||
}
|
||||
// failure
|
||||
}
|
||||
|
||||
int syncTime(uint32_t t) { // t is UTC time in seconds epoch
|
||||
return syncTime(static_cast<time_t>(t));
|
||||
int syncTime(uint32_t const t, uint8_t const timesource) { // t is UTC time in seconds epoch
|
||||
return syncTime(static_cast<time_t>(t), timesource);
|
||||
}
|
||||
|
||||
// helper function to sync moment on timepulse
|
||||
@ -132,7 +139,7 @@ void IRAM_ATTR CLOCKIRQ(void) {
|
||||
}
|
||||
|
||||
// helper function to check plausibility of a time
|
||||
int TimeIsValid(time_t t) {
|
||||
int TimeIsValid(time_t const t) {
|
||||
// is it a time in the past? we use compile date to guess
|
||||
ESP_LOGD(TAG, "t=%d, tt=%d, valid: %s", t, compiledUTC(),
|
||||
(t >= compiledUTC()) ? "yes" : "no");
|
||||
@ -146,7 +153,7 @@ time_t compiledUTC(void) {
|
||||
}
|
||||
|
||||
// helper function to convert gps date/time into time_t
|
||||
time_t tmConvert_t(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh,
|
||||
time_t tmConvert(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh,
|
||||
uint8_t mm, uint8_t ss) {
|
||||
tmElements_t tm;
|
||||
tm.Year = CalendarYrToTm(YYYY); // year offset from 1970 in time.h
|
||||
|
Loading…
Reference in New Issue
Block a user