14static constexpr size_t MR60FDA2_MAX_LOG_BYTES = 64;
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_);
25 LOG_BUTTON(
" ",
"Get Radar Parameters Button", this->get_radar_parameters_button_);
26 LOG_BUTTON(
" ",
"Reset Radar Button", this->factory_reset_button_);
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_);
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;
44 memset(this->current_frame_buf_, 0, FRAME_BUF_MAX_SIZE);
45 memset(this->current_data_buf_, 0, DATA_BUF_MAX_SIZE);
50 this->
read_available([
this](uint8_t b) { this->split_frame_(b); });
54static uint8_t calculate_checksum(
const uint8_t *data,
size_t len) {
return ~xor_bytes(data,
len); }
67static bool validate_checksum(
const uint8_t *data,
size_t len, uint8_t expected_checksum) {
68 return calculate_checksum(data,
len) == expected_checksum;
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) {
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};
91void MR60FDA2Component::split_frame_(uint8_t
buffer) {
92 switch (this->current_frame_locate_) {
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_++;
101 this->current_frame_id_ =
buffer << 8;
102 this->current_frame_buf_[this->current_frame_len_++] =
buffer;
103 this->current_frame_locate_++;
106 this->current_frame_id_ +=
buffer;
107 this->current_frame_buf_[this->current_frame_len_++] =
buffer;
108 this->current_frame_locate_++;
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_++;
120 this->current_data_frame_len_ +=
buffer;
121 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
124 this->current_frame_buf_[this->current_frame_len_++] =
buffer;
125 this->current_frame_locate_++;
129 this->current_frame_type_ =
buffer << 8;
130 this->current_frame_buf_[this->current_frame_len_++] =
buffer;
131 this->current_frame_locate_++;
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_++;
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_++;
150 ESP_LOGD(TAG,
"HEAD_CKSUM_FRAME ERROR: 0x%02x",
buffer);
151#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
155 ESP_LOGV(TAG,
"CURRENT_FRAME: %s %s",
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);
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_++;
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_();
179 ESP_LOGD(TAG,
"DATA_CKSUM_FRAME ERROR: 0x%02x",
buffer);
180#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
184 ESP_LOGV(TAG,
"GET CURRENT_FRAME: %s %s",
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]);
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]);
212 case RESULT_INSTALL_HEIGHT:
213 if (this->current_data_buf_[0]) {
214 ESP_LOGD(TAG,
"Successfully set the mounting height");
216 ESP_LOGD(TAG,
"Failed to set the mounting height");
221 case RESULT_HEIGHT_THRESHOLD:
222 if (this->current_data_buf_[0]) {
223 ESP_LOGD(TAG,
"Successfully set the height threshold");
225 ESP_LOGD(TAG,
"Failed to set the height threshold");
230 case RESULT_SENSITIVITY:
231 if (this->current_data_buf_[0]) {
232 ESP_LOGD(TAG,
"Successfully set the sensitivity");
234 ESP_LOGD(TAG,
"Failed to set the sensitivity");
239 case RESULT_PARAMETERS: {
240 float install_height_float = 0;
241 float height_threshold_float = 0;
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]);
249 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
250 this->install_height_select_->publish_state(select_index);
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]);
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);
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]);
266 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
267 this->sensitivity_select_->publish_state(select_index);
271 ESP_LOGD(TAG,
"Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
272 height_threshold_float, current_sensitivity);
283 if (index >= std::size(INSTALL_HEIGHT))
285 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
287 send_data[12] = calculate_checksum(send_data + 8, 4);
289 ESP_LOGV_HEX(TAG, send_data, 13, 13,
"SEND INSTALL HEIGHT FRAME: %s");
293 if (index >= std::size(HEIGHT_THRESHOLD))
295 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00};
297 send_data[12] = calculate_checksum(send_data + 8, 4);
299 ESP_LOGV_HEX(TAG, send_data, 13, 13,
"SEND HEIGHT THRESHOLD: %s");
303 if (index >= std::size(SENSITIVITY))
305 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
309 send_data[12] = calculate_checksum(send_data + 8, 4);
311 ESP_LOGV_HEX(TAG, send_data, 13, 13,
"SEND SET SENSITIVITY: %s");
315 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
317 ESP_LOGV_HEX(TAG, send_data, 8, 8,
"SEND GET PARAMETERS: %s");
321 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
323 ESP_LOGV_HEX(TAG, send_data, 8, 8,
"SEND RESET: %s");