ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
dfrobot_sen0465_uart.cpp
Go to the documentation of this file.
2
4
5using dfrobot_sen0465_base::FRAME_SIZE;
6using dfrobot_sen0465_base::RESPONSE_TIMEOUT_ID;
7
8// The DFRobot library waits up to this long for an answer
9static constexpr uint32_t RESPONSE_TIMEOUT_MS = 3000;
10
12 // loop() only runs while an answer is expected
13 this->disable_loop();
14 DFRobotSEN0465Component::setup();
15}
16
18 // The tail of a reading the probe sent on its own may precede the answer; the reader skips to the frame head.
19 const uint8_t *frame = nullptr;
20 this->read_available<FRAME_SIZE>([&](uint8_t c) { return (frame = this->reader_.feed(c)) == nullptr; });
21 if (frame == nullptr)
22 return;
23
24 this->cancel_timeout(RESPONSE_TIMEOUT_ID);
25 this->disable_loop();
26 this->handle_response_(frame);
27}
28
29bool DFRobotSEN0465UARTComponent::write_frame(const uint8_t *frame) {
30 // Drop anything left over, such as the answer to the mode change or readings sent before passive mode was set
31 this->discard_available();
32 this->reader_.reset();
33 this->write_array(frame, FRAME_SIZE);
34 return true;
35}
36
38 this->enable_loop();
39 this->set_timeout(RESPONSE_TIMEOUT_ID, RESPONSE_TIMEOUT_MS, [this]() {
40 this->disable_loop();
41 this->reader_.reset();
42 this->handle_no_response_();
43 });
44}
45
46} // namespace esphome::dfrobot_sen0465_uart
bool cancel_timeout(const char *name)
Cancel a timeout function.
void enable_loop()
Enable this component's loop.
Definition component.h:261
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 disable_loop()
Disable this component's loop.
const uint8_t * feed(uint8_t c)
Feed one byte; returns the completed frame, header included, valid until the next call,...
void reset()
Forget the partial frame and look for a header from the next byte.
FrameReader< dfrobot_sen0465_base::FRAME_SIZE, dfrobot_sen0465_base::FRAME_HEAD > reader_
void discard_available()
Read and drop every byte waiting in the receive buffer.
Definition uart.cpp:36
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
static void uint32_t