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