ESPHome 2025.9.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
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 this->check_uart_settings(115200);
35
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 uint8_t byte;
50
51 // Is there data on the serial port
52 while (this->available()) {
53 this->read_byte(&byte);
54 this->split_frame_(byte); // split data frame
55 }
56}
57
68static uint8_t calculate_checksum(const uint8_t *data, size_t len) {
69 uint8_t checksum = 0;
70 for (size_t i = 0; i < len; i++) {
71 checksum ^= data[i];
72 }
73 checksum = ~checksum;
74 return checksum;
75}
76
88static bool validate_checksum(const uint8_t *data, size_t len, uint8_t expected_checksum) {
89 return calculate_checksum(data, len) == expected_checksum;
90}
91
92static uint8_t find_nearest_index(float value, const float *arr, int size) {
93 int nearest_index = 0;
94 float min_diff = std::abs(value - arr[0]);
95 for (int i = 1; i < size; ++i) {
96 float diff = std::abs(value - arr[i]);
97 if (diff < min_diff) {
98 min_diff = diff;
99 nearest_index = i;
100 }
101 }
102 return nearest_index;
103}
104
113static void float_to_bytes(float value, unsigned char *bytes) {
114 union {
115 float float_value;
116 unsigned char byte_array[4];
117 } u;
118
119 u.float_value = value;
120 memcpy(bytes, u.byte_array, 4);
121}
122
131static void int_to_bytes(uint32_t value, unsigned char *bytes) {
132 bytes[0] = value & 0xFF;
133 bytes[1] = (value >> 8) & 0xFF;
134 bytes[2] = (value >> 16) & 0xFF;
135 bytes[3] = (value >> 24) & 0xFF;
136}
137
138void MR60FDA2Component::split_frame_(uint8_t buffer) {
139 switch (this->current_frame_locate_) {
140 case LOCATE_FRAME_HEADER: // starting buffer
141 if (buffer == FRAME_HEADER_BUFFER) {
142 this->current_frame_len_ = 1;
143 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
144 this->current_frame_locate_++;
145 }
146 break;
147 case LOCATE_ID_FRAME1:
148 this->current_frame_id_ = buffer << 8;
149 this->current_frame_len_++;
150 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
151 this->current_frame_locate_++;
152 break;
153 case LOCATE_ID_FRAME2:
154 this->current_frame_id_ += buffer;
155 this->current_frame_len_++;
156 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
157 this->current_frame_locate_++;
158 break;
160 this->current_data_frame_len_ = buffer << 8;
161 if (this->current_data_frame_len_ == 0x00) {
162 this->current_frame_len_++;
163 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
164 this->current_frame_locate_++;
165 } else {
166 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
167 }
168 break;
170 this->current_data_frame_len_ += buffer;
171 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
172 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
173 } else {
174 this->current_frame_len_++;
175 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
176 this->current_frame_locate_++;
177 }
178 break;
180 this->current_frame_type_ = buffer << 8;
181 this->current_frame_len_++;
182 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
183 this->current_frame_locate_++;
184 break;
186 this->current_frame_type_ += buffer;
187 if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
188 (this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
189 (this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
190 (this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
191 this->current_frame_len_++;
192 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
193 this->current_frame_locate_++;
194 } else {
195 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
196 }
197 break;
199 if (validate_checksum(this->current_frame_buf_, this->current_frame_len_, buffer)) {
200 this->current_frame_len_++;
201 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
202 this->current_frame_locate_++;
203 } else {
204 ESP_LOGD(TAG, "HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer);
205 ESP_LOGV(TAG, "CURRENT_FRAME: %s %s",
206 format_hex_pretty(this->current_frame_buf_, this->current_frame_len_).c_str(),
207 format_hex_pretty(&buffer, 1).c_str());
208 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
209 }
210 break;
212 this->current_frame_len_++;
213 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
214 this->current_data_buf_[this->current_frame_len_ - LEN_TO_DATA_FRAME] = buffer;
215 if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) {
216 this->current_frame_locate_++;
217 }
218 if (this->current_frame_len_ > FRAME_BUF_MAX_SIZE) {
219 ESP_LOGD(TAG, "PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM);
220 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
221 }
222 break;
224 if (validate_checksum(this->current_data_buf_, this->current_data_frame_len_, buffer)) {
225 this->current_frame_len_++;
226 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
227 this->current_frame_locate_++;
228 this->process_frame_();
229 } else {
230 ESP_LOGD(TAG, "DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
231 ESP_LOGV(TAG, "GET CURRENT_FRAME: %s %s",
232 format_hex_pretty(this->current_frame_buf_, this->current_frame_len_).c_str(),
233 format_hex_pretty(&buffer, 1).c_str());
234
235 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
236 }
237 break;
238 default:
239 break;
240 }
241}
242
243void MR60FDA2Component::process_frame_() {
244 switch (this->current_frame_type_) {
245 case IS_FALL_TYPE_BUFFER:
246 if (this->fall_detected_binary_sensor_ != nullptr) {
247 this->fall_detected_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
248 }
249 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
250 break;
251
252 case PEOPLE_EXIST_TYPE_BUFFER:
253 if (this->people_exist_binary_sensor_ != nullptr)
254 this->people_exist_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
255 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
256 break;
257
258 case RESULT_INSTALL_HEIGHT:
259 if (this->current_data_buf_[0]) {
260 ESP_LOGD(TAG, "Successfully set the mounting height");
261 } else {
262 ESP_LOGD(TAG, "Failed to set the mounting height");
263 }
264 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
265 break;
266
267 case RESULT_HEIGHT_THRESHOLD:
268 if (this->current_data_buf_[0]) {
269 ESP_LOGD(TAG, "Successfully set the height threshold");
270 } else {
271 ESP_LOGD(TAG, "Failed to set the height threshold");
272 }
273 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
274 break;
275
276 case RESULT_SENSITIVITY:
277 if (this->current_data_buf_[0]) {
278 ESP_LOGD(TAG, "Successfully set the sensitivity");
279 } else {
280 ESP_LOGD(TAG, "Failed to set the sensitivity");
281 }
282 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
283 break;
284
285 case RESULT_PARAMETERS: {
286 float install_height_float = 0;
287 float height_threshold_float = 0;
288 uint32_t current_sensitivity = 0;
289 if (this->install_height_select_ != nullptr) {
290 uint32_t current_install_height_int =
291 encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
292
293 install_height_float = bit_cast<float>(current_install_height_int);
294 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
295 this->install_height_select_->publish_state(this->install_height_select_->at(select_index).value());
296 }
297
298 if (this->height_threshold_select_ != nullptr) {
299 uint32_t current_height_threshold_int =
300 encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
301
302 height_threshold_float = bit_cast<float>(current_height_threshold_int);
303 size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
304 this->height_threshold_select_->publish_state(this->height_threshold_select_->at(select_index).value());
305 }
306
307 if (this->sensitivity_select_ != nullptr) {
308 current_sensitivity =
309 encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
310
311 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
312 this->sensitivity_select_->publish_state(this->sensitivity_select_->at(select_index).value());
313 }
314
315 ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
316 height_threshold_float, current_sensitivity);
317 this->current_frame_locate_ = LOCATE_FRAME_HEADER;
318 break;
319 }
320 default:
321 break;
322 }
323}
324
325// Send Heartbeat Packet Command
327 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
328 float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
329 send_data[12] = calculate_checksum(send_data + 8, 4);
330 this->write_array(send_data, 13);
331 ESP_LOGV(TAG, "SEND INSTALL HEIGHT FRAME: %s", format_hex_pretty(send_data, 13).c_str());
332}
333
335 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00};
336 float_to_bytes(HEIGHT_THRESHOLD[index], &send_data[8]);
337 send_data[12] = calculate_checksum(send_data + 8, 4);
338 this->write_array(send_data, 13);
339 ESP_LOGV(TAG, "SEND HEIGHT THRESHOLD: %s", format_hex_pretty(send_data, 13).c_str());
340}
341
343 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
344
345 int_to_bytes(SENSITIVITY[index], &send_data[8]);
346
347 send_data[12] = calculate_checksum(send_data + 8, 4);
348 this->write_array(send_data, 13);
349 ESP_LOGV(TAG, "SEND SET SENSITIVITY: %s", format_hex_pretty(send_data, 13).c_str());
350}
351
353 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
354 this->write_array(send_data, 8);
355 ESP_LOGV(TAG, "SEND GET PARAMETERS: %s", format_hex_pretty(send_data, 8).c_str());
356}
357
359 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
360 this->write_array(send_data, 8);
361 ESP_LOGV(TAG, "SEND RESET: %s", format_hex_pretty(send_data, 8).c_str());
362 this->get_radar_parameters();
363}
364
365} // namespace seeed_mr60fda2
366} // 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:279
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
Definition helpers.cpp:280
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:181
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition helpers.h:66