SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
time/SensorRTC_POSIX.hpp
Go to the documentation of this file.
1
30#pragma once
31
32#include "../SensorBuildOpt.h"
33#if !SENSORLIB_EXCLUDE_RTC
34
35#include "SensorRTC.h"
36#include "SensorPlatform.hpp"
37
41#if defined(ARDUINO_ARCH_ESP32) || defined(CONFIG_IDF_TARGET)
42
49class SensorRTC_POSIX : public SensorRTC
50{
51public:
56
61
68 SensorRTC_POSIX ()
69 {
70 }
71
77 ~SensorRTC_POSIX ()
78 {
79 }
80
81#if defined(ARDUINO)
93 bool begin(TwoWire &wire, int sda, int scl)
94 {
95 return true;
96 }
97#endif //ARDUINO
98
109 {
110 return true;
111 }
112
113
122 void setDateTime(RTC_DateTime datetime)
123 {
124 struct tm tm_time;
125 struct timespec ts;
126 memset(&tm_time, 0, sizeof(struct tm));
127 tm_time.tm_year = datetime.getYear() - 1900;
128 tm_time.tm_mon = datetime.getMonth() - 1;
129 tm_time.tm_mday = datetime.getDay();
130 tm_time.tm_hour = datetime.getHour();
131 tm_time.tm_min = datetime.getMinute();
132 tm_time.tm_sec = datetime.getSecond();
133
134 time_t epoch_seconds = mktime(&tm_time);
135 if (epoch_seconds == (time_t) -1) {
136 SENSORLIB_LOG_E("Invalid date and time");
137 return;
138 }
139 ts.tv_sec = epoch_seconds;
140 ts.tv_nsec = 0;
141 if (clock_settime(CLOCK_REALTIME, &ts) == -1) {
142 SENSORLIB_LOG_E("set system time failed");
143 }
144 }
145
155 {
156 time_t t = time(NULL);
157 struct tm *local_tm = localtime(&t);
158 return RTC_DateTime(local_tm->tm_year + 1900, local_tm->tm_mon + 1, local_tm->tm_mday,
159 local_tm->tm_hour, local_tm->tm_min, local_tm->tm_sec, local_tm->tm_wday);
160 }
161
169 const char *getChipName()
170 {
171 return "PosixRTC";
172 }
173};
174
175#endif
176
177
178#endif
#define SENSORLIB_LOG_E(...)
@license MIT License
@license MIT License
uint8_t getMinute() const
Definition SensorRTC.h:149
uint8_t getMonth() const
Definition SensorRTC.h:146
uint8_t getDay() const
Definition SensorRTC.h:147
uint16_t getYear() const
Definition SensorRTC.h:145
uint8_t getHour() const
Definition SensorRTC.h:148
uint8_t getSecond() const
Definition SensorRTC.h:150
bool(*)(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite) CustomCallback
virtual const char * getChipName()=0
Get the name of the RTC chip.
virtual void setDateTime(RTC_DateTime datetime)=0
Set the date and time of the RTC using an RTC_DateTime object.
virtual bool begin(SensorCommCustom::CustomCallback callback)=0
Initialize the RTC device using a custom callback.
virtual RTC_DateTime getDateTime()=0
Get the current date and time from the RTC.