9namespace seeed_mr60fda2 {
11static const char *
const TAG =
"seeed_mr60fda2";
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_);
22 LOG_BUTTON(
" ",
"Get Radar Parameters Button", this->get_radar_parameters_button_);
23 LOG_BUTTON(
" ",
"Reset Radar Button", this->factory_reset_button_);
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_);
34 ESP_LOGCONFIG(TAG,
"Running setup");
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);
47 ESP_LOGCONFIG(TAG,
"Set up MR60FDA2 complete");
57 this->split_frame_(
byte);
71static uint8_t calculate_checksum(
const uint8_t *data,
size_t len) {
73 for (
size_t i = 0; i <
len; i++) {
91static bool validate_checksum(
const uint8_t *data,
size_t len, uint8_t expected_checksum) {
92 return calculate_checksum(data,
len) == expected_checksum;
95static uint8_t find_nearest_index(
float value,
const float *arr,
int size) {
96 int nearest_index = 0;
97 float min_diff = std::abs(value - arr[0]);
98 for (
int i = 1; i < size; ++i) {
99 float diff = std::abs(value - arr[i]);
100 if (diff < min_diff) {
105 return nearest_index;
116static void float_to_bytes(
float value,
unsigned char *bytes) {
119 unsigned char byte_array[4];
122 u.float_value = value;
123 memcpy(bytes, u.byte_array, 4);
134static void int_to_bytes(uint32_t value,
unsigned char *bytes) {
135 bytes[0] = value & 0xFF;
136 bytes[1] = (value >> 8) & 0xFF;
137 bytes[2] = (value >> 16) & 0xFF;
138 bytes[3] = (value >> 24) & 0xFF;
141void MR60FDA2Component::split_frame_(uint8_t buffer) {
142 switch (this->current_frame_locate_) {
144 if (buffer == FRAME_HEADER_BUFFER) {
145 this->current_frame_len_ = 1;
146 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
147 this->current_frame_locate_++;
151 this->current_frame_id_ = buffer << 8;
152 this->current_frame_len_++;
153 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
154 this->current_frame_locate_++;
157 this->current_frame_id_ += buffer;
158 this->current_frame_len_++;
159 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
160 this->current_frame_locate_++;
163 this->current_data_frame_len_ = buffer << 8;
164 if (this->current_data_frame_len_ == 0x00) {
165 this->current_frame_len_++;
166 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
167 this->current_frame_locate_++;
173 this->current_data_frame_len_ += buffer;
174 if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
177 this->current_frame_len_++;
178 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
179 this->current_frame_locate_++;
183 this->current_frame_type_ = buffer << 8;
184 this->current_frame_len_++;
185 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
186 this->current_frame_locate_++;
189 this->current_frame_type_ += buffer;
190 if ((this->current_frame_type_ == IS_FALL_TYPE_BUFFER) ||
191 (this->current_frame_type_ == PEOPLE_EXIST_TYPE_BUFFER) ||
192 (this->current_frame_type_ == RESULT_INSTALL_HEIGHT) || (this->current_frame_type_ == RESULT_PARAMETERS) ||
193 (this->current_frame_type_ == RESULT_HEIGHT_THRESHOLD) || (this->current_frame_type_ == RESULT_SENSITIVITY)) {
194 this->current_frame_len_++;
195 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
196 this->current_frame_locate_++;
202 if (validate_checksum(this->current_frame_buf_, this->current_frame_len_, buffer)) {
203 this->current_frame_len_++;
204 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
205 this->current_frame_locate_++;
207 ESP_LOGD(TAG,
"HEAD_CKSUM_FRAME ERROR: 0x%02x", buffer);
208 ESP_LOGV(TAG,
"CURRENT_FRAME: %s %s",
215 this->current_frame_len_++;
216 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
217 this->current_data_buf_[this->current_frame_len_ - LEN_TO_DATA_FRAME] = buffer;
218 if (this->current_frame_len_ - LEN_TO_HEAD_CKSUM == this->current_data_frame_len_) {
219 this->current_frame_locate_++;
221 if (this->current_frame_len_ > FRAME_BUF_MAX_SIZE) {
222 ESP_LOGD(TAG,
"PRACTICE_DATA_FRAME_LEN ERROR: %d", this->current_frame_len_ - LEN_TO_HEAD_CKSUM);
227 if (validate_checksum(this->current_data_buf_, this->current_data_frame_len_, buffer)) {
228 this->current_frame_len_++;
229 this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
230 this->current_frame_locate_++;
231 this->process_frame_();
233 ESP_LOGD(TAG,
"DATA_CKSUM_FRAME ERROR: 0x%02x", buffer);
234 ESP_LOGV(TAG,
"GET CURRENT_FRAME: %s %s",
246void MR60FDA2Component::process_frame_() {
247 switch (this->current_frame_type_) {
248 case IS_FALL_TYPE_BUFFER:
249 if (this->fall_detected_binary_sensor_ !=
nullptr) {
250 this->fall_detected_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
255 case PEOPLE_EXIST_TYPE_BUFFER:
256 if (this->people_exist_binary_sensor_ !=
nullptr)
257 this->people_exist_binary_sensor_->publish_state(this->current_frame_buf_[LEN_TO_HEAD_CKSUM]);
261 case RESULT_INSTALL_HEIGHT:
262 if (this->current_data_buf_[0]) {
263 ESP_LOGD(TAG,
"Successfully set the mounting height");
265 ESP_LOGD(TAG,
"Failed to set the mounting height");
270 case RESULT_HEIGHT_THRESHOLD:
271 if (this->current_data_buf_[0]) {
272 ESP_LOGD(TAG,
"Successfully set the height threshold");
274 ESP_LOGD(TAG,
"Failed to set the height threshold");
279 case RESULT_SENSITIVITY:
280 if (this->current_data_buf_[0]) {
281 ESP_LOGD(TAG,
"Successfully set the sensitivity");
283 ESP_LOGD(TAG,
"Failed to set the sensitivity");
288 case RESULT_PARAMETERS: {
289 float install_height_float = 0;
290 float height_threshold_float = 0;
291 uint32_t current_sensitivity = 0;
292 if (this->install_height_select_ !=
nullptr) {
293 uint32_t current_install_height_int =
294 encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
297 uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
298 this->install_height_select_->publish_state(this->install_height_select_->at(select_index).value());
301 if (this->height_threshold_select_ !=
nullptr) {
302 uint32_t current_height_threshold_int =
303 encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
305 height_threshold_float =
bit_cast<float>(current_height_threshold_int);
306 size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
307 this->height_threshold_select_->publish_state(this->height_threshold_select_->at(select_index).value());
310 if (this->sensitivity_select_ !=
nullptr) {
311 current_sensitivity =
312 encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
314 uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
315 this->sensitivity_select_->publish_state(this->sensitivity_select_->at(select_index).value());
318 ESP_LOGD(TAG,
"Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
319 height_threshold_float, current_sensitivity);
330 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x04, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00};
331 float_to_bytes(INSTALL_HEIGHT[index], &send_data[8]);
332 send_data[12] = calculate_checksum(send_data + 8, 4);
334 ESP_LOGV(TAG,
"SEND INSTALL HEIGHT FRAME: %s",
format_hex_pretty(send_data, 13).c_str());
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(TAG,
"SEND HEIGHT THRESHOLD: %s",
format_hex_pretty(send_data, 13).c_str());
346 uint8_t send_data[13] = {0x01, 0x00, 0x00, 0x00, 0x04, 0x0E, 0x0A, 0xFE, 0x00, 0x00, 0x00, 0x00, 0x00};
348 int_to_bytes(SENSITIVITY[index], &send_data[8]);
350 send_data[12] = calculate_checksum(send_data + 8, 4);
352 ESP_LOGV(TAG,
"SEND SET SENSITIVITY: %s",
format_hex_pretty(send_data, 13).c_str());
356 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x06, 0xF6};
358 ESP_LOGV(TAG,
"SEND GET PARAMETERS: %s",
format_hex_pretty(send_data, 8).c_str());
362 uint8_t send_data[8] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x21, 0x10, 0xCF};
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)
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 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.
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.
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.