SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
SensorRtcHelper.cpp
Go to the documentation of this file.
1
30#include "../SensorBuildOpt.h"
31#if !SENSORLIB_EXCLUDE_RTC
32
33#include "SensorRtcHelper.hpp"
34
35SensorRtcHelper::DriverCreator SensorRtcHelper::driverCreators[SensorRtcHelper::driverCreatorMaxNum] = {
36 nullptr,
37#if !SENSORLIB_EXCLUDE_PCF85063
38 []() -> std::unique_ptr<SensorRTC> { return std::make_unique<SensorPCF85063>(); },
39#else
40 nullptr,
41#endif
42#if !SENSORLIB_EXCLUDE_PCF8563
43 []() -> std::unique_ptr<SensorRTC> { return std::make_unique<SensorPCF8563>(); },
44#else
45 nullptr,
46#endif
47};
48
50{
51
52}
53
57
58
60{
61 _drvType = model;
62}
63
64#if defined(ARDUINO)
65bool SensorRtcHelper::begin(TwoWire &wire, int sda, int scl)
66{
67 bool success = false;
68 for (int i = (_drvType == RtcDrv_UNKNOWN) ? RtcDrv_PCF85063 : _drvType; i < driverCreatorMaxNum; ++i) {
69 _drv = createDriver(static_cast<SensorRTCType>(i));
70
71 if (_drv && _drv->begin(wire, sda, scl)) {
72 _drvType = static_cast<SensorRTCType>(i);
73 success = true;
74 break;
75 }
76 }
77 if (!success) {
78 _drvType = RtcDrv_UNKNOWN;
79 }
80 return success;
81}
82
83#elif defined(ESP_PLATFORM)
84
85#if defined(SENSORLIB_USE_I2C_LEGACY)
86bool SensorRtcHelper::begin(i2c_port_t port_num, int sda, int scl)
87{
88 bool success = false;
89 for (int i = (_drvType == RtcDrv_UNKNOWN) ? RtcDrv_PCF85063 : _drvType; i < driverCreatorMaxNum; ++i) {
90 _drv = createDriver(static_cast<SensorRTCType>(i));
91
92 if (_drv && _drv->begin(port_num, sda, scl)) {
93 _drvType = static_cast<SensorRTCType>(i);
94 success = true;
95 break;
96 }
97 }
98 if (!success) {
99 _drvType = RtcDrv_UNKNOWN;
100 }
101 return success;
102}
103
104#else
105
106bool SensorRtcHelper::begin(i2c_master_bus_handle_t handle)
107{
108 bool success = false;
109 for (int i = (_drvType == RtcDrv_UNKNOWN) ? RtcDrv_PCF85063 : _drvType; i < driverCreatorMaxNum; ++i) {
110 _drv = createDriver(static_cast<SensorRTCType>(i));
111
112 if (_drv && _drv->begin(handle)) {
113 _drvType = static_cast<SensorRTCType>(i);
114 success = true;
115 break;
116 }
117 }
118 if (!success) {
119 _drvType = RtcDrv_UNKNOWN;
120 }
121 return success;
122}
123
124#endif // ESP_PLATFORM
125#endif // ARDUINO
126
128{
129 bool success = false;
130 for (int i = (_drvType == RtcDrv_UNKNOWN) ? RtcDrv_PCF85063 : _drvType; i < driverCreatorMaxNum; ++i) {
131 _drv = createDriver(static_cast<SensorRTCType>(i));
132 if (_drv && _drv->begin(callback)) {
133 _drvType = static_cast<SensorRTCType>(i);
134 success = true;
135 break;
136 }
137 }
138 if (!success) {
139 _drvType = RtcDrv_UNKNOWN;
140 }
141 return success;
142}
143
145{
146 _drv->setDateTime(datetime);
147}
148
150{
151 return _drv->getDateTime();
152}
153
155{
156 return _drv->getChipName();
157}
158
159std::unique_ptr<SensorRTC> SensorRtcHelper::createDriver(SensorRTCType type)
160{
161 if (type < sizeof(driverCreators) / sizeof(driverCreators[0])) {
162 if (driverCreators[type]) {
163 return driverCreators[type]();
164 }
165 }
166 return nullptr;
167}
168
169#endif
bool(*)(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite) CustomCallback
const char * getChipName()
Get the name of the RTC chip.
void setDateTime(RTC_DateTime datetime)
Set the date and time on the RTC device.
RTC_DateTime getDateTime()
Get the current date and time from the RTC device.
bool begin(SensorCommCustom::CustomCallback callback)
Initialize the RTC driver using a custom callback.
void setRtcDrvModel(SensorRTCType model)
Set the model of the RTC (real-time clock) driver.
uint8_t i
@license MIT License
SensorRTCType
Enumeration of different RTC (Real-Time Clock) driver types.
@ RtcDrv_PCF85063