13static constexpr size_t MR60FDA2_MAX_LOG_BYTES = 64;
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_);
24 LOG_BUTTON(
" ",
"Get Radar Parameters Button", this->get_radar_parameters_button_);
25 LOG_BUTTON(
" ",
"Reset Radar Button", this->factory_reset_button_);
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_);
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;
43 memset(this->current_frame_buf_, 0, FRAME_BUF_MAX_SIZE);
44 memset(this->current_data_buf_, 0, DATA_BUF_MAX_SIZE);
53 size_t to_read = std::min(avail,
sizeof(buf));
59 for (
size_t i = 0; i < to_read; i++) {
60 this->split_frame_(buf[i]);
66static uint8_t calculate_checksum(
const uint8_t *data,
size_t len) {
return ~xor_bytes(data,
len); }
79static bool validate_checksum(
const uint8_t *data,
size_t len, uint8_t expected_checksum) {
80 return calculate_checksum(data,
len) == expected_checksum;
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) {
106static void float_to_bytes(
float value,
unsigned char *bytes) {
109 unsigned char byte_array[4];
112 u.float_value =
value;
113 memcpy(bytes, u.byte_array, 4);
124static void int_to_bytes(
uint32_t value,
unsigned char *bytes) {
131void MR60FDA2Component::split_frame_(uint8_t
buffer) {
132 switch (this->current_frame_locate_) {
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_++;
141 this->current_frame_id_ =
buffer << 8;
142 this->current_frame_buf_[this->current_frame_len_++] =
buffer;
143 this->current_frame_locate_++;
146 this->current_frame_id_ +=
buffer;
147 this->current_frame_buf_[this->current_frame_len_++] =
buffer;
148 this->current_frame_locate_++;
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_++;
160 this->current_data_frame_len_ +=
buffer;
161 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
164 this->current_frame_buf_[this->current_frame_len_++] =
buffer;
165 this->current_frame_locate_++;
169 this->current_frame_type_ =
buffer << 8;
170 this->current_frame_buf_[this->current_frame_len_++] =
buffer;
171 this->current_frame_locate_++;
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_++;
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_++;
190 ESP_LOGD(TAG,
"HEAD_CKSUM_FRAME ERROR: 0x%02x",
buffer);
191#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
195 ESP_LOGV(TAG,
"CURRENT_FRAME: %s %s",
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);
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_++;
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_();
219 ESP_LOGD(TAG,
"DATA_CKSUM_FRAME ERROR: 0x%02x",
buffer);
220#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
224 ESP_LOGV(TAG,
"GET CURRENT_FRAME: %s %s",
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]);
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]);
255 case RESULT_INSTALL_HEIGHT:
256 if (this->current_data_buf_[0]) {
257 ESP_LOGD(TAG,
"Successfully set the mounting height");
259 ESP_LOGD(TAG,
"Failed to set the mounting height");
264 case RESULT_HEIGHT_THRESHOLD:
265 if (this->current_data_buf_[0]) {
266 ESP_LOGD(TAG,
"Successfully set the height threshold");
268 ESP_LOGD(TAG,
"Failed to set the height threshold");
273 case RESULT_SENSITIVITY:
274 if (this->current_data_buf_[0]) {
275 ESP_LOGD(TAG,
"Successfully set the sensitivity");
277 ESP_LOGD(TAG,
"Failed to set the sensitivity");
282 case RESULT_PARAMETERS: {
283 float install_height_float = 0;
284 float height_threshold_float = 0;
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]);
292 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
293 this->install_height_select_->publish_state(select_index);
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]);
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);
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]);
309 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
310 this->sensitivity_select_->publish_state(select_index);
314 ESP_LOGD(TAG,
"Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
315 height_threshold_float, current_sensitivity);
326 if (index >= std::size(INSTALL_HEIGHT))
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);
332 ESP_LOGV_HEX(TAG, send_data, 13, 13,
"SEND INSTALL HEIGHT FRAME: %s");
336 if (index >= std::size(HEIGHT_THRESHOLD))
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);
342 ESP_LOGV_HEX(TAG, send_data, 13, 13,
"SEND HEIGHT THRESHOLD: %s");
346 if (index >= std::size(SENSITIVITY))
348 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
350 int_to_bytes(SENSITIVITY[index], &send_data[8]);
352 send_data[12] = calculate_checksum(send_data + 8, 4);
354 ESP_LOGV_HEX(TAG, send_data, 13, 13,
"SEND SET SENSITIVITY: %s");
358 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
360 ESP_LOGV_HEX(TAG, send_data, 8, 8,
"SEND GET PARAMETERS: %s");
364 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
366 ESP_LOGV_HEX(TAG, send_data, 8, 8,
"SEND RESET: %s");
void get_radar_parameters()
void set_height_threshold(uint8_t index)
void set_sensitivity(uint8_t index)
void dump_config() override
void set_install_height(uint8_t index)
optional< std::array< uint8_t, N > > read_array()
void write_array(const uint8_t *data, size_t len)
@ LOCATE_HEAD_CKSUM_FRAME
@ LOCATE_DATA_CKSUM_FRAME
ESPHOME_LOG_TAG(TAG, "seeed_mr60fda2")
std::vector< uint8_t > bytes
value[LAYOUT.day_index()]
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).
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".
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.
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.