AlteriomPainlessMesh v2.1.1
Easy ESP32/ESP8266 mesh networking with Alteriom IoT extensions
Loading...
Searching...
No Matches
rtc.hpp
Go to the documentation of this file.
1#ifndef _PAINLESS_MESH_RTC_HPP_
2#define _PAINLESS_MESH_RTC_HPP_
3
4#include "Arduino.h"
6
8
9namespace painlessmesh {
10namespace rtc {
11
23
31 public:
32 virtual ~RTCInterface() {}
33
39 virtual bool begin() = 0;
40
46 virtual bool isAvailable() = 0;
47
53 virtual uint32_t getUnixTime() = 0;
54
61 virtual bool setUnixTime(uint32_t timestamp) = 0;
62
68 virtual RTCType getType() = 0;
69};
70
77 public:
78 RTCManager() : rtcInterface(nullptr), rtcEnabled(false), lastSyncTime(0) {}
79
86 bool enable(RTCInterface* interface) {
87 if (!interface) {
88 Log(logger::ERROR, "RTCManager::enable() - NULL interface provided\n");
89 return false;
90 }
91
92 rtcInterface = interface;
93
94 if (!rtcInterface->begin()) {
95 Log(logger::ERROR, "RTCManager::enable() - RTC initialization failed\n");
96 rtcInterface = nullptr;
97 return false;
98 }
99
100 if (!rtcInterface->isAvailable()) {
101 Log(logger::ERROR, "RTCManager::enable() - RTC not available\n");
102 rtcInterface = nullptr;
103 return false;
104 }
105
106 rtcEnabled = true;
107 Log(logger::GENERAL, "RTCManager::enable() - RTC type %d enabled successfully\n",
108 rtcInterface->getType());
109 return true;
110 }
111
115 void disable() {
116 rtcEnabled = false;
117 rtcInterface = nullptr;
118 Log(logger::GENERAL, "RTCManager::disable() - RTC disabled\n");
119 }
120
126 bool isEnabled() const {
127 return rtcEnabled && rtcInterface != nullptr && rtcInterface->isAvailable();
128 }
129
135 uint32_t getTime() {
136 if (!isEnabled()) {
137 return 0;
138 }
139 return rtcInterface->getUnixTime();
140 }
141
148 bool syncFromNTP(uint32_t ntpTimestamp) {
149 if (!isEnabled()) {
150 Log(logger::ERROR, "RTCManager::syncFromNTP() - RTC not enabled\n");
151 return false;
152 }
153
154 if (ntpTimestamp == 0) {
155 Log(logger::ERROR, "RTCManager::syncFromNTP() - Invalid timestamp\n");
156 return false;
157 }
158
159 if (!rtcInterface->setUnixTime(ntpTimestamp)) {
160 Log(logger::ERROR, "RTCManager::syncFromNTP() - Failed to set RTC time\n");
161 return false;
162 }
163
164 lastSyncTime = millis();
165 Log(logger::GENERAL, "RTCManager::syncFromNTP() - RTC synced to %u\n",
166 ntpTimestamp);
167 return true;
168 }
169
175 uint32_t getTimeSinceLastSync() const {
176 if (lastSyncTime == 0) {
177 return 0;
178 }
179 return millis() - lastSyncTime;
180 }
181
187 RTCType getType() const {
188 if (!isEnabled()) {
189 return RTC_NONE;
190 }
191 return rtcInterface->getType();
192 }
193
194 private:
195 RTCInterface* rtcInterface = nullptr;
196 bool rtcEnabled = false;
197 uint32_t lastSyncTime = 0;
198};
199
200} // namespace rtc
201} // namespace painlessmesh
202
203#endif // _PAINLESS_MESH_RTC_HPP_
Definition logger.hpp:46
Definition rtc.hpp:30
virtual ~RTCInterface()
Definition rtc.hpp:32
virtual uint32_t getUnixTime()=0
virtual bool setUnixTime(uint32_t timestamp)=0
virtual RTCType getType()=0
Definition rtc.hpp:76
bool enable(RTCInterface *interface)
Definition rtc.hpp:86
bool syncFromNTP(uint32_t ntpTimestamp)
Definition rtc.hpp:148
RTCManager()
Definition rtc.hpp:78
uint32_t getTime()
Definition rtc.hpp:135
void disable()
Definition rtc.hpp:115
RTCType getType() const
Definition rtc.hpp:187
bool isEnabled() const
Definition rtc.hpp:126
uint32_t getTimeSinceLastSync() const
Definition rtc.hpp:175
@ GENERAL
Definition logger.hpp:26
@ ERROR
Definition logger.hpp:19
RTCType
Definition rtc.hpp:15
@ RTC_DS3231
Definition rtc.hpp:17
@ RTC_PCF8523
Definition rtc.hpp:19
@ RTC_NONE
Definition rtc.hpp:16
@ RTC_DS1307
Definition rtc.hpp:18
@ RTC_PCF8563
Definition rtc.hpp:20
@ RTC_ESP32_INTERNAL
Definition rtc.hpp:21
Definition wifi.hpp:34
painlessmesh::logger::LogClass Log
Definition wifi.cpp:2
painlessmesh::logger::LogClass Log
Definition wifi.cpp:2