refactor(mr60fda2): refine some details

This commit is contained in:
Spencer Yan 2024-10-24 10:55:25 +08:00
parent ebda1e8a41
commit 0e26f405bc
3 changed files with 32 additions and 45 deletions

View file

@ -16,7 +16,7 @@ GetRadarParametersButton = mr60fda2_ns.class_("GetRadarParametersButton", button
ResetRadarButton = mr60fda2_ns.class_("ResetRadarButton", button.Button)
CONF_GET_RADAR_PARAMETERS = "get_radar_parameters"
CONF_RESET_RADAR = "reset_radar"
CONF_FACTORY_RESET = "reset_radar"
CONFIG_SCHEMA = {
cv.GenerateID(CONF_MR60FDA2_ID): cv.use_id(MR60FDA2Component),
@ -25,7 +25,7 @@ CONFIG_SCHEMA = {
device_class=DEVICE_CLASS_UPDATE,
entity_category=ENTITY_CATEGORY_NONE,
),
cv.Optional(CONF_RESET_RADAR): button.button_schema(
cv.Optional(CONF_FACTORY_RESET): button.button_schema(
ResetRadarButton,
device_class=DEVICE_CLASS_RESTART,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
@ -39,7 +39,7 @@ async def to_code(config):
b = await button.new_button(get_radar_parameters_config)
await cg.register_parented(b, config[CONF_MR60FDA2_ID])
cg.add(mr60fda2_component.set_get_radar_parameters_button(b))
if reset_radar_config := config.get(CONF_RESET_RADAR):
if reset_radar_config := config.get(CONF_FACTORY_RESET):
b = await button.new_button(reset_radar_config)
await cg.register_parented(b, config[CONF_MR60FDA2_ID])
cg.add(mr60fda2_component.set_reset_radar_button(b))

View file

@ -136,17 +136,12 @@ void MR60FDA2Component::split_frame_(uint8_t buffer) {
this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
this->current_frame_locate_++;
} else {
// ESP_LOGD(TAG, "DATA_FRAME_LEN_H: 0x%02x", buffer);
// ESP_LOGD(TAG, "CURRENT_FRAME_LEN_H: 0x%04x", this->current_data_frame_len_);
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
}
break;
case LOCATE_LENGTH_FRAME_L:
this->current_data_frame_len_ += buffer;
if (this->current_data_frame_len_ > DATA_BUF_MAX_SIZE) {
// ESP_LOGD(TAG, "DATA_FRAME_LEN_L: 0x%02x", buffer);
// ESP_LOGD(TAG, "CURRENT_FRAME_LEN: 0x%04x", this->current_data_frame_len_);
// ESP_LOGD(TAG, "DATA_FRAME_LEN ERROR: %d", this->current_data_frame_len_);
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
} else {
this->current_frame_len_++;
@ -159,7 +154,6 @@ void MR60FDA2Component::split_frame_(uint8_t buffer) {
this->current_frame_len_++;
this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
this->current_frame_locate_++;
// ESP_LOGD(TAG, "GET LOCATE_TYPE_FRAME1: 0x%02x", this->current_frame_buf_[this->current_frame_len_ - 1]);
break;
case LOCATE_TYPE_FRAME2:
this->current_frame_type_ += buffer;
@ -170,13 +164,7 @@ void MR60FDA2Component::split_frame_(uint8_t buffer) {
this->current_frame_len_++;
this->current_frame_buf_[this->current_frame_len_ - 1] = buffer;
this->current_frame_locate_++;
// ESP_LOGD(TAG, "GET CURRENT_FRAME_TYPE: 0x%02x 0x%02x", this->current_frame_buf_[this->current_frame_len_ -
// 2],
// this->current_frame_buf_[this->current_frame_len_ - 1]);
} else {
// ESP_LOGD(TAG, "CURRENT_FRAME_TYPE NOT FOUND: 0x%02x 0x%02x",
// this->current_frame_buf_[this->current_frame_len_ - 2],
// this->current_frame_buf_[this->current_frame_len_ - 1]);
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
}
break;
@ -263,36 +251,35 @@ void MR60FDA2Component::process_frame_() {
this->current_frame_locate_ = LOCATE_FRAME_HEADER;
break;
case RESULT_PARAMETERS:
// ESP_LOGD(
// TAG,
// "GET CURRENT_FRAME: 0x%02x 0x%02x 0x%02x 0x%02x, 0x%02x 0x%02x 0x%02x 0x%02x, 0x%02x 0x%02x 0x%02x 0x%02x",
// this->current_frame_buf_[8], this->current_frame_buf_[9], this->current_frame_buf_[10],
// this->current_frame_buf_[11], this->current_frame_buf_[12], this->current_frame_buf_[13],
// this->current_frame_buf_[14], this->current_frame_buf_[15], this->current_frame_buf_[16],
// this->current_frame_buf_[17], this->current_frame_buf_[18], this->current_frame_buf_[19]);
// ESP_LOGD(
// TAG,
// "GET CURRENT_FRAME_2: 0x%02x 0x%02x 0x%02x 0x%02x, 0x%02x 0x%02x 0x%02x 0x%02x, 0x%02x 0x%02x 0x%02x
// 0x%02x", this->current_data_buf_[0], this->current_data_buf_[1], this->current_data_buf_[2],
// this->current_data_buf_[3], this->current_data_buf_[4], this->current_data_buf_[5],
// this->current_data_buf_[6], this->current_data_buf_[7], this->current_data_buf_[8],
// this->current_data_buf_[9], this->current_data_buf_[10], this->current_data_buf_[11]);
this->current_install_height_int_ =
encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
float install_height_float;
memcpy(&install_height_float, &current_install_height_int_, sizeof(float));
select_index_ = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
this->install_height_select_->publish_state(this->install_height_select_->at(select_index_).value());
this->current_height_threshold_int_ =
encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
float height_threshold_float;
memcpy(&height_threshold_float, &current_height_threshold_int_, sizeof(float));
select_index_ = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
this->height_threshold_select_->publish_state(this->height_threshold_select_->at(select_index_).value());
this->current_sensitivity_ =
encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
select_index_ = find_nearest_index(this->current_sensitivity_, SENSITIVITY, 3);
this->sensitivity_select_->publish_state(this->sensitivity_select_->at(select_index_).value());
if (this->install_height_select_ != nullptr) {
this->current_install_height_int_ =
encode_uint32(current_data_buf_[3], current_data_buf_[2], current_data_buf_[1], current_data_buf_[0]);
install_height_float = bit_cast<float>(current_install_height_int_);
select_index_ = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
this->install_height_select_->publish_state(this->install_height_select_->at(select_index_).value());
}
if (this->height_threshold_select_ != nullptr) {
this->current_height_threshold_int_ =
encode_uint32(current_data_buf_[7], current_data_buf_[6], current_data_buf_[5], current_data_buf_[4]);
height_threshold_float = bit_cast<float>(current_height_threshold_int_);
select_index_ = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
this->height_threshold_select_->publish_state(this->height_threshold_select_->at(select_index_).value());
}
if (this->sensitivity_select_ != nullptr) {
this->current_sensitivity_ =
encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
select_index_ = find_nearest_index(this->current_sensitivity_, SENSITIVITY, 3);
this->sensitivity_select_->publish_state(this->sensitivity_select_->at(select_index_).value());
}
ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %u", install_height_float,
height_threshold_float, this->current_sensitivity_);
this->current_frame_locate_ = LOCATE_FRAME_HEADER;

View file

@ -19,12 +19,12 @@ CONFIG_SCHEMA = {
cv.Optional(CONF_INSTALL_HEIGHT): select.select_schema(
InstallHeightSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon="mdi:axis-z-arrow",
icon="ICON_ACCELERATION_Z",
),
cv.Optional(CONF_HEIGHT_THRESHOLD): select.select_schema(
HeightThresholdSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon="mdi:axis-z-arrow",
icon="ICON_ACCELERATION_Z",
),
cv.Optional(CONF_SENSITIVITY): select.select_schema(
SensitivitySelect,