ESPHome 2026.10.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
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->current_frame_locate_ = LOCATE_FRAME_HEADER;
37 this->current_frame_id_ = 0;
38 this->current_frame_len_ = 0;
39 this->current_data_frame_len_ = 0;
40 this->current_frame_type_ = 0;
42
43 memset(this->current_frame_buf_, 0, FRAME_BUF_MAX_SIZE);
44 memset(this->current_data_buf_, 0, DATA_BUF_MAX_SIZE);
45}
46
47// main loop
49 // Read all available bytes in batches to reduce UART call overhead.
50 size_t avail = this->available();
51 uint8_t buf[64];
52 while (avail > 0) {
53 size_t to_read = std::min(avail, sizeof(buf));
54 if (!this->read_array(buf, to_read)) {
55 break;
56 }
57 avail -= to_read;
58
59 for (size_t i = 0; i < to_read; i++) {
60 this->split_frame_(buf[i]); // split data frame
61 }
62 }
63}
64
75static uint8_t calculate_checksum(const uint8_t *data, size_t len) {
76 uint8_t checksum = 0;
77 for (size_t i = 0; i < len; i++) {
78 checksum ^= data[i];
79 }
80 checksum = ~checksum;
81 return checksum;
82}
83
95static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
96 return calculate_checksum(data, len) == expected_checksum;
97}
98
99static uint8_t find_nearest_index(float value, const float *arr, int size) {
100 int nearest_index = 0;
101 float min_diff = std::abs(value - arr[0]);
102 for (int i = 1; i < size; ++i) {
103 float diff = std::abs(value - arr[i]);
104 if (diff < min_diff) {
105 min_diff = diff;
106 nearest_index = i;
107 }
108 }
109 return nearest_index;
110}
111
120static void float_to_bytes(float value, unsigned char *bytes) {
121 union {
122 float float_value;
123 unsigned char byte_array[4];
124 } u;
125
126 u.float_value = value;
127 memcpy(bytes, u.byte_array, 4);
128}
129
138static void int_to_bytes(uint32_t value, unsigned char *bytes) {
139 bytes[0] = value & 0xFF;
140 bytes[1] = (value >> 8) & 0xFF;
141 bytes[2] = (value >> 16) & 0xFF;
142 bytes[3] = (value >> 24) & 0xFF;
143}
144
145void MR60FDA2Component::split_frame_(uint8_t buffer) {
146 switch (this->current_frame_locate_) {
147 case LOCATE_FRAME_HEADER: // starting buffer
148 if (buffer == FRAME_HEADER_BUFFER) {
149 this->current_frame_len_ = 0;
150 this->current_frame_buf_[this->current_frame_len_++] = buffer;
151 this->current_frame_locate_++;
152 }
153 break;
154 case LOCATE_ID_FRAME1:
155 this->current_frame_id_ = buffer << 8;
156 this->current_frame_buf_[this->current_frame_len_++] = buffer;
157 this->current_frame_locate_++;
158 break;
159 case LOCATE_ID_FRAME2:
160 this->current_frame_id_ += buffer;
161 this->current_frame_buf_[this->current_frame_len_++] = buffer;
162 this->current_frame_locate_++;
163 break;
165 this->current_data_frame_len_ = buffer << 8;
166 if (this->current_data_frame_len_ == 0) {
167 this->current_frame_buf_[this->current_frame_len_++] = buffer;
168 this->current_frame_locate_++;
169 } else {
170 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
171 }
172 break;
174 this->current_data_frame_len_ += buffer;
175 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
176 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
177 } else {
178 this->current_frame_buf_[this->current_frame_len_++] = buffer;
179 this->current_frame_locate_++;
180 }
181 break;
183 this->current_frame_type_ = buffer << 8;
184 this->current_frame_buf_[this->current_frame_len_++] = buffer;
185 this->current_frame_locate_++;
186 break;
188 this->current_frame_type_ += buffer;
189 if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
190 (this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
191 (this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
192 (this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
193 this->current_frame_buf_[this->current_frame_len_++] = buffer;
194 this->current_frame_locate_++;
195 } else {
196 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
197 }
198 break;
200 if (validate_checksum(this->current_frame_buf_, this->current_frame_len_, buffer)) {
201 this->current_frame_buf_[this->current_frame_len_++] = buffer;
202 this->current_frame_locate_++;
203 } else {
204 ESP_LOGD(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer);
205#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
206 char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
207 char byte_buf[format_hex_pretty_size(1)];
208#endif
209 ESP_LOGV(TAG, "CURRENT_FRAME: %s %s",
210 format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
211 format_hex_pretty_to(byte_buf, &buffer, 1));
212 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
213 }
214 break;
216 if (this->current_frame_len_ >= FRAME_BUF_MAX_SIZE) {
217 ESP_LOGD(TAG, "PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM);
218 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
219 break;
220 }
221 this->current_data_buf_[this->current_frame_len_ - LEN_TO_DATA_FRAME + 1] = buffer;
222 this->current_frame_buf_[this->current_frame_len_++] = buffer;
223 if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) {
224 this->current_frame_locate_++;
225 }
226 break;
228 if (validate_checksum(this->current_data_buf_, this->current_data_frame_len_, buffer)) {
229 this->current_frame_buf_[this->current_frame_len_++] = buffer;
230 this->current_frame_locate_++;
231 this->process_frame_();
232 } else {
233 ESP_LOGD(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
234#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
235 char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
236 char byte_buf[format_hex_pretty_size(1)];
237#endif
238 ESP_LOGV(TAG, "GET CURRENT_FRAME: %s %s",
239 format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
240 format_hex_pretty_to(byte_buf, &buffer, 1));
241
242 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
243 }
244 break;
245 default:
246 break;
247 }
248}
249
250void MR60FDA2Component::process_frame_() {
251 switch (this->current_frame_type_) {
252 case IS_FALL_TYPE_BUFFER:
253 if (this->fall_detected_binary_sensor_ != nullptr) {
254 this->fall_detected_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
255 }
256 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
257 break;
258
259 case PEOPLE_EXIST_TYPE_BUFFER:
260 if (this->people_exist_binary_sensor_ != nullptr)
261 this->people_exist_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
262 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
263 break;
264
265 case RESULT_INSTALL_HEIGHT:
266 if (this->current_data_buf_[0]) {
267 ESP_LOGD(TAG, "Successfully set the mounting height");
268 } else {
269 ESP_LOGD(TAG, "Failed to set the mounting height");
270 }
271 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
272 break;
273
274 case RESULT_HEIGHT_THRESHOLD:
275 if (this->current_data_buf_[0]) {
276 ESP_LOGD(TAG, "Successfully set the height threshold");
277 } else {
278 ESP_LOGD(TAG, "Failed to set the height threshold");
279 }
280 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
281 break;
282
283 case RESULT_SENSITIVITY:
284 if (this->current_data_buf_[0]) {
285 ESP_LOGD(TAG, "Successfully set the sensitivity");
286 } else {
287 ESP_LOGD(TAG, "Failed to set the sensitivity");
288 }
289 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
290 break;
291
292 case RESULT_PARAMETERS: {
293 float install_height_float = 0;
294 float height_threshold_float = 0;
295 uint32_t current_sensitivity = 0;
296 if (this->install_height_select_ != nullptr) {
297 uint32_t current_install_height_int =
298 encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
299
300 install_height_float = bit_cast<float>(current_install_height_int);
301 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
302 this->install_height_select_->publish_state(select_index);
303 }
304
305 if (this->height_threshold_select_ != nullptr) {
306 uint32_t current_height_threshold_int =
307 encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
308
309 height_threshold_float = bit_cast<float>(current_height_threshold_int);
310 size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
311 this->height_threshold_select_->publish_state(select_index);
312 }
313
314 if (this->sensitivity_select_ != nullptr) {
315 current_sensitivity =
316 encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
317
318 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
319 this->sensitivity_select_->publish_state(select_index);
320 }
321
322 ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
323 height_threshold_float, current_sensitivity);
324 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
325 break;
326 }
327 default:
328 break;
329 }
330}
331
332// Send Heartbeat Packet Command
334 if (index >= std::size(INSTALL_HEIGHT))
335 return;
336 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
337 float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
338 send_data[12] = calculate_checksum(send_data + 8, 4);
339 this->write_array(send_data, 13);
340#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
341 char hex_buf[format_hex_pretty_size(13)];
342#endif
343 ESP_LOGV(TAG, "SEND INSTALL HEIGHT FRAME: %s", format_hex_pretty_to(hex_buf, send_data, 13));
344}
345
347 if (index >= std::size(HEIGHT_THRESHOLD))
348 return;
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 if (index >= std::size(SENSITIVITY))
361 return;
362 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
363
364 int_to_bytes(SENSITIVITY[index], &send_data[8]);
365
366 send_data[12] = calculate_checksum(send_data + 8, 4);
367 this->write_array(send_data, 13);
368#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
369 char hex_buf[format_hex_pretty_size(13)];
370#endif
371 ESP_LOGV(TAG, "SEND SET SENSITIVITY: %s", format_hex_pretty_to(hex_buf, send_data, 13));
372}
373
375 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
376 this->write_array(send_data, 8);
377#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
378 char hex_buf[format_hex_pretty_size(8)];
379#endif
380 ESP_LOGV(TAG, "SEND GET PARAMETERS: %s", format_hex_pretty_to(hex_buf, send_data, 8));
381}
382
384 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
385 this->write_array(send_data, 8);
386#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
387 char hex_buf[format_hex_pretty_size(8)];
388#endif
389 ESP_LOGV(TAG, "SEND RESET: %s", format_hex_pretty_to(hex_buf, send_data, 8));
390 this->get_radar_parameters();
391}
392
393} // namespace esphome::seeed_mr60fda2
uint8_t checksum
Definition bl0906.h:3
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:39
void write_array(const uint8_t *data, size_t len)
Definition uart.h:27
std::vector< uint8_t > bytes
Definition sml_parser.h:12
const void size_t len
Definition hal.h:64
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:425
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:1438
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:892
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition helpers.h:84
static void uint32_t