Skip to content

Commit e28d303

Browse files
committed
adjust RTC usermod for "MM Style"
1 parent acc2d3a commit e28d303

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

usermods/RTC/usermod_rtc.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@
88

99
#define RTC_DELTA 2 // only modify RTC time if delta exceeds this number of seconds
1010

11-
//Connect DS1307 to standard I2C pins (ESP32: GPIO 21 (SDA)/GPIO 22 (SCL))
11+
//Connect DS1307 or DS3231 to standard I2C pins (ESP32: GPIO 21 (SDA)/GPIO 22 (SCL))
1212

1313
class RTCUsermod : public Usermod {
1414
private:
1515
unsigned long lastTime = 0;
16-
bool disabled = false;
16+
bool RTCfound = false; // WLEDMM to prevent errors after anabling the usermod (Wire.cpp:526] write(): NULL TX buffer pointer)
1717
public:
1818

19+
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
20+
1921
void setup() {
22+
RTCfound = false; // WLEDMM
2023
PinManagerPinType pins[2] = { { i2c_scl, true }, { i2c_sda, true } };
21-
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
24+
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
25+
if (!enabled) { DEBUG_PRINTLN(F("RTC usermod not enabled.")); return; }
2226

2327
// WLEDMM join hardware I2C
2428
if (!pinManager.joinWire()) { // WLEDMM - this allocates global I2C pins, then starts Wire - if not started previously
25-
disabled = true;
29+
enabled = false;
2630
return;
2731
}
2832

@@ -40,20 +44,31 @@ class RTCUsermod : public Usermod {
4044
updateLocalTime();
4145
USER_PRINTLN(F("Localtime updated from RTC."));
4246
} else {
43-
if (!RTC.chipPresent()) disabled = true; //don't waste time if H/W error
47+
if (!RTC.chipPresent()) {
48+
enabled = false; //don't waste time if H/W error
49+
USER_PRINTLN(F("RTC board not present."));
50+
}
4451
}
52+
if (enabled) RTCfound = true; // WLEDMM
4553
}
4654

4755
void loop() {
48-
if (strip.isUpdating()) return;
49-
if (!disabled && toki.isTick()) {
56+
// WLEDMM begin
57+
if (!enabled) return;
58+
if (!RTCfound) return; // WLEDMM
59+
unsigned long currentTime = millis(); // get the current elapsed time
60+
if (strip.isUpdating() && (currentTime - lastTime < 10000)) return; // WLEDMM: be nice
61+
// WLEDMM end
62+
63+
if (enabled && toki.isTick()) { // WLEDMM
5064
time_t t = toki.second();
65+
lastTime = millis(); // WLEDMM
5166

5267
if (abs(t - RTC.get())> RTC_DELTA) { // WLEDMM only consider time diffs > 2 seconds
5368
if ( (toki.getTimeSource() == TOKI_TS_NTP)
5469
||( (toki.getTimeSource() != TOKI_TS_NONE) && (toki.getTimeSource() != TOKI_TS_RTC)
5570
&& (toki.getTimeSource() != TOKI_TS_BAD) && (toki.getTimeSource() != TOKI_TS_UDP_SEC) && (toki.getTimeSource() != TOKI_TS_UDP)))
56-
{ // WLEMM update RTC if we have a reliable time source
71+
{ // WLEDMM update RTC if we have a reliable time source
5772
RTC.set(t); //set RTC to NTP/UI-provided value - WLEDMM allow up to 3 sec deviation
5873
USER_PRINTLN(F("RTC updated using localtime."));
5974
} else {

wled00/usermods_list.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ void registerUsermods()
290290
#endif
291291

292292
#ifdef USERMOD_RTC
293-
usermods.add(new RTCUsermod());
293+
usermods.add(new RTCUsermod("RTC", true));
294294
#endif
295295

296296
#ifdef USERMOD_ELEKSTUBE_IPS

0 commit comments

Comments
 (0)