Replace Time with Toki
This commit is contained in:
parent
852f758be3
commit
8431d0bd5c
@ -481,7 +481,6 @@ build_flags = ${common.build_flags_esp8266}
|
||||
board = esp32dev
|
||||
platform = espressif32@3.2
|
||||
upload_speed = 921600
|
||||
upload_port = COM8
|
||||
lib_deps = ${env.lib_deps}
|
||||
TFT_eSPI
|
||||
build_flags = ${common.build_flags_esp32} -D WLED_DISABLE_BROWNOUT_DET -D WLED_DISABLE_INFRARED
|
||||
|
@ -14,7 +14,7 @@ class RTCUsermod : public Usermod {
|
||||
void setup() {
|
||||
time_t rtcTime = RTC.get();
|
||||
if (rtcTime) {
|
||||
setTime(rtcTime);
|
||||
toki.setTime(rtcTime,0);
|
||||
updateLocalTime();
|
||||
} else {
|
||||
if (!RTC.chipPresent()) disabled = true; //don't waste time if H/W error
|
||||
@ -22,11 +22,9 @@ class RTCUsermod : public Usermod {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (!disabled && millis() - lastTime > 500) {
|
||||
time_t t = now();
|
||||
if (!disabled && toki.isTick()) {
|
||||
time_t t = toki.second();
|
||||
if (t != RTC.get()) RTC.set(t); //set RTC to NTP/UI-provided value
|
||||
|
||||
lastTime = millis();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ public:
|
||||
if (m_pD2D && (999000000L != ntpLastSyncTime))
|
||||
{
|
||||
// to prevent needing to import all the timezone stuff from other modules, work completely in UTC
|
||||
time_t timeUTC = now();
|
||||
time_t timeUTC = toki.second();
|
||||
tmElements_t tmNow;
|
||||
breakTime(timeUTC, tmNow);
|
||||
int nCurMinute = tmNow.Minute;
|
||||
|
@ -210,10 +210,10 @@ bool deserializeState(JsonObject root)
|
||||
|
||||
unsigned long timein = root[F("time")] | UINT32_MAX; //backup time source if NTP not synced
|
||||
if (timein != UINT32_MAX) {
|
||||
time_t prev = now();
|
||||
time_t prev = toki.second();
|
||||
if (millis() - ntpLastSyncTime > 50000000L) {
|
||||
setTime(timein);
|
||||
if (abs(now() - prev) > 60L) {
|
||||
toki.setTime(timein,toki.millisecond());
|
||||
if (abs(timein - prev) > 60L) {
|
||||
updateLocalTime();
|
||||
calculateSunriseAndSunset();
|
||||
}
|
||||
|
@ -5,6 +5,8 @@
|
||||
/*
|
||||
* Acquires time from NTP server
|
||||
*/
|
||||
//#define WLED_DEBUG_NTP
|
||||
|
||||
Timezone* tz;
|
||||
|
||||
#define TZ_UTC 0
|
||||
@ -182,44 +184,46 @@ void sendNTPPacket()
|
||||
bool checkNTPResponse()
|
||||
{
|
||||
int cb = ntpUdp.parsePacket();
|
||||
if (cb) {
|
||||
uint32_t ntpPacketReceivedTime = millis();
|
||||
DEBUG_PRINT(F("NTP recv, l="));
|
||||
DEBUG_PRINTLN(cb);
|
||||
byte pbuf[NTP_PACKET_SIZE];
|
||||
ntpUdp.read(pbuf, NTP_PACKET_SIZE); // read the packet into the buffer
|
||||
if (!cb) return false;
|
||||
|
||||
Toki::Time arrived = toki.fromNTP(pbuf + 32);
|
||||
Toki::Time departed = toki.fromNTP(pbuf + 40);
|
||||
//basic half roundtrip estimation
|
||||
uint32_t serverDelay = toki.msDifference(arrived, departed);
|
||||
uint32_t offset = (ntpPacketReceivedTime - ntpPacketSentTime - serverDelay) >> 1;
|
||||
toki.printTime(departed);
|
||||
toki.adjust(departed, offset);
|
||||
toki.setTime(departed);
|
||||
Serial.print("Arrived: ");
|
||||
toki.printTime(arrived);
|
||||
Serial.print("Time: ");
|
||||
toki.printTime(departed);
|
||||
Serial.print("Roundtrip: ");
|
||||
Serial.println(ntpPacketReceivedTime - ntpPacketSentTime);
|
||||
Serial.print("Offset: ");
|
||||
Serial.println(offset);
|
||||
Serial.print("Serverdelay: ");
|
||||
Serial.println(serverDelay);
|
||||
uint32_t ntpPacketReceivedTime = millis();
|
||||
DEBUG_PRINT(F("NTP recv, l="));
|
||||
DEBUG_PRINTLN(cb);
|
||||
byte pbuf[NTP_PACKET_SIZE];
|
||||
ntpUdp.read(pbuf, NTP_PACKET_SIZE); // read the packet into the buffer
|
||||
|
||||
DEBUG_PRINT(F("Unix time = "));
|
||||
uint32_t epoch = toki.second();
|
||||
if (epoch == 0) return false;
|
||||
setTime(epoch); //legacy
|
||||
DEBUG_PRINTLN(epoch);
|
||||
if (countdownTime - now() > 0) countdownOverTriggered = false;
|
||||
// if time changed re-calculate sunrise/sunset
|
||||
updateLocalTime();
|
||||
calculateSunriseAndSunset();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
Toki::Time arrived = toki.fromNTP(pbuf + 32);
|
||||
Toki::Time departed = toki.fromNTP(pbuf + 40);
|
||||
if (departed.sec == 0) return false;
|
||||
//basic half roundtrip estimation
|
||||
uint32_t serverDelay = toki.msDifference(arrived, departed);
|
||||
uint32_t offset = (ntpPacketReceivedTime - ntpPacketSentTime - serverDelay) >> 1;
|
||||
#ifdef WLED_DEBUG_NTP
|
||||
//the time the packet departed the NTP server
|
||||
toki.printTime(departed);
|
||||
#endif
|
||||
|
||||
toki.adjust(departed, offset);
|
||||
toki.setTime(departed);
|
||||
|
||||
#ifdef WLED_DEBUG_NTP
|
||||
Serial.print("Arrived: ");
|
||||
toki.printTime(arrived);
|
||||
Serial.print("Time: ");
|
||||
toki.printTime(departed);
|
||||
Serial.print("Roundtrip: ");
|
||||
Serial.println(ntpPacketReceivedTime - ntpPacketSentTime);
|
||||
Serial.print("Offset: ");
|
||||
Serial.println(offset);
|
||||
Serial.print("Serverdelay: ");
|
||||
Serial.println(serverDelay);
|
||||
#endif
|
||||
|
||||
if (countdownTime - toki.second() > 0) countdownOverTriggered = false;
|
||||
// if time changed re-calculate sunrise/sunset
|
||||
updateLocalTime();
|
||||
calculateSunriseAndSunset();
|
||||
return true;
|
||||
}
|
||||
|
||||
void updateLocalTime()
|
||||
@ -249,13 +253,13 @@ void setCountdown()
|
||||
{
|
||||
if (currentTimezone != tzCurrent) updateTimezone();
|
||||
countdownTime = tz->toUTC(getUnixTime(countdownHour, countdownMin, countdownSec, countdownDay, countdownMonth, countdownYear));
|
||||
if (countdownTime - now() > 0) countdownOverTriggered = false;
|
||||
if (countdownTime - toki.second() > 0) countdownOverTriggered = false;
|
||||
}
|
||||
|
||||
//returns true if countdown just over
|
||||
bool checkCountdown()
|
||||
{
|
||||
unsigned long n = now();
|
||||
unsigned long n = toki.second();
|
||||
if (countdownMode) localTime = countdownTime - n + utcOffsetSecs;
|
||||
if (n > countdownTime) {
|
||||
if (countdownMode) localTime = n - countdownTime + utcOffsetSecs;
|
||||
|
@ -82,9 +82,9 @@ void _overlayAnalogClock()
|
||||
|
||||
void _overlayAnalogCountdown()
|
||||
{
|
||||
if ((unsigned long)now() < countdownTime)
|
||||
if ((unsigned long)toki.second() < countdownTime)
|
||||
{
|
||||
long diff = countdownTime - now();
|
||||
long diff = countdownTime - toki.second();
|
||||
double pval = 60;
|
||||
if (diff > 31557600L) //display in years if more than 365 days
|
||||
{
|
||||
|
@ -69,13 +69,13 @@ void savePreset(byte index, bool persist, const char* pname, JsonObject saveobj)
|
||||
|
||||
writeObjectToFileUsingId("/presets.json", index, fileDoc);
|
||||
}
|
||||
presetsModifiedTime = now(); //unix time
|
||||
presetsModifiedTime = toki.second(); //unix time
|
||||
updateFSInfo();
|
||||
}
|
||||
|
||||
void deletePreset(byte index) {
|
||||
StaticJsonDocument<24> empty;
|
||||
writeObjectToFileUsingId("/presets.json", index, &empty);
|
||||
presetsModifiedTime = now(); //unix time
|
||||
presetsModifiedTime = toki.second(); //unix time
|
||||
updateFSInfo();
|
||||
}
|
@ -790,14 +790,14 @@ bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply)
|
||||
//set time (unix timestamp)
|
||||
pos = req.indexOf(F("ST="));
|
||||
if (pos > 0) {
|
||||
setTime(getNumVal(&req, pos));
|
||||
toki.setTime(getNumVal(&req, pos),toki.millisecond());
|
||||
}
|
||||
|
||||
//set countdown goal (unix timestamp)
|
||||
pos = req.indexOf(F("CT="));
|
||||
if (pos > 0) {
|
||||
countdownTime = getNumVal(&req, pos);
|
||||
if (countdownTime - now() > 0) countdownOverTriggered = false;
|
||||
if (countdownTime - toki.second() > 0) countdownOverTriggered = false;
|
||||
}
|
||||
|
||||
pos = req.indexOf(F("LO="));
|
||||
|
@ -1,5 +1,9 @@
|
||||
Readme file for Arduino Time Library
|
||||
|
||||
! MODIFIED DISTRIBUTION FOR WLED
|
||||
All timekeeping functions are removed, only conversion functions used.
|
||||
Please see https://github.com/PaulStoffregen/Time for the full, original library
|
||||
|
||||
Time is a library that provides timekeeping functionality for Arduino.
|
||||
|
||||
The code is derived from the Playground DateTime library but is updated
|
||||
@ -14,26 +18,10 @@ for time synchronization.
|
||||
|
||||
The functions available in the library include:
|
||||
|
||||
hour(); // the hour now (0-23)
|
||||
minute(); // the minute now (0-59)
|
||||
second(); // the second now (0-59)
|
||||
day(); // the day now (1-31)
|
||||
weekday(); // day of the week (1-7), Sunday is day 1
|
||||
month(); // the month now (1-12)
|
||||
year(); // the full four digit year: (2009, 2010 etc)
|
||||
|
||||
there are also functions to return the hour in 12 hour format
|
||||
hourFormat12(); // the hour now in 12 hour format
|
||||
isAM(); // returns true if time now is AM
|
||||
isPM(); // returns true if time now is PM
|
||||
|
||||
now(); // returns the current time as seconds since Jan 1 1970
|
||||
|
||||
The time and date functions can take an optional parameter for the time. This prevents
|
||||
The time and date functions take a parameter for the time. This prevents
|
||||
errors if the time rolls over between elements. For example, if a new minute begins
|
||||
between getting the minute and second, the values will be inconsistent. Using the
|
||||
following functions eliminates this probglem
|
||||
time_t t = now(); // store the current time in time variable t
|
||||
following functions eliminates this problem
|
||||
hour(t); // returns the hour for the given time t
|
||||
minute(t); // returns the minute for the given time t
|
||||
second(t); // returns the second for the given time t
|
||||
@ -43,25 +31,6 @@ following functions eliminates this probglem
|
||||
year(t); // the year for the given time t
|
||||
|
||||
|
||||
Functions for managing the timer services are:
|
||||
|
||||
setTime(t); // set the system time to the give time t
|
||||
setTime(hr,min,sec,day,mnth,yr); // alternative to above, yr is 2 or 4 digit yr
|
||||
// (2010 or 10 sets year to 2010)
|
||||
adjustTime(adjustment); // adjust system time by adding the adjustment value
|
||||
timeStatus(); // indicates if time has been set and recently synchronized
|
||||
// returns one of the following enumerations:
|
||||
timeNotSet // the time has never been set, the clock started at Jan 1 1970
|
||||
timeNeedsSync // the time had been set but a sync attempt did not succeed
|
||||
timeSet // the time is set and is synced
|
||||
|
||||
Time and Date values are not valid if the status is timeNotSet. Otherwise values can be used but
|
||||
the returned time may have drifted if the status is timeNeedsSync.
|
||||
|
||||
setSyncProvider(getTimeFunction); // set the external time provider
|
||||
setSyncInterval(interval); // set the number of seconds between re-sync
|
||||
|
||||
|
||||
There are many convenience macros in the time.h file for time constants and conversion
|
||||
of time units.
|
||||
|
||||
|
@ -25,6 +25,7 @@
|
||||
examples, add error checking and messages to RTC examples,
|
||||
add examples to DS1307RTC library.
|
||||
1.4 5 Sep 2014 - compatibility with Arduino 1.5.7
|
||||
2.0 25 May 2021 - removed timing code, only used for conversion between unix and time
|
||||
*/
|
||||
|
||||
#if ARDUINO >= 100
|
||||
@ -45,19 +46,11 @@ void refreshCache(time_t t) {
|
||||
}
|
||||
}
|
||||
|
||||
int hour() { // the hour now
|
||||
return hour(now());
|
||||
}
|
||||
|
||||
int hour(time_t t) { // the hour for the given time
|
||||
refreshCache(t);
|
||||
return tm.Hour;
|
||||
}
|
||||
|
||||
int hourFormat12() { // the hour now in 12 hour format
|
||||
return hourFormat12(now());
|
||||
}
|
||||
|
||||
int hourFormat12(time_t t) { // the hour for the given time in 12 hour format
|
||||
refreshCache(t);
|
||||
if( tm.Hour == 0 )
|
||||
@ -68,71 +61,39 @@ int hourFormat12(time_t t) { // the hour for the given time in 12 hour format
|
||||
return tm.Hour ;
|
||||
}
|
||||
|
||||
uint8_t isAM() { // returns true if time now is AM
|
||||
return !isPM(now());
|
||||
}
|
||||
|
||||
uint8_t isAM(time_t t) { // returns true if given time is AM
|
||||
return !isPM(t);
|
||||
}
|
||||
|
||||
uint8_t isPM() { // returns true if PM
|
||||
return isPM(now());
|
||||
}
|
||||
|
||||
uint8_t isPM(time_t t) { // returns true if PM
|
||||
return (hour(t) >= 12);
|
||||
}
|
||||
|
||||
int minute() {
|
||||
return minute(now());
|
||||
}
|
||||
|
||||
int minute(time_t t) { // the minute for the given time
|
||||
refreshCache(t);
|
||||
return tm.Minute;
|
||||
}
|
||||
|
||||
int second() {
|
||||
return second(now());
|
||||
}
|
||||
|
||||
int second(time_t t) { // the second for the given time
|
||||
refreshCache(t);
|
||||
return tm.Second;
|
||||
}
|
||||
|
||||
int day(){
|
||||
return(day(now()));
|
||||
}
|
||||
|
||||
int day(time_t t) { // the day for the given time (0-6)
|
||||
refreshCache(t);
|
||||
return tm.Day;
|
||||
}
|
||||
|
||||
int weekday() { // Sunday is day 1
|
||||
return weekday(now());
|
||||
}
|
||||
|
||||
int weekday(time_t t) {
|
||||
refreshCache(t);
|
||||
return tm.Wday;
|
||||
}
|
||||
|
||||
int month(){
|
||||
return month(now());
|
||||
}
|
||||
|
||||
int month(time_t t) { // the month for the given time
|
||||
refreshCache(t);
|
||||
return tm.Month;
|
||||
}
|
||||
|
||||
int year() { // as in Processing, the full four digit year: (2009, 2010 etc)
|
||||
return year(now());
|
||||
}
|
||||
|
||||
int year(time_t t) { // the year for the given time
|
||||
refreshCache(t);
|
||||
return tmYearToCalendar(tm.Year);
|
||||
@ -230,33 +191,6 @@ time_t makeTime(tmElements_t &tm){
|
||||
seconds+= tm.Second;
|
||||
return (time_t)seconds;
|
||||
}
|
||||
/*=====================================================*/
|
||||
/* Low level system time functions */
|
||||
|
||||
static uint32_t sysTime = 0; //seconds
|
||||
static uint16_t sysMillis = 0;
|
||||
static uint32_t prevMillis = 0;
|
||||
|
||||
time_t now() {
|
||||
// calculate number of seconds passed since last call to now()
|
||||
while (millis() - prevMillis >= 1000) {
|
||||
// millis() and prevMillis are both unsigned ints thus the subtraction will always be the absolute value of the difference
|
||||
sysTime++;
|
||||
prevMillis += 1000;
|
||||
}
|
||||
|
||||
return (time_t)sysTime;
|
||||
}
|
||||
|
||||
uint16_t millisecond() { // the millisecond (0-999) now
|
||||
return (sysMillis - millis()) % 1000;
|
||||
}
|
||||
|
||||
void setTime(time_t t, uint16_t ms) {
|
||||
sysTime = (uint32_t)t;
|
||||
sysMillis = ms;
|
||||
prevMillis = millis(); // restart counting from now (thanks to Korman for this fix)
|
||||
}
|
||||
|
||||
time_t getUnixTime(int hr,int min,int sec,int dy, int mnth, int yr){
|
||||
// year can be given as full four digit year or two digts (2010 or 10 for 2010);
|
||||
@ -273,11 +207,3 @@ time_t getUnixTime(int hr,int min,int sec,int dy, int mnth, int yr){
|
||||
tm.Second = sec;
|
||||
return makeTime(tm);
|
||||
}
|
||||
|
||||
void setTime(int hr,int min,int sec,int dy, int mnth, int yr, uint16_t ms){
|
||||
setTime(getUnixTime(hr,min,sec,dy,mnth,yr), ms);
|
||||
}
|
||||
|
||||
void adjustTime(long adjustment) {
|
||||
sysTime += adjustment;
|
||||
}
|
@ -94,33 +94,18 @@ typedef time_t(*getExternalTime)();
|
||||
|
||||
/*============================================================================*/
|
||||
/* time and date functions */
|
||||
int hour(); // the hour now
|
||||
int hour(time_t t); // the hour for the given time
|
||||
int hourFormat12(); // the hour now in 12 hour format
|
||||
int hourFormat12(time_t t); // the hour for the given time in 12 hour format
|
||||
uint8_t isAM(); // returns true if time now is AM
|
||||
uint8_t isAM(time_t t); // returns true the given time is AM
|
||||
uint8_t isPM(); // returns true if time now is PM
|
||||
uint8_t isPM(time_t t); // returns true the given time is PM
|
||||
int minute(); // the minute now
|
||||
int minute(time_t t); // the minute for the given time
|
||||
int second(); // the second now
|
||||
int second(time_t t); // the second for the given time
|
||||
int day(); // the day now
|
||||
int day(time_t t); // the day for the given time
|
||||
int weekday(); // the weekday now (Sunday is day 1)
|
||||
int weekday(time_t t); // the weekday for the given time
|
||||
int month(); // the month now (Jan is month 1)
|
||||
int month(time_t t); // the month for the given time
|
||||
int year(); // the full four digit year: (2009, 2010 etc)
|
||||
int year(time_t t); // the year for the given time
|
||||
uint16_t millisecond(); // the millisecond now
|
||||
|
||||
time_t now(); // return the current time as seconds since Jan 1 1970
|
||||
void setTime(time_t t, uint16_t ms = 0);
|
||||
void setTime(int hr,int min,int sec,int day, int month, int yr, uint16_t ms = 0);
|
||||
time_t getUnixTime(int hr,int min,int sec,int day, int month, int yr); //added by Aircoookie to get epoch time
|
||||
void adjustTime(long adjustment);
|
||||
|
||||
/* date strings */
|
||||
#define dt_MAX_STRING_LEN 9 // length of longest date string (excluding terminating null)
|
||||
|
@ -272,7 +272,7 @@ void WLED::loop()
|
||||
if (millis() - debugTime > 9999) {
|
||||
DEBUG_PRINTLN("---DEBUG INFO---");
|
||||
DEBUG_PRINT("Runtime: "); DEBUG_PRINTLN(millis());
|
||||
DEBUG_PRINT("Unix time: "); DEBUG_PRINTLN(now());
|
||||
DEBUG_PRINT("Unix time: "); toki.printTime(toki.getTime());
|
||||
DEBUG_PRINT("Free heap: "); DEBUG_PRINTLN(ESP.getFreeHeap());
|
||||
DEBUG_PRINT("Wifi state: "); DEBUG_PRINTLN(WiFi.status());
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user