adjust RTC usermod for "MM Style"

This commit is contained in:
Frank
2023-09-19 22:54:08 +02:00
parent acc2d3aa10
commit e28d303d4c
2 changed files with 24 additions and 9 deletions

View File

@@ -8,21 +8,25 @@
#define RTC_DELTA 2 // only modify RTC time if delta exceeds this number of seconds #define RTC_DELTA 2 // only modify RTC time if delta exceeds this number of seconds
//Connect DS1307 to standard I2C pins (ESP32: GPIO 21 (SDA)/GPIO 22 (SCL)) //Connect DS1307 or DS3231 to standard I2C pins (ESP32: GPIO 21 (SDA)/GPIO 22 (SCL))
class RTCUsermod : public Usermod { class RTCUsermod : public Usermod {
private: private:
unsigned long lastTime = 0; unsigned long lastTime = 0;
bool disabled = false; bool RTCfound = false; // WLEDMM to prevent errors after anabling the usermod (Wire.cpp:526] write(): NULL TX buffer pointer)
public: public:
RTCUsermod(const char *name, bool enabled):Usermod(name, enabled) {} //WLEDMM: this shouldn't be necessary (passthrough of constructor), maybe because Usermod is an abstract class
void setup() { void setup() {
RTCfound = false; // WLEDMM
PinManagerPinType pins[2] = { { i2c_scl, true }, { i2c_sda, true } }; PinManagerPinType pins[2] = { { i2c_scl, true }, { i2c_sda, true } };
if (pins[1].pin < 0 || pins[0].pin < 0) { disabled=true; return; } //WLEDMM bugfix - ensure that "final" GPIO are valid and no "-1" sneaks trough if (pins[1].pin < 0 || pins[0].pin < 0) { enabled=false; return; } //WLEDMM bugfix - ensure that "final" GPIO are valid and no "-1" sneaks trough
if (!enabled) { DEBUG_PRINTLN(F("RTC usermod not enabled.")); return; }
// WLEDMM join hardware I2C // WLEDMM join hardware I2C
if (!pinManager.joinWire()) { // WLEDMM - this allocates global I2C pins, then starts Wire - if not started previously if (!pinManager.joinWire()) { // WLEDMM - this allocates global I2C pins, then starts Wire - if not started previously
disabled = true; enabled = false;
return; return;
} }
@@ -40,20 +44,31 @@ class RTCUsermod : public Usermod {
updateLocalTime(); updateLocalTime();
USER_PRINTLN(F("Localtime updated from RTC.")); USER_PRINTLN(F("Localtime updated from RTC."));
} else { } else {
if (!RTC.chipPresent()) disabled = true; //don't waste time if H/W error if (!RTC.chipPresent()) {
enabled = false; //don't waste time if H/W error
USER_PRINTLN(F("RTC board not present."));
} }
} }
if (enabled) RTCfound = true; // WLEDMM
}
void loop() { void loop() {
if (strip.isUpdating()) return; // WLEDMM begin
if (!disabled && toki.isTick()) { if (!enabled) return;
if (!RTCfound) return; // WLEDMM
unsigned long currentTime = millis(); // get the current elapsed time
if (strip.isUpdating() && (currentTime - lastTime < 10000)) return; // WLEDMM: be nice
// WLEDMM end
if (enabled && toki.isTick()) { // WLEDMM
time_t t = toki.second(); time_t t = toki.second();
lastTime = millis(); // WLEDMM
if (abs(t - RTC.get())> RTC_DELTA) { // WLEDMM only consider time diffs > 2 seconds if (abs(t - RTC.get())> RTC_DELTA) { // WLEDMM only consider time diffs > 2 seconds
if ( (toki.getTimeSource() == TOKI_TS_NTP) if ( (toki.getTimeSource() == TOKI_TS_NTP)
||( (toki.getTimeSource() != TOKI_TS_NONE) && (toki.getTimeSource() != TOKI_TS_RTC) ||( (toki.getTimeSource() != TOKI_TS_NONE) && (toki.getTimeSource() != TOKI_TS_RTC)
&& (toki.getTimeSource() != TOKI_TS_BAD) && (toki.getTimeSource() != TOKI_TS_UDP_SEC) && (toki.getTimeSource() != TOKI_TS_UDP))) && (toki.getTimeSource() != TOKI_TS_BAD) && (toki.getTimeSource() != TOKI_TS_UDP_SEC) && (toki.getTimeSource() != TOKI_TS_UDP)))
{ // WLEMM update RTC if we have a reliable time source { // WLEDMM update RTC if we have a reliable time source
RTC.set(t); //set RTC to NTP/UI-provided value - WLEDMM allow up to 3 sec deviation RTC.set(t); //set RTC to NTP/UI-provided value - WLEDMM allow up to 3 sec deviation
USER_PRINTLN(F("RTC updated using localtime.")); USER_PRINTLN(F("RTC updated using localtime."));
} else { } else {

View File

@@ -290,7 +290,7 @@ void registerUsermods()
#endif #endif
#ifdef USERMOD_RTC #ifdef USERMOD_RTC
usermods.add(new RTCUsermod()); usermods.add(new RTCUsermod("RTC", true));
#endif #endif
#ifdef USERMOD_ELEKSTUBE_IPS #ifdef USERMOD_ELEKSTUBE_IPS