ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
dfrobot_sen0395.h
Go to the documentation of this file.
1#pragma once
2
8
9#ifdef USE_BINARY_SENSOR
11#endif
12#ifdef USE_SWITCH
14#endif
15
17
18static constexpr uint8_t MMWAVE_READ_BUFFER_LENGTH = 253; // largest LineReader size with a one byte position
19static constexpr uint8_t COMMAND_QUEUE_SIZE = 20;
20
21// Keep in sync with the BusyActions table in dfrobot_sen0395.cpp
34
37struct Command {
39 uint8_t arg;
40};
41
43#ifdef USE_SWITCH
44 SUB_SWITCH(sensor_active)
45 SUB_SWITCH(turn_on_led)
46 SUB_SWITCH(presence_via_uart)
47 SUB_SWITCH(start_after_boot)
48#endif
49
50 public:
51 void dump_config() override;
52 void loop() override;
53 void set_active(bool active) {
54 if (active != active_) {
55#ifdef USE_SWITCH
56 PUBLISH_STATE(this->sensor_active_switch_, active);
57#endif
58 active_ = active;
59 }
60 }
61 bool is_active() { return active_; }
62
63 void set_led_active(bool active) {
64 if (led_active_ != active) {
65#ifdef USE_SWITCH
66 PUBLISH_STATE(this->turn_on_led_switch_, active);
67#endif
68 led_active_ = active;
69 }
70 }
71 bool is_led_active() { return led_active_; }
72
73 void set_uart_presence_active(bool active) {
74 uart_presence_active_ = active;
75#ifdef USE_SWITCH
76 PUBLISH_STATE(this->presence_via_uart_switch_, active);
77#endif
78 }
79 bool is_uart_presence_active() { return uart_presence_active_; }
80
81 void set_start_after_boot(bool start) {
82 start_after_boot_ = start;
83#ifdef USE_SWITCH
84 PUBLISH_STATE(this->start_after_boot_switch_, start);
85#endif
86 }
87 bool does_start_after_boot() { return start_after_boot_; }
88
89#ifdef USE_BINARY_SENSOR
90 SUB_BINARY_SENSOR(detected)
91#endif
92
93 // Commands run one at a time from a queue, at least 1 s apart, so loop() never blocks
94 bool enqueue(CommandType type, uint8_t arg = 0);
96 bool has_queue_room(size_t count) const;
98 void enqueue_det_range_cfg(float min1, float max1, float min2, float max2, float min3, float max3, float min4,
99 float max4);
101 void enqueue_set_latency(float delay_after_detection, float delay_after_disappear);
103 void enqueue_saved_setting(CommandType type, bool value);
105 void reset_system() { this->enqueue(CommandType::COMMAND_TYPE_RESET_SYSTEM); }
107 void format_command(const Command &cmd, char *buf, size_t size) const;
108
109 protected:
110 bool detected_{false};
111 bool active_{false};
112 bool led_active_{false};
114 bool start_after_boot_{false};
116 bool cmd_sent_{false};
117 uint8_t retries_left_{2};
120 // Range and latency of the queued commands, in sensor steps (0.15 m, 0.025 s); a later command overwrites them
121 int16_t det_range_steps_[8]{};
122 uint16_t latency_steps_[2]{};
123
125 const char *read_message_();
126 void read_state_();
128 bool run_command_(const Command &cmd, uint32_t now);
130 bool handle_reply_(const Command &cmd, const char *message);
131
132 void set_detected_(bool detected);
133};
134
135} // namespace esphome::dfrobot_sen0395
virtual void dump_config()
Collects bytes into NUL terminated lines of up to N - 1 characters; IGNORE_CHAR bytes are skipped and...
Definition line_reader.h:15
Fixed-size circular buffer with FIFO semantics and iteration support.
Definition helpers.h:408
StaticRingBuffer< Command, COMMAND_QUEUE_SIZE > cmd_queue_
LineReader< MMWAVE_READ_BUFFER_LENGTH > line_reader_
const char * read_message_()
The next complete line, valid until the next call, or nullptr.
bool handle_reply_(const Command &cmd, const char *message)
Handle a reply to a sent command; true when the command is done.
bool run_command_(const Command &cmd, uint32_t now)
Run the command at the front of the queue; true when it is done.
const LogString * message
Definition component.cpp:35
uint8_t type
unsigned count
Definition gif.cpp:51
uint32_t value
Definition hal.cpp:41
uint16_t size
Definition helpers.cpp:34
static void uint32_t