ESPHome 2026.11.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
10ESPHOME_LOG_TAG(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
65// The checksum is the inverted XOR of the bytes
66static uint8_t calculate_checksum(const uint8_t *data, size_t len) { return ~xor_bytes(data, len); }
67
79static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
80 return calculate_checksum(data, len) == expected_checksum;
81}
82
83#ifdef USE_SELECT
84static uint8_t find_nearest_index(float value, const float *arr, int size) {
85 int nearest_index = 0;
86 float min_diff = std::abs(value - arr[0]);
87 for (int i = 1; i < size; ++i) {
88 float diff = std::abs(value - arr[i]);
89 if (diff < min_diff) {
90 min_diff = diff;
91 nearest_index = i;
92 }
93 }
94 return nearest_index;
95}
96#endif
97
106static void float_to_bytes(float value, unsigned char *bytes) {
107 union {
108 float float_value;
109 unsigned char byte_array[4];
110 } u;
111
112 u.float_value = value;
113 memcpy(bytes, u.byte_array, 4);
114}
115
124static void int_to_bytes(uint32_t value, unsigned char *bytes) {
125 bytes[0] = value & 0xFF;
126 bytes[1] = (value >> 8) & 0xFF;
127 bytes[2] = (value >> 16) & 0xFF;
128 bytes[3] = (value >> 24) & 0xFF;
129}
130
131void MR60FDA2Component::split_frame_(uint8_t buffer) {
132 switch (this->current_frame_locate_) {
133 case LOCATE_FRAME_HEADER: // starting buffer
134 if (buffer == FRAME_HEADER_BUFFER) {
135 this->current_frame_len_ = 0;
136 this->current_frame_buf_[this->current_frame_len_++] = buffer;
137 this->current_frame_locate_++;
138 }
139 break;
140 case LOCATE_ID_FRAME1:
141 this->current_frame_id_ = buffer << 8;
142 this->current_frame_buf_[this->current_frame_len_++] = buffer;
143 this->current_frame_locate_++;
144 break;
145 case LOCATE_ID_FRAME2:
146 this->current_frame_id_ += buffer;
147 this->current_frame_buf_[this->current_frame_len_++] = buffer;
148 this->current_frame_locate_++;
149 break;
151 this->current_data_frame_len_ = buffer << 8;
152 if (this->current_data_frame_len_ == 0) {
153 this->current_frame_buf_[this->current_frame_len_++] = buffer;
154 this->current_frame_locate_++;
155 } else {
156 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
157 }
158 break;
160 this->current_data_frame_len_ += buffer;
161 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
162 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
163 } else {
164 this->current_frame_buf_[this->current_frame_len_++] = buffer;
165 this->current_frame_locate_++;
166 }
167 break;
169 this->current_frame_type_ = buffer << 8;
170 this->current_frame_buf_[this->current_frame_len_++] = buffer;
171 this->current_frame_locate_++;
172 break;
174 this->current_frame_type_ += buffer;
175 if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
176 (this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
177 (this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
178 (this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
179 this->current_frame_buf_[this->current_frame_len_++] = buffer;
180 this->current_frame_locate_++;
181 } else {
182 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
183 }
184 break;
186 if (validate_checksum(this->current_frame_buf_, this->current_frame_len_, buffer)) {
187 this->current_frame_buf_[this->current_frame_len_++] = buffer;
188 this->current_frame_locate_++;
189 } else {
190 ESP_LOGD(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer);
191#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
192 char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
193 char byte_buf[format_hex_pretty_size(1)];
194#endif
195 ESP_LOGV(TAG, "CURRENT_FRAME: %s %s",
196 format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
197 format_hex_pretty_to(byte_buf, &buffer, 1));
198 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
199 }
200 break;
202 if (this->current_frame_len_ >= FRAME_BUF_MAX_SIZE) {
203 ESP_LOGD(TAG, "PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM);
204 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
205 break;
206 }
207 this->current_data_buf_[this->current_frame_len_ - LEN_TO_DATA_FRAME + 1] = buffer;
208 this->current_frame_buf_[this->current_frame_len_++] = buffer;
209 if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) {
210 this->current_frame_locate_++;
211 }
212 break;
214 if (validate_checksum(this->current_data_buf_, this->current_data_frame_len_, buffer)) {
215 this->current_frame_buf_[this->current_frame_len_++] = buffer;
216 this->current_frame_locate_++;
217 this->process_frame_();
218 } else {
219 ESP_LOGD(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
220#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
221 char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
222 char byte_buf[format_hex_pretty_size(1)];
223#endif
224 ESP_LOGV(TAG, "GET CURRENT_FRAME: %s %s",
225 format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
226 format_hex_pretty_to(byte_buf, &buffer, 1));
227
228 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
229 }
230 break;
231 default:
232 break;
233 }
234}
235
236void MR60FDA2Component::process_frame_() {
237 switch (this->current_frame_type_) {
238 case IS_FALL_TYPE_BUFFER:
239#ifdef USE_BINARY_SENSOR
240 if (this->fall_detected_binary_sensor_ != nullptr) {
241 this->fall_detected_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
242 }
243#endif
244 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
245 break;
246
247 case PEOPLE_EXIST_TYPE_BUFFER:
248#ifdef USE_BINARY_SENSOR
249 if (this->people_exist_binary_sensor_ != nullptr)
250 this->people_exist_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
251#endif
252 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
253 break;
254
255 case RESULT_INSTALL_HEIGHT:
256 if (this->current_data_buf_[0]) {
257 ESP_LOGD(TAG, "Successfully set the mounting height");
258 } else {
259 ESP_LOGD(TAG, "Failed to set the mounting height");
260 }
261 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
262 break;
263
264 case RESULT_HEIGHT_THRESHOLD:
265 if (this->current_data_buf_[0]) {
266 ESP_LOGD(TAG, "Successfully set the height threshold");
267 } else {
268 ESP_LOGD(TAG, "Failed to set the height threshold");
269 }
270 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
271 break;
272
273 case RESULT_SENSITIVITY:
274 if (this->current_data_buf_[0]) {
275 ESP_LOGD(TAG, "Successfully set the sensitivity");
276 } else {
277 ESP_LOGD(TAG, "Failed to set the sensitivity");
278 }
279 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
280 break;
281
282 case RESULT_PARAMETERS: {
283 float install_height_float = 0;
284 float height_threshold_float = 0;
285 uint32_t current_sensitivity = 0;
286#ifdef USE_SELECT
287 if (this->install_height_select_ != nullptr) {
288 uint32_t current_install_height_int =
289 encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
290
291 install_height_float = bit_cast<float>(current_install_height_int);
292 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
293 this->install_height_select_->publish_state(select_index);
294 }
295
296 if (this->height_threshold_select_ != nullptr) {
297 uint32_t current_height_threshold_int =
298 encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
299
300 height_threshold_float = bit_cast<float>(current_height_threshold_int);
301 size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
302 this->height_threshold_select_->publish_state(select_index);
303 }
304
305 if (this->sensitivity_select_ != nullptr) {
306 current_sensitivity =
307 encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
308
309 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
310 this->sensitivity_select_->publish_state(select_index);
311 }
312#endif
313
314 ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
315 height_threshold_float, current_sensitivity);
316 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
317 break;
318 }
319 default:
320 break;
321 }
322}
323
324// Send Heartbeat Packet Command
326 if (index >= std::size(INSTALL_HEIGHT))
327 return;
328 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
329 float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
330 send_data[12] = calculate_checksum(send_data + 8, 4);
331 this->write_array(send_data, 13);
332 ESP_LOGV_HEX(TAG, send_data, 13, 13, "SEND INSTALL HEIGHT FRAME: %s");
333}
334
336 if (index >= std::size(HEIGHT_THRESHOLD))
337 return;
338 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00};
339 float_to_bytes(HEIGHT_THRESHOLD[index], &send_data[8]);
340 send_data[12] = calculate_checksum(send_data + 8, 4);
341 this->write_array(send_data, 13);
342 ESP_LOGV_HEX(TAG, send_data, 13, 13, "SEND HEIGHT THRESHOLD: %s");
343}
344
346 if (index >= std::size(SENSITIVITY))
347 return;
348 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
349
350 int_to_bytes(SENSITIVITY[index], &send_data[8]);
351
352 send_data[12] = calculate_checksum(send_data + 8, 4);
353 this->write_array(send_data, 13);
354 ESP_LOGV_HEX(TAG, send_data, 13, 13, "SEND SET SENSITIVITY: %s");
355}
356
358 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
359 this->write_array(send_data, 8);
360 ESP_LOGV_HEX(TAG, send_data, 8, 8, "SEND GET PARAMETERS: %s");
361}
362
364 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
365 this->write_array(send_data, 8);
366 ESP_LOGV_HEX(TAG, send_data, 8, 8, "SEND RESET: %s");
367 this->get_radar_parameters();
368}
369
370} // namespace esphome::seeed_mr60fda2
optional< std::array< uint8_t, N > > read_array()
Definition uart.h:43
void write_array(const uint8_t *data, size_t len)
Definition uart.h:30
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "seeed_mr60fda2")
std::vector< uint8_t > bytes
Definition sml_parser.h:12
value[LAYOUT.day_index()]
const void size_t len
Definition hal.h:68
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:498
uint16_t size
Definition helpers.cpp:34
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:1609
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:1006
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition helpers.h:84
static void uint32_t