ESPHome 2026.11.0b6
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, BootFailureNames::LAST_INDEX));
263 if (attempts < RX8111_BOOT_MAX_ATTEMPTS) {
264 this->set_timeout(config.retry_id, this->boot_retry_delay_ms_,
265 [this, configure = config.configure]() { (this->*configure)(); });
266 }
267}
268
270 bool success = true;
271 if (this->alarm_enabled_) {
274 this->alarm_day_ != ALARM_FIELD_UNSET) {
275 success = this->program_alarm_(this->alarm_second_, this->alarm_minute_, this->alarm_hour_, this->alarm_weekday_,
276 this->alarm_day_, true);
277 if (success) {
278 // Only the first successful boot program can introduce a boot wake-up edge.
279 if (!this->boot_alarm_event_seen_)
280 this->alarm_flag_seen_ = false;
281 this->start_event_polling_();
282 }
283 } else {
284 // Preserve an alarm programmed by an action before a reboot or deep sleep.
285 uint8_t control;
286 success = this->read_byte(RX8111_REG_CONTROL, &control);
287 if (success) {
288 this->alarm_armed_ = (control & RX8111_CONTROL_AIE) != 0;
289 if (this->alarm_armed_)
290 this->start_event_polling_();
291 }
292 }
293 } else if (this->alarm_explicitly_configured_) {
294 success = this->set_alarm_interrupt_(false);
295 }
296 this->boot_config_result_(BOOT_ALARM, success);
297}
298
300 bool success = true;
301 // An omitted timer setting leaves battery-backed state alone; explicit false stops it.
302 if (this->timer_configured_) {
303 const bool start_timer = this->timer_enabled_;
304 if (start_timer) {
305 success = this->configure_timer_();
306 } else {
307 uint8_t extension, control;
308 success = this->disable_timer_(extension, control);
309 }
310 if (success && !start_timer && this->timer_count_ != 0) {
312 this->start_event_polling_();
313 }
314 if (success && start_timer)
316 }
317 this->boot_config_result_(BOOT_TIMER, success);
318}
319
320// A runtime action supersedes pending boot programming of the same feature
322 this->cancel_timeout(alarm ? RX8111_BOOT_ALARM_RETRY_ID : RX8111_BOOT_TIMER_RETRY_ID);
323}
324
326 if (this->poll_running_) {
327 if (!this->deferred_update_running_)
328 this->defer(RX8111_DEFERRED_UPDATE_ID, [this]() { this->run_deferred_update_(false); });
329 return;
330 }
331 // A normal poll also satisfies a refresh requested during time synchronization.
332 this->cancel_defer(RX8111_DEFERRED_STATUS_ID);
333 this->poll_running_ = true;
334 if (this->watching_())
335 this->poll_event_flags_(flags);
336 this->poll_running_ = false;
337 // A callback or a runtime action may have stopped the last watched feature
338 if (!this->watching_() && this->event_polling_started_) {
339 this->cancel_interval(RX8111_EVENT_INTERVAL_ID);
340 this->event_polling_started_ = false;
341 }
342}
343
345 if (this->event_polling_started_)
346 return;
347 this->set_interval(RX8111_EVENT_INTERVAL_ID, this->event_poll_interval_, [this]() { this->check_event_flags_(); });
348 this->event_polling_started_ = true;
349}
350
352 // One follow-up satisfies synchronous callback requests; its own callbacks
353 // must not perpetually queue more work, even if they keep changing the flags.
354 if (this->deferred_update_running_ && (this->update_running_ || this->poll_running_ || this->sync_running_))
355 return;
356 // Synchronizing again would retrigger the same on_time_sync automation forever.
357 // Keep its diagnostic refresh, without starting another calendar synchronization.
358 if (this->sync_running_) {
359 this->defer(RX8111_DEFERRED_STATUS_ID, [this]() { this->run_deferred_update_(true); });
360 return;
361 }
362 // Entity callbacks may request another update synchronously.
363 // Coalesce them without interleaving snapshots or recursing on the stack.
364 if (this->update_running_ || this->poll_running_) {
365 this->defer(RX8111_DEFERRED_UPDATE_ID, [this]() { this->run_deferred_update_(false); });
366 return;
367 }
368 this->update_running_ = true;
369 this->cancel_defer(RX8111_DEFERRED_UPDATE_ID);
371 this->read_time_(flags);
372 this->check_event_flags_(flags);
373 this->update_running_ = false;
374}
375
377 this->deferred_update_running_ = true;
378 if (status_only) {
379 this->check_event_flags_();
380 } else {
381 this->update();
382 }
383 this->deferred_update_running_ = false;
384}
385
386void RX8111Component::set_warning_(uint16_t bit, const LogString *message) {
387 this->warnings_ |= bit;
388 this->status_set_warning(message);
389}
390
392 if ((this->warnings_ & bit) == 0)
393 return;
394 this->warnings_ &= ~bit;
395 if (this->warnings_ == 0)
396 this->status_clear_warning();
397}
398
400 ESP_LOGCONFIG(TAG, "RX8111:");
401 LOG_I2C_DEVICE(this);
402#ifdef USE_BINARY_SENSOR
403 for (uint8_t i = 0; i < STATUS_SENSOR_COUNT; i++) {
404 if (this->status_sensors_[i] != nullptr) {
405 log_binary_sensor(TAG, " ", LOG_STR_ARG(StatusSensorNames::get_log_str(i, StatusSensorNames::LAST_INDEX)),
406 this->status_sensors_[i]);
407 }
408 }
409#endif
410#ifdef USE_RX8111_TIMESTAMP
411 LOG_TEXT_SENSOR(" ", "Event timestamp", this->timestamp_sensor_);
412 if (this->timestamp_sensor_ != nullptr) {
413 ESP_LOGCONFIG(TAG, " Timestamp record mode: %s",
414 LOG_STR_ARG(TimestampModeNames::get_log_str(this->timestamp_mode_, TimestampModeNames::LAST_INDEX)));
415 }
416#endif
417 if (this->battery_configured_) {
418 ESP_LOGCONFIG(TAG, " Battery backup: %s, charging: %s", ONOFF(this->battery_backup_),
419 ONOFF(this->battery_charging_));
420 }
421 if (this->fout_configured_) {
422 ESP_LOGCONFIG(TAG, " FOUT: %s", LOG_STR_ARG(FoutNames::get_log_str(this->fout_frequency_, FoutNames::LAST_INDEX)));
423 }
424 if (this->event_configured_) {
425 ESP_LOGCONFIG(TAG, " EVIN interrupt: %s", ONOFF(this->event_enabled_));
426 bool evin_used = this->event_enabled_;
427#ifdef USE_RX8111_TIMESTAMP
428 evin_used = evin_used || this->timestamp_sensor_ != nullptr;
429#endif
430 if (evin_used) {
431 ESP_LOGCONFIG(
432 TAG, " EVIN: %s, filter: %u, pull: %s",
433 (this->evin_settings_ & 0x80) ? LOG_STR_LITERAL("high") : LOG_STR_LITERAL("low"),
434 (this->evin_settings_ >> 5) & 3,
435 LOG_STR_ARG(EvinPullNames::get_log_str((this->evin_settings_ >> 2) & 7, EvinPullNames::LAST_INDEX)));
436 }
437 }
438 ESP_LOGCONFIG(TAG,
439 " Alarm interrupt: %s\n"
440 " Wake-up timer: %s",
441 ONOFF(this->alarm_enabled_), ONOFF(this->timer_enabled_));
442 if (this->timer_enabled_) {
443 ESP_LOGCONFIG(TAG, " Count: %" PRIu32 ", clock: %s", this->timer_count_,
444 LOG_STR_ARG(TimerClockNames::get_log_str(this->timer_clock_, TimerClockNames::LAST_INDEX)));
445 }
446 RealTimeClock::dump_config();
447}
448
451 this->read_time_(flags);
452}
453
455 ESPTime rtc_time;
456 if (!this->read_calendar_(rtc_time, &flags))
457 return;
458 ESP_LOGD(TAG, "Read UTC time: %04d-%02d-%02d %02d:%02d:%02d", rtc_time.year, rtc_time.month, rtc_time.day_of_month,
459 rtc_time.hour, rtc_time.minute, rtc_time.second);
460 // Synchronization callbacks can clear flags or program the RTC. Only reuse
461 // the snapshot when no user code will run between the read and the poll.
462 if (!this->time_sync_callback_.empty())
464 const bool was_sync_running = this->sync_running_;
465 this->sync_running_ = true;
467 this->sync_running_ = was_sync_running;
468}
469
471 if (this->time_write_invalid_) {
472 this->set_warning_(WARN_CALENDAR, LOG_STR("RTC write incomplete; write trusted time again"));
473 return false;
474 }
475 uint8_t regs[2]; // Adjacent FLAG and CONTROL registers.
476 if (flags != nullptr)
477 flags->state = FlagSnapshot::FAILED; // Do not repeat a failed shared read in the same update.
478 if (!this->read_bytes(RX8111_REG_FLAG, regs, sizeof(regs))) {
479 this->set_warning_(WARN_CALENDAR, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
480 return false;
481 }
482 if (flags != nullptr)
483 *flags = {FlagSnapshot::VALID, regs[0]};
484 if (regs[1] & RX8111_CTRL_STOP) {
485 this->set_warning_(WARN_CALENDAR, LOG_STR("RTC counter stopped; not synchronizing"));
486 return false;
487 }
488 if (regs[0] & (RX8111_FLAG_VLF | RX8111_FLAG_XST | RX8111_FLAG_POR)) {
489 ESP_LOGW(TAG, "RTC time invalidated by power loss or oscillator stop; write trusted time");
490 this->set_warning_(WARN_CALENDAR, LOG_STR("RTC time invalidated; write trusted time"));
491 return false;
492 }
493
494 uint8_t buf[7];
495 // I2C access freezes the RX8111 counter, yielding a coherent calendar snapshot.
496 if (!this->read_bytes(RX8111_REG_SEC, buf, sizeof(buf))) {
497 this->set_warning_(WARN_CALENDAR, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
498 return false;
499 }
500 // decode_bcd_calendar() masks the same bits; the check here catches a corrupted register
501 bool valid = mask_bcd_calendar(buf);
502 const uint8_t onehot = buf[3];
503 valid = valid && onehot != 0 && onehot <= 0x40 && (onehot & (onehot - 1)) == 0;
504 if (valid) {
505 rtc_time = time::decode_bcd_calendar<time::EPSON_BCD_CALENDAR>(buf);
506 valid = rtc_time.is_valid(true, false) && rtc_time.second < 60 &&
508 }
509 if (!valid) {
510 ESP_LOGW(TAG, "Invalid RTC time, not syncing to system clock");
511 this->set_warning_(WARN_CALENDAR, LOG_STR("Invalid RTC time"));
512 return false;
513 }
515 return true;
516}
517
520 if (!now.is_valid() || now.year > 2099) {
521 ESP_LOGE(TAG, "Invalid system time, not syncing to RTC");
522 return;
523 }
524 if (!this->set_stop_bit_(true)) {
525 this->set_warning_(WARN_WRITE_TIME, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
526 return;
527 }
528
529 uint8_t buf[7];
530 time::encode_bcd_calendar<time::EPSON_BCD_CALENDAR>(now, buf);
531 this->time_write_invalid_ = true;
532 const bool calendar_written = this->write_bytes(RX8111_REG_SEC, buf, sizeof(buf));
533 if (!this->finish_time_write_(calendar_written))
534 return;
535 ESP_LOGD(TAG, "Wrote UTC time: %04d-%02d-%02d %02d:%02d:%02d", now.year, now.month, now.day_of_month, now.hour,
537}
538
539bool RX8111Component::finish_time_write_(bool calendar_written) {
540 // STOP is the battery-backed invalidity marker, including across host resets.
541 // Never release a partial or delayed snapshot; only a fresh trusted write can restart it.
542 if (!calendar_written) {
543 this->set_warning_(WARN_WRITE_TIME, LOG_STR("RTC write incomplete; write trusted time again"));
544 return false;
545 }
546 if (!this->write_byte(RX8111_REG_FLAG, RX8111_KEEP_EVENT_FLAGS) || !this->set_stop_bit_(false)) {
547 this->set_warning_(WARN_WRITE_TIME, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
548 return false;
549 }
550 this->time_write_invalid_ = false;
553 return true;
554}
555
557 return this->update_register(RX8111_REG_CONTROL, RX8111_CTRL_STOP, stop ? RX8111_CTRL_STOP : 0);
558}
559
561 return this->update_register(RX8111_REG_CONTROL, RX8111_CONTROL_AIE, enabled ? RX8111_CONTROL_AIE : 0);
562}
563
564bool RX8111Component::program_alarm_(uint8_t second, uint8_t minute, uint8_t hour, uint8_t weekday, uint8_t day,
565 bool boot) {
566 uint8_t extension;
567 if (!this->read_byte(RX8111_REG_EXTENSION, &extension))
568 return false;
569 if (weekday != ALARM_FIELD_UNSET) {
570 extension &= ~RX8111_EXTENSION_WADA;
571 } else if (day != ALARM_FIELD_UNSET) {
572 extension |= RX8111_EXTENSION_WADA;
573 }
574
575 const uint8_t fields[] = {
576 second == ALARM_FIELD_UNSET ? RX8111_ALARM_DISABLED : uint8_to_bcd(second),
577 minute == ALARM_FIELD_UNSET ? RX8111_ALARM_DISABLED : uint8_to_bcd(minute),
578 hour == ALARM_FIELD_UNSET ? RX8111_ALARM_DISABLED : uint8_to_bcd(hour),
579 weekday != ALARM_FIELD_UNSET ? static_cast<uint8_t>(1U << (weekday - 1))
581 : RX8111_ALARM_DISABLED,
582 };
583 // Disable the interrupt during partial programming; keep AF (past events) latched.
584 if (!this->set_alarm_interrupt_(false))
585 return false;
586 this->alarm_armed_ = false;
587 this->alarm_reprogrammed_ = true;
588 uint8_t flags;
589 if (!this->read_byte(RX8111_REG_FLAG, &flags))
590 return false;
591 if (!boot)
592 this->alarm_flag_seen_ = (flags & RX8111_FLAG_AF) != 0;
593 if (!this->write_byte(RX8111_REG_SEC_ALARM, fields[0]) || !this->write_bytes(RX8111_REG_MIN_ALARM, &fields[1], 3) ||
594 !this->write_byte(RX8111_REG_EXTENSION, extension))
595 return false;
596 if (!this->set_alarm_interrupt_(this->alarm_enabled_))
597 return false;
598 this->alarm_armed_ = true;
599 return true;
600}
601
602bool RX8111Component::set_alarm(uint16_t second, uint16_t minute, uint16_t hour, uint16_t weekday, uint16_t day) {
603 if (!this->alarm_enabled_) {
604 ESP_LOGE(TAG, "Alarm interrupt disabled; set alarm_enabled: true");
605 return false;
606 }
607 constexpr uint16_t unset = ALARM_FIELD_UNSET;
608 if ((second != unset && second > 59) || (minute != unset && minute > 59) || (hour != unset && hour > 23) ||
609 (weekday != unset && (weekday < 1 || weekday > 7)) || (day != unset && (day < 1 || day > 31)) ||
610 (weekday != unset && day != unset) ||
611 (second == unset && minute == unset && hour == unset && weekday == unset && day == unset)) {
612 ESP_LOGE(TAG, "Invalid alarm fields");
613 return false;
614 }
615 this->cancel_boot_retry_(true);
616 if (!this->program_alarm_(second, minute, hour, weekday, day)) {
617 this->set_warning_(WARN_ALARM, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
618 return false;
619 }
621 this->boot_alarm_event_seen_ = true;
622 this->start_event_polling_();
623 return true;
624}
625
627 if (!this->alarm_enabled_ || seconds == 0 || seconds >= 28U * 86400U) {
628 ESP_LOGE(TAG, "Relative alarm requires alarm_enabled and a duration of 1 second to less than 28 days");
629 return false;
630 }
631 ESPTime now;
632 if (!this->read_calendar_(now)) {
633 ESP_LOGE(TAG, "Cannot read valid RTC time for relative alarm");
634 return false;
635 }
636 auto target = ESPTime::from_epoch_utc(now.timestamp + seconds);
637 if (!target.is_valid() || target.year > 2099) {
638 ESP_LOGE(TAG, "Relative alarm target is outside RTC year range");
639 return false;
640 }
641 return this->set_alarm(target.second, target.minute, target.hour, ALARM_FIELD_UNSET, target.day_of_month);
642}
643
645 this->cancel_boot_retry_(true);
646 if (!this->set_alarm_interrupt_(false)) {
647 this->set_warning_(WARN_ALARM, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
648 return false;
649 }
650 this->alarm_armed_ = false;
651 // Minute, hour and day alarm registers are contiguous
652 static constexpr uint8_t DISABLED_FIELDS[3] = {RX8111_ALARM_DISABLED, RX8111_ALARM_DISABLED, RX8111_ALARM_DISABLED};
653 if (!this->write_byte(RX8111_REG_SEC_ALARM, RX8111_ALARM_DISABLED) ||
654 !this->write_bytes(RX8111_REG_MIN_ALARM, DISABLED_FIELDS, sizeof(DISABLED_FIELDS))) {
655 this->set_warning_(WARN_ALARM, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
656 return false;
657 }
659 return true;
660}
661
663 if (!this->write_byte(RX8111_REG_FLAG, static_cast<uint8_t>(~RX8111_FLAG_AF))) {
664 this->set_warning_(WARN_ALARM_FLAG_CLEAR, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
665 return false;
666 }
667 this->alarm_flag_seen_ = false;
669 return true;
670}
671
672// Clears TE and TIE; returns the register values read so callers can build on them
673bool RX8111Component::disable_timer_(uint8_t &extension, uint8_t &control) {
674 return this->read_byte(RX8111_REG_EXTENSION, &extension) &&
675 this->write_byte(RX8111_REG_EXTENSION, extension & ~RX8111_EXTENSION_TE) &&
676 this->read_byte(RX8111_REG_CONTROL, &control) &&
677 this->write_byte(RX8111_REG_CONTROL, control & ~RX8111_CONTROL_TIE);
678}
679
680bool RX8111Component::configure_timer_(bool clear_stale_flag) {
681 // Stop counting before updating the 24-bit reload value; preserve the boot flag unless explicitly starting.
682 uint8_t extension;
683 uint8_t control;
684 if (!this->disable_timer_(extension, control))
685 return false;
686
687 // Explicit starts own the timer mode: count in both power modes, do not pause,
688 // and route interrupt pulses to /INT rather than FOUT. Leave reserved bits alone.
689 if (!this->update_register(RX8111_REG_TIMER_CONTROL, RX8111_TIMER_MODE, 0))
690 return false;
691
692 const uint8_t count[] = {static_cast<uint8_t>(this->timer_count_), static_cast<uint8_t>(this->timer_count_ >> 8),
693 static_cast<uint8_t>(this->timer_count_ >> 16)};
694 if (!this->write_bytes(RX8111_REG_TIMER0, count, sizeof(count)) ||
695 !this->write_byte(RX8111_REG_EXTENSION,
696 (extension & ~(RX8111_EXTENSION_TE | RX8111_EXTENSION_TSEL)) | this->timer_clock_) ||
697 (clear_stale_flag && !this->clear_timer_flag()) ||
698 !this->write_byte(RX8111_REG_CONTROL, control | RX8111_CONTROL_TIE))
699 return false;
700 return this->write_byte(RX8111_REG_EXTENSION,
701 (extension & ~RX8111_EXTENSION_TSEL) | this->timer_clock_ | RX8111_EXTENSION_TE);
702}
703
705 if (this->timer_count_ == 0 || this->timer_count_ > 0xFFFFFF) {
706 ESP_LOGW(TAG, "Cannot start timer without a valid timer_count");
707 return false;
708 }
709 this->cancel_boot_retry_(false);
710 if (!this->configure_timer_(true)) {
711 // A partial configuration can leave either enable bit set. Disable both if the bus permits it;
712 // otherwise keep watching a timer that may still be running.
713 uint8_t extension, control;
714 if (this->disable_timer_(extension, control))
715 this->timer_enabled_ = false;
716 this->set_warning_(WARN_TIMER, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
717 return false;
718 }
720 this->timer_enabled_ = true;
721 this->timer_retained_event_pending_ = false;
722 this->start_event_polling_();
724 return true;
725}
726
728 if (!this->write_byte(RX8111_REG_FLAG, static_cast<uint8_t>(~RX8111_FLAG_TF))) {
729 this->set_warning_(WARN_TIMER_FLAG_CLEAR, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
730 return false;
731 }
732 this->timer_flag_seen_ = false;
733 this->timer_retained_event_pending_ = false;
735 return true;
736}
737
739 this->cancel_boot_retry_(false);
740 uint8_t extension, control;
741 if (!this->disable_timer_(extension, control)) {
742 this->set_warning_(WARN_TIMER, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
743 return false;
744 }
745 this->timer_enabled_ = false;
747 return true;
748}
749
751 uint8_t flags = 0;
752 bool flags_valid = false;
753 switch (snapshot.state) {
755 flags_valid = this->read_byte(RX8111_REG_FLAG, &flags);
756 break;
758 break;
760 flags = snapshot.value;
761 flags_valid = true;
762 break;
763 }
764 bool success = flags_valid;
765#ifdef USE_BINARY_SENSOR
766 uint8_t status = 0;
767 const bool monitor_requested = this->status_sensors_[STATUS_SENSOR_BATTERY_LOW] != nullptr ||
768 this->status_sensors_[STATUS_SENSOR_EVIN] != nullptr;
769 const bool status_valid = !monitor_requested || this->read_byte(RX8111_REG_STATUS, &status);
770 success = success && status_valid;
771#endif
772 // Settle this snapshot before callbacks, never after a callback's newer result.
773 if (success) {
775 } else {
776 this->set_warning_(WARN_EVENT_POLL, LOG_STR(ESP_LOG_MSG_COMM_FAIL));
777 }
778 // EVF is not cleared here: users may use it to hold /INT asserted across sleep.
779 const bool event_active = (flags & RX8111_FLAG_EVF) != 0;
780 const bool event_rising =
781 flags_valid && event_active && !this->event_flag_seen_ && this->event_enabled_ && !this->event_boot_pending_;
782 if (flags_valid && this->event_enabled_ && !this->event_boot_pending_)
783 this->event_flag_seen_ = event_active;
784 const bool alarm_active = flags_valid && (flags & RX8111_FLAG_AF) != 0;
785 const bool timer_active = flags_valid && (flags & RX8111_FLAG_TF) != 0;
786 // alarm_armed_ is only ever set while the alarm is enabled
787 const bool timer_watched = this->timer_enabled_ || this->timer_retained_event_pending_;
788 if (alarm_active && this->alarm_armed_)
789 this->boot_alarm_event_seen_ = true;
790 const bool alarm_rising = alarm_active && !this->alarm_flag_seen_ && this->alarm_armed_;
791 const bool timer_rising = timer_active && !this->timer_flag_seen_ && timer_watched;
792 const bool deliver_stopped_event = timer_rising && this->timer_retained_event_pending_;
793 // Record eligible edges before callbacks, which may clear flags synchronously.
794 // Leave flags unseen while boot configuration is still waiting to enable delivery.
795 if (flags_valid && this->alarm_armed_)
796 this->alarm_flag_seen_ = alarm_active;
797 this->alarm_reprogrammed_ = false;
798 if (flags_valid && timer_watched)
799 this->timer_flag_seen_ = timer_active;
800 if (flags_valid)
801 this->timer_retained_event_pending_ = false;
802#ifdef USE_BINARY_SENSOR
803 // Read the live monitor only when requested. Its failure must not suppress
804 // valid FLAG states, timestamp publication, or alarm/timer/event callbacks.
805 for (uint8_t i = 0; i < STATUS_SENSOR_COUNT; i++) {
806 const bool uses_flags = i < STATUS_SENSOR_FIRST_MONITOR;
807 if (this->status_sensors_[i] != nullptr && (uses_flags ? flags_valid : status_valid)) {
809 ((uses_flags ? flags : status) & progmem_read_byte(&STATUS_SENSOR_MASKS[i])) != 0);
810 }
811 }
812#endif
813#ifdef USE_RX8111_TIMESTAMP
814 // Stage the event edges first; text callbacks may acknowledge any of the flags.
815 if (flags_valid && this->timestamp_sensor_ != nullptr && !this->event_boot_pending_) {
816 if (this->publish_timestamps_(flags, this->timestamp_mode_ != TIMESTAMP_MODE_LATEST)) {
818 } else {
819 this->set_warning_(WARN_TIMESTAMP, LOG_STR("RX8111 timestamp read failed"));
820 }
821 }
822#endif
823 // An entity callback that set a new alarm superseded the one that fired
824 if (alarm_rising && this->alarm_flag_seen_ && this->alarm_armed_ && !this->alarm_reprogrammed_)
825 this->alarm_callbacks_.call();
826 // An alarm callback may stop the timer before its callback is delivered.
827 if (timer_rising && this->timer_flag_seen_ && (this->timer_enabled_ || deliver_stopped_event))
828 this->timer_callbacks_.call();
829 if (event_rising && this->event_flag_seen_)
830 this->event_callbacks_.call();
831 return success;
832}
833
834#ifdef USE_RX8111_TIMESTAMP
836 if (!this->timestamps_staged_ || this->defer_timestamps_)
837 return;
838 uint8_t records[64];
839 std::memcpy(records, this->staged_timestamps_, sizeof(records));
840 const uint8_t count = this->staged_count_;
841 const bool buffered = this->staged_buffered_;
842 // Consume the staging before callbacks, including callbacks that acknowledge flags synchronously.
843 this->timestamps_staged_ = false;
844 this->staged_count_ = 0;
845 this->deliver_timestamps_(records, count, buffered);
846}
847
848// An unbuffered count is 0 or 1: the latest-bank record, which becomes the new baseline.
849void RX8111Component::deliver_timestamps_(const uint8_t *records, uint8_t count, bool buffered) {
850 if (!buffered && count != 0) {
851 std::memcpy(this->last_timestamp_, records, 10);
852 this->timestamp_seen_ = true;
853 }
854 for (uint8_t i = 0; i < count; i++)
855 this->publish_timestamp_(records + i * 8, buffered);
856}
857
858void RX8111Component::publish_timestamp_(const uint8_t *raw, bool buffered) {
859 // Buffered records omit weekday and the two least significant fractional bits.
860 const uint8_t offset = buffered ? 1 : 2;
861 uint8_t calendar[7] = {raw[offset], raw[offset + 1], raw[offset + 2], 1,
862 raw[buffered ? 4 : 6], raw[buffered ? 5 : 7], raw[buffered ? 6 : 8]};
863 // Captured XST invalidates this record.
864 const bool valid = mask_bcd_calendar(calendar) && (raw[buffered ? 7 : 9] & 2) == 0;
865 const auto captured = time::decode_bcd_calendar<time::EPSON_BCD_CALENDAR>(calendar);
866 if (!valid || !captured.is_valid(true, false) || captured.second > 59) {
867 ESP_LOGW(TAG, "Skipping invalid RTC timestamp record");
868 return;
869 }
870 const uint16_t fraction = buffered ? raw[0] * 4 : raw[1] * 4 + (raw[0] & 3);
871 // Truncate rather than round: the last tick must not become an invalid .1000 fraction.
872 const unsigned milliseconds = fraction * 1000U / 1024U;
873 char value[25];
874 const int len =
875 std::snprintf(value, sizeof(value), "%04u-%02u-%02uT%02u:%02u:%02u.%03uZ", captured.year, captured.month,
876 captured.day_of_month, captured.hour, captured.minute, captured.second, milliseconds);
877 this->timestamp_sensor_->publish_state(value, len);
878}
879
880bool RX8111Component::publish_timestamps_(uint8_t flags, bool buffered, bool startup) {
881 if (this->capture_restore_pending_) {
882 if (!this->update_register(RX8111_REG_EXTENSION, RX8111_EXTENSION_ETS, RX8111_EXTENSION_ETS))
883 return false;
884 this->capture_restore_pending_ = false;
885 }
886 // EVF acknowledgement is independent of timestamp consumption. Read latest storage even after an acknowledgement.
887 uint8_t records[64]{};
888 if (!startup && !buffered && this->timestamp_seen_) {
889 // Only use this active-capture read to skip unchanged storage, never to publish a potentially torn record.
890 if (!this->read_bytes(RX8111_REG_TIMESTAMP_LATEST, records, 10))
891 return false;
892 if (std::memcmp(records, this->last_timestamp_, 10) == 0)
893 return true;
894 }
895 if (!startup && buffered) {
896 uint8_t status;
897 if (!this->read_byte(RX8111_REG_TIMESTAMP_STATUS, &status))
898 return false;
899 if (status & RX8111_TIMESTAMP_EMPTY)
900 return true;
901 }
902 uint8_t extension;
903 if (!this->read_byte(RX8111_REG_EXTENSION, &extension))
904 return false;
905 const bool restore = (extension & RX8111_EXTENSION_ETS) != 0;
906 if (startup && !this->retained_evidence_seen_) {
908 (flags & (RX8111_FLAG_POR | RX8111_FLAG_VLF | RX8111_FLAG_XST)) == 0 && ((flags & RX8111_FLAG_EVF) || restore);
909 this->retained_evidence_seen_ = true;
910 }
911 if (restore && !this->write_byte(RX8111_REG_EXTENSION, extension & ~RX8111_EXTENSION_ETS)) {
912 this->capture_restore_pending_ = !startup;
913 return false;
914 }
915 uint8_t count = 0;
916 bool success = true;
917 if (startup && !this->timestamps_staged_ && buffered &&
918 !this->read_bytes(RX8111_REG_TIMESTAMP_LATEST, this->last_timestamp_, 10))
919 return false;
920 if (startup && this->timestamps_staged_) {
921 buffered = this->staged_buffered_;
922 count = this->staged_count_;
923 std::memcpy(records, this->staged_timestamps_, sizeof(records));
924 } else if (!buffered) {
925 success = this->read_bytes(RX8111_REG_TIMESTAMP_LATEST, records, 10);
926 // Across reboot there is no consumed marker. Recover clean retained capture, accepting possible redelivery.
927 const bool publish_latest =
928 success && (startup ? this->retained_latest_qualified_
929 : (this->timestamp_seen_ && std::memcmp(records, this->last_timestamp_, 10) != 0));
930 count = publish_latest ? 1 : 0;
931 } else {
932 uint8_t status;
933 success = this->read_byte(RX8111_REG_TIMESTAMP_STATUS, &status);
934 if (success && !(status & RX8111_TIMESTAMP_EMPTY)) {
935 const uint8_t latest = status & 7;
936 const uint8_t available = (status & RX8111_TIMESTAMP_FULL) ? 8 : latest + 1;
937 const uint8_t first = available == 8 ? (latest + 1) & 7 : 0;
938 for (uint8_t i = 0; i < available && success; i++)
939 success = this->read_bytes(RX8111_REG_TIMESTAMP_BUFFER + (((first + i) & 7) * 8), records + i * 8, 8);
940 if (success) {
941 count = available;
942 if (startup) {
943 this->timestamp_clear_pending_ = true;
944 } else {
945 success = this->update_register(RX8111_REG_TIMESTAMP_CONTROL, RX8111_TIMESTAMP_CLEAR, RX8111_TIMESTAMP_CLEAR);
946 }
947 }
948 }
949 }
950 if (startup) {
951 // Keep capture paused and retain staged data across configuration failures, including a failed final enable.
952 if (!success)
953 return false;
954 if (!this->timestamps_staged_) {
955 // Baseline the inactive latest bank too: switching from RAM must not publish old latest-bank contents.
956 if (!buffered) {
957 std::memcpy(this->last_timestamp_, records, 10);
958 }
959 this->timestamp_seen_ = true;
960 std::memcpy(this->staged_timestamps_, records, count * (buffered ? 8 : 10));
961 this->staged_count_ = count;
962 this->staged_buffered_ = buffered;
963 this->timestamps_staged_ = true;
964 }
965 // A failed acknowledgement does not prove TSCLR was rejected. Keep the host copy until delivery.
966 if (this->timestamp_clear_pending_) {
967 if (!this->update_register(RX8111_REG_TIMESTAMP_CONTROL, RX8111_TIMESTAMP_CLEAR, RX8111_TIMESTAMP_CLEAR))
968 return false;
969 this->timestamp_clear_pending_ = false;
970 }
971 if (!this->configure_event_())
972 return false;
974 return true;
975 } else {
976 const bool restored = !restore || this->write_byte(RX8111_REG_EXTENSION, extension);
977 this->capture_restore_pending_ = !restored;
978 success = success && restored;
979 }
980 // Deliver fully staged data even when runtime cleanup fails; buffered data may be redelivered.
981 this->deliver_timestamps_(records, count, buffered);
982 return success;
983}
984#endif
985
986} // namespace esphome::rx8111
BedjetMode mode
BedJet operating mode.
uint8_t raw[35]
Definition bl0939.h:0
uint8_t status
Definition bl0942.h:8
bool cancel_interval(const char *name)
Cancel an interval function.
Definition component.cpp:92
bool cancel_timeout(const char *name)
Cancel a timeout function.
void set_timeout(const char *name, uint32_t timeout, std::function< void()> &&f)
Set a timeout function with a const char* name.
Definition component.cpp:96
void defer(const char *name, std::function< void()> &&f)
Defer a callback to the next loop() call with a const char* name.
void set_interval(const char *name, uint32_t interval, std::function< void()> &&f)
Set an interval function with a const char* name.
Definition component.cpp:88
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:309
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:95
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:849
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:602
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:564
bool poll_event_flags_(FlagSnapshot snapshot={})
Definition rx8111.cpp:750
bool disable_timer_(uint8_t &extension, uint8_t &control)
Definition rx8111.cpp:673
bool read_calendar_(ESPTime &rtc_time, FlagSnapshot *flags=nullptr)
Definition rx8111.cpp:470
void publish_timestamp_(const uint8_t *raw, bool buffered)
Definition rx8111.cpp:858
bool schedule_alarm_in(uint32_t seconds)
Definition rx8111.cpp:626
binary_sensor::BinarySensor * status_sensors_[STATUS_SENSOR_COUNT]
Definition rx8111.h:193
void read_time_(FlagSnapshot &flags)
Definition rx8111.cpp:454
bool configure_timer_(bool clear_stale_flag=false)
Definition rx8111.cpp:680
void run_deferred_update_(bool status_only)
Definition rx8111.cpp:376
bool set_alarm_interrupt_(bool enabled)
Definition rx8111.cpp:560
text_sensor::TextSensor * timestamp_sensor_
Definition rx8111.h:168
bool finish_time_write_(bool calendar_written)
Definition rx8111.cpp:539
void cancel_boot_retry_(bool alarm)
Definition rx8111.cpp:321
void boot_config_result_(BootFeature feature, bool success)
Definition rx8111.cpp:242
void clear_warning_(uint16_t bit)
Definition rx8111.cpp:391
void check_event_flags_(FlagSnapshot flags={})
Definition rx8111.cpp:325
void set_warning_(uint16_t bit, const LogString *message)
Definition rx8111.cpp:386
bool publish_timestamps_(uint8_t flags, bool buffered, bool startup=false)
Definition rx8111.cpp:880
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:1146
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint32_t value
Definition hal.cpp:41
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: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
static ESPTime from_epoch_utc(time_t epoch)
Convert an UTC epoch timestamp to a UTC time ESPTime instance.
Definition time.h:143
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
uint8_t day_of_week
day of the week; sunday=1 [1-7]
Definition time.h:36