ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
seeed_mr60fda2.cpp
Go to the documentation of this file.
1#include "seeed_mr60fda2.h"
2#include "esphome/core/hal.h"
4#include "esphome/core/log.h"
5
6#include <cinttypes>
7#include <utility>
8
10
11ESPHOME_LOG_TAG(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->current_frame_locate_ = LOCATE_FRAME_HEADER;
38 this->current_frame_id_ = 0;
39 this->current_frame_len_ = 0;
40 this->current_data_frame_len_ = 0;
41 this->current_frame_type_ = 0;
43
44 memset(this->current_frame_buf_, 0, FRAME_BUF_MAX_SIZE);
45 memset(this->current_data_buf_, 0, DATA_BUF_MAX_SIZE);
46}
47
48// main loop
50 this->read_available([this](uint8_t b) { this->split_frame_(b); });
51}
52
53// The checksum is the inverted XOR of the bytes
54static uint8_t calculate_checksum(const uint8_t *data, size_t len) { return ~xor_bytes(data, len); }
55
67static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
68 return calculate_checksum(data, len) == expected_checksum;
69}
70
71#ifdef USE_SELECT
72static uint8_t find_nearest_index(float value, const float *arr, int size) {
73 int nearest_index = 0;
74 float min_diff = std::abs(value - arr[0]);
75 for (int i = 1; i < size; ++i) {
76 float diff = std::abs(value - arr[i]);
77 if (diff < min_diff) {
78 min_diff = diff;
79 nearest_index = i;
80 }
81 }
82 return nearest_index;
83}
84#endif
85
86// Read with 32-bit loads or progmem_memcpy only; the radar takes the floats little-endian
87alignas(4) static const float INSTALL_HEIGHT[7] PROGMEM = {2.4f, 2.5f, 2.6f, 2.7f, 2.8f, 2.9f, 3.0f};
88alignas(4) static const float HEIGHT_THRESHOLD[7] PROGMEM = {0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.5f, 0.6f};
89alignas(4) static const float SENSITIVITY[3] PROGMEM = {3, 15, 30};
90
91void MR60FDA2Component::split_frame_(uint8_t buffer) {
92 switch (this->current_frame_locate_) {
93 case LOCATE_FRAME_HEADER: // starting buffer
94 if (buffer == FRAME_HEADER_BUFFER) {
95 this->current_frame_len_ = 0;
96 this->current_frame_buf_[this->current_frame_len_++] = buffer;
97 this->current_frame_locate_++;
98 }
99 break;
100 case LOCATE_ID_FRAME1:
101 this->current_frame_id_ = buffer << 8;
102 this->current_frame_buf_[this->current_frame_len_++] = buffer;
103 this->current_frame_locate_++;
104 break;
105 case LOCATE_ID_FRAME2:
106 this->current_frame_id_ += buffer;
107 this->current_frame_buf_[this->current_frame_len_++] = buffer;
108 this->current_frame_locate_++;
109 break;
111 this->current_data_frame_len_ = buffer << 8;
112 if (this->current_data_frame_len_ == 0) {
113 this->current_frame_buf_[this->current_frame_len_++] = buffer;
114 this->current_frame_locate_++;
115 } else {
116 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
117 }
118 break;
120 this->current_data_frame_len_ += buffer;
121 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
122 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
123 } else {
124 this->current_frame_buf_[this->current_frame_len_++] = buffer;
125 this->current_frame_locate_++;
126 }
127 break;
129 this->current_frame_type_ = buffer << 8;
130 this->current_frame_buf_[this->current_frame_len_++] = buffer;
131 this->current_frame_locate_++;
132 break;
134 this->current_frame_type_ += buffer;
135 if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
136 (this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
137 (this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
138 (this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
139 this->current_frame_buf_[this->current_frame_len_++] = buffer;
140 this->current_frame_locate_++;
141 } else {
142 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
143 }
144 break;
146 if (validate_checksum(this->current_frame_buf_, this->current_frame_len_, buffer)) {
147 this->current_frame_buf_[this->current_frame_len_++] = buffer;
148 this->current_frame_locate_++;
149 } else {
150 ESP_LOGD(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer);
151#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
152 char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
153 char byte_buf[format_hex_pretty_size(1)];
154#endif
155 ESP_LOGV(TAG, "CURRENT_FRAME: %s %s",
156 format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
157 format_hex_pretty_to(byte_buf, &buffer, 1));
158 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
159 }
160 break;
162 if (this->current_frame_len_ >= FRAME_BUF_MAX_SIZE) {
163 ESP_LOGD(TAG, "PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM);
164 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
165 break;
166 }
167 this->current_data_buf_[this->current_frame_len_ - LEN_TO_DATA_FRAME + 1] = buffer;
168 this->current_frame_buf_[this->current_frame_len_++] = buffer;
169 if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) {
170 this->current_frame_locate_++;
171 }
172 break;
174 if (validate_checksum(this->current_data_buf_, this->current_data_frame_len_, buffer)) {
175 this->current_frame_buf_[this->current_frame_len_++] = buffer;
176 this->current_frame_locate_++;
177 this->process_frame_();
178 } else {
179 ESP_LOGD(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
180#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
181 char frame_buf[format_hex_pretty_size(MR60FDA2_MAX_LOG_BYTES)];
182 char byte_buf[format_hex_pretty_size(1)];
183#endif
184 ESP_LOGV(TAG, "GET CURRENT_FRAME: %s %s",
185 format_hex_pretty_to(frame_buf, this->current_frame_buf_, this->current_frame_len_),
186 format_hex_pretty_to(byte_buf, &buffer, 1));
187
188 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
189 }
190 break;
191 default:
192 break;
193 }
194}
195
196void MR60FDA2Component::process_frame_() {
197 switch (this->current_frame_type_) {
198 case IS_FALL_TYPE_BUFFER:
199#ifdef USE_BINARY_SENSOR
200 PUBLISH_STATE(this->fall_detected_binary_sensor_, this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
201#endif
202 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
203 break;
204
205 case PEOPLE_EXIST_TYPE_BUFFER:
206#ifdef USE_BINARY_SENSOR
207 PUBLISH_STATE(this->people_exist_binary_sensor_, this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
208#endif
209 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
210 break;
211
212 case RESULT_INSTALL_HEIGHT:
213 if (this->current_data_buf_[0]) {
214 ESP_LOGD(TAG, "Successfully set the mounting height");
215 } else {
216 ESP_LOGD(TAG, "Failed to set the mounting height");
217 }
218 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
219 break;
220
221 case RESULT_HEIGHT_THRESHOLD:
222 if (this->current_data_buf_[0]) {
223 ESP_LOGD(TAG, "Successfully set the height threshold");
224 } else {
225 ESP_LOGD(TAG, "Failed to set the height threshold");
226 }
227 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
228 break;
229
230 case RESULT_SENSITIVITY:
231 if (this->current_data_buf_[0]) {
232 ESP_LOGD(TAG, "Successfully set the sensitivity");
233 } else {
234 ESP_LOGD(TAG, "Failed to set the sensitivity");
235 }
236 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
237 break;
238
239 case RESULT_PARAMETERS: {
240 float install_height_float = 0;
241 float height_threshold_float = 0;
242 uint32_t current_sensitivity = 0;
243#ifdef USE_SELECT
244 if (this->install_height_select_ != nullptr) {
245 uint32_t current_install_height_int =
246 encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
247
248 install_height_float = bit_cast<float>(current_install_height_int);
249 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
250 this->install_height_select_->publish_state(select_index);
251 }
252
253 if (this->height_threshold_select_ != nullptr) {
254 uint32_t current_height_threshold_int =
255 encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
256
257 height_threshold_float = bit_cast<float>(current_height_threshold_int);
258 size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
259 this->height_threshold_select_->publish_state(select_index);
260 }
261
262 if (this->sensitivity_select_ != nullptr) {
263 current_sensitivity =
264 encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
265
266 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
267 this->sensitivity_select_->publish_state(select_index);
268 }
269#endif
270
271 ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
272 height_threshold_float, current_sensitivity);
273 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
274 break;
275 }
276 default:
277 break;
278 }
279}
280
281// Send Heartbeat Packet Command
283 if (index >= std::size(INSTALL_HEIGHT))
284 return;
285 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
286 progmem_memcpy(&send_data[8], &INSTALL_HEIGHT[index], 4);
287 send_data[12] = calculate_checksum(send_data + 8, 4);
288 this->write_array(send_data, 13);
289 ESP_LOGV_HEX(TAG, send_data, 13, 13, "SEND INSTALL HEIGHT FRAME: %s");
290}
291
293 if (index >= std::size(HEIGHT_THRESHOLD))
294 return;
295 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00};
296 progmem_memcpy(&send_data[8], &HEIGHT_THRESHOLD[index], 4);
297 send_data[12] = calculate_checksum(send_data + 8, 4);
298 this->write_array(send_data, 13);
299 ESP_LOGV_HEX(TAG, send_data, 13, 13, "SEND HEIGHT THRESHOLD: %s");
300}
301
303 if (index >= std::size(SENSITIVITY))
304 return;
305 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
306
307 put_le32(send_data + 8, static_cast<uint32_t>(SENSITIVITY[index]));
308
309 send_data[12] = calculate_checksum(send_data + 8, 4);
310 this->write_array(send_data, 13);
311 ESP_LOGV_HEX(TAG, send_data, 13, 13, "SEND SET SENSITIVITY: %s");
312}
313
315 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
316 this->write_array(send_data, 8);
317 ESP_LOGV_HEX(TAG, send_data, 8, 8, "SEND GET PARAMETERS: %s");
318}
319
321 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
322 this->write_array(send_data, 8);
323 ESP_LOGV_HEX(TAG, send_data, 8, 8, "SEND RESET: %s");
324 this->get_radar_parameters();
325}
326
327} // namespace esphome::seeed_mr60fda2
bool read_available(F &&on_byte)
Same as read_available_chunks(), calling on_byte(uint8_t) once per byte.
Definition uart.h:77
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")
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
uint32_t value
Definition hal.cpp:41
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:1687
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:1051
constexpr uint8_t * put_le32(uint8_t *dst, uint32_t value)
Definition helpers.h:1135
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition helpers.h:85
static void uint32_t