ESPHome 2026.11.0-dev
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
28
29#include <array>
30
32
33static constexpr uint8_t MAX_LINE_LENGTH = 80;
34static constexpr uint8_t MAX_COMMAND_LENGTH = 40;
35static constexpr uint8_t MAX_RESPONSE_VALUES = 3;
36
37enum class RunMode : uint8_t {
40};
41
55
63
69
88
99
101#ifdef USE_BINARY_SENSOR
102 SUB_BINARY_SENSOR(has_target)
103#endif
104#ifdef USE_SENSOR
105 SUB_SENSOR(target_distance)
106 SUB_SENSOR(target_speed)
107 SUB_SENSOR(target_energy)
108#endif
109#ifdef USE_TEXT_SENSOR
110 SUB_TEXT_SENSOR(version)
111 SUB_TEXT_SENSOR(hardware_version)
112#endif
113#ifdef USE_SELECT
114 SUB_SELECT(out_pin_level)
115#endif
116#ifdef USE_BUTTON
117 SUB_BUTTON(factory_reset)
118 SUB_BUTTON(query)
119 SUB_BUTTON(restart)
120#endif
121
122 public:
124
125 void setup() override;
126 void loop() override;
127 void dump_config() override;
128
129#ifdef USE_NUMBER
130 void set_number(NumberSetting setting, number::Number *number) { this->numbers_[setting] = number; }
131 void control_number(NumberSetting setting, float value);
132#endif
133#ifdef USE_SWITCH
134 void set_switch(SwitchSetting setting, switch_::Switch *sw) { this->switches_[setting] = sw; }
135 void control_switch(SwitchSetting setting, bool state);
136#endif
137#ifdef USE_SELECT
138 void control_out_pin_level(size_t index);
139#endif
141 void restart();
143 void factory_reset();
145 void query_params();
146
147 protected:
148 void readline_(uint8_t c);
149 void handle_line_(char *line);
150 void handle_frame_(char *line);
151 void log_bad_frame_(const LogString *problem, const char *line);
152 void parse_response_(const char *args);
153
155 uint16_t wanted_reads_() const;
156 void request_write_(Param param);
157 void run_sequencer_();
158 void check_frames_(uint32_t now);
159 bool build_command_(Operation operation, Param param);
160 bool apply_read_(Param param);
161 void finish_operation_();
162 void fail_operation_();
163 void hold_(uint32_t duration_ms);
167
168#ifdef USE_NUMBER
169 void publish_number_(NumberSetting setting, float value);
170 void forget_number_changes_(Param param, bool unknown);
171#endif
172#ifdef USE_SWITCH
173 void publish_switch_(SwitchSetting setting, bool state);
174#endif
175#ifdef USE_SENSOR
176 void publish_target_(float distance, float speed, float energy);
180#endif
181
182#ifdef USE_NUMBER
183 std::array<number::Number *, NUMBER_COUNT> numbers_{};
184 std::array<float, NUMBER_COUNT> number_values_;
185#endif
186#ifdef USE_SWITCH
187 std::array<switch_::Switch *, SWITCH_COUNT> switches_{};
188 std::array<bool, SWITCH_COUNT> switch_states_{};
189#endif
190
191 char line_[MAX_LINE_LENGTH];
192 char command_[MAX_COMMAND_LENGTH]{};
193 float response_[MAX_RESPONSE_VALUES]{};
200 uint16_t pending_writes_{0};
201 uint16_t pending_reads_{0};
202 // Reads made for a write whose paired value is unknown; the write is dropped when the read fails
203 uint16_t paired_reads_{0};
204 // Writes given up on that are tried again in a later sequence
205 uint16_t deferred_writes_{0};
206#ifdef USE_NUMBER
207 // Numbers changed since their last write was sent, one bit per NumberSetting
208 uint16_t changed_numbers_{0};
209#endif
213 uint8_t line_pos_{0};
214 uint8_t response_count_{0};
215 uint8_t out_pin_level_{0};
216 uint8_t attempts_{0};
217 uint8_t aux_attempts_{0};
218#ifdef USE_SWITCH
219 // The module cannot be asked for its LED states: the ones it last accepted, one bit per SwitchSetting
220 uint8_t led_known_{0};
221 uint8_t led_on_{0};
222 bool sent_led_on_{false};
223#endif
224 bool line_overflow_{false};
225 bool read_failed_{false};
226 bool bad_frame_logged_{false};
227 bool echo_seen_{false};
228 bool version_received_{false};
229 bool stopped_{false};
230 bool save_pending_{false};
231 bool deferred_save_{false};
232 bool restart_pending_{false};
234 bool sequence_failed_{false};
235};
236
237} // namespace esphome::dfrobot_sen0609
BedjetMode mode
BedJet operating mode.
Helper class to deduplicate items in a series of values.
Definition helpers.h:1969
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:38
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.
static void uint32_t