mirror of
https://github.com/esphome/esphome.git
synced 2025-01-22 12:26:01 +01:00
Add Zone still and moving target sensors
This commit is contained in:
parent
fab4fe8f28
commit
15d3f17179
4 changed files with 87 additions and 16 deletions
|
@ -91,6 +91,12 @@ void LD2450Component::dump_config() {
|
||||||
for (sensor::Sensor *s : this->zone_target_count_sensors_) {
|
for (sensor::Sensor *s : this->zone_target_count_sensors_) {
|
||||||
LOG_SENSOR(" ", "NthZoneTargetCountSensor", s);
|
LOG_SENSOR(" ", "NthZoneTargetCountSensor", s);
|
||||||
}
|
}
|
||||||
|
for (sensor::Sensor *s : this->zone_still_target_count_sensors_) {
|
||||||
|
LOG_SENSOR(" ", "NthZoneStillTargetCountSensor", s);
|
||||||
|
}
|
||||||
|
for (sensor::Sensor *s : this->zone_moving_target_count_sensors_) {
|
||||||
|
LOG_SENSOR(" ", "NthZoneMovingTargetCountSensor", s);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
LOG_TEXT_SENSOR(" ", "VersionTextSensor", this->version_text_sensor_);
|
LOG_TEXT_SENSOR(" ", "VersionTextSensor", this->version_text_sensor_);
|
||||||
|
@ -136,10 +142,11 @@ void LD2450Component::loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count targets in zone
|
// Count targets in zone
|
||||||
uint8_t LD2450Component::count_targets_in_zone_(const Zone &zone) {
|
uint8_t LD2450Component::count_targets_in_zone_(const Zone &zone, bool is_moving) {
|
||||||
uint8_t count = 0;
|
uint8_t count = 0;
|
||||||
for (auto &index : this->target_info_) {
|
for (auto &index : this->target_info_) {
|
||||||
if (index.x >= zone.x1 && index.x <= zone.x2 && index.y >= zone.y1 && index.y <= zone.y2) {
|
if (index.x > zone.x1 && index.x < zone.x2 && index.y > zone.y1 && index.y < zone.y2 &&
|
||||||
|
index.is_moving == is_moving) {
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -318,12 +325,14 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, int len) {
|
||||||
int16_t ts = 0;
|
int16_t ts = 0;
|
||||||
int16_t angle;
|
int16_t angle;
|
||||||
std::string direction;
|
std::string direction;
|
||||||
|
bool is_moving;
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
// Loop thru targets
|
// Loop thru targets
|
||||||
// X
|
// X
|
||||||
for (index = 0; index < MAX_TARGETS; index++) {
|
for (index = 0; index < MAX_TARGETS; index++) {
|
||||||
start = TARGET_X + index * 8;
|
start = TARGET_X + index * 8;
|
||||||
|
is_moving = false;
|
||||||
sensor::Sensor *sx = this->move_x_sensors_[index];
|
sensor::Sensor *sx = this->move_x_sensors_[index];
|
||||||
if (sx != nullptr) {
|
if (sx != nullptr) {
|
||||||
val = this->decode_coordinate_(buffer[start], buffer[start + 1]);
|
val = this->decode_coordinate_(buffer[start], buffer[start + 1]);
|
||||||
|
@ -348,8 +357,10 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, int len) {
|
||||||
if (ss != nullptr) {
|
if (ss != nullptr) {
|
||||||
val = this->decode_speed_(buffer[start], buffer[start + 1]);
|
val = this->decode_speed_(buffer[start], buffer[start + 1]);
|
||||||
ts = val;
|
ts = val;
|
||||||
if (val > 0)
|
if (val > 0) {
|
||||||
|
is_moving = true;
|
||||||
moving_target_count++;
|
moving_target_count++;
|
||||||
|
}
|
||||||
if (ss->get_state() != val) {
|
if (ss->get_state() != val) {
|
||||||
ss->publish_state(val);
|
ss->publish_state(val);
|
||||||
}
|
}
|
||||||
|
@ -404,20 +415,44 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, int len) {
|
||||||
|
|
||||||
this->target_info_[index].x = tx;
|
this->target_info_[index].x = tx;
|
||||||
this->target_info_[index].y = ty;
|
this->target_info_[index].y = ty;
|
||||||
|
this->target_info_[index].is_moving = is_moving;
|
||||||
|
|
||||||
} // End loop thru targets
|
} // End loop thru targets
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
// Loop thru zones
|
// Loop thru zones
|
||||||
|
uint8_t zone_still_targets = 0;
|
||||||
|
uint8_t zone_moving_targets = 0;
|
||||||
|
uint8_t zone_all_targets = 0;
|
||||||
for (index = 0; index < MAX_ZONES; index++) {
|
for (index = 0; index < MAX_ZONES; index++) {
|
||||||
// Publish Target Count in Zones
|
// Publish Still Target Count in Zones
|
||||||
sensor::Sensor *sztc = this->zone_target_count_sensors_[index];
|
sensor::Sensor *szstc = this->zone_still_target_count_sensors_[index];
|
||||||
if (sztc != nullptr) {
|
if (szstc != nullptr) {
|
||||||
val = this->count_targets_in_zone_(this->zone_config_[index]);
|
zone_still_targets = this->count_targets_in_zone_(this->zone_config_[index], false);
|
||||||
if (sztc->get_state() != val) {
|
if (szstc->get_state() != zone_still_targets) {
|
||||||
sztc->publish_state(val);
|
szstc->publish_state(zone_still_targets);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Publish Moving Target Count in Zones
|
||||||
|
sensor::Sensor *szmtc = this->zone_moving_target_count_sensors_[index];
|
||||||
|
if (szmtc != nullptr) {
|
||||||
|
zone_moving_targets = this->count_targets_in_zone_(this->zone_config_[index], true);
|
||||||
|
if (szmtc->get_state() != zone_moving_targets) {
|
||||||
|
szmtc->publish_state(zone_moving_targets);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
zone_all_targets = zone_still_targets + zone_moving_targets;
|
||||||
|
|
||||||
|
// Publish All Target Count in Zones
|
||||||
|
sensor::Sensor *sztc = this->zone_target_count_sensors_[index];
|
||||||
|
if (sztc != nullptr) {
|
||||||
|
if (sztc->get_state() != zone_all_targets) {
|
||||||
|
sztc->publish_state(zone_all_targets);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // End loop thru zones
|
} // End loop thru zones
|
||||||
|
|
||||||
still_target_count = target_count - moving_target_count;
|
still_target_count = target_count - moving_target_count;
|
||||||
|
@ -744,6 +779,12 @@ void LD2450Component::set_move_resolution_sensor(int target, sensor::Sensor *s)
|
||||||
void LD2450Component::set_zone_target_count_sensor(int zone, sensor::Sensor *s) {
|
void LD2450Component::set_zone_target_count_sensor(int zone, sensor::Sensor *s) {
|
||||||
this->zone_target_count_sensors_[zone] = s;
|
this->zone_target_count_sensors_[zone] = s;
|
||||||
}
|
}
|
||||||
|
void LD2450Component::set_zone_still_target_count_sensor(int zone, sensor::Sensor *s) {
|
||||||
|
this->zone_still_target_count_sensors_[zone] = s;
|
||||||
|
}
|
||||||
|
void LD2450Component::set_zone_moving_target_count_sensor(int zone, sensor::Sensor *s) {
|
||||||
|
this->zone_moving_target_count_sensors_[zone] = s;
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
void LD2450Component::set_direction_text_sensor(int target, text_sensor::TextSensor *s) {
|
void LD2450Component::set_direction_text_sensor(int target, text_sensor::TextSensor *s) {
|
||||||
|
|
|
@ -52,6 +52,7 @@ static const uint8_t MAX_ZONES = 3; // Max 3 Zones in LD2450
|
||||||
struct Target {
|
struct Target {
|
||||||
int16_t x;
|
int16_t x;
|
||||||
int16_t y;
|
int16_t y;
|
||||||
|
bool is_moving;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Zone coordinate struct
|
// Zone coordinate struct
|
||||||
|
@ -189,6 +190,8 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
||||||
void set_move_distance_sensor(int target, sensor::Sensor *s);
|
void set_move_distance_sensor(int target, sensor::Sensor *s);
|
||||||
void set_move_resolution_sensor(int target, sensor::Sensor *s);
|
void set_move_resolution_sensor(int target, sensor::Sensor *s);
|
||||||
void set_zone_target_count_sensor(int zone, sensor::Sensor *s);
|
void set_zone_target_count_sensor(int zone, sensor::Sensor *s);
|
||||||
|
void set_zone_still_target_count_sensor(int zone, sensor::Sensor *s);
|
||||||
|
void set_zone_moving_target_count_sensor(int zone, sensor::Sensor *s);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -261,7 +264,7 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
||||||
std::string version_;
|
std::string version_;
|
||||||
std::string mac_;
|
std::string mac_;
|
||||||
bool get_timeout_status_(int32_t check_millis);
|
bool get_timeout_status_(int32_t check_millis);
|
||||||
uint8_t count_targets_in_zone_(const Zone &zone);
|
uint8_t count_targets_in_zone_(const Zone &zone, bool is_moving);
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
std::vector<text_sensor::TextSensor *> direction_text_sensors_ = std::vector<text_sensor::TextSensor *>(3);
|
std::vector<text_sensor::TextSensor *> direction_text_sensors_ = std::vector<text_sensor::TextSensor *>(3);
|
||||||
#endif
|
#endif
|
||||||
|
@ -279,6 +282,8 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
||||||
std::vector<sensor::Sensor *> move_distance_sensors_ = std::vector<sensor::Sensor *>(MAX_TARGETS);
|
std::vector<sensor::Sensor *> move_distance_sensors_ = std::vector<sensor::Sensor *>(MAX_TARGETS);
|
||||||
std::vector<sensor::Sensor *> move_resolution_sensors_ = std::vector<sensor::Sensor *>(MAX_TARGETS);
|
std::vector<sensor::Sensor *> move_resolution_sensors_ = std::vector<sensor::Sensor *>(MAX_TARGETS);
|
||||||
std::vector<sensor::Sensor *> zone_target_count_sensors_ = std::vector<sensor::Sensor *>(MAX_ZONES);
|
std::vector<sensor::Sensor *> zone_target_count_sensors_ = std::vector<sensor::Sensor *>(MAX_ZONES);
|
||||||
|
std::vector<sensor::Sensor *> zone_still_target_count_sensors_ = std::vector<sensor::Sensor *>(MAX_ZONES);
|
||||||
|
std::vector<sensor::Sensor *> zone_moving_target_count_sensors_ = std::vector<sensor::Sensor *>(MAX_ZONES);
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -85,6 +85,12 @@ CONFIG_SCHEMA = CONFIG_SCHEMA.extend(
|
||||||
cv.Optional(CONF_TARGET_COUNT): sensor.sensor_schema(
|
cv.Optional(CONF_TARGET_COUNT): sensor.sensor_schema(
|
||||||
icon="mdi:map-marker-account",
|
icon="mdi:map-marker-account",
|
||||||
),
|
),
|
||||||
|
cv.Optional(CONF_STILL_TARGET_COUNT): sensor.sensor_schema(
|
||||||
|
icon="mdi:map-marker-account",
|
||||||
|
),
|
||||||
|
cv.Optional(CONF_MOVING_TARGET_COUNT): sensor.sensor_schema(
|
||||||
|
icon="mdi:map-marker-account",
|
||||||
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
for n in range(MAX_ZONES)
|
for n in range(MAX_ZONES)
|
||||||
|
@ -127,6 +133,13 @@ async def to_code(config):
|
||||||
sens = await sensor.new_sensor(resolution_config)
|
sens = await sensor.new_sensor(resolution_config)
|
||||||
cg.add(ld2450_component.set_move_resolution_sensor(n, sens))
|
cg.add(ld2450_component.set_move_resolution_sensor(n, sens))
|
||||||
for n in range(MAX_ZONES):
|
for n in range(MAX_ZONES):
|
||||||
if zone_target_count_config := config.get(f"zone_{n+1}_target_count"):
|
if zone_config := config.get(f"zone_{n+1}"):
|
||||||
sens = await sensor.new_sensor(zone_target_count_config)
|
if target_count_config := zone_config.get(CONF_TARGET_COUNT):
|
||||||
cg.add(ld2450_component.set_zone_target_count_sensor(n, sens))
|
sens = await sensor.new_sensor(target_count_config)
|
||||||
|
cg.add(ld2450_component.set_zone_target_count_sensor(n, sens))
|
||||||
|
if still_target_count_config := zone_config.get(CONF_STILL_TARGET_COUNT):
|
||||||
|
sens = await sensor.new_sensor(still_target_count_config)
|
||||||
|
cg.add(ld2450_component.set_zone_still_target_count_sensor(n, sens))
|
||||||
|
if moving_target_count_config := zone_config.get(CONF_MOVING_TARGET_COUNT):
|
||||||
|
sens = await sensor.new_sensor(moving_target_count_config)
|
||||||
|
cg.add(ld2450_component.set_zone_moving_target_count_sensor(n, sens))
|
||||||
|
|
|
@ -302,13 +302,25 @@ sensor:
|
||||||
name: Target-3 Resolution
|
name: Target-3 Resolution
|
||||||
zone_1:
|
zone_1:
|
||||||
target_count:
|
target_count:
|
||||||
name: "Zone-1 Target Count"
|
name: Zone-1 All Target Count
|
||||||
|
still_target_count:
|
||||||
|
name: Zone-1 Still Target Count
|
||||||
|
moving_target_count:
|
||||||
|
name: Zone-1 Moving Target Count
|
||||||
zone_2:
|
zone_2:
|
||||||
target_count:
|
target_count:
|
||||||
name: "Zone-2 Target Count"
|
name: Zone-2 All Target Count
|
||||||
|
still_target_count:
|
||||||
|
name: Zone-2 Still Target Count
|
||||||
|
moving_target_count:
|
||||||
|
name: Zone-2 Moving Target Count
|
||||||
zone_3:
|
zone_3:
|
||||||
target_count:
|
target_count:
|
||||||
name: "Zone-3 Target Count"
|
name: Zone-3 All Target Count
|
||||||
|
still_target_count:
|
||||||
|
name: Zone-3 Still Target Count
|
||||||
|
moving_target_count:
|
||||||
|
name: Zone-3 Moving Target Count
|
||||||
|
|
||||||
binary_sensor:
|
binary_sensor:
|
||||||
- platform: ld2450
|
- platform: ld2450
|
||||||
|
|
Loading…
Reference in a new issue