From 4da42dedc86d21aeb35260b0ed66380aebe234c5 Mon Sep 17 00:00:00 2001 From: Keith Burzinski Date: Thu, 27 Feb 2025 11:58:19 -0600 Subject: [PATCH] [ld2450] Fix misplaced ``ifdef`` and related logic (#8335) --- esphome/components/ld2450/ld2450.cpp | 54 +++++++++++++++------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/esphome/components/ld2450/ld2450.cpp b/esphome/components/ld2450/ld2450.cpp index d622d2fca7..dd564e6ed5 100644 --- a/esphome/components/ld2450/ld2450.cpp +++ b/esphome/components/ld2450/ld2450.cpp @@ -386,10 +386,11 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { std::string direction{}; bool is_moving = false; -#ifdef USE_SENSOR +#if defined(USE_BINARY_SENSOR) || defined(USE_SENSOR) || defined(USE_TEXT_SENSOR) // Loop thru targets - // X for (index = 0; index < MAX_TARGETS; index++) { +#ifdef USE_SENSOR + // X start = TARGET_X + index * 8; is_moving = false; sensor::Sensor *sx = this->move_x_sensors_[index]; @@ -406,18 +407,6 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { ty = val; sy->publish_state(val); } - // SPEED - start = TARGET_SPEED + index * 8; - sensor::Sensor *ss = this->move_speed_sensors_[index]; - if (ss != nullptr) { - val = ld2450::decode_speed(buffer[start], buffer[start + 1]); - ts = val; - if (val) { - is_moving = true; - moving_target_count++; - } - ss->publish_state(val); - } // RESOLUTION start = TARGET_RESOLUTION + index * 8; sensor::Sensor *sr = this->move_resolution_sensors_[index]; @@ -425,17 +414,32 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { val = (buffer[start + 1] << 8) | buffer[start]; sr->publish_state(val); } +#endif + // SPEED + start = TARGET_SPEED + index * 8; + val = ld2450::decode_speed(buffer[start], buffer[start + 1]); + ts = val; + if (val) { + is_moving = true; + moving_target_count++; + } +#ifdef USE_SENSOR + sensor::Sensor *ss = this->move_speed_sensors_[index]; + if (ss != nullptr) { + ss->publish_state(val); + } +#endif // DISTANCE + val = (uint16_t) sqrt( + pow(ld2450::decode_coordinate(buffer[TARGET_X + index * 8], buffer[(TARGET_X + index * 8) + 1]), 2) + + pow(ld2450::decode_coordinate(buffer[TARGET_Y + index * 8], buffer[(TARGET_Y + index * 8) + 1]), 2)); + td = val; + if (val > 0) { + target_count++; + } +#ifdef USE_SENSOR sensor::Sensor *sd = this->move_distance_sensors_[index]; if (sd != nullptr) { - val = (uint16_t) sqrt( - pow(ld2450::decode_coordinate(buffer[TARGET_X + index * 8], buffer[(TARGET_X + index * 8) + 1]), 2) + - pow(ld2450::decode_coordinate(buffer[TARGET_Y + index * 8], buffer[(TARGET_Y + index * 8) + 1]), 2)); - td = val; - if (val > 0) { - target_count++; - } - sd->publish_state(val); } // ANGLE @@ -448,8 +452,8 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { sa->publish_state(angle); } #endif - // DIRECTION #ifdef USE_TEXT_SENSOR + // DIRECTION direction = get_direction(ts); if (td == 0) { direction = "NA"; @@ -467,6 +471,9 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { } // End loop thru targets + still_target_count = target_count - moving_target_count; +#endif + #ifdef USE_SENSOR // Loop thru zones uint8_t zone_still_targets = 0; @@ -496,7 +503,6 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { } // End loop thru zones - still_target_count = target_count - moving_target_count; // Target Count if (this->target_count_sensor_ != nullptr) { this->target_count_sensor_->publish_state(target_count);