8 namespace seeed_mr60fda2 {
10 static const char *
const TAG =
"seeed_mr60fda2";
15 ESP_LOGCONFIG(TAG,
"MR60FDA2:");
16 #ifdef USE_BINARY_SENSOR 17 LOG_BINARY_SENSOR(
" ",
"People Exist Binary Sensor", this->people_exist_binary_sensor_);
18 LOG_BINARY_SENSOR(
" ",
"Is Fall Binary Sensor", this->fall_detected_binary_sensor_);
21 LOG_BUTTON(
" ",
"Get Radar Parameters Button", this->get_radar_parameters_button_);
22 LOG_BUTTON(
" ",
"Reset Radar Button", this->factory_reset_button_);
25 LOG_SELECT(
" ",
"Install Height Select", this->install_height_select_);
26 LOG_SELECT(
" ",
"Height Threshold Select", this->height_threshold_select_);
27 LOG_SELECT(
" ",
"Sensitivity Select", this->sensitivity_select_);
33 ESP_LOGCONFIG(TAG,
"Setting up MR60FDA2...");
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);
46 ESP_LOGCONFIG(TAG,
"Set up MR60FDA2 complete");
56 this->split_frame_(byte);
70 static uint8_t calculate_checksum(
const uint8_t *data,
size_t len) {
72 for (
size_t i = 0; i <
len; i++) {
90 static bool validate_checksum(
const uint8_t *data,
size_t len, uint8_t expected_checksum) {
91 return calculate_checksum(data, len) == expected_checksum;
94 static uint8_t find_nearest_index(
float value,
const float *arr,
int size) {
95 int nearest_index = 0;
96 float min_diff = std::abs(value - arr[0]);
97 for (
int i = 1; i < size; ++i) {
98 float diff = std::abs(value - arr[i]);
99 if (diff < min_diff) {
104 return nearest_index;
115 static void float_to_bytes(
float value,
unsigned char *
bytes) {
118 unsigned char byte_array[4];
121 u.float_value = value;
122 memcpy(bytes, u.byte_array, 4);
133 static void int_to_bytes(uint32_t value,
unsigned char *bytes) {
134 bytes[0] = value & 0xFF;
135 bytes[1] = (value >> 8) & 0xFF;
136 bytes[2] = (value >> 16) & 0xFF;
137 bytes[3] = (value >> 24) & 0xFF;
140 void MR60FDA2Component::split_frame_(uint8_t buffer) {
141 switch (this->current_frame_locate_) {
143 if (buffer == FRAME_HEADER_BUFFER) {
144 this->current_frame_len_ = 1;
145 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
146 this->current_frame_locate_++;
150 this->current_frame_id_ = buffer << 8;
151 this->current_frame_len_++;
152 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
153 this->current_frame_locate_++;
156 this->current_frame_id_ += buffer;
157 this->current_frame_len_++;
158 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
159 this->current_frame_locate_++;
162 this->current_data_frame_len_ = buffer << 8;
163 if (this->current_data_frame_len_ == 0x00) {
164 this->current_frame_len_++;
165 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
166 this->current_frame_locate_++;
172 this->current_data_frame_len_ += buffer;
173 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
176 this->current_frame_len_++;
177 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
178 this->current_frame_locate_++;
182 this->current_frame_type_ = buffer << 8;
183 this->current_frame_len_++;
184 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
185 this->current_frame_locate_++;
188 this->current_frame_type_ += buffer;
189 if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
190 (this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
191 (this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
192 (this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
193 this->current_frame_len_++;
194 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
195 this->current_frame_locate_++;
201 if (validate_checksum(this->current_frame_buf_, this->current_frame_len_, buffer)) {
202 this->current_frame_len_++;
203 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
204 this->current_frame_locate_++;
206 ESP_LOGD(TAG,
"HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer);
207 ESP_LOGV(TAG,
"CURRENT_FRAME: %s %s",
214 this->current_frame_len_++;
215 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
216 this->current_data_buf_[this->current_frame_len_ - LEN_TO_DATA_FRAME] = buffer;
217 if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) {
218 this->current_frame_locate_++;
220 if (this->current_frame_len_ > FRAME_BUF_MAX_SIZE) {
221 ESP_LOGD(TAG,
"PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM);
226 if (validate_checksum(this->current_data_buf_, this->current_data_frame_len_, buffer)) {
227 this->current_frame_len_++;
228 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
229 this->current_frame_locate_++;
230 this->process_frame_();
232 ESP_LOGD(TAG,
"DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
233 ESP_LOGV(TAG,
"GET CURRENT_FRAME: %s %s",
245 void MR60FDA2Component::process_frame_() {
246 switch (this->current_frame_type_) {
247 case IS_FALL_TYPE_BUFFER:
248 if (this->fall_detected_binary_sensor_ !=
nullptr) {
249 this->fall_detected_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
254 case PEOPLE_EXIST_TYPE_BUFFER:
255 if (this->people_exist_binary_sensor_ !=
nullptr)
256 this->people_exist_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
260 case RESULT_INSTALL_HEIGHT:
261 if (this->current_data_buf_[0]) {
262 ESP_LOGD(TAG,
"Successfully set the mounting height");
264 ESP_LOGD(TAG,
"Failed to set the mounting height");
269 case RESULT_HEIGHT_THRESHOLD:
270 if (this->current_data_buf_[0]) {
271 ESP_LOGD(TAG,
"Successfully set the height threshold");
273 ESP_LOGD(TAG,
"Failed to set the height threshold");
278 case RESULT_SENSITIVITY:
279 if (this->current_data_buf_[0]) {
280 ESP_LOGD(TAG,
"Successfully set the sensitivity");
282 ESP_LOGD(TAG,
"Failed to set the sensitivity");
287 case RESULT_PARAMETERS: {
288 float install_height_float = 0;
289 float height_threshold_float = 0;
290 uint32_t current_sensitivity = 0;
291 if (this->install_height_select_ !=
nullptr) {
292 uint32_t current_install_height_int =
293 encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
295 install_height_float =
bit_cast<
float>(current_install_height_int);
296 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
297 this->install_height_select_->publish_state(this->install_height_select_->at(select_index).value());
300 if (this->height_threshold_select_ !=
nullptr) {
301 uint32_t current_height_threshold_int =
302 encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
304 height_threshold_float =
bit_cast<
float>(current_height_threshold_int);
305 size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
306 this->height_threshold_select_->publish_state(this->height_threshold_select_->at(select_index).value());
309 if (this->sensitivity_select_ !=
nullptr) {
310 current_sensitivity =
311 encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
313 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
314 this->sensitivity_select_->publish_state(this->sensitivity_select_->at(select_index).value());
317 ESP_LOGD(TAG,
"Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
318 height_threshold_float, current_sensitivity);
329 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
330 float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
331 send_data[12] = calculate_checksum(send_data + 8, 4);
333 ESP_LOGV(TAG,
"SEND INSTALL HEIGHT FRAME: %s",
format_hex_pretty(send_data, 13).c_str());
337 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00};
338 float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
339 send_data[12] = calculate_checksum(send_data + 8, 4);
341 ESP_LOGV(TAG,
"SEND HEIGHT THRESHOLD: %s",
format_hex_pretty(send_data, 13).c_str());
345 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
347 int_to_bytes(SENSITIVITY[index], &send_data[8]);
349 send_data[12] = calculate_checksum(send_data + 8, 4);
351 ESP_LOGV(TAG,
"SEND SET SENSITIVITY: %s",
format_hex_pretty(send_data, 13).c_str());
355 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
357 ESP_LOGV(TAG,
"SEND GET PARAMETERS: %s",
format_hex_pretty(send_data, 8).c_str());
361 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
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.
void write_array(const uint8_t *data, size_t len)
void set_sensitivity(uint8_t index)
void set_install_height(uint8_t index)
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.
void set_height_threshold(uint8_t index)
void dump_config() override
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...
bool read_byte(uint8_t *data)
void get_radar_parameters()
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Implementation of SPI Controller mode.
std::vector< uint8_t > bytes