ESPHome 2025.6.3
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// Prints the component's configuration data. dump_config() prints all of the component's configuration
14// items in an easy-to-read format, including the configuration key-value pairs.
16 ESP_LOGCONFIG(TAG, "MR60FDA2:");
17#ifdef USE_BINARY_SENSOR
18 LOG_BINARY_SENSOR(" ", "People Exist Binary Sensor", this->people_exist_binary_sensor_);
19 LOG_BINARY_SENSOR(" ", "Is Fall Binary Sensor", this->fall_detected_binary_sensor_);
20#endif
21#ifdef USE_BUTTON
22 LOG_BUTTON(" ", "Get Radar Parameters Button", this->get_radar_parameters_button_);
23 LOG_BUTTON(" ", "Reset Radar Button", this->factory_reset_button_);
24#endif
25#ifdef USE_SELECT
26 LOG_SELECT(" ", "Install Height Select", this->install_height_select_);
27 LOG_SELECT(" ", "Height Threshold Select", this->height_threshold_select_);
28 LOG_SELECT(" ", "Sensitivity Select", this->sensitivity_select_);
29#endif
30}
31
32// Initialisation functions
34 ESP_LOGCONFIG(TAG, "Running setup");
35 this->check_uart_settings(115200);
36
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 ESP_LOGCONFIG(TAG, "Set up MR60FDA2 complete");
48}
49
50// main loop
52 uint8_t byte;
53
54 // Is there data on the serial port
55 while (this->available()) {
56 this->read_byte(&byte);
57 this->split_frame_(byte); // split data frame
58 }
59}
60
71static uint8_t calculate_checksum(const uint8_t *data, size_t len) {
72 uint8_t checksum = 0;
73 for (size_t i = 0; i < len; i++) {
74 checksum ^= data[i];
75 }
76 checksum = ~checksum;
77 return checksum;
78}
79
91static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
92 return calculate_checksum(data, len) == expected_checksum;
93}
94
95static uint8_t find_nearest_index(float value, const float *arr, int size) {
96 int nearest_index = 0;
97 float min_diff = std::abs(value - arr[0]);
98 for (int i = 1; i < size; ++i) {
99 float diff = std::abs(value - arr[i]);
100 if (diff < min_diff) {
101 min_diff = diff;
102 nearest_index = i;
103 }
104 }
105 return nearest_index;
106}
107
116static void float_to_bytes(float value, unsigned char *bytes) {
117 union {
118 float float_value;
119 unsigned char byte_array[4];
120 } u;
121
122 u.float_value = value;
123 memcpy(bytes, u.byte_array, 4);
124}
125
134static void int_to_bytes(uint32_t value, unsigned char *bytes) {
135 bytes[0] = value & 0xFF;
136 bytes[1] = (value >> 8) & 0xFF;
137 bytes[2] = (value >> 16) & 0xFF;
138 bytes[3] = (value >> 24) & 0xFF;
139}
140
141void MR60FDA2Component::split_frame_(uint8_t buffer) {
142 switch (this->current_frame_locate_) {
143 case LOCATE_FRAME_HEADER: // starting buffer
144 if (buffer == FRAME_HEADER_BUFFER) {
145 this->current_frame_len_ = 1;
146 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
147 this->current_frame_locate_++;
148 }
149 break;
150 case LOCATE_ID_FRAME1:
151 this->current_frame_id_ = buffer << 8;
152 this->current_frame_len_++;
153 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
154 this->current_frame_locate_++;
155 break;
156 case LOCATE_ID_FRAME2:
157 this->current_frame_id_ += buffer;
158 this->current_frame_len_++;
159 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
160 this->current_frame_locate_++;
161 break;
163 this->current_data_frame_len_ = buffer << 8;
164 if (this->current_data_frame_len_ == 0x00) {
165 this->current_frame_len_++;
166 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
167 this->current_frame_locate_++;
168 } else {
169 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
170 }
171 break;
173 this->current_data_frame_len_ += buffer;
174 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
175 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
176 } else {
177 this->current_frame_len_++;
178 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
179 this->current_frame_locate_++;
180 }
181 break;
183 this->current_frame_type_ = buffer << 8;
184 this->current_frame_len_++;
185 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
186 this->current_frame_locate_++;
187 break;
189 this->current_frame_type_ += buffer;
190 if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
191 (this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
192 (this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
193 (this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
194 this->current_frame_len_++;
195 this->current_frame_buf_[this->current_frame_len_ - 1] = 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_len_++;
204 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
205 this->current_frame_locate_++;
206 } else {
207 ESP_LOGD(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer);
208 ESP_LOGV(TAG, "CURRENT_FRAME: %s %s",
209 format_hex_pretty(this->current_frame_buf_, this->current_frame_len_).c_str(),
210 format_hex_pretty(&buffer, 1).c_str());
211 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
212 }
213 break;
215 this->current_frame_len_++;
216 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
217 this->current_data_buf_[this->current_frame_len_ - LEN_TO_DATA_FRAME] = buffer;
218 if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) {
219 this->current_frame_locate_++;
220 }
221 if (this->current_frame_len_ > FRAME_BUF_MAX_SIZE) {
222 ESP_LOGD(TAG, "PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM);
223 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
224 }
225 break;
227 if (validate_checksum(this->current_data_buf_, this->current_data_frame_len_, buffer)) {
228 this->current_frame_len_++;
229 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
230 this->current_frame_locate_++;
231 this->process_frame_();
232 } else {
233 ESP_LOGD(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
234 ESP_LOGV(TAG, "GET CURRENT_FRAME: %s %s",
235 format_hex_pretty(this->current_frame_buf_, this->current_frame_len_).c_str(),
236 format_hex_pretty(&buffer, 1).c_str());
237
238 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
239 }
240 break;
241 default:
242 break;
243 }
244}
245
246void MR60FDA2Component::process_frame_() {
247 switch (this->current_frame_type_) {
248 case IS_FALL_TYPE_BUFFER:
249 if (this->fall_detected_binary_sensor_ != nullptr) {
250 this->fall_detected_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
251 }
252 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
253 break;
254
255 case PEOPLE_EXIST_TYPE_BUFFER:
256 if (this->people_exist_binary_sensor_ != nullptr)
257 this->people_exist_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
258 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
259 break;
260
261 case RESULT_INSTALL_HEIGHT:
262 if (this->current_data_buf_[0]) {
263 ESP_LOGD(TAG, "Successfully set the mounting height");
264 } else {
265 ESP_LOGD(TAG, "Failed to set the mounting height");
266 }
267 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
268 break;
269
270 case RESULT_HEIGHT_THRESHOLD:
271 if (this->current_data_buf_[0]) {
272 ESP_LOGD(TAG, "Successfully set the height threshold");
273 } else {
274 ESP_LOGD(TAG, "Failed to set the height threshold");
275 }
276 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
277 break;
278
279 case RESULT_SENSITIVITY:
280 if (this->current_data_buf_[0]) {
281 ESP_LOGD(TAG, "Successfully set the sensitivity");
282 } else {
283 ESP_LOGD(TAG, "Failed to set the sensitivity");
284 }
285 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
286 break;
287
288 case RESULT_PARAMETERS: {
289 float install_height_float = 0;
290 float height_threshold_float = 0;
291 uint32_t current_sensitivity = 0;
292 if (this->install_height_select_ != nullptr) {
293 uint32_t current_install_height_int =
294 encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
295
296 install_height_float = bit_cast<float>(current_install_height_int);
297 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
298 this->install_height_select_->publish_state(this->install_height_select_->at(select_index).value());
299 }
300
301 if (this->height_threshold_select_ != nullptr) {
302 uint32_t current_height_threshold_int =
303 encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
304
305 height_threshold_float = bit_cast<float>(current_height_threshold_int);
306 size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
307 this->height_threshold_select_->publish_state(this->height_threshold_select_->at(select_index).value());
308 }
309
310 if (this->sensitivity_select_ != nullptr) {
311 current_sensitivity =
312 encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
313
314 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
315 this->sensitivity_select_->publish_state(this->sensitivity_select_->at(select_index).value());
316 }
317
318 ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
319 height_threshold_float, current_sensitivity);
320 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
321 break;
322 }
323 default:
324 break;
325 }
326}
327
328// Send Heartbeat Packet Command
330 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
331 float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
332 send_data[12] = calculate_checksum(send_data + 8, 4);
333 this->write_array(send_data, 13);
334 ESP_LOGV(TAG, "SEND INSTALL HEIGHT FRAME: %s", format_hex_pretty(send_data, 13).c_str());
335}
336
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(TAG, "SEND HEIGHT THRESHOLD: %s", format_hex_pretty(send_data, 13).c_str());
343}
344
346 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
347
348 int_to_bytes(SENSITIVITY[index], &send_data[8]);
349
350 send_data[12] = calculate_checksum(send_data + 8, 4);
351 this->write_array(send_data, 13);
352 ESP_LOGV(TAG, "SEND SET SENSITIVITY: %s", format_hex_pretty(send_data, 13).c_str());
353}
354
356 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
357 this->write_array(send_data, 8);
358 ESP_LOGV(TAG, "SEND GET PARAMETERS: %s", format_hex_pretty(send_data, 8).c_str());
359}
360
362 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
363 this->write_array(send_data, 8);
364 ESP_LOGV(TAG, "SEND RESET: %s", format_hex_pretty(send_data, 8).c_str());
365 this->get_radar_parameters();
366}
367
368} // namespace seeed_mr60fda2
369} // namespace esphome
uint8_t checksum
Definition bl0906.h:3
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:13
bool read_byte(uint8_t *data)
Definition uart.h:29
void write_array(const uint8_t *data, size_t len)
Definition uart.h:21
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:302
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:200
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition helpers.h:131
std::string format_hex_pretty(const uint8_t *data, size_t length)
Format the byte array data of length len in pretty-printed, human-readable hex.
Definition helpers.cpp:372