ESPHome 2026.5.0b1
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
9
10static const char *const TAG = "seeed_mr60fda2";
11
12// Maximum bytes to log in verbose hex output
13static constexpr size_t MR60FDA2_MAX_LOG_BYTES = 64;
14
15// Prints the component's configuration data. dump_config() prints all of the component's configuration
16// items in an easy-to-read format, including the configuration key-value pairs.
18 ESP_LOGCONFIG(TAG, "MR60FDA2:");
19#ifdef USE_BINARY_SENSOR
20 LOG_BINARY_SENSOR(" ", "People Exist Binary Sensor", this->people_exist_binary_sensor_);
21 LOG_BINARY_SENSOR(" ", "Is Fall Binary Sensor", this->fall_detected_binary_sensor_);
22#endif
23#ifdef USE_BUTTON
24 LOG_BUTTON(" ", "Get Radar Parameters Button", this->get_radar_parameters_button_);
25 LOG_BUTTON(" ", "Reset Radar Button", this->factory_reset_button_);
26#endif
27#ifdef USE_SELECT
28 LOG_SELECT(" ", "Install Height Select", this->install_height_select_);
29 LOG_SELECT(" ", "Height Threshold Select", this->height_threshold_select_);
30 LOG_SELECT(" ", "Sensitivity Select", this->sensitivity_select_);
31#endif
32}
33
34// Initialisation functions
36 this->check_uart_settings(115200);
37
38 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
39 this->current_frame_id_ = 0;
40 this->current_frame_len_ = 0;
41 this->current_data_frame_len_ = 0;
42 this->current_frame_type_ = 0;
44
45 memset(this->current_frame_buf_, 0, FRAME_BUF_MAX_SIZE);
46 memset(this->current_data_buf_, 0, DATA_BUF_MAX_SIZE);
47}
48
49// main loop
51 // Read all available bytes in batches to reduce UART call overhead.
52 size_t avail = this->available();
53 uint8_t buf[64];
54 while (avail > 0) {
55 size_t to_read = std::min(avail, sizeof(buf));
56 if (!this->read_array(buf, to_read)) {
57 break;
58 }
59 avail -= to_read;
60
61 for (size_t i = 0; i < to_read; i++) {
62 this->split_frame_(buf[i]); // split data frame
63 }
64 }
65}
66
77static uint8_t calculate_checksum(const uint8_t *data, size_t len) {
78 uint8_t checksum = 0;
79 for (size_t i = 0; i < len; i++) {
80 checksum ^= data[i];
81 }
82 checksum = ~checksum;
83 return checksum;
84}
85
97static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
98 return calculate_checksum(data, len) == expected_checksum;
99}
100
101static uint8_t find_nearest_index(float value, const float *arr, int size) {
102 int nearest_index = 0;
103 float min_diff = std::abs(value - arr[0]);
104 for (int i = 1; i < size; ++i) {
105 float diff = std::abs(value - arr[i]);
106 if (diff < min_diff) {
107 min_diff = diff;
108 nearest_index = i;
109 }
110 }
111 return nearest_index;
112}
113
122static void float_to_bytes(float value, unsigned char *bytes) {
123 union {
124 float float_value;
125 unsigned char byte_array[4];
126 } u;
127
128 u.float_value = value;
129 memcpy(bytes, u.byte_array, 4);
130}
131
140static void int_to_bytes(uint32_t value, unsigned char *bytes) {
141 bytes[0] = value & 0xFF;
142 bytes[1] = (value >> 8) & 0xFF;
143 bytes[2] = (value >> 16) & 0xFF;
144 bytes[3] = (value >> 24) & 0xFF;
145}
146
147void MR60FDA2Component::split_frame_(uint8_t buffer) {
148 switch (this->current_frame_locate_) {
149 case LOCATE_FRAME_HEADER: // starting buffer
150 if (buffer == FRAME_HEADER_BUFFER) {
151 this->current_frame_len_ = 0;
152 this->current_frame_buf_[this->current_frame_len_++] = buffer;
153 this->current_frame_locate_++;
154 }
155 break;
156 case LOCATE_ID_FRAME1:
157 this->current_frame_id_ = buffer << 8;
158 this->current_frame_buf_[this->current_frame_len_++] = buffer;
159 this->current_frame_locate_++;
160 break;
161 case LOCATE_ID_FRAME2:
162 this->current_frame_id_ += buffer;
163 this->current_frame_buf_[this->current_frame_len_++] = buffer;
164 this->current_frame_locate_++;
165 break;
167 this->current_data_frame_len_ = buffer << 8;
168 if (this->current_data_frame_len_ == 0) {
169 this->current_frame_buf_[this->current_frame_len_++] = buffer;
170 this->current_frame_locate_++;
171 } else {
172 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
173 }
174 break;
176 this->current_data_frame_len_ += buffer;
177 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
178 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
179 } else {
180 this->current_frame_buf_[this->current_frame_len_++] = buffer;
181 this->current_frame_locate_++;
182 }
183 break;
185 this->current_frame_type_ = buffer << 8;
186 this->current_frame_buf_[this->current_frame_len_++] = buffer;
187 this->current_frame_locate_++;
188 break;
190 this->current_frame_type_ += buffer;
191 if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
192 (this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
193 (this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
194 (this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
195 this->current_frame_buf_[this->current_frame_len_++] = 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_buf_[this->current_frame_len_++] = buffer;
204 this->current_frame_locate_++;
205 } else {
206 ESP_LOGD(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer);
207#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
208 char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
209 char byte_buf[format_hex_pretty_size(1)];
210#endif
211 ESP_LOGV(TAG, "CURRENT_FRAME: %s %s",
212 format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
213 format_hex_pretty_to(byte_buf, &buffer, 1));
214 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
215 }
216 break;
218 if (this->current_frame_len_ >= FRAME_BUF_MAX_SIZE) {
219 ESP_LOGD(TAG, "PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM);
220 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
221 break;
222 }
223 this->current_data_buf_[this->current_frame_len_ - LEN_TO_DATA_FRAME + 1] = buffer;
224 this->current_frame_buf_[this->current_frame_len_++] = buffer;
225 if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) {
226 this->current_frame_locate_++;
227 }
228 break;
230 if (validate_checksum(this->current_data_buf_, this->current_data_frame_len_, buffer)) {
231 this->current_frame_buf_[this->current_frame_len_++] = buffer;
232 this->current_frame_locate_++;
233 this->process_frame_();
234 } else {
235 ESP_LOGD(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
236#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
237 char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
238 char byte_buf[format_hex_pretty_size(1)];
239#endif
240 ESP_LOGV(TAG, "GET CURRENT_FRAME: %s %s",
241 format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
242 format_hex_pretty_to(byte_buf, &buffer, 1));
243
244 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
245 }
246 break;
247 default:
248 break;
249 }
250}
251
252void MR60FDA2Component::process_frame_() {
253 switch (this->current_frame_type_) {
254 case IS_FALL_TYPE_BUFFER:
255 if (this->fall_detected_binary_sensor_ != nullptr) {
256 this->fall_detected_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
257 }
258 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
259 break;
260
261 case PEOPLE_EXIST_TYPE_BUFFER:
262 if (this->people_exist_binary_sensor_ != nullptr)
263 this->people_exist_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
264 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
265 break;
266
267 case RESULT_INSTALL_HEIGHT:
268 if (this->current_data_buf_[0]) {
269 ESP_LOGD(TAG, "Successfully set the mounting height");
270 } else {
271 ESP_LOGD(TAG, "Failed to set the mounting height");
272 }
273 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
274 break;
275
276 case RESULT_HEIGHT_THRESHOLD:
277 if (this->current_data_buf_[0]) {
278 ESP_LOGD(TAG, "Successfully set the height threshold");
279 } else {
280 ESP_LOGD(TAG, "Failed to set the height threshold");
281 }
282 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
283 break;
284
285 case RESULT_SENSITIVITY:
286 if (this->current_data_buf_[0]) {
287 ESP_LOGD(TAG, "Successfully set the sensitivity");
288 } else {
289 ESP_LOGD(TAG, "Failed to set the sensitivity");
290 }
291 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
292 break;
293
294 case RESULT_PARAMETERS: {
295 float install_height_float = 0;
296 float height_threshold_float = 0;
297 uint32_t current_sensitivity = 0;
298 if (this->install_height_select_ != nullptr) {
299 uint32_t current_install_height_int =
300 encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
301
302 install_height_float = bit_cast<float>(current_install_height_int);
303 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
304 this->install_height_select_->publish_state(select_index);
305 }
306
307 if (this->height_threshold_select_ != nullptr) {
308 uint32_t current_height_threshold_int =
309 encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
310
311 height_threshold_float = bit_cast<float>(current_height_threshold_int);
312 size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
313 this->height_threshold_select_->publish_state(select_index);
314 }
315
316 if (this->sensitivity_select_ != nullptr) {
317 current_sensitivity =
318 encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
319
320 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
321 this->sensitivity_select_->publish_state(select_index);
322 }
323
324 ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
325 height_threshold_float, current_sensitivity);
326 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
327 break;
328 }
329 default:
330 break;
331 }
332}
333
334// Send Heartbeat Packet Command
336 if (index >= std::size(INSTALL_HEIGHT))
337 return;
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 if (index >= std::size(HEIGHT_THRESHOLD))
350 return;
351 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00};
352 float_to_bytes(HEIGHT_THRESHOLD[index], &send_data[8]);
353 send_data[12] = calculate_checksum(send_data + 8, 4);
354 this->write_array(send_data, 13);
355#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
356 char hex_buf[format_hex_pretty_size(13)];
357#endif
358 ESP_LOGV(TAG, "SEND HEIGHT THRESHOLD: %s", format_hex_pretty_to(hex_buf, send_data, 13));
359}
360
362 if (index >= std::size(SENSITIVITY))
363 return;
364 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
365
366 int_to_bytes(SENSITIVITY[index], &send_data[8]);
367
368 send_data[12] = calculate_checksum(send_data + 8, 4);
369 this->write_array(send_data, 13);
370#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
371 char hex_buf[format_hex_pretty_size(13)];
372#endif
373 ESP_LOGV(TAG, "SEND SET SENSITIVITY: %s", format_hex_pretty_to(hex_buf, send_data, 13));
374}
375
377 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
378 this->write_array(send_data, 8);
379#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
380 char hex_buf[format_hex_pretty_size(8)];
381#endif
382 ESP_LOGV(TAG, "SEND GET PARAMETERS: %s", format_hex_pretty_to(hex_buf, send_data, 8));
383}
384
386 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
387 this->write_array(send_data, 8);
388#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
389 char hex_buf[format_hex_pretty_size(8)];
390#endif
391 ESP_LOGV(TAG, "SEND RESET: %s", format_hex_pretty_to(hex_buf, send_data, 8));
392 this->get_radar_parameters();
393}
394
395} // namespace esphome::seeed_mr60fda2
uint8_t checksum
Definition bl0906.h:3
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:38
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:16
void write_array(const uint8_t *data, size_t len)
Definition uart.h:26
std::vector< uint8_t > bytes
Definition sml_parser.h:12
std::string size_t len
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:341
uint16_t size
Definition helpers.cpp:25
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:1386
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:867
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition helpers.h:84
static void uint32_t