9namespace seeed_mr60fda2 {
11static const char *
const TAG =
"seeed_mr60fda2";
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_);
40 this->current_frame_id_ = 0;
41 this->current_frame_len_ = 0;
42 this->current_data_frame_len_ = 0;
43 this->current_frame_type_ = 0;
46 memset(this->current_frame_buf_, 0, FRAME_BUF_MAX_SIZE);
47 memset(this->current_data_buf_, 0, DATA_BUF_MAX_SIZE);
56 size_t to_read = std::min(avail,
sizeof(buf));
62 for (
size_t i = 0; i < to_read; i++) {
63 this->split_frame_(buf[i]);
78static uint8_t calculate_checksum(
const uint8_t *data,
size_t len) {
80 for (
size_t i = 0; i <
len; i++) {
98static bool validate_checksum(
const uint8_t *data,
size_t len, uint8_t expected_checksum) {
99 return calculate_checksum(data,
len) == expected_checksum;
102static uint8_t find_nearest_index(
float value,
const float *arr,
int size) {
103 int nearest_index = 0;
104 float min_diff = std::abs(value - arr[0]);
105 for (
int i = 1; i <
size; ++i) {
106 float diff = std::abs(value - arr[i]);
107 if (diff < min_diff) {
112 return nearest_index;
123static void float_to_bytes(
float value,
unsigned char *bytes) {
126 unsigned char byte_array[4];
129 u.float_value = value;
130 memcpy(bytes, u.byte_array, 4);
141static void int_to_bytes(uint32_t value,
unsigned char *bytes) {
142 bytes[0] = value & 0xFF;
143 bytes[1] = (value >> 8) & 0xFF;
144 bytes[2] = (value >> 16) & 0xFF;
145 bytes[3] = (value >> 24) & 0xFF;
148void MR60FDA2Component::split_frame_(uint8_t buffer) {
149 switch (this->current_frame_locate_) {
151 if (buffer == FRAME_HEADER_BUFFER) {
152 this->current_frame_len_ = 1;
153 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
154 this->current_frame_locate_++;
158 this->current_frame_id_ = buffer << 8;
159 this->current_frame_len_++;
160 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
161 this->current_frame_locate_++;
164 this->current_frame_id_ += buffer;
165 this->current_frame_len_++;
166 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
167 this->current_frame_locate_++;
170 this->current_data_frame_len_ = buffer << 8;
171 if (this->current_data_frame_len_ == 0x00) {
172 this->current_frame_len_++;
173 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
174 this->current_frame_locate_++;
180 this->current_data_frame_len_ += buffer;
181 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
184 this->current_frame_len_++;
185 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
186 this->current_frame_locate_++;
190 this->current_frame_type_ = buffer << 8;
191 this->current_frame_len_++;
192 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
193 this->current_frame_locate_++;
196 this->current_frame_type_ += buffer;
197 if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
198 (this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
199 (this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
200 (this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
201 this->current_frame_len_++;
202 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
203 this->current_frame_locate_++;
209 if (validate_checksum(this->current_frame_buf_, this->current_frame_len_, buffer)) {
210 this->current_frame_len_++;
211 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
212 this->current_frame_locate_++;
214 ESP_LOGD(TAG,
"HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer);
215#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
219 ESP_LOGV(TAG,
"CURRENT_FRAME: %s %s",
226 this->current_frame_len_++;
227 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
228 this->current_data_buf_[this->current_frame_len_ - LEN_TO_DATA_FRAME] = buffer;
229 if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) {
230 this->current_frame_locate_++;
232 if (this->current_frame_len_ > FRAME_BUF_MAX_SIZE) {
233 ESP_LOGD(TAG,
"PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM);
238 if (validate_checksum(this->current_data_buf_, this->current_data_frame_len_, buffer)) {
239 this->current_frame_len_++;
240 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
241 this->current_frame_locate_++;
242 this->process_frame_();
244 ESP_LOGD(TAG,
"DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
245#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
249 ESP_LOGV(TAG,
"GET CURRENT_FRAME: %s %s",
261void MR60FDA2Component::process_frame_() {
262 switch (this->current_frame_type_) {
263 case IS_FALL_TYPE_BUFFER:
264 if (this->fall_detected_binary_sensor_ !=
nullptr) {
265 this->fall_detected_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
270 case PEOPLE_EXIST_TYPE_BUFFER:
271 if (this->people_exist_binary_sensor_ !=
nullptr)
272 this->people_exist_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
276 case RESULT_INSTALL_HEIGHT:
277 if (this->current_data_buf_[0]) {
278 ESP_LOGD(TAG,
"Successfully set the mounting height");
280 ESP_LOGD(TAG,
"Failed to set the mounting height");
285 case RESULT_HEIGHT_THRESHOLD:
286 if (this->current_data_buf_[0]) {
287 ESP_LOGD(TAG,
"Successfully set the height threshold");
289 ESP_LOGD(TAG,
"Failed to set the height threshold");
294 case RESULT_SENSITIVITY:
295 if (this->current_data_buf_[0]) {
296 ESP_LOGD(TAG,
"Successfully set the sensitivity");
298 ESP_LOGD(TAG,
"Failed to set the sensitivity");
303 case RESULT_PARAMETERS: {
304 float install_height_float = 0;
305 float height_threshold_float = 0;
307 if (this->install_height_select_ !=
nullptr) {
308 uint32_t current_install_height_int =
309 encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
312 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
313 this->install_height_select_->publish_state(select_index);
316 if (this->height_threshold_select_ !=
nullptr) {
317 uint32_t current_height_threshold_int =
318 encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
320 height_threshold_float =
bit_cast<float>(current_height_threshold_int);
321 size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
322 this->height_threshold_select_->publish_state(select_index);
325 if (this->sensitivity_select_ !=
nullptr) {
326 current_sensitivity =
327 encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
329 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
330 this->sensitivity_select_->publish_state(select_index);
333 ESP_LOGD(TAG,
"Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
334 height_threshold_float, current_sensitivity);
345 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
346 float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
347 send_data[12] = calculate_checksum(send_data + 8, 4);
349#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
356 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x08, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00};
357 float_to_bytes(HEIGHT_THRESHOLD[index], &send_data[8]);
358 send_data[12] = calculate_checksum(send_data + 8, 4);
360#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
367 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
369 int_to_bytes(SENSITIVITY[index], &send_data[8]);
371 send_data[12] = calculate_checksum(send_data + 8, 4);
373#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
380 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
382#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
389 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
391#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
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 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...
void write_array(const uint8_t *data, size_t len)
@ LOCATE_HEAD_CKSUM_FRAME
@ LOCATE_DATA_CKSUM_FRAME
std::vector< uint8_t > bytes
Providing packet encoding functions for exchanging data with a remote host.
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.