ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
rx8111.cpp
Go to the documentation of this file.
1#include "rx8111.h"
3#include "esphome/core/log.h"
5#ifdef USE_RX8111_TIMESTAMP
6#include <cstdio>
7#include <cstring>
8#endif
9
10namespace esphome::rx8111 {
11
12ESPHOME_LOG_TAG(TAG, "rx8111");
13
14// Indexed by the TSEL value stored in timer_clock_
15PROGMEM_STRING_TABLE(TimerClockNames, "4096 Hz", "64 Hz", "1 Hz", "1/60 Hz");
16PROGMEM_STRING_TABLE(FoutNames, "32768 Hz", "1024 Hz", "1 Hz", "off");
17PROGMEM_STRING_TABLE(EvinPullNames, "none", "pullup_500k", "pullup_1m", "pullup_10m", "pulldown_500k");
18#ifdef USE_BINARY_SENSOR
19PROGMEM_STRING_TABLE(StatusSensorNames, "Voltage low flag", "Alarm flag", "Timer flag", "Oscillation stop flag",
20 "Event flag", "Battery low", "EVIN");
21#endif
22#ifdef USE_RX8111_TIMESTAMP
23PROGMEM_STRING_TABLE(TimestampModeNames, "latest", "stop_when_full", "overwrite");
24#endif
25PROGMEM_STRING_TABLE(BootFailureNames, "RX8111 battery configuration failed", "RX8111 FOUT configuration failed",
26 "RX8111 EVIN configuration failed", ESP_LOG_MSG_COMM_FAIL, ESP_LOG_MSG_COMM_FAIL);
27
28static constexpr uint8_t RX8111_REG_SEC = 0x10;
29static constexpr uint8_t RX8111_REG_MIN_ALARM = 0x17;
30static constexpr uint8_t RX8111_REG_EXTENSION = 0x1D;
31static constexpr uint8_t RX8111_REG_FLAG = 0x1E;
32static constexpr uint8_t RX8111_REG_TIMER0 = 0x1A;
33static constexpr uint8_t RX8111_REG_CONTROL = 0x1F;
34static constexpr uint8_t RX8111_REG_SEC_ALARM = 0x2C;
35static constexpr uint8_t RX8111_REG_TIMER_CONTROL = 0x2D;
36static constexpr uint8_t RX8111_REG_POWER_SWITCH = 0x32;
37static constexpr uint8_t RX8111_REG_TIMESTAMP_CONTROL = 0x34;
38#ifdef USE_RX8111_TIMESTAMP
39static constexpr uint8_t RX8111_REG_TIMESTAMP_STATUS = 0x36;
40static constexpr uint8_t RX8111_REG_TIMESTAMP_LATEST = 0x20;
41static constexpr uint8_t RX8111_REG_TIMESTAMP_BUFFER = 0x40;
42static constexpr uint8_t RX8111_TIMESTAMP_RAM = 0x01;
43static constexpr uint8_t RX8111_TIMESTAMP_CLEAR = 0x02;
44static constexpr uint8_t RX8111_TIMESTAMP_EMPTY = 0x08;
45static constexpr uint8_t RX8111_TIMESTAMP_FULL = 0x10;
46static constexpr uint8_t RX8111_EVIN_OVERWRITE = 0x02; // Full buffer overwrites its oldest record
47#endif
48static constexpr uint8_t RX8111_TIMESTAMP_EISEL = 0x04;
49static constexpr uint8_t RX8111_REG_EVIN = 0x2B;
50static constexpr uint8_t RX8111_EXTENSION_ETS = 0x04;
51static constexpr uint8_t RX8111_CONTROL_EIE = 0x04;
52static constexpr uint8_t RX8111_FLAG_EVF = 0x04;
53static constexpr uint8_t RX8111_POWER_CHGEN = 0x80;
54static constexpr uint8_t RX8111_POWER_INIEN = 0x40;
55static constexpr uint8_t RX8111_POWER_SWSEL = 0x0C;
56static constexpr uint8_t RX8111_POWER_VDD_ONLY = 0x08;
57static constexpr uint8_t RX8111_FOUT_FSEL = 0xC0;
58static constexpr uint8_t RX8111_TIMER_TMPIN = 0x02;
59static constexpr uint8_t RX8111_TIMER_MODE = 0x0F; // TBKON, TBKE, TMPIN, TSTP
60static constexpr uint8_t RX8111_ALARM_DISABLED = 0x80;
61static constexpr uint8_t RX8111_EXTENSION_WADA = 0x08;
62static constexpr uint8_t RX8111_CONTROL_AIE = 0x08;
63static constexpr uint8_t RX8111_CONTROL_TIE = 0x10;
64static constexpr uint8_t RX8111_EXTENSION_TE = 0x10;
65static constexpr uint8_t RX8111_EXTENSION_TSEL = 0x03;
66static constexpr uint8_t RX8111_FLAG_TF = 0x10;
67static constexpr uint8_t RX8111_FLAG_AF = 0x08;
68static constexpr uint8_t RX8111_FLAG_POR = 0x80;
69static constexpr uint8_t RX8111_FLAG_VLF = 0x02;
70static constexpr uint8_t RX8111_FLAG_XST = 0x01;
71#ifdef USE_BINARY_SENSOR
72static constexpr uint8_t RX8111_REG_STATUS = 0x33;
73static constexpr uint8_t RX8111_STATUS_VLOW = 0x02;
74static constexpr uint8_t RX8111_STATUS_EVIN = 0x40;
75static constexpr uint8_t STATUS_SENSOR_MASKS[] PROGMEM = {
76 RX8111_FLAG_VLF, RX8111_FLAG_AF, RX8111_FLAG_TF, RX8111_FLAG_XST,
77 RX8111_FLAG_EVF, RX8111_STATUS_VLOW, RX8111_STATUS_EVIN,
78};
79static_assert(sizeof(STATUS_SENSOR_MASKS) == STATUS_SENSOR_COUNT);
80#endif
81static constexpr uint8_t RX8111_CTRL_STOP = 0x01;
82// FLAG bits are write-zero-to-clear; keep UF/TF/AF/EVF even if they latch during the write.
83static constexpr uint8_t RX8111_KEEP_EVENT_FLAGS = 0x3C;
84static constexpr uint32_t RX8111_EVENT_INTERVAL_ID = 0;
85static constexpr uint32_t RX8111_BOOT_ALARM_RETRY_ID = 1;
86static constexpr uint32_t RX8111_BOOT_TIMER_RETRY_ID = 2;
87static constexpr uint32_t RX8111_BOOT_BATTERY_RETRY_ID = 3;
88static constexpr uint32_t RX8111_BOOT_FOUT_RETRY_ID = 4;
89static constexpr uint32_t RX8111_BOOT_EVENT_RETRY_ID = 5;
90static constexpr uint32_t RX8111_DEFERRED_UPDATE_ID = 6;
91static constexpr uint32_t RX8111_DEFERRED_STATUS_ID = 7;
92static constexpr uint8_t RX8111_BOOT_MAX_ATTEMPTS = 3;
93
94// Masks a 7-byte Epson calendar in place and checks its BCD digits; the one-hot weekday byte is skipped.
95static bool mask_bcd_calendar(uint8_t *calendar) {
96 static constexpr uint8_t MASKS[7] PROGMEM = {0x7F, 0x7F, 0x3F, 0x00, 0x3F, 0x1F, 0xFF};
97 bool valid = true;
98 for (size_t i = 0; i < sizeof(MASKS); i++) {
99 if (i == 3)
100 continue;
101 calendar[i] &= progmem_read_byte(&MASKS[i]);
102 valid = valid && (calendar[i] & 0x0F) <= 9 && (calendar[i] >> 4) <= 9;
103 }
104 return valid;
105}
106
108 this->update_running_ = true;
109 if (this->battery_configured_)
111 if (this->fout_configured_)
112 this->configure_boot_fout_();
113#ifdef USE_RX8111_TIMESTAMP
114 // Retained records are delivered after the alarm and timer are set up, so callbacks that change them win
115 this->defer_timestamps_ = true;
116#endif
117 if (this->event_configured_)
118 this->configure_boot_event_();
119 this->configure_boot_alarm_();
120 this->configure_boot_timer_();
121#ifdef USE_RX8111_TIMESTAMP
122 this->defer_timestamps_ = false;
123 // A failed attempt keeps its staged records for the retries, which deliver them
124 if (!this->event_boot_pending_)
126#endif
128 this->read_time_(flags);
129#ifdef USE_BINARY_SENSOR
130 // Publish initial status even when periodic calendar synchronization is disabled.
131 if (this->has_status_sensor_())
132 this->check_event_flags_(flags);
133#endif
134 if (this->watching_()) {
135 this->start_event_polling_();
136 }
137 this->update_running_ = false;
138}
139
141 bool success;
142#ifdef USE_RX8111_TIMESTAMP
143 if (this->timestamp_sensor_ != nullptr) {
144 // Preserve startup validity and event evidence before any subsequent bus operation can fail.
145 if (!this->retained_flags_seen_ && this->read_byte(RX8111_REG_FLAG, &this->retained_flags_))
146 this->retained_flags_seen_ = true;
147 uint8_t control;
148 success = this->retained_flags_seen_ && this->read_byte(RX8111_REG_TIMESTAMP_CONTROL, &control) &&
149 this->publish_timestamps_(this->retained_flags_, (control & RX8111_TIMESTAMP_RAM) != 0, true);
150 } else
151#endif
152 {
153 success = this->configure_event_();
154 }
155 this->event_boot_pending_ = !success;
156 if (success && this->watching_())
157 this->start_event_polling_();
158 this->boot_config_result_(BOOT_EVENT, success);
159#ifdef USE_RX8111_TIMESTAMP
160 if (!success && this->timestamp_sensor_ != nullptr && this->boot_attempts_[BOOT_EVENT] >= RX8111_BOOT_MAX_ATTEMPTS)
162#endif
163}
164
166#ifdef USE_RX8111_TIMESTAMP
167 if (this->timestamp_sensor_ != nullptr) {
168 // Pause capture while changing storage. Enable it last so a retry cannot erase new records.
169 uint8_t control;
170 if (!this->update_register(RX8111_REG_EXTENSION, RX8111_EXTENSION_ETS, 0) ||
171 !this->read_byte(RX8111_REG_TIMESTAMP_CONTROL, &control))
172 return false;
173 const bool buffered = this->timestamp_mode_ != TIMESTAMP_MODE_LATEST;
174 const uint8_t mode = buffered ? RX8111_TIMESTAMP_RAM : 0;
175 // A previously unused RAM area has no meaningful occupancy pointer.
176 if (buffered && !(control & RX8111_TIMESTAMP_RAM))
177 control |= RX8111_TIMESTAMP_CLEAR; // TSCLR self-clears; ETS must be off.
178 control = (control & ~(RX8111_TIMESTAMP_RAM | RX8111_TIMESTAMP_EISEL)) | mode;
179 const uint8_t overwrite = this->timestamp_mode_ == TIMESTAMP_MODE_OVERWRITE ? RX8111_EVIN_OVERWRITE : 0;
180 return this->update_register(RX8111_REG_EVIN, 0xFE, this->evin_settings_ | overwrite) &&
181 this->write_byte(RX8111_REG_TIMESTAMP_CONTROL, control) &&
182 this->update_register(RX8111_REG_CONTROL, RX8111_CONTROL_EIE,
183 this->event_enabled_ ? RX8111_CONTROL_EIE : 0) &&
184 this->update_register(RX8111_REG_EXTENSION, RX8111_EXTENSION_ETS, RX8111_EXTENSION_ETS);
185 }
186#endif
187 // An explicit disable affects only /INT; leave retained capture settings untouched.
188 if (!this->event_enabled_)
189 return this->update_register(RX8111_REG_CONTROL, RX8111_CONTROL_EIE, 0);
190 // ETS gates EVIN detection, not just timestamp publication. Preserve all other
191 // extension bits, including FOUT, alarm and timer settings, and retained records.
192 return this->update_register(RX8111_REG_EVIN, 0xFC, this->evin_settings_ & 0xFC) &&
193 this->update_register(RX8111_REG_TIMESTAMP_CONTROL, RX8111_TIMESTAMP_EISEL, 0) &&
194 this->update_register(RX8111_REG_EXTENSION, RX8111_EXTENSION_ETS, RX8111_EXTENSION_ETS) &&
195 this->update_register(RX8111_REG_CONTROL, RX8111_CONTROL_EIE, RX8111_CONTROL_EIE);
196}
197
199 // FLAG is write-zero-to-clear; ones preserve other events, even if they latch during the write.
200 if (!this->write_byte(RX8111_REG_FLAG, static_cast<uint8_t>(~RX8111_FLAG_EVF))) {
201 this->set_warning_(WARN_EVENT_FLAG_CLEAR, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
202 return false;
203 }
204 this->event_flag_seen_ = false;
206 return true;
207}
208
210 // Own SWSEL as well as INIEN/CHGEN: 10 selects VDD when automatic switching is disabled
211 // and is legal when enabled (unlike 11). Preserve SMPT and reserved bits.
212 const uint8_t value = RX8111_POWER_VDD_ONLY | (this->battery_backup_ ? RX8111_POWER_INIEN : 0) |
213 (this->battery_backup_ && this->battery_charging_ ? RX8111_POWER_CHGEN : 0);
215 this->update_register(RX8111_REG_POWER_SWITCH,
216 RX8111_POWER_INIEN | RX8111_POWER_CHGEN | RX8111_POWER_SWSEL, value));
217}
218
220 // TMPIN routes timer interrupts to FOUT instead of /INT; do not silently change that routing.
221 uint8_t timer_control;
222 bool success = this->read_byte(RX8111_REG_TIMER_CONTROL, &timer_control);
223 if (success && this->fout_frequency_ != 3 && (timer_control & RX8111_TIMER_TMPIN)) {
224 ESP_LOGW(TAG, "FOUT is routed to the wake-up timer (TMPIN); cannot configure clock output");
225 success = false;
226 } else if (success) {
227 success =
228 this->update_register(RX8111_REG_EXTENSION, RX8111_FOUT_FSEL, static_cast<uint8_t>(this->fout_frequency_ << 6));
229 }
230 this->boot_config_result_(BOOT_FOUT, success);
231}
232
233// A timer start clears TMPIN, which may be what failed FOUT; give it a fresh set of attempts.
235 if (!this->fout_configured_ || (this->warnings_ & WARN_FOUT) == 0)
236 return;
237 this->cancel_timeout(RX8111_BOOT_FOUT_RETRY_ID);
238 this->boot_attempts_[BOOT_FOUT] = 0;
239 this->configure_boot_fout_();
240}
241
243 struct BootConfig {
244 uint32_t retry_id;
245 void (RX8111Component::*configure)();
246 uint16_t warning;
247 };
248 static constexpr BootConfig CONFIGS[BOOT_FEATURE_COUNT] PROGMEM = {
249 {RX8111_BOOT_BATTERY_RETRY_ID, &RX8111Component::configure_boot_battery_, WARN_BATTERY},
250 {RX8111_BOOT_FOUT_RETRY_ID, &RX8111Component::configure_boot_fout_, WARN_FOUT},
251 {RX8111_BOOT_EVENT_RETRY_ID, &RX8111Component::configure_boot_event_, WARN_EVENT},
252 {RX8111_BOOT_ALARM_RETRY_ID, &RX8111Component::configure_boot_alarm_, WARN_ALARM},
253 {RX8111_BOOT_TIMER_RETRY_ID, &RX8111Component::configure_boot_timer_, WARN_TIMER},
254 };
255 BootConfig config;
256 progmem_memcpy(&config, &CONFIGS[feature], sizeof(config));
257 const auto attempts = ++this->boot_attempts_[feature];
258 if (success) {
259 this->clear_warning_(config.warning);
260 return;
261 }
262 this->set_warning_(config.warning, BootFailureNames::get_log_str(feature));
263 if (attempts < RX8111_BOOT_MAX_ATTEMPTS) {
264 // Capture the feature, not the 8-byte member pointer, so the callback fits std::function's inline buffer
265 this->set_timeout(config.retry_id, this->boot_retry_delay_ms_, [this, feature]() {
266 BootConfig retry;
267 progmem_memcpy(&retry, &CONFIGS[feature], sizeof(retry));
268 (this->*retry.configure)();
269 });
270 }
271}
272
274 bool success = true;
275 if (this->alarm_enabled_) {
278 this->alarm_day_ != ALARM_FIELD_UNSET) {
279 success = this->program_alarm_(this->alarm_second_, this->alarm_minute_, this->alarm_hour_, this->alarm_weekday_,
280 this->alarm_day_, true);
281 if (success) {
282 // Only the first successful boot program can introduce a boot wake-up edge.
283 if (!this->boot_alarm_event_seen_)
284 this->alarm_flag_seen_ = false;
285 this->start_event_polling_();
286 }
287 } else {
288 // Preserve an alarm programmed by an action before a reboot or deep sleep.
289 uint8_t control;
290 success = this->read_byte(RX8111_REG_CONTROL, &control);
291 if (success) {
292 this->alarm_armed_ = (control & RX8111_CONTROL_AIE) != 0;
293 if (this->alarm_armed_)
294 this->start_event_polling_();
295 }
296 }
297 } else if (this->alarm_explicitly_configured_) {
298 success = this->set_alarm_interrupt_(false);
299 }
300 this->boot_config_result_(BOOT_ALARM, success);
301}
302
304 bool success = true;
305 // An omitted timer setting leaves battery-backed state alone; explicit false stops it.
306 if (this->timer_configured_) {
307 const bool start_timer = this->timer_enabled_;
308 if (start_timer) {
309 success = this->configure_timer_();
310 } else {
311 uint8_t extension, control;
312 success = this->disable_timer_(extension, control);
313 }
314 if (success && !start_timer && this->timer_count_ != 0) {
316 this->start_event_polling_();
317 }
318 if (success && start_timer)
320 }
321 this->boot_config_result_(BOOT_TIMER, success);
322}
323
324// A runtime action supersedes pending boot programming of the same feature
326 this->cancel_timeout(alarm ? RX8111_BOOT_ALARM_RETRY_ID : RX8111_BOOT_TIMER_RETRY_ID);
327}
328
330 if (this->poll_running_) {
331 if (!this->deferred_update_running_)
332 this->defer(RX8111_DEFERRED_UPDATE_ID, [this]() { this->run_deferred_update_(false); });
333 return;
334 }
335 // A normal poll also satisfies a refresh requested during time synchronization.
336 this->cancel_defer(RX8111_DEFERRED_STATUS_ID);
337 this->poll_running_ = true;
338 if (this->watching_())
339 this->poll_event_flags_(flags);
340 this->poll_running_ = false;
341 // A callback or a runtime action may have stopped the last watched feature
342 if (!this->watching_() && this->event_polling_started_) {
343 this->cancel_interval(RX8111_EVENT_INTERVAL_ID);
344 this->event_polling_started_ = false;
345 }
346}
347
349 if (this->event_polling_started_)
350 return;
351 this->set_interval(RX8111_EVENT_INTERVAL_ID, this->event_poll_interval_, [this]() { this->check_event_flags_(); });
352 this->event_polling_started_ = true;
353}
354
356 // One follow-up satisfies synchronous callback requests; its own callbacks
357 // must not perpetually queue more work, even if they keep changing the flags.
358 if (this->deferred_update_running_ && (this->update_running_ || this->poll_running_ || this->sync_running_))
359 return;
360 // Synchronizing again would retrigger the same on_time_sync automation forever.
361 // Keep its diagnostic refresh, without starting another calendar synchronization.
362 if (this->sync_running_) {
363 this->defer(RX8111_DEFERRED_STATUS_ID, [this]() { this->run_deferred_update_(true); });
364 return;
365 }
366 // Entity callbacks may request another update synchronously.
367 // Coalesce them without interleaving snapshots or recursing on the stack.
368 if (this->update_running_ || this->poll_running_) {
369 this->defer(RX8111_DEFERRED_UPDATE_ID, [this]() { this->run_deferred_update_(false); });
370 return;
371 }
372 this->update_running_ = true;
373 this->cancel_defer(RX8111_DEFERRED_UPDATE_ID);
375 this->read_time_(flags);
376 this->check_event_flags_(flags);
377 this->update_running_ = false;
378}
379
381 this->deferred_update_running_ = true;
382 if (status_only) {
383 this->check_event_flags_();
384 } else {
385 this->update();
386 }
387 this->deferred_update_running_ = false;
388}
389
390void RX8111Component::set_warning_(uint16_t bit, const LogString *message) {
391 this->warnings_ |= bit;
392 this->status_set_warning(message);
393}
394
396 if ((this->warnings_ & bit) == 0)
397 return;
398 this->warnings_ &= ~bit;
399 if (this->warnings_ == 0)
400 this->status_clear_warning();
401}
402
404 ESP_LOGCONFIG(TAG, "RX8111:");
405 LOG_I2C_DEVICE(this);
406#ifdef USE_BINARY_SENSOR
407 for (uint8_t i = 0; i < STATUS_SENSOR_COUNT; i++) {
408 if (this->status_sensors_[i] != nullptr) {
409 log_binary_sensor(TAG, " ", LOG_STR_ARG(StatusSensorNames::get_log_str(i)), this->status_sensors_[i]);
410 }
411 }
412#endif
413#ifdef USE_RX8111_TIMESTAMP
414 LOG_TEXT_SENSOR(" ", "Event timestamp", this->timestamp_sensor_);
415 if (this->timestamp_sensor_ != nullptr) {
416 ESP_LOGCONFIG(TAG, " Timestamp record mode: %s",
417 LOG_STR_ARG(TimestampModeNames::get_log_str(this->timestamp_mode_)));
418 }
419#endif
420 if (this->battery_configured_) {
421 ESP_LOGCONFIG(TAG, " Battery backup: %s, charging: %s", ONOFF(this->battery_backup_),
422 ONOFF(this->battery_charging_));
423 }
424 if (this->fout_configured_) {
425 ESP_LOGCONFIG(TAG, " FOUT: %s", LOG_STR_ARG(FoutNames::get_log_str(this->fout_frequency_)));
426 }
427 if (this->event_configured_) {
428 ESP_LOGCONFIG(TAG, " EVIN interrupt: %s", ONOFF(this->event_enabled_));
429 bool evin_used = this->event_enabled_;
430#ifdef USE_RX8111_TIMESTAMP
431 evin_used = evin_used || this->timestamp_sensor_ != nullptr;
432#endif
433 if (evin_used) {
434 ESP_LOGCONFIG(TAG, " EVIN: %s, filter: %u, pull: %s",
435 (this->evin_settings_ & 0x80) ? LOG_STR_LITERAL("high") : LOG_STR_LITERAL("low"),
436 (this->evin_settings_ >> 5) & 3,
437 LOG_STR_ARG(EvinPullNames::get_log_str((this->evin_settings_ >> 2) & 7)));
438 }
439 }
440 ESP_LOGCONFIG(TAG,
441 " Alarm interrupt: %s\n"
442 " Wake-up timer: %s",
443 ONOFF(this->alarm_enabled_), ONOFF(this->timer_enabled_));
444 if (this->timer_enabled_) {
445 ESP_LOGCONFIG(TAG, " Count: %" PRIu32 ", clock: %s", this->timer_count_,
446 LOG_STR_ARG(TimerClockNames::get_log_str(this->timer_clock_)));
447 }
448 RealTimeClock::dump_config();
449}
450
453 this->read_time_(flags);
454}
455
457 ESPTime rtc_time;
458 if (!this->read_calendar_(rtc_time, &flags))
459 return;
460 ESP_LOGD(TAG, "Read UTC time: %04d-%02d-%02d %02d:%02d:%02d", rtc_time.year, rtc_time.month, rtc_time.day_of_month,
461 rtc_time.hour, rtc_time.minute, rtc_time.second);
462 // Synchronization callbacks can clear flags or program the RTC. Only reuse
463 // the snapshot when no user code will run between the read and the poll.
464 if (!this->time_sync_callback_.empty())
466 const bool was_sync_running = this->sync_running_;
467 this->sync_running_ = true;
469 this->sync_running_ = was_sync_running;
470}
471
473 if (this->time_write_invalid_) {
474 this->set_warning_(WARN_CALENDAR, LOG_STR("RTC write incomplete; write trusted time again"));
475 return false;
476 }
477 uint8_t regs[2]; // Adjacent FLAG and CONTROL registers.
478 if (flags != nullptr)
479 flags->state = FlagSnapshot::FAILED; // Do not repeat a failed shared read in the same update.
480 if (!this->read_bytes(RX8111_REG_FLAG, regs, sizeof(regs))) {
481 this->set_warning_(WARN_CALENDAR, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
482 return false;
483 }
484 if (flags != nullptr)
485 *flags = {FlagSnapshot::VALID, regs[0]};
486 if (regs[1] & RX8111_CTRL_STOP) {
487 this->set_warning_(WARN_CALENDAR, LOG_STR("RTC counter stopped; not synchronizing"));
488 return false;
489 }
490 if (regs[0] & (RX8111_FLAG_VLF | RX8111_FLAG_XST | RX8111_FLAG_POR)) {
491 ESP_LOGW(TAG, "RTC time invalidated by power loss or oscillator stop; write trusted time");
492 this->set_warning_(WARN_CALENDAR, LOG_STR("RTC time invalidated; write trusted time"));
493 return false;
494 }
495
496 uint8_t buf[7];
497 // I2C access freezes the RX8111 counter, yielding a coherent calendar snapshot.
498 if (!this->read_bytes(RX8111_REG_SEC, buf, sizeof(buf))) {
499 this->set_warning_(WARN_CALENDAR, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
500 return false;
501 }
502 // decode_bcd_calendar() masks the same bits; the check here catches a corrupted register
503 bool valid = mask_bcd_calendar(buf);
504 const uint8_t onehot = buf[3];
505 valid = valid && onehot != 0 && onehot <= 0x40 && (onehot & (onehot - 1)) == 0;
506 if (valid) {
507 rtc_time = time::decode_bcd_calendar<time::EPSON_BCD_CALENDAR>(buf);
508 valid = rtc_time.is_valid(true, false) && rtc_time.second < 60 &&
510 }
511 if (!valid) {
512 ESP_LOGW(TAG, "Invalid RTC time, not syncing to system clock");
513 this->set_warning_(WARN_CALENDAR, LOG_STR("Invalid RTC time"));
514 return false;
515 }
517 return true;
518}
519
522 if (!now.is_valid() || now.year > 2099) {
523 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC");
524 return;
525 }
526 if (!this->set_stop_bit_(true)) {
527 this->set_warning_(WARN_WRITE_TIME, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
528 return;
529 }
530
531 uint8_t buf[7];
532 time::encode_bcd_calendar<time::EPSON_BCD_CALENDAR>(now, buf);
533 this->time_write_invalid_ = true;
534 const bool calendar_written = this->write_bytes(RX8111_REG_SEC, buf, sizeof(buf));
535 if (!this->finish_time_write_(calendar_written))
536 return;
537 ESP_LOGD(TAG, "Wrote UTC time: %04d-%02d-%02d %02d:%02d:%02d", now.year, now.month, now.day_of_month, now.hour,
539}
540
541bool RX8111Component::finish_time_write_(bool calendar_written) {
542 // STOP is the battery-backed invalidity marker, including across host resets.
543 // Never release a partial or delayed snapshot; only a fresh trusted write can restart it.
544 if (!calendar_written) {
545 this->set_warning_(WARN_WRITE_TIME, LOG_STR("RTC write incomplete; write trusted time again"));
546 return false;
547 }
548 if (!this->write_byte(RX8111_REG_FLAG, RX8111_KEEP_EVENT_FLAGS) || !this->set_stop_bit_(false)) {
549 this->set_warning_(WARN_WRITE_TIME, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
550 return false;
551 }
552 this->time_write_invalid_ = false;
555 return true;
556}
557
559 return this->update_register(RX8111_REG_CONTROL, RX8111_CTRL_STOP, stop ? RX8111_CTRL_STOP : 0);
560}
561
563 return this->update_register(RX8111_REG_CONTROL, RX8111_CONTROL_AIE, enabled ? RX8111_CONTROL_AIE : 0);
564}
565
566bool RX8111Component::program_alarm_(uint8_t second, uint8_t minute, uint8_t hour, uint8_t weekday, uint8_t day,
567 bool boot) {
568 uint8_t extension;
569 if (!this->read_byte(RX8111_REG_EXTENSION, &extension))
570 return false;
571 if (weekday != ALARM_FIELD_UNSET) {
572 extension &= ~RX8111_EXTENSION_WADA;
573 } else if (day != ALARM_FIELD_UNSET) {
574 extension |= RX8111_EXTENSION_WADA;
575 }
576
577 const uint8_t fields[] = {
578 second == ALARM_FIELD_UNSET ? RX8111_ALARM_DISABLED : uint8_to_bcd(second),
579 minute == ALARM_FIELD_UNSET ? RX8111_ALARM_DISABLED : uint8_to_bcd(minute),
580 hour == ALARM_FIELD_UNSET ? RX8111_ALARM_DISABLED : uint8_to_bcd(hour),
581 weekday != ALARM_FIELD_UNSET ? static_cast<uint8_t>(1U << (weekday - 1))
583 : RX8111_ALARM_DISABLED,
584 };
585 // Disable the interrupt during partial programming; keep AF (past events) latched.
586 if (!this->set_alarm_interrupt_(false))
587 return false;
588 this->alarm_armed_ = false;
589 this->alarm_reprogrammed_ = true;
590 uint8_t flags;
591 if (!this->read_byte(RX8111_REG_FLAG, &flags))
592 return false;
593 if (!boot)
594 this->alarm_flag_seen_ = (flags & RX8111_FLAG_AF) != 0;
595 if (!this->write_byte(RX8111_REG_SEC_ALARM, fields[0]) || !this->write_bytes(RX8111_REG_MIN_ALARM, &fields[1], 3) ||
596 !this->write_byte(RX8111_REG_EXTENSION, extension))
597 return false;
598 if (!this->set_alarm_interrupt_(this->alarm_enabled_))
599 return false;
600 this->alarm_armed_ = true;
601 return true;
602}
603
604bool RX8111Component::set_alarm(uint16_t second, uint16_t minute, uint16_t hour, uint16_t weekday, uint16_t day) {
605 if (!this->alarm_enabled_) {
606 ESP_LOGE(TAG, "Alarm interrupt disabled; set alarm_enabled: true");
607 return false;
608 }
609 constexpr uint16_t unset = ALARM_FIELD_UNSET;
610 if ((second != unset && second > 59) || (minute != unset && minute > 59) || (hour != unset && hour > 23) ||
611 (weekday != unset && (weekday < 1 || weekday > 7)) || (day != unset && (day < 1 || day > 31)) ||
612 (weekday != unset && day != unset) ||
613 (second == unset && minute == unset && hour == unset && weekday == unset && day == unset)) {
614 ESP_LOGE(TAG, "Invalid alarm fields");
615 return false;
616 }
617 this->cancel_boot_retry_(true);
618 if (!this->program_alarm_(second, minute, hour, weekday, day)) {
619 this->set_warning_(WARN_ALARM, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
620 return false;
621 }
623 this->boot_alarm_event_seen_ = true;
624 this->start_event_polling_();
625 return true;
626}
627
629 if (!this->alarm_enabled_ || seconds == 0 || seconds >= 28U * 86400U) {
630 ESP_LOGE(TAG, "Relative alarm requires alarm_enabled and a duration of 1 second to less than 28 days");
631 return false;
632 }
633 ESPTime now;
634 if (!this->read_calendar_(now)) {
635 ESP_LOGE(TAG, "Cannot read valid RTC time for relative alarm");
636 return false;
637 }
638 auto target = ESPTime::from_epoch_utc(now.timestamp + seconds);
639 if (!target.is_valid() || target.year > 2099) {
640 ESP_LOGE(TAG, "Relative alarm target is outside RTC year range");
641 return false;
642 }
643 return this->set_alarm(target.second, target.minute, target.hour, ALARM_FIELD_UNSET, target.day_of_month);
644}
645
647 this->cancel_boot_retry_(true);
648 if (!this->set_alarm_interrupt_(false)) {
649 this->set_warning_(WARN_ALARM, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
650 return false;
651 }
652 this->alarm_armed_ = false;
653 // Minute, hour and day alarm registers are contiguous
654 static constexpr uint8_t DISABLED_FIELDS[3] = {RX8111_ALARM_DISABLED, RX8111_ALARM_DISABLED, RX8111_ALARM_DISABLED};
655 if (!this->write_byte(RX8111_REG_SEC_ALARM, RX8111_ALARM_DISABLED) ||
656 !this->write_bytes(RX8111_REG_MIN_ALARM, DISABLED_FIELDS, sizeof(DISABLED_FIELDS))) {
657 this->set_warning_(WARN_ALARM, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
658 return false;
659 }
661 return true;
662}
663
665 if (!this->write_byte(RX8111_REG_FLAG, static_cast<uint8_t>(~RX8111_FLAG_AF))) {
666 this->set_warning_(WARN_ALARM_FLAG_CLEAR, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
667 return false;
668 }
669 this->alarm_flag_seen_ = false;
671 return true;
672}
673
674// Clears TE and TIE; returns the register values read so callers can build on them
675bool RX8111Component::disable_timer_(uint8_t &extension, uint8_t &control) {
676 return this->read_byte(RX8111_REG_EXTENSION, &extension) &&
677 this->write_byte(RX8111_REG_EXTENSION, extension & ~RX8111_EXTENSION_TE) &&
678 this->read_byte(RX8111_REG_CONTROL, &control) &&
679 this->write_byte(RX8111_REG_CONTROL, control & ~RX8111_CONTROL_TIE);
680}
681
682bool RX8111Component::configure_timer_(bool clear_stale_flag) {
683 // Stop counting before updating the 24-bit reload value; preserve the boot flag unless explicitly starting.
684 uint8_t extension;
685 uint8_t control;
686 if (!this->disable_timer_(extension, control))
687 return false;
688
689 // Explicit starts own the timer mode: count in both power modes, do not pause,
690 // and route interrupt pulses to /INT rather than FOUT. Leave reserved bits alone.
691 if (!this->update_register(RX8111_REG_TIMER_CONTROL, RX8111_TIMER_MODE, 0))
692 return false;
693
694 const uint8_t count[] = {static_cast<uint8_t>(this->timer_count_), static_cast<uint8_t>(this->timer_count_ >> 8),
695 static_cast<uint8_t>(this->timer_count_ >> 16)};
696 if (!this->write_bytes(RX8111_REG_TIMER0, count, sizeof(count)) ||
697 !this->write_byte(RX8111_REG_EXTENSION,
698 (extension & ~(RX8111_EXTENSION_TE | RX8111_EXTENSION_TSEL)) | this->timer_clock_) ||
699 (clear_stale_flag && !this->clear_timer_flag()) ||
700 !this->write_byte(RX8111_REG_CONTROL, control | RX8111_CONTROL_TIE))
701 return false;
702 return this->write_byte(RX8111_REG_EXTENSION,
703 (extension & ~RX8111_EXTENSION_TSEL) | this->timer_clock_ | RX8111_EXTENSION_TE);
704}
705
707 if (this->timer_count_ == 0 || this->timer_count_ > 0xFFFFFF) {
708 ESP_LOGW(TAG, "Cannot start timer without a valid timer_count");
709 return false;
710 }
711 this->cancel_boot_retry_(false);
712 if (!this->configure_timer_(true)) {
713 // A partial configuration can leave either enable bit set. Disable both if the bus permits it;
714 // otherwise keep watching a timer that may still be running.
715 uint8_t extension, control;
716 if (this->disable_timer_(extension, control))
717 this->timer_enabled_ = false;
718 this->set_warning_(WARN_TIMER, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
719 return false;
720 }
722 this->timer_enabled_ = true;
723 this->timer_retained_event_pending_ = false;
724 this->start_event_polling_();
726 return true;
727}
728
730 if (!this->write_byte(RX8111_REG_FLAG, static_cast<uint8_t>(~RX8111_FLAG_TF))) {
731 this->set_warning_(WARN_TIMER_FLAG_CLEAR, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
732 return false;
733 }
734 this->timer_flag_seen_ = false;
735 this->timer_retained_event_pending_ = false;
737 return true;
738}
739
741 this->cancel_boot_retry_(false);
742 uint8_t extension, control;
743 if (!this->disable_timer_(extension, control)) {
744 this->set_warning_(WARN_TIMER, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
745 return false;
746 }
747 this->timer_enabled_ = false;
749 return true;
750}
751
753 uint8_t flags = 0;
754 bool flags_valid = false;
755 switch (snapshot.state) {
757 flags_valid = this->read_byte(RX8111_REG_FLAG, &flags);
758 break;
760 break;
762 flags = snapshot.value;
763 flags_valid = true;
764 break;
765 }
766 bool success = flags_valid;
767#ifdef USE_BINARY_SENSOR
768 uint8_t status = 0;
769 const bool monitor_requested = this->status_sensors_[STATUS_SENSOR_BATTERY_LOW] != nullptr ||
770 this->status_sensors_[STATUS_SENSOR_EVIN] != nullptr;
771 const bool status_valid = !monitor_requested || this->read_byte(RX8111_REG_STATUS, &status);
772 success = success && status_valid;
773#endif
774 // Settle this snapshot before callbacks, never after a callback's newer result.
775 if (success) {
777 } else {
778 this->set_warning_(WARN_EVENT_POLL, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
779 }
780 // EVF is not cleared here: users may use it to hold /INT asserted across sleep.
781 const bool event_active = (flags & RX8111_FLAG_EVF) != 0;
782 const bool event_rising =
783 flags_valid && event_active && !this->event_flag_seen_ && this->event_enabled_ && !this->event_boot_pending_;
784 if (flags_valid && this->event_enabled_ && !this->event_boot_pending_)
785 this->event_flag_seen_ = event_active;
786 const bool alarm_active = flags_valid && (flags & RX8111_FLAG_AF) != 0;
787 const bool timer_active = flags_valid && (flags & RX8111_FLAG_TF) != 0;
788 // alarm_armed_ is only ever set while the alarm is enabled
789 const bool timer_watched = this->timer_enabled_ || this->timer_retained_event_pending_;
790 if (alarm_active && this->alarm_armed_)
791 this->boot_alarm_event_seen_ = true;
792 const bool alarm_rising = alarm_active && !this->alarm_flag_seen_ && this->alarm_armed_;
793 const bool timer_rising = timer_active && !this->timer_flag_seen_ && timer_watched;
794 const bool deliver_stopped_event = timer_rising && this->timer_retained_event_pending_;
795 // Record eligible edges before callbacks, which may clear flags synchronously.
796 // Leave flags unseen while boot configuration is still waiting to enable delivery.
797 if (flags_valid && this->alarm_armed_)
798 this->alarm_flag_seen_ = alarm_active;
799 this->alarm_reprogrammed_ = false;
800 if (flags_valid && timer_watched)
801 this->timer_flag_seen_ = timer_active;
802 if (flags_valid)
803 this->timer_retained_event_pending_ = false;
804#ifdef USE_BINARY_SENSOR
805 // Read the live monitor only when requested. Its failure must not suppress
806 // valid FLAG states, timestamp publication, or alarm/timer/event callbacks.
807 for (uint8_t i = 0; i < STATUS_SENSOR_COUNT; i++) {
808 const bool uses_flags = i < STATUS_SENSOR_FIRST_MONITOR;
809 if (this->status_sensors_[i] != nullptr && (uses_flags ? flags_valid : status_valid)) {
811 ((uses_flags ? flags : status) & progmem_read_byte(&STATUS_SENSOR_MASKS[i])) != 0);
812 }
813 }
814#endif
815#ifdef USE_RX8111_TIMESTAMP
816 // Stage the event edges first; text callbacks may acknowledge any of the flags.
817 if (flags_valid && this->timestamp_sensor_ != nullptr && !this->event_boot_pending_) {
818 if (this->publish_timestamps_(flags, this->timestamp_mode_ != TIMESTAMP_MODE_LATEST)) {
820 } else {
821 this->set_warning_(WARN_TIMESTAMP, LOG_STR("RX8111 timestamp read failed"));
822 }
823 }
824#endif
825 // An entity callback that set a new alarm superseded the one that fired
826 if (alarm_rising && this->alarm_flag_seen_ && this->alarm_armed_ && !this->alarm_reprogrammed_)
827 this->alarm_callbacks_.call();
828 // An alarm callback may stop the timer before its callback is delivered.
829 if (timer_rising && this->timer_flag_seen_ && (this->timer_enabled_ || deliver_stopped_event))
830 this->timer_callbacks_.call();
831 if (event_rising && this->event_flag_seen_)
832 this->event_callbacks_.call();
833 return success;
834}
835
836#ifdef USE_RX8111_TIMESTAMP
838 if (!this->timestamps_staged_ || this->defer_timestamps_)
839 return;
840 uint8_t records[64];
841 std::memcpy(records, this->staged_timestamps_, sizeof(records));
842 const uint8_t count = this->staged_count_;
843 const bool buffered = this->staged_buffered_;
844 // Consume the staging before callbacks, including callbacks that acknowledge flags synchronously.
845 this->timestamps_staged_ = false;
846 this->staged_count_ = 0;
847 this->deliver_timestamps_(records, count, buffered);
848}
849
850// An unbuffered count is 0 or 1: the latest-bank record, which becomes the new baseline.
851void RX8111Component::deliver_timestamps_(const uint8_t *records, uint8_t count, bool buffered) {
852 if (!buffered && count != 0) {
853 std::memcpy(this->last_timestamp_, records, 10);
854 this->timestamp_seen_ = true;
855 }
856 for (uint8_t i = 0; i < count; i++)
857 this->publish_timestamp_(records + i * 8, buffered);
858}
859
860void RX8111Component::publish_timestamp_(const uint8_t *raw, bool buffered) {
861 // Buffered records omit weekday and the two least significant fractional bits.
862 const uint8_t offset = buffered ? 1 : 2;
863 uint8_t calendar[7] = {raw[offset], raw[offset + 1], raw[offset + 2], 1,
864 raw[buffered ? 4 : 6], raw[buffered ? 5 : 7], raw[buffered ? 6 : 8]};
865 // Captured XST invalidates this record.
866 const bool valid = mask_bcd_calendar(calendar) && (raw[buffered ? 7 : 9] & 2) == 0;
867 const auto captured = time::decode_bcd_calendar<time::EPSON_BCD_CALENDAR>(calendar);
868 if (!valid || !captured.is_valid(true, false) || captured.second > 59) {
869 ESP_LOGW(TAG, "Skipping invalid RTC timestamp record");
870 return;
871 }
872 const uint16_t fraction = buffered ? raw[0] * 4 : raw[1] * 4 + (raw[0] & 3);
873 // Truncate rather than round: the last tick must not become an invalid .1000 fraction.
874 const unsigned milliseconds = fraction * 1000U / 1024U;
875 char value[25];
876 const int len =
877 std::snprintf(value, sizeof(value), "%04u-%02u-%02uT%02u:%02u:%02u.%03uZ", captured.year, captured.month,
878 captured.day_of_month, captured.hour, captured.minute, captured.second, milliseconds);
879 this->timestamp_sensor_->publish_state(value, len);
880}
881
882bool RX8111Component::publish_timestamps_(uint8_t flags, bool buffered, bool startup) {
883 if (this->capture_restore_pending_) {
884 if (!this->update_register(RX8111_REG_EXTENSION, RX8111_EXTENSION_ETS, RX8111_EXTENSION_ETS))
885 return false;
886 this->capture_restore_pending_ = false;
887 }
888 // EVF acknowledgement is independent of timestamp consumption. Read latest storage even after an acknowledgement.
889 uint8_t records[64]{};
890 if (!startup && !buffered && this->timestamp_seen_) {
891 // Only use this active-capture read to skip unchanged storage, never to publish a potentially torn record.
892 if (!this->read_bytes(RX8111_REG_TIMESTAMP_LATEST, records, 10))
893 return false;
894 if (std::memcmp(records, this->last_timestamp_, 10) == 0)
895 return true;
896 }
897 if (!startup && buffered) {
898 uint8_t status;
899 if (!this->read_byte(RX8111_REG_TIMESTAMP_STATUS, &status))
900 return false;
901 if (status & RX8111_TIMESTAMP_EMPTY)
902 return true;
903 }
904 uint8_t extension;
905 if (!this->read_byte(RX8111_REG_EXTENSION, &extension))
906 return false;
907 const bool restore = (extension & RX8111_EXTENSION_ETS) != 0;
908 if (startup && !this->retained_evidence_seen_) {
910 (flags & (RX8111_FLAG_POR | RX8111_FLAG_VLF | RX8111_FLAG_XST)) == 0 && ((flags & RX8111_FLAG_EVF) || restore);
911 this->retained_evidence_seen_ = true;
912 }
913 if (restore && !this->write_byte(RX8111_REG_EXTENSION, extension & ~RX8111_EXTENSION_ETS)) {
914 this->capture_restore_pending_ = !startup;
915 return false;
916 }
917 uint8_t count = 0;
918 bool success = true;
919 if (startup && !this->timestamps_staged_ && buffered &&
920 !this->read_bytes(RX8111_REG_TIMESTAMP_LATEST, this->last_timestamp_, 10))
921 return false;
922 if (startup && this->timestamps_staged_) {
923 buffered = this->staged_buffered_;
924 count = this->staged_count_;
925 std::memcpy(records, this->staged_timestamps_, sizeof(records));
926 } else if (!buffered) {
927 success = this->read_bytes(RX8111_REG_TIMESTAMP_LATEST, records, 10);
928 // Across reboot there is no consumed marker. Recover clean retained capture, accepting possible redelivery.
929 const bool publish_latest =
930 success && (startup ? this->retained_latest_qualified_
931 : (this->timestamp_seen_ && std::memcmp(records, this->last_timestamp_, 10) != 0));
932 count = publish_latest ? 1 : 0;
933 } else {
934 uint8_t status;
935 success = this->read_byte(RX8111_REG_TIMESTAMP_STATUS, &status);
936 if (success && !(status & RX8111_TIMESTAMP_EMPTY)) {
937 const uint8_t latest = status & 7;
938 const uint8_t available = (status & RX8111_TIMESTAMP_FULL) ? 8 : latest + 1;
939 const uint8_t first = available == 8 ? (latest + 1) & 7 : 0;
940 for (uint8_t i = 0; i < available && success; i++)
941 success = this->read_bytes(RX8111_REG_TIMESTAMP_BUFFER + (((first + i) & 7) * 8), records + i * 8, 8);
942 if (success) {
943 count = available;
944 if (startup) {
945 this->timestamp_clear_pending_ = true;
946 } else {
947 success = this->update_register(RX8111_REG_TIMESTAMP_CONTROL, RX8111_TIMESTAMP_CLEAR, RX8111_TIMESTAMP_CLEAR);
948 }
949 }
950 }
951 }
952 if (startup) {
953 // Keep capture paused and retain staged data across configuration failures, including a failed final enable.
954 if (!success)
955 return false;
956 if (!this->timestamps_staged_) {
957 // Baseline the inactive latest bank too: switching from RAM must not publish old latest-bank contents.
958 if (!buffered) {
959 std::memcpy(this->last_timestamp_, records, 10);
960 }
961 this->timestamp_seen_ = true;
962 std::memcpy(this->staged_timestamps_, records, count * (buffered ? 8 : 10));
963 this->staged_count_ = count;
964 this->staged_buffered_ = buffered;
965 this->timestamps_staged_ = true;
966 }
967 // A failed acknowledgement does not prove TSCLR was rejected. Keep the host copy until delivery.
968 if (this->timestamp_clear_pending_) {
969 if (!this->update_register(RX8111_REG_TIMESTAMP_CONTROL, RX8111_TIMESTAMP_CLEAR, RX8111_TIMESTAMP_CLEAR))
970 return false;
971 this->timestamp_clear_pending_ = false;
972 }
973 if (!this->configure_event_())
974 return false;
976 return true;
977 } else {
978 const bool restored = !restore || this->write_byte(RX8111_REG_EXTENSION, extension);
979 this->capture_restore_pending_ = !restored;
980 success = success && restored;
981 }
982 // Deliver fully staged data even when runtime cleanup fails; buffered data may be redelivered.
983 this->deliver_timestamps_(records, count, buffered);
984 return success;
985}
986#endif
987
988} // namespace esphome::rx8111
BedjetMode mode
BedJet operating mode.
uint8_t raw[35]
Definition bl0939.h:0
uint8_t status
Definition bl0942.h:8
void defer(const char *name, SchedulerCallback &&f)
Defer a callback to the next loop() call with a const char* name.
bool cancel_interval(const char *name)
Cancel an interval function.
Definition component.cpp:92
bool cancel_timeout(const char *name)
Cancel a timeout function.
bool cancel_defer(const char *name)
Cancel a defer callback using the specified name, name must not be empty.
void status_clear_warning()
Definition component.h:318
void set_interval(const char *name, uint32_t interval, SchedulerCallback &&f)
Set an interval function with a const char* name.
Definition component.cpp:88
void set_timeout(const char *name, uint32_t timeout, SchedulerCallback &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void publish_state(bool new_state)
Publish a new state to the front-end.
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 update_register(uint8_t a_register, uint8_t mask, uint8_t value)
Read a register, replace the bits in mask with those of value and write it back.
Definition i2c.cpp:111
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
void deliver_timestamps_(const uint8_t *records, uint8_t count, bool buffered)
Definition rx8111.cpp:851
static constexpr uint8_t ALARM_FIELD_UNSET
Definition rx8111.h:119
bool set_alarm(uint16_t second, uint16_t minute, uint16_t hour, uint16_t weekday, uint16_t day)
Definition rx8111.cpp:604
LazyCallbackManager< void()> alarm_callbacks_
Definition rx8111.h:205
uint8_t boot_attempts_[BOOT_FEATURE_COUNT]
Definition rx8111.h:212
bool program_alarm_(uint8_t second, uint8_t minute, uint8_t hour, uint8_t weekday, uint8_t day, bool boot=false)
Definition rx8111.cpp:566
bool poll_event_flags_(FlagSnapshot snapshot={})
Definition rx8111.cpp:752
bool disable_timer_(uint8_t &extension, uint8_t &control)
Definition rx8111.cpp:675
bool read_calendar_(ESPTime &rtc_time, FlagSnapshot *flags=nullptr)
Definition rx8111.cpp:472
void publish_timestamp_(const uint8_t *raw, bool buffered)
Definition rx8111.cpp:860
bool schedule_alarm_in(uint32_t seconds)
Definition rx8111.cpp:628
binary_sensor::BinarySensor * status_sensors_[STATUS_SENSOR_COUNT]
Definition rx8111.h:193
void read_time_(FlagSnapshot &flags)
Definition rx8111.cpp:456
bool configure_timer_(bool clear_stale_flag=false)
Definition rx8111.cpp:682
void run_deferred_update_(bool status_only)
Definition rx8111.cpp:380
bool set_alarm_interrupt_(bool enabled)
Definition rx8111.cpp:562
text_sensor::TextSensor * timestamp_sensor_
Definition rx8111.h:168
bool finish_time_write_(bool calendar_written)
Definition rx8111.cpp:541
void cancel_boot_retry_(bool alarm)
Definition rx8111.cpp:325
void boot_config_result_(BootFeature feature, bool success)
Definition rx8111.cpp:242
void clear_warning_(uint16_t bit)
Definition rx8111.cpp:395
void check_event_flags_(FlagSnapshot flags={})
Definition rx8111.cpp:329
void set_warning_(uint16_t bit, const LogString *message)
Definition rx8111.cpp:390
bool publish_timestamps_(uint8_t flags, bool buffered, bool startup=false)
Definition rx8111.cpp:882
LazyCallbackManager< void()> event_callbacks_
Definition rx8111.h:207
LazyCallbackManager< void()> timer_callbacks_
Definition rx8111.h:206
void publish_state(const std::string &state)
Definition text_sensor.h:42
ESPTime now()
Get the time in the currently defined timezone.
ESPTime utcnow()
Get the time without any time zone or DST corrections.
LazyCallbackManager< void()> time_sync_callback_
void synchronize_epoch_(uint32_t epoch)
Report a unix epoch as current time.
const LogString * message
Definition component.cpp:35
uint8_t day
Definition date_entity.h:2
uint8_t second
uint8_t minute
uint8_t hour
uint16_t flags
unsigned count
Definition gif.cpp:51
@ TIMESTAMP_MODE_OVERWRITE
Definition rx8111.h:18
@ TIMESTAMP_MODE_LATEST
Definition rx8111.h:16
ESPHOME_LOG_TAG(TAG, "rx8111")
@ STATUS_SENSOR_BATTERY_LOW
Definition rx8111.h:28
@ STATUS_SENSOR_COUNT
Definition rx8111.h:30
@ STATUS_SENSOR_FIRST_MONITOR
Definition rx8111.h:31
@ STATUS_SENSOR_EVIN
Definition rx8111.h:29
PROGMEM_STRING_TABLE(TimerClockNames, "4096 Hz", "64 Hz", "1 Hz", "1/60 Hz")
constexpr uint8_t uint8_to_bcd(uint8_t value)
Encode a value from 0 to 99 as a two-digit packed BCD byte, e.g. 59 to 0x59.
Definition helpers.h:1330
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:51
bool valid
static void uint32_t
A more user-friendly version of struct tm from time.h.
Definition time.h:25
uint8_t minute
minutes after the hour [0-59]
Definition time.h:34
uint8_t second
seconds after the minute [0-60]
Definition time.h:32
uint8_t hour
hours since midnight [0-23]
Definition time.h:36
time_t timestamp
unix epoch time (seconds since UTC Midnight January 1, 1970)
Definition time.h:50
bool is_valid(bool check_day_of_week=true, bool check_day_of_year=true) const
Check if this ESPTime is valid (year >= internal::MIN_VALID_YEAR and the requested fields are in rang...
Definition time.h:84
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition time.cpp:104
uint8_t day_of_month
day of the month [1-31]
Definition time.h:40
uint16_t year
year
Definition time.h:46
uint8_t month
month; january=1 [1-12]
Definition time.h:44
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:38