ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
rx8025t.cpp
Go to the documentation of this file.
1#include "rx8025t.h"
2#include "esphome/core/log.h"
3
4// Datasheet:
5// - https://support.epson.biz/td/api/doc_check.php?dl=app_RX8025T
6
7namespace esphome::rx8025t {
8
9static constexpr uint8_t RX8025T_REG_SEC = 0x00;
10static constexpr uint8_t RX8025T_REG_FLAG = 0x0E;
11static constexpr uint8_t RX8025T_FLAG_VDET = 0x01;
12static constexpr uint8_t RX8025T_FLAG_VLF = 0x02;
13
14ESPHOME_LOG_TAG(TAG, "rx8025t");
15
17 uint8_t flags;
18 if (!this->read_flags_(&flags)) {
19 this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
20 return;
21 }
22
23 if (flags & RX8025T_FLAG_VLF) {
24 ESP_LOGW(TAG, "VLF flag is set - Loss of oscillator detected. Time may be invalid.");
25 }
26}
27
29
31 ESP_LOGCONFIG(TAG, "RX8025T:");
32 LOG_I2C_DEVICE(this);
34}
35
37 if (!this->read_byte(RX8025T_REG_FLAG, flags)) {
38 ESP_LOGE(TAG, "Can't read flag register.");
39 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
40 return false;
41 }
42 return true;
43}
44
46 uint8_t flags;
47 if (!this->read_flags_(&flags)) {
48 return;
49 }
50
51 uint8_t date[7];
52 if (!this->status_comm_result(this->read_bytes(RX8025T_REG_SEC, date, sizeof(date)))) {
53 ESP_LOGE(TAG, "Can't read I2C data.");
54 return;
55 }
56
57 if (flags & RX8025T_FLAG_VLF) {
58 ESP_LOGW(TAG, "VLF flag is set - time data may be invalid, not syncing to system clock.");
59 return;
60 }
61
62 ESPTime rtc_time = time::decode_bcd_calendar<time::EPSON_BCD_CALENDAR>(date);
63 if (!rtc_time.is_valid(/*check_day_of_week=*/true, /*check_day_of_year=*/false)) {
64 ESP_LOGE(TAG, "Invalid RTC time, not syncing to system clock.");
65 return;
66 }
67 ESP_LOGD(TAG, "Read UTC time: %04d-%02d-%02d %02d:%02d:%02d VDET:%s", rtc_time.year, rtc_time.month,
68 rtc_time.day_of_month, rtc_time.hour, rtc_time.minute, rtc_time.second, ONOFF(flags & RX8025T_FLAG_VDET));
69 this->synchronize_epoch_(rtc_time.timestamp);
70}
71
73 auto now = this->utcnow();
74 if (!now.is_valid()) {
75 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC.");
76 return;
77 }
78
79 uint8_t buff[7];
80 time::encode_bcd_calendar<time::EPSON_BCD_CALENDAR>(now, buff);
81 if (!this->write_bytes(RX8025T_REG_SEC, buff, sizeof(buff))) {
82 ESP_LOGE(TAG, "Can't write I2C data.");
83 this->status_set_warning(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
84 return;
85 }
86 ESP_LOGD(TAG, "Wrote UTC time: %04d-%02d-%02d %02d:%02d:%02d", now.year, now.month, now.day_of_month, now.hour,
88
89 // Clear VLF and VDET flags via read-modify-write of flag register
90 uint8_t flags;
91 if (!this->read_flags_(&flags)) {
92 return;
93 }
94 flags &= ~(RX8025T_FLAG_VLF | RX8025T_FLAG_VDET);
95 this->status_comm_result(this->write_byte(RX8025T_REG_FLAG, flags));
96}
97
98} // namespace esphome::rx8025t
void mark_failed()
Mark this component as failed.
bool status_comm_result(bool ok)
Clears the warning when ok, else sets it with message (default "Communication failed"); returns ok.
bool write_byte(uint8_t a_register, uint8_t data) const
Definition i2c.h:312
bool read_byte(uint8_t a_register, uint8_t *data)
Definition i2c.h:272
bool write_bytes(uint8_t a_register, const uint8_t *data, uint8_t len) const
Definition i2c.h:298
bool read_bytes(uint8_t a_register, uint8_t *data, uint8_t len)
Compat APIs All methods below have been added for compatibility reasons.
Definition i2c.h:249
bool read_flags_(uint8_t *flags)
Definition rx8025t.cpp:36
ESPTime now()
Get the time in the currently defined timezone.
ESPTime utcnow()
Get the time without any time zone or DST corrections.
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
uint16_t flags
ESPHOME_LOG_TAG(TAG, "rx8025t")
A more user-friendly version of struct tm from time.h.
Definition time.h:23
uint8_t minute
minutes after the hour [0-59]
Definition time.h:32
uint8_t second
seconds after the minute [0-60]
Definition time.h:30
uint8_t hour
hours since midnight [0-23]
Definition time.h:34
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:48
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= 2019 and the requested fields are in range).
Definition time.h:82
uint8_t day_of_month
day of the month [1-31]
Definition time.h:38
uint16_t year
year
Definition time.h:44
uint8_t month
month; january=1 [1-12]
Definition time.h:42