SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
sensor_pcf8563.cpp
Go to the documentation of this file.
1
30#include "sdkconfig.h"
31#include "esp_log.h"
32#include "esp_err.h"
33#include "i2c_driver.h"
34#include "freertos/FreeRTOS.h"
35
36#ifdef CONFIG_PCF8563
37
38#include "RtcDrv.hpp"
39
40static const char *TAG = "RTC";
41
43
44static bool init_done = false;
45
46esp_err_t pcf8563_init()
47{
48 ESP_LOGI(TAG, "----DRIVER PCF8563 ----");
49
50 //* Implemented using read and write callback methods, applicable to other platforms
51#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
52
53 uint8_t address = 0x51;
54
55 // * Provide the device address to the callback function
56 i2c_drv_device_init(address);
57
58 ESP_LOGI(TAG, "Implemented using read and write callback methods");
60 ESP_LOGI(TAG, "Initializing PCF8563 real-time clock successfully!");
61 } else {
62 ESP_LOGE(TAG, "Failed to initialize PCF8563 real time clock!");
63 return ESP_FAIL;
64 }
65#endif
66
67 //* Use the built-in esp-idf communication method
68#if CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
69#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
70
71 ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use higher version >= 5.0 API)");
72
73 // * Using the new API of esp-idf 5.x, you need to pass the I2C BUS handle,
74 // * which is useful when the bus shares multiple devices.
75 extern i2c_master_bus_handle_t bus_handle;
76
77 if (rtc.begin(bus_handle)) {
78 ESP_LOGI(TAG, "Initializing PCF8563 real-time clock successfully!");
79 } else {
80 ESP_LOGE(TAG, "Failed to initialize PCF8563 real time clock!");
81 return ESP_FAIL;
82 }
83
84#else
85
86 ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use lower version < 5.0 API)");
87 if (rtc.begin((i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM, CONFIG_SENSOR_SDA, CONFIG_SENSOR_SCL)) {
88 ESP_LOGI(TAG, "Initializing PCF8563 real-time clock successfully!");
89 } else {
90 ESP_LOGE(TAG, "Failed to initialize PCF8563 real time clock!");
91 return ESP_FAIL;
92 }
93#endif //ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)
94#endif //CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
95
96 // Unix tm structure sets the time
97 struct tm timeinfo;
98 timeinfo.tm_yday = 2025 - 1900; //Counting starts from 1900, so subtract 1900 here
99 timeinfo.tm_mon = 1 - 1; //Months start at 0, so you need to subtract 1.
100 timeinfo.tm_mday = 17;
101 timeinfo.tm_hour = 4;
102 timeinfo.tm_min = 30;
103 timeinfo.tm_sec = 30;
104 rtc.setDateTime(timeinfo);
105
106 init_done = true;
107
108 return ESP_OK;
109}
110
111void pcf8563_loop()
112{
113 if (!init_done) {
114 return;
115 }
116 char buf[64];
117 struct tm timeinfo;
118 // Get the time C library structure
119 rtc.getDateTime(&timeinfo);
120 // Format the output using the strftime function
121 // For more formats, please refer to :
122 // https://man7.org/linux/man-pages/man3/strftime.3.html
123 size_t written = strftime(buf, 64, "%A, %B %d %Y %H:%M:%S", &timeinfo);
124 if (written != 0) {
125 ESP_LOGI("RTC", "%s", buf);
126 }
127 written = strftime(buf, 64, "%b %d %Y %H:%M:%S", &timeinfo);
128 if (written != 0) {
129 ESP_LOGI("RTC", "%s", buf);
130 }
131 written = strftime(buf, 64, "%A, %d. %B %Y %I:%M%p", &timeinfo);
132 if (written != 0) {
133 ESP_LOGI("RTC", "%s", buf);
134 }
135}
136
137#endif
@license MIT License
void setDateTime(RTC_DateTime datetime) override
Set the RTC date and time.
bool begin(SensorCommCustom::CustomCallback callback)
Initialize device using a custom transport callback.
RTC_DateTime getDateTime() override
Read the RTC date and time.
Driver for the PCF8563 real-time clock (RTC) over I2C.
bool i2c_wr_function(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite)
@license MIT License
SensorPCF85063 rtc
char buf[64]