ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
dfrobot_sen0609.h
Go to the documentation of this file.
1#pragma once
2
6#ifdef USE_BINARY_SENSOR
8#endif
9#ifdef USE_SENSOR
11#endif
12#ifdef USE_TEXT_SENSOR
14#endif
15#ifdef USE_NUMBER
17#endif
18#ifdef USE_SWITCH
20#endif
21#ifdef USE_SELECT
23#endif
24#ifdef USE_BUTTON
26#endif
29
30#include <array>
31
33
34static constexpr uint8_t MAX_LINE_LENGTH = 80;
35static constexpr uint8_t MAX_COMMAND_LENGTH = 40;
36static constexpr uint8_t MAX_RESPONSE_VALUES = 3;
37
38enum class RunMode : uint8_t {
41};
42
56
64
83
94
96#ifdef USE_BINARY_SENSOR
97 SUB_BINARY_SENSOR(has_target)
98#endif
99#ifdef USE_SENSOR
100 SUB_SENSOR_WITH_DEDUP(target_distance, float)
101 SUB_SENSOR_WITH_DEDUP(target_speed, float)
102 SUB_SENSOR_WITH_DEDUP(target_energy, float)
103#endif
104#ifdef USE_TEXT_SENSOR
105 SUB_TEXT_SENSOR(version)
106 SUB_TEXT_SENSOR(hardware_version)
107#endif
108#ifdef USE_SELECT
109 SUB_SELECT(out_pin_level)
110#endif
111#ifdef USE_BUTTON
112 SUB_BUTTON(factory_reset)
113 SUB_BUTTON(query)
114 SUB_BUTTON(restart)
115#endif
116
117 public:
119
120 void setup() override;
121 void loop() override;
122 void dump_config() override;
123
124#ifdef USE_NUMBER
125 void set_number(NumberSetting setting, number::Number *number) { this->numbers_[setting] = number; }
126 void control_number(NumberSetting setting, float value);
127 // Finds the setting by pointer so every number shares one Apply function
128 void control_number(number::Number *number, float value);
129#endif
130#ifdef USE_SWITCH
131 void set_switch(SwitchSetting setting, switch_::Switch *sw) { this->switches_[setting] = sw; }
132 void control_switch(SwitchSetting setting, bool state);
133#endif
134#ifdef USE_SELECT
135 void control_out_pin_level(size_t index);
136#endif
138 void restart();
140 void factory_reset();
142 void query_params();
143
144 protected:
145 void readline_(uint8_t c);
146 void handle_line_(char *line);
147 void handle_frame_(char *line);
148 void log_bad_frame_(const LogString *problem, const char *line);
149 void parse_response_(const char *args);
150
152 uint16_t wanted_reads_() const;
153 void request_write_(Param param);
154 void run_sequencer_();
155 void check_frames_(uint32_t now);
156 bool build_command_(Operation operation, Param param);
157 bool apply_read_(Param param);
158 void finish_operation_();
159 void fail_operation_();
160 void hold_(uint32_t duration_ms);
164
165#ifdef USE_NUMBER
166 void publish_number_(NumberSetting setting, float value);
167 void forget_number_changes_(Param param, bool unknown);
168#endif
169#ifdef USE_SWITCH
170 void publish_switch_(SwitchSetting setting, bool state);
171#endif
172#ifdef USE_SENSOR
173 void publish_target_(float distance, float speed, float energy);
174#endif
175
176#ifdef USE_NUMBER
177 std::array<number::Number *, NUMBER_COUNT> numbers_{};
178 std::array<float, NUMBER_COUNT> number_values_;
179#endif
180#ifdef USE_SWITCH
181 std::array<switch_::Switch *, SWITCH_COUNT> switches_{};
182 std::array<bool, SWITCH_COUNT> switch_states_{};
183#endif
184
186 char command_[MAX_COMMAND_LENGTH]{};
187 float response_[MAX_RESPONSE_VALUES]{};
194 uint16_t pending_writes_{0};
195 uint16_t pending_reads_{0};
196 // Reads made for a write whose paired value is unknown; the write is dropped when the read fails
197 uint16_t paired_reads_{0};
198 // Writes given up on that are tried again in a later sequence
199 uint16_t deferred_writes_{0};
200#ifdef USE_NUMBER
201 // Numbers changed since their last write was sent, one bit per NumberSetting
202 uint16_t changed_numbers_{0};
203#endif
207 uint8_t response_count_{0};
208 uint8_t out_pin_level_{0};
209 uint8_t attempts_{0};
210 uint8_t aux_attempts_{0};
211#ifdef USE_SWITCH
212 // The module cannot be asked for its LED states: the ones it last accepted, one bit per SwitchSetting
213 uint8_t led_known_{0};
214 uint8_t led_on_{0};
215 bool sent_led_on_{false};
216#endif
217 bool read_failed_{false};
218 bool bad_frame_logged_{false};
219 bool echo_seen_{false};
220 bool version_received_{false};
221 bool stopped_{false};
222 bool save_pending_{false};
223 bool deferred_save_{false};
224 bool restart_pending_{false};
226 bool sequence_failed_{false};
227};
228
229} // namespace esphome::dfrobot_sen0609
BedjetMode mode
BedJet operating mode.
Collects bytes into NUL terminated lines of up to N - 1 characters; IGNORE_CHAR bytes are skipped and...
Definition line_reader.h:15
void log_bad_frame_(const LogString *problem, const char *line)
std::array< switch_::Switch *, SWITCH_COUNT > switches_
void publish_switch_(SwitchSetting setting, bool state)
bool build_command_(Operation operation, Param param)
std::array< bool, SWITCH_COUNT > switch_states_
void forget_number_changes_(Param param, bool unknown)
std::array< float, NUMBER_COUNT > number_values_
void publish_number_(NumberSetting setting, float value)
std::array< number::Number *, NUMBER_COUNT > numbers_
void publish_target_(float distance, float speed, float energy)
Base-class for all numbers.
Definition number.h:29
Base class for all switches.
Definition switch.h:43
bool state
Definition fan.h:2
NumberSetting
Settings shown as number entities.
Param
Module parameters; a write or read of each is one command. Pending work runs in this order.
SwitchSetting
Settings shown as switch entities.
uint32_t value
Definition hal.cpp:41
static void uint32_t