ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
dfrobot_sen0465_i2c.cpp
Go to the documentation of this file.
2#include "esphome/core/log.h"
3
5
6ESPHOME_LOG_TAG(TAG, "dfrobot_sen0465_i2c");
7
8using dfrobot_sen0465_base::FRAME_SIZE;
9using dfrobot_sen0465_base::RESPONSE_TIMEOUT_ID;
10
11static constexpr uint8_t DATA_REGISTER = 0x00;
12// The DFRobot library waits 10 ms, but on an ESP32-C6 reading the answer to the alarm command
13// 10 ms after sending it timed out
14static constexpr uint32_t RESPONSE_DELAY_MS = 100;
15
17 DFRobotSEN0465Component::dump_config();
18 LOG_I2C_DEVICE(this);
19}
20
21bool DFRobotSEN0465I2CComponent::write_frame(const uint8_t *frame) {
22 return this->write_register(DATA_REGISTER, frame, FRAME_SIZE) == i2c::ERROR_OK;
23}
24
26 this->set_timeout(RESPONSE_TIMEOUT_ID, RESPONSE_DELAY_MS, [this]() {
27 uint8_t frame[FRAME_SIZE];
28 // Like the DFRobot library, end the register write with a STOP before reading
29 if (this->write(&DATA_REGISTER, 1) != i2c::ERROR_OK || this->read(frame, FRAME_SIZE) != i2c::ERROR_OK) {
30 this->handle_no_response_();
31 return;
32 }
33 this->handle_response_(frame);
34 });
35}
36
37} // namespace esphome::dfrobot_sen0465_i2c
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
ErrorCode write_register(uint8_t a_register, const uint8_t *data, size_t len) const
writes an array of bytes to a specific register in the I²C device
Definition i2c.cpp:37
ErrorCode write(const uint8_t *data, size_t len) const
writes an array of bytes to a device using an I2CBus
Definition i2c.h:195
ErrorCode read(uint8_t *data, size_t len) const
reads an array of bytes from the device using an I2CBus
Definition i2c.h:175
ESPHOME_LOG_TAG(TAG, "dfrobot_sen0465_i2c")
@ ERROR_OK
No error found during execution of method.
Definition i2c_bus.h:15
static void uint32_t