mirror of
https://github.com/esphome/esphome.git
synced 2026-06-02 03:02:19 +08:00
[ld24xx] Replace heap-allocated SensorWithDedup with inline SensorWithDedup (#15676)
This commit is contained in:
@@ -360,8 +360,8 @@ void LD2410Component::handle_periodic_data_() {
|
|||||||
*/
|
*/
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
SAFE_PUBLISH_SENSOR(this->moving_target_distance_sensor_,
|
SAFE_PUBLISH_SENSOR(this->moving_target_distance_sensor_,
|
||||||
encode_uint16(this->buffer_data_[MOVING_TARGET_HIGH], this->buffer_data_[MOVING_TARGET_LOW]))
|
encode_uint16(this->buffer_data_[MOVING_TARGET_HIGH], this->buffer_data_[MOVING_TARGET_LOW]));
|
||||||
SAFE_PUBLISH_SENSOR(this->moving_target_energy_sensor_, this->buffer_data_[MOVING_ENERGY])
|
SAFE_PUBLISH_SENSOR(this->moving_target_energy_sensor_, this->buffer_data_[MOVING_ENERGY]);
|
||||||
SAFE_PUBLISH_SENSOR(this->still_target_distance_sensor_,
|
SAFE_PUBLISH_SENSOR(this->still_target_distance_sensor_,
|
||||||
encode_uint16(this->buffer_data_[STILL_TARGET_HIGH], this->buffer_data_[STILL_TARGET_LOW]));
|
encode_uint16(this->buffer_data_[STILL_TARGET_HIGH], this->buffer_data_[STILL_TARGET_LOW]));
|
||||||
SAFE_PUBLISH_SENSOR(this->still_target_energy_sensor_, this->buffer_data_[STILL_ENERGY]);
|
SAFE_PUBLISH_SENSOR(this->still_target_energy_sensor_, this->buffer_data_[STILL_ENERGY]);
|
||||||
@@ -375,26 +375,26 @@ void LD2410Component::handle_periodic_data_() {
|
|||||||
Moving energy: 20~28th bytes
|
Moving energy: 20~28th bytes
|
||||||
*/
|
*/
|
||||||
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
|
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
|
||||||
SAFE_PUBLISH_SENSOR(this->gate_move_sensors_[i], this->buffer_data_[MOVING_SENSOR_START + i])
|
SAFE_PUBLISH_SENSOR(this->gate_move_sensors_[i], this->buffer_data_[MOVING_SENSOR_START + i]);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
Still energy: 29~37th bytes
|
Still energy: 29~37th bytes
|
||||||
*/
|
*/
|
||||||
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
|
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
|
||||||
SAFE_PUBLISH_SENSOR(this->gate_still_sensors_[i], this->buffer_data_[STILL_SENSOR_START + i])
|
SAFE_PUBLISH_SENSOR(this->gate_still_sensors_[i], this->buffer_data_[STILL_SENSOR_START + i]);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
Light sensor: 38th bytes
|
Light sensor: 38th bytes
|
||||||
*/
|
*/
|
||||||
SAFE_PUBLISH_SENSOR(this->light_sensor_, this->buffer_data_[LIGHT_SENSOR])
|
SAFE_PUBLISH_SENSOR(this->light_sensor_, this->buffer_data_[LIGHT_SENSOR]);
|
||||||
} else {
|
} else {
|
||||||
for (auto &gate_move_sensor : this->gate_move_sensors_) {
|
for (auto &gate_move_sensor : this->gate_move_sensors_) {
|
||||||
SAFE_PUBLISH_SENSOR_UNKNOWN(gate_move_sensor)
|
SAFE_PUBLISH_SENSOR_UNKNOWN(gate_move_sensor);
|
||||||
}
|
}
|
||||||
for (auto &gate_still_sensor : this->gate_still_sensors_) {
|
for (auto &gate_still_sensor : this->gate_still_sensors_) {
|
||||||
SAFE_PUBLISH_SENSOR_UNKNOWN(gate_still_sensor)
|
SAFE_PUBLISH_SENSOR_UNKNOWN(gate_still_sensor);
|
||||||
}
|
}
|
||||||
SAFE_PUBLISH_SENSOR_UNKNOWN(this->light_sensor_)
|
SAFE_PUBLISH_SENSOR_UNKNOWN(this->light_sensor_);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_BINARY_SENSOR
|
#ifdef USE_BINARY_SENSOR
|
||||||
@@ -786,13 +786,12 @@ void LD2410Component::set_light_out_control() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
// These could leak memory, but they are only set once prior to 'setup()' and should never be used again.
|
|
||||||
void LD2410Component::set_gate_move_sensor(uint8_t gate, sensor::Sensor *s) {
|
void LD2410Component::set_gate_move_sensor(uint8_t gate, sensor::Sensor *s) {
|
||||||
this->gate_move_sensors_[gate] = new SensorWithDedup<uint8_t>(s);
|
this->gate_move_sensors_[gate].set_sensor(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LD2410Component::set_gate_still_sensor(uint8_t gate, sensor::Sensor *s) {
|
void LD2410Component::set_gate_still_sensor(uint8_t gate, sensor::Sensor *s) {
|
||||||
this->gate_still_sensors_[gate] = new SensorWithDedup<uint8_t>(s);
|
this->gate_still_sensors_[gate].set_sensor(s);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -129,8 +129,8 @@ class LD2410Component : public Component, public uart::UARTDevice {
|
|||||||
std::array<number::Number *, TOTAL_GATES> gate_still_threshold_numbers_{};
|
std::array<number::Number *, TOTAL_GATES> gate_still_threshold_numbers_{};
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_move_sensors_{};
|
std::array<SensorWithDedup<uint8_t>, TOTAL_GATES> gate_move_sensors_{};
|
||||||
std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_still_sensors_{};
|
std::array<SensorWithDedup<uint8_t>, TOTAL_GATES> gate_still_sensors_{};
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -397,12 +397,12 @@ void LD2412Component::handle_periodic_data_() {
|
|||||||
*/
|
*/
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
SAFE_PUBLISH_SENSOR(this->moving_target_distance_sensor_,
|
SAFE_PUBLISH_SENSOR(this->moving_target_distance_sensor_,
|
||||||
encode_uint16(this->buffer_data_[MOVING_TARGET_HIGH], this->buffer_data_[MOVING_TARGET_LOW]))
|
encode_uint16(this->buffer_data_[MOVING_TARGET_HIGH], this->buffer_data_[MOVING_TARGET_LOW]));
|
||||||
SAFE_PUBLISH_SENSOR(this->moving_target_energy_sensor_, this->buffer_data_[MOVING_ENERGY])
|
SAFE_PUBLISH_SENSOR(this->moving_target_energy_sensor_, this->buffer_data_[MOVING_ENERGY]);
|
||||||
SAFE_PUBLISH_SENSOR(this->still_target_distance_sensor_,
|
SAFE_PUBLISH_SENSOR(this->still_target_distance_sensor_,
|
||||||
encode_uint16(this->buffer_data_[STILL_TARGET_HIGH], this->buffer_data_[STILL_TARGET_LOW]))
|
encode_uint16(this->buffer_data_[STILL_TARGET_HIGH], this->buffer_data_[STILL_TARGET_LOW]));
|
||||||
SAFE_PUBLISH_SENSOR(this->still_target_energy_sensor_, this->buffer_data_[STILL_ENERGY])
|
SAFE_PUBLISH_SENSOR(this->still_target_energy_sensor_, this->buffer_data_[STILL_ENERGY]);
|
||||||
if (this->detection_distance_sensor_ != nullptr) {
|
if (this->detection_distance_sensor_.has_sensor()) {
|
||||||
int new_detect_distance = 0;
|
int new_detect_distance = 0;
|
||||||
if (target_state != 0x00 && (target_state & MOVE_BITMASK)) {
|
if (target_state != 0x00 && (target_state & MOVE_BITMASK)) {
|
||||||
new_detect_distance =
|
new_detect_distance =
|
||||||
@@ -410,7 +410,7 @@ void LD2412Component::handle_periodic_data_() {
|
|||||||
} else if (target_state != 0x00) {
|
} else if (target_state != 0x00) {
|
||||||
new_detect_distance = encode_uint16(this->buffer_data_[STILL_TARGET_HIGH], this->buffer_data_[STILL_TARGET_LOW]);
|
new_detect_distance = encode_uint16(this->buffer_data_[STILL_TARGET_HIGH], this->buffer_data_[STILL_TARGET_LOW]);
|
||||||
}
|
}
|
||||||
this->detection_distance_sensor_->publish_state_if_not_dup(new_detect_distance);
|
this->detection_distance_sensor_.publish_state_if_not_dup(new_detect_distance);
|
||||||
}
|
}
|
||||||
if (engineering_mode) {
|
if (engineering_mode) {
|
||||||
// Engineering mode needs at least LIGHT_SENSOR + 1 bytes
|
// Engineering mode needs at least LIGHT_SENSOR + 1 bytes
|
||||||
@@ -423,27 +423,27 @@ void LD2412Component::handle_periodic_data_() {
|
|||||||
Moving energy: 20~28th bytes
|
Moving energy: 20~28th bytes
|
||||||
*/
|
*/
|
||||||
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
|
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
|
||||||
SAFE_PUBLISH_SENSOR(this->gate_move_sensors_[i], this->buffer_data_[MOVING_SENSOR_START + i])
|
SAFE_PUBLISH_SENSOR(this->gate_move_sensors_[i], this->buffer_data_[MOVING_SENSOR_START + i]);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
Still energy: 29~37th bytes
|
Still energy: 29~37th bytes
|
||||||
*/
|
*/
|
||||||
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
|
for (uint8_t i = 0; i < TOTAL_GATES; i++) {
|
||||||
SAFE_PUBLISH_SENSOR(this->gate_still_sensors_[i], this->buffer_data_[STILL_SENSOR_START + i])
|
SAFE_PUBLISH_SENSOR(this->gate_still_sensors_[i], this->buffer_data_[STILL_SENSOR_START + i]);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
Light sensor value
|
Light sensor value
|
||||||
*/
|
*/
|
||||||
SAFE_PUBLISH_SENSOR(this->light_sensor_, this->buffer_data_[LIGHT_SENSOR])
|
SAFE_PUBLISH_SENSOR(this->light_sensor_, this->buffer_data_[LIGHT_SENSOR]);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for (auto &gate_move_sensor : this->gate_move_sensors_) {
|
for (auto &gate_move_sensor : this->gate_move_sensors_) {
|
||||||
SAFE_PUBLISH_SENSOR_UNKNOWN(gate_move_sensor)
|
SAFE_PUBLISH_SENSOR_UNKNOWN(gate_move_sensor);
|
||||||
}
|
}
|
||||||
for (auto &gate_still_sensor : this->gate_still_sensors_) {
|
for (auto &gate_still_sensor : this->gate_still_sensors_) {
|
||||||
SAFE_PUBLISH_SENSOR_UNKNOWN(gate_still_sensor)
|
SAFE_PUBLISH_SENSOR_UNKNOWN(gate_still_sensor);
|
||||||
}
|
}
|
||||||
SAFE_PUBLISH_SENSOR_UNKNOWN(this->light_sensor_)
|
SAFE_PUBLISH_SENSOR_UNKNOWN(this->light_sensor_);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
// the radar module won't tell us when it's done, so we just have to keep polling...
|
// the radar module won't tell us when it's done, so we just have to keep polling...
|
||||||
@@ -846,12 +846,11 @@ void LD2412Component::set_light_out_control() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
// These could leak memory, but they are only set once prior to 'setup()' and should never be used again.
|
|
||||||
void LD2412Component::set_gate_move_sensor(uint8_t gate, sensor::Sensor *s) {
|
void LD2412Component::set_gate_move_sensor(uint8_t gate, sensor::Sensor *s) {
|
||||||
this->gate_move_sensors_[gate] = new SensorWithDedup<uint8_t>(s);
|
this->gate_move_sensors_[gate].set_sensor(s);
|
||||||
}
|
}
|
||||||
void LD2412Component::set_gate_still_sensor(uint8_t gate, sensor::Sensor *s) {
|
void LD2412Component::set_gate_still_sensor(uint8_t gate, sensor::Sensor *s) {
|
||||||
this->gate_still_sensors_[gate] = new SensorWithDedup<uint8_t>(s);
|
this->gate_still_sensors_[gate].set_sensor(s);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -133,8 +133,8 @@ class LD2412Component : public Component, public uart::UARTDevice {
|
|||||||
std::array<number::Number *, TOTAL_GATES> gate_still_threshold_numbers_{};
|
std::array<number::Number *, TOTAL_GATES> gate_still_threshold_numbers_{};
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_move_sensors_{};
|
std::array<SensorWithDedup<uint8_t>, TOTAL_GATES> gate_move_sensors_{};
|
||||||
std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_still_sensors_{};
|
std::array<SensorWithDedup<uint8_t>, TOTAL_GATES> gate_still_sensors_{};
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -565,6 +565,7 @@ void LD2450Component::handle_periodic_data_() {
|
|||||||
SAFE_PUBLISH_SENSOR(this->still_target_count_sensor_, still_target_count);
|
SAFE_PUBLISH_SENSOR(this->still_target_count_sensor_, still_target_count);
|
||||||
// Moving Target Count
|
// Moving Target Count
|
||||||
SAFE_PUBLISH_SENSOR(this->moving_target_count_sensor_, moving_target_count);
|
SAFE_PUBLISH_SENSOR(this->moving_target_count_sensor_, moving_target_count);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_BINARY_SENSOR
|
#ifdef USE_BINARY_SENSOR
|
||||||
@@ -872,33 +873,32 @@ void LD2450Component::query_target_tracking_mode_() { this->send_command_(CMD_QU
|
|||||||
void LD2450Component::query_zone_() { this->send_command_(CMD_QUERY_ZONE, nullptr, 0); }
|
void LD2450Component::query_zone_() { this->send_command_(CMD_QUERY_ZONE, nullptr, 0); }
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
// These could leak memory, but they are only set once prior to 'setup()' and should never be used again.
|
|
||||||
void LD2450Component::set_move_x_sensor(uint8_t target, sensor::Sensor *s) {
|
void LD2450Component::set_move_x_sensor(uint8_t target, sensor::Sensor *s) {
|
||||||
this->move_x_sensors_[target] = new SensorWithDedup<int16_t>(s);
|
this->move_x_sensors_[target].set_sensor(s);
|
||||||
}
|
}
|
||||||
void LD2450Component::set_move_y_sensor(uint8_t target, sensor::Sensor *s) {
|
void LD2450Component::set_move_y_sensor(uint8_t target, sensor::Sensor *s) {
|
||||||
this->move_y_sensors_[target] = new SensorWithDedup<int16_t>(s);
|
this->move_y_sensors_[target].set_sensor(s);
|
||||||
}
|
}
|
||||||
void LD2450Component::set_move_speed_sensor(uint8_t target, sensor::Sensor *s) {
|
void LD2450Component::set_move_speed_sensor(uint8_t target, sensor::Sensor *s) {
|
||||||
this->move_speed_sensors_[target] = new SensorWithDedup<int16_t>(s);
|
this->move_speed_sensors_[target].set_sensor(s);
|
||||||
}
|
}
|
||||||
void LD2450Component::set_move_angle_sensor(uint8_t target, sensor::Sensor *s) {
|
void LD2450Component::set_move_angle_sensor(uint8_t target, sensor::Sensor *s) {
|
||||||
this->move_angle_sensors_[target] = new SensorWithDedup<float>(s);
|
this->move_angle_sensors_[target].set_sensor(s);
|
||||||
}
|
}
|
||||||
void LD2450Component::set_move_distance_sensor(uint8_t target, sensor::Sensor *s) {
|
void LD2450Component::set_move_distance_sensor(uint8_t target, sensor::Sensor *s) {
|
||||||
this->move_distance_sensors_[target] = new SensorWithDedup<uint16_t>(s);
|
this->move_distance_sensors_[target].set_sensor(s);
|
||||||
}
|
}
|
||||||
void LD2450Component::set_move_resolution_sensor(uint8_t target, sensor::Sensor *s) {
|
void LD2450Component::set_move_resolution_sensor(uint8_t target, sensor::Sensor *s) {
|
||||||
this->move_resolution_sensors_[target] = new SensorWithDedup<uint16_t>(s);
|
this->move_resolution_sensors_[target].set_sensor(s);
|
||||||
}
|
}
|
||||||
void LD2450Component::set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
|
void LD2450Component::set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
|
||||||
this->zone_target_count_sensors_[zone] = new SensorWithDedup<uint8_t>(s);
|
this->zone_target_count_sensors_[zone].set_sensor(s);
|
||||||
}
|
}
|
||||||
void LD2450Component::set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
|
void LD2450Component::set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
|
||||||
this->zone_still_target_count_sensors_[zone] = new SensorWithDedup<uint8_t>(s);
|
this->zone_still_target_count_sensors_[zone].set_sensor(s);
|
||||||
}
|
}
|
||||||
void LD2450Component::set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
|
void LD2450Component::set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s) {
|
||||||
this->zone_moving_target_count_sensors_[zone] = new SensorWithDedup<uint8_t>(s);
|
this->zone_moving_target_count_sensors_[zone].set_sensor(s);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
|
|||||||
@@ -182,15 +182,15 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
|||||||
ZoneOfNumbers zone_numbers_[MAX_ZONES];
|
ZoneOfNumbers zone_numbers_[MAX_ZONES];
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
std::array<SensorWithDedup<int16_t> *, MAX_TARGETS> move_x_sensors_{};
|
std::array<SensorWithDedup<int16_t>, MAX_TARGETS> move_x_sensors_{};
|
||||||
std::array<SensorWithDedup<int16_t> *, MAX_TARGETS> move_y_sensors_{};
|
std::array<SensorWithDedup<int16_t>, MAX_TARGETS> move_y_sensors_{};
|
||||||
std::array<SensorWithDedup<int16_t> *, MAX_TARGETS> move_speed_sensors_{};
|
std::array<SensorWithDedup<int16_t>, MAX_TARGETS> move_speed_sensors_{};
|
||||||
std::array<SensorWithDedup<float> *, MAX_TARGETS> move_angle_sensors_{};
|
std::array<SensorWithDedup<float>, MAX_TARGETS> move_angle_sensors_{};
|
||||||
std::array<SensorWithDedup<uint16_t> *, MAX_TARGETS> move_distance_sensors_{};
|
std::array<SensorWithDedup<uint16_t>, MAX_TARGETS> move_distance_sensors_{};
|
||||||
std::array<SensorWithDedup<uint16_t> *, MAX_TARGETS> move_resolution_sensors_{};
|
std::array<SensorWithDedup<uint16_t>, MAX_TARGETS> move_resolution_sensors_{};
|
||||||
std::array<SensorWithDedup<uint8_t> *, MAX_ZONES> zone_target_count_sensors_{};
|
std::array<SensorWithDedup<uint8_t>, MAX_ZONES> zone_target_count_sensors_{};
|
||||||
std::array<SensorWithDedup<uint8_t> *, MAX_ZONES> zone_still_target_count_sensors_{};
|
std::array<SensorWithDedup<uint8_t>, MAX_ZONES> zone_still_target_count_sensors_{};
|
||||||
std::array<SensorWithDedup<uint8_t> *, MAX_ZONES> zone_moving_target_count_sensors_{};
|
std::array<SensorWithDedup<uint8_t>, MAX_ZONES> zone_moving_target_count_sensors_{};
|
||||||
#endif
|
#endif
|
||||||
#ifdef USE_TEXT_SENSOR
|
#ifdef USE_TEXT_SENSOR
|
||||||
std::array<text_sensor::TextSensor *, MAX_TARGETS> direction_text_sensors_{};
|
std::array<text_sensor::TextSensor *, MAX_TARGETS> direction_text_sensors_{};
|
||||||
|
|||||||
@@ -11,28 +11,20 @@
|
|||||||
|
|
||||||
#define SUB_SENSOR_WITH_DEDUP(name, dedup_type) \
|
#define SUB_SENSOR_WITH_DEDUP(name, dedup_type) \
|
||||||
protected: \
|
protected: \
|
||||||
ld24xx::SensorWithDedup<dedup_type> *name##_sensor_{nullptr}; \
|
ld24xx::SensorWithDedup<dedup_type> name##_sensor_{}; \
|
||||||
\
|
\
|
||||||
public: \
|
public: \
|
||||||
void set_##name##_sensor(sensor::Sensor *sensor) { \
|
void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_.set_sensor(sensor); }
|
||||||
this->name##_sensor_ = new ld24xx::SensorWithDedup<dedup_type>(sensor); \
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define LOG_SENSOR_WITH_DEDUP_SAFE(tag, name, sensor) \
|
#define LOG_SENSOR_WITH_DEDUP_SAFE(tag, name, sensor) \
|
||||||
if ((sensor) != nullptr) { \
|
if ((sensor).has_sensor()) { \
|
||||||
LOG_SENSOR(tag, name, (sensor)->sens); \
|
LOG_SENSOR(tag, name, (sensor).get_sensor()); \
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SAFE_PUBLISH_SENSOR(sensor, value) \
|
#define SAFE_PUBLISH_SENSOR(sensor, value) (sensor).publish_state_if_not_dup(value)
|
||||||
if ((sensor) != nullptr) { \
|
|
||||||
(sensor)->publish_state_if_not_dup(value); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define SAFE_PUBLISH_SENSOR_UNKNOWN(sensor) \
|
#define SAFE_PUBLISH_SENSOR_UNKNOWN(sensor) (sensor).publish_state_unknown()
|
||||||
if ((sensor) != nullptr) { \
|
|
||||||
(sensor)->publish_state_unknown(); \
|
|
||||||
}
|
|
||||||
|
|
||||||
#define highbyte(val) (uint8_t)((val) >> 8)
|
#define highbyte(val) (uint8_t)((val) >> 8)
|
||||||
#define lowbyte(val) (uint8_t)((val) &0xff)
|
#define lowbyte(val) (uint8_t)((val) &0xff)
|
||||||
@@ -70,25 +62,33 @@ inline void format_version_str(const uint8_t *version, std::span<char, 20> buffe
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_SENSOR
|
#ifdef USE_SENSOR
|
||||||
// Helper class to store a sensor with a deduplicator & publish state only when the value changes
|
/// Sensor with deduplication — sensor may be null, null check is internal.
|
||||||
|
/// Stored inline, no heap allocation. Does nothing when no sensor is set.
|
||||||
template<typename T> class SensorWithDedup {
|
template<typename T> class SensorWithDedup {
|
||||||
public:
|
public:
|
||||||
SensorWithDedup(sensor::Sensor *sens) : sens(sens) {}
|
void set_sensor(sensor::Sensor *sens) {
|
||||||
|
this->sens_ = sens;
|
||||||
|
this->dedup_ = {};
|
||||||
|
}
|
||||||
|
|
||||||
void publish_state_if_not_dup(T state) {
|
void publish_state_if_not_dup(T state) {
|
||||||
if (this->publish_dedup.next(state)) {
|
if (this->sens_ != nullptr && this->dedup_.next(state)) {
|
||||||
this->sens->publish_state(static_cast<float>(state));
|
this->sens_->publish_state(static_cast<float>(state));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void publish_state_unknown() {
|
void publish_state_unknown() {
|
||||||
if (this->publish_dedup.next_unknown()) {
|
if (this->sens_ != nullptr && this->dedup_.next_unknown()) {
|
||||||
this->sens->publish_state(NAN);
|
this->sens_->publish_state(NAN);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sensor::Sensor *sens;
|
bool has_sensor() const { return this->sens_ != nullptr; }
|
||||||
Deduplicator<T> publish_dedup;
|
sensor::Sensor *get_sensor() const { return this->sens_; }
|
||||||
|
|
||||||
|
protected:
|
||||||
|
sensor::Sensor *sens_{nullptr};
|
||||||
|
Deduplicator<T> dedup_;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
} // namespace esphome::ld24xx
|
} // namespace esphome::ld24xx
|
||||||
|
|||||||
Reference in New Issue
Block a user