WLED/usermods/RTC/usermod_rtc.h

52 lines
1.6 KiB
C
Raw Permalink Normal View History

#pragma once
#include "src/dependencies/time/DS1307RTC.h"
#include "wled.h"
//Connect DS1307 to standard I2C pins (ESP32: GPIO 21 (SDA)/GPIO 22 (SCL))
class RTCUsermod : public Usermod {
private:
unsigned long lastTime = 0;
bool disabled = false;
public:
void setup() {
2022-08-14 13:05:59 +02:00
PinManagerPinType pins[2] = { { i2c_scl, true }, { i2c_sda, true } };
2022-02-01 09:33:57 +01:00
if (!pinManager.allocateMultiplePins(pins, 2, PinOwner::HW_I2C)) { disabled = true; return; }
RTC.begin();
time_t rtcTime = RTC.get();
if (rtcTime) {
2021-05-27 02:02:02 +02:00
toki.setTime(rtcTime,TOKI_NO_MS_ACCURACY,TOKI_TS_RTC);
updateLocalTime();
} else {
if (!RTC.chipPresent()) disabled = true; //don't waste time if H/W error
}
}
void loop() {
2022-02-01 09:33:57 +01:00
if (strip.isUpdating()) return;
2021-05-25 09:59:19 +02:00
if (!disabled && toki.isTick()) {
time_t t = toki.second();
if (t != RTC.get()) RTC.set(t); //set RTC to NTP/UI-provided value
}
}
2022-02-01 09:33:57 +01:00
/*
* addToConfig() can be used to add custom persistent settings to the cfg.json file in the "um" (usermod) object.
* It will be called by WLED when settings are actually saved (for example, LED settings are saved)
* I highly recommend checking out the basics of ArduinoJson serialization and deserialization in order to use custom settings!
*/
2022-08-14 13:05:59 +02:00
// void addToConfig(JsonObject& root)
// {
// JsonObject top = root.createNestedObject("RTC");
// JsonArray pins = top.createNestedArray("pin");
// pins.add(i2c_scl);
// pins.add(i2c_sda);
// }
2022-02-01 09:33:57 +01:00
uint16_t getId()
{
return USERMOD_ID_RTC;
}
};