ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
seeed_mr60fda2.cpp
Go to the documentation of this file.
1#include "seeed_mr60fda2.h"
3#include "esphome/core/log.h"
4
5#include <cinttypes>
6#include <utility>
7
8namespace esphome {
9namespace seeed_mr60fda2 {
10
11static const char *const TAG = "seeed_mr60fda2";
12
13// Maximum bytes to log in verbose hex output
14static constexpr size_t MR60FDA2_MAX_LOG_BYTES = 64;
15
16// Prints the component's configuration data. dump_config() prints all of the component's configuration
17// items in an easy-to-read format, including the configuration key-value pairs.
19 ESP_LOGCONFIG(TAG, "MR60FDA2:");
20#ifdef USE_BINARY_SENSOR
21 LOG_BINARY_SENSOR(" ", "People Exist Binary Sensor", this->people_exist_binary_sensor_);
22 LOG_BINARY_SENSOR(" ", "Is Fall Binary Sensor", this->fall_detected_binary_sensor_);
23#endif
24#ifdef USE_BUTTON
25 LOG_BUTTON(" ", "Get Radar Parameters Button", this->get_radar_parameters_button_);
26 LOG_BUTTON(" ", "Reset Radar Button", this->factory_reset_button_);
27#endif
28#ifdef USE_SELECT
29 LOG_SELECT(" ", "Install Height Select", this->install_height_select_);
30 LOG_SELECT(" ", "Height Threshold Select", this->height_threshold_select_);
31 LOG_SELECT(" ", "Sensitivity Select", this->sensitivity_select_);
32#endif
33}
34
35// Initialisation functions
37 this->check_uart_settings(115200);
38
39 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
40 this->current_frame_id_ = 0;
41 this->current_frame_len_ = 0;
42 this->current_data_frame_len_ = 0;
43 this->current_frame_type_ = 0;
45
46 memset(this->current_frame_buf_, 0, FRAME_BUF_MAX_SIZE);
47 memset(this->current_data_buf_, 0, DATA_BUF_MAX_SIZE);
48}
49
50// main loop
52 uint8_t byte;
53
54 // Is there data on the serial port
55 while (this->available()) {
56 this->read_byte(&byte);
57 this->split_frame_(byte); // split data frame
58 }
59}
60
71static uint8_t calculate_checksum(const uint8_t *data, size_t len) {
72 uint8_t checksum = 0;
73 for (size_t i = 0; i < len; i++) {
74 checksum ^= data[i];
75 }
76 checksum = ~checksum;
77 return checksum;
78}
79
91static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
92 return calculate_checksum(data, len) == expected_checksum;
93}
94
95static uint8_t find_nearest_index(float value, const float *arr, int size) {
96 int nearest_index = 0;
97 float min_diff = std::abs(value - arr[0]);
98 for (int i = 1; i < size; ++i) {
99 float diff = std::abs(value - arr[i]);
100 if (diff < min_diff) {
101 min_diff = diff;
102 nearest_index = i;
103 }
104 }
105 return nearest_index;
106}
107
116static void float_to_bytes(float value, unsigned char *bytes) {
117 union {
118 float float_value;
119 unsigned char byte_array[4];
120 } u;
121
122 u.float_value = value;
123 memcpy(bytes, u.byte_array, 4);
124}
125
134static void int_to_bytes(uint32_t value, unsigned char *bytes) {
135 bytes[0] = value & 0xFF;
136 bytes[1] = (value >> 8) & 0xFF;
137 bytes[2] = (value >> 16) & 0xFF;
138 bytes[3] = (value >> 24) & 0xFF;
139}
140
141void MR60FDA2Component::split_frame_(uint8_t buffer) {
142 switch (this->current_frame_locate_) {
143 case LOCATE_FRAME_HEADER: // starting buffer
144 if (buffer == FRAME_HEADER_BUFFER) {
145 this->current_frame_len_ = 1;
146 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
147 this->current_frame_locate_++;
148 }
149 break;
150 case LOCATE_ID_FRAME1:
151 this->current_frame_id_ = buffer << 8;
152 this->current_frame_len_++;
153 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
154 this->current_frame_locate_++;
155 break;
156 case LOCATE_ID_FRAME2:
157 this->current_frame_id_ += buffer;
158 this->current_frame_len_++;
159 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
160 this->current_frame_locate_++;
161 break;
163 this->current_data_frame_len_ = buffer << 8;
164 if (this->current_data_frame_len_ == 0x00) {
165 this->current_frame_len_++;
166 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
167 this->current_frame_locate_++;
168 } else {
169 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
170 }
171 break;
173 this->current_data_frame_len_ += buffer;
174 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
175 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
176 } else {
177 this->current_frame_len_++;
178 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
179 this->current_frame_locate_++;
180 }
181 break;
183 this->current_frame_type_ = buffer << 8;
184 this->current_frame_len_++;
185 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
186 this->current_frame_locate_++;
187 break;
189 this->current_frame_type_ += buffer;
190 if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
191 (this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
192 (this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
193 (this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
194 this->current_frame_len_++;
195 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
196 this->current_frame_locate_++;
197 } else {
198 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
199 }
200 break;
202 if (validate_checksum(this->current_frame_buf_, this->current_frame_len_, buffer)) {
203 this->current_frame_len_++;
204 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
205 this->current_frame_locate_++;
206 } else {
207 ESP_LOGD(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer);
208#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
209 char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
210 char byte_buf[format_hex_pretty_size(1)];
211#endif
212 ESP_LOGV(TAG, "CURRENT_FRAME: %s %s",
213 format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
214 format_hex_pretty_to(byte_buf, &buffer, 1));
215 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
216 }
217 break;
219 this->current_frame_len_++;
220 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
221 this->current_data_buf_[this->current_frame_len_ - LEN_TO_DATA_FRAME] = buffer;
222 if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) {
223 this->current_frame_locate_++;
224 }
225 if (this->current_frame_len_ > FRAME_BUF_MAX_SIZE) {
226 ESP_LOGD(TAG, "PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM);
227 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
228 }
229 break;
231 if (validate_checksum(this->current_data_buf_, this->current_data_frame_len_, buffer)) {
232 this->current_frame_len_++;
233 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
234 this->current_frame_locate_++;
235 this->process_frame_();
236 } else {
237 ESP_LOGD(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
238#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
239 char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
240 char byte_buf[format_hex_pretty_size(1)];
241#endif
242 ESP_LOGV(TAG, "GET CURRENT_FRAME: %s %s",
243 format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
244 format_hex_pretty_to(byte_buf, &buffer, 1));
245
246 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
247 }
248 break;
249 default:
250 break;
251 }
252}
253
254void MR60FDA2Component::process_frame_() {
255 switch (this->current_frame_type_) {
256 case IS_FALL_TYPE_BUFFER:
257 if (this->fall_detected_binary_sensor_ != nullptr) {
258 this->fall_detected_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
259 }
260 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
261 break;
262
263 case PEOPLE_EXIST_TYPE_BUFFER:
264 if (this->people_exist_binary_sensor_ != nullptr)
265 this->people_exist_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
266 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
267 break;
268
269 case RESULT_INSTALL_HEIGHT:
270 if (this->current_data_buf_[0]) {
271 ESP_LOGD(TAG, "Successfully set the mounting height");
272 } else {
273 ESP_LOGD(TAG, "Failed to set the mounting height");
274 }
275 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
276 break;
277
278 case RESULT_HEIGHT_THRESHOLD:
279 if (this->current_data_buf_[0]) {
280 ESP_LOGD(TAG, "Successfully set the height threshold");
281 } else {
282 ESP_LOGD(TAG, "Failed to set the height threshold");
283 }
284 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
285 break;
286
287 case RESULT_SENSITIVITY:
288 if (this->current_data_buf_[0]) {
289 ESP_LOGD(TAG, "Successfully set the sensitivity");
290 } else {
291 ESP_LOGD(TAG, "Failed to set the sensitivity");
292 }
293 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
294 break;
295
296 case RESULT_PARAMETERS: {
297 float install_height_float = 0;
298 float height_threshold_float = 0;
299 uint32_t current_sensitivity = 0;
300 if (this->install_height_select_ != nullptr) {
301 uint32_t current_install_height_int =
302 encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
303
304 install_height_float = bit_cast<float>(current_install_height_int);
305 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
306 this->install_height_select_->publish_state(select_index);
307 }
308
309 if (this->height_threshold_select_ != nullptr) {
310 uint32_t current_height_threshold_int =
311 encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
312
313 height_threshold_float = bit_cast<float>(current_height_threshold_int);
314 size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
315 this->height_threshold_select_->publish_state(select_index);
316 }
317
318 if (this->sensitivity_select_ != nullptr) {
319 current_sensitivity =
320 encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
321
322 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
323 this->sensitivity_select_->publish_state(select_index);
324 }
325
326 ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
327 height_threshold_float, current_sensitivity);
328 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
329 break;
330 }
331 default:
332 break;
333 }
334}
335
336// Send Heartbeat Packet Command
338 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
339 float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
340 send_data[12] = calculate_checksum(send_data + 8, 4);
341 this->write_array(send_data, 13);
342#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
343 char hex_buf[format_hex_pretty_size(13)];
344#endif
345 ESP_LOGV(TAG, "SEND INSTALL HEIGHT FRAME: %s", format_hex_pretty_to(hex_buf, send_data, 13));
346}
347
349 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00};
350 float_to_bytes(HEIGHT_THRESHOLD[index], &send_data[8]);
351 send_data[12] = calculate_checksum(send_data + 8, 4);
352 this->write_array(send_data, 13);
353#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
354 char hex_buf[format_hex_pretty_size(13)];
355#endif
356 ESP_LOGV(TAG, "SEND HEIGHT THRESHOLD: %s", format_hex_pretty_to(hex_buf, send_data, 13));
357}
358
360 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
361
362 int_to_bytes(SENSITIVITY[index], &send_data[8]);
363
364 send_data[12] = calculate_checksum(send_data + 8, 4);
365 this->write_array(send_data, 13);
366#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
367 char hex_buf[format_hex_pretty_size(13)];
368#endif
369 ESP_LOGV(TAG, "SEND SET SENSITIVITY: %s", format_hex_pretty_to(hex_buf, send_data, 13));
370}
371
373 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
374 this->write_array(send_data, 8);
375#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
376 char hex_buf[format_hex_pretty_size(8)];
377#endif
378 ESP_LOGV(TAG, "SEND GET PARAMETERS: %s", format_hex_pretty_to(hex_buf, send_data, 8));
379}
380
382 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
383 this->write_array(send_data, 8);
384#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
385 char hex_buf[format_hex_pretty_size(8)];
386#endif
387 ESP_LOGV(TAG, "SEND RESET: %s", format_hex_pretty_to(hex_buf, send_data, 8));
388 this->get_radar_parameters();
389}
390
391} // namespace seeed_mr60fda2
392} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
void check_uart_settings(uint32_t baud_rate, uint8_t stop_bits=1, UARTParityOptions parity=UART_CONFIG_PARITY_NONE, uint8_t data_bits=8)
Check that the configuration of the UART bus matches the provided values and otherwise print a warnin...
Definition uart.cpp:12
bool read_byte(uint8_t *data)
Definition uart.h:34
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
std::vector< uint8_t > bytes
Definition sml_parser.h:13
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:533
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:331
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:735
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:428
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition helpers.h:71