[ld2410] Reduce RAM usage, general clean-up (#9346)

This commit is contained in:
Keith Burzinski
2025-07-06 09:16:30 -05:00
committed by GitHub
parent e061b6dc55
commit e5a699a004
6 changed files with 357 additions and 306 deletions
+3 -3
View File
@@ -14,8 +14,8 @@ from esphome.const import (
from .. import CONF_LD2410_ID, LD2410Component, ld2410_ns
FactoryResetButton = ld2410_ns.class_("FactoryResetButton", button.Button)
QueryButton = ld2410_ns.class_("QueryButton", button.Button)
ResetButton = ld2410_ns.class_("ResetButton", button.Button)
RestartButton = ld2410_ns.class_("RestartButton", button.Button)
CONF_QUERY_PARAMS = "query_params"
@@ -23,7 +23,7 @@ CONF_QUERY_PARAMS = "query_params"
CONFIG_SCHEMA = {
cv.GenerateID(CONF_LD2410_ID): cv.use_id(LD2410Component),
cv.Optional(CONF_FACTORY_RESET): button.button_schema(
ResetButton,
FactoryResetButton,
device_class=DEVICE_CLASS_RESTART,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_RESTART_ALERT,
@@ -47,7 +47,7 @@ async def to_code(config):
if factory_reset_config := config.get(CONF_FACTORY_RESET):
b = await button.new_button(factory_reset_config)
await cg.register_parented(b, config[CONF_LD2410_ID])
cg.add(ld2410_component.set_reset_button(b))
cg.add(ld2410_component.set_factory_reset_button(b))
if restart_config := config.get(CONF_RESTART):
b = await button.new_button(restart_config)
await cg.register_parented(b, config[CONF_LD2410_ID])
@@ -0,0 +1,9 @@
#include "factory_reset_button.h"
namespace esphome {
namespace ld2410 {
void FactoryResetButton::press_action() { this->parent_->factory_reset(); }
} // namespace ld2410
} // namespace esphome
@@ -6,9 +6,9 @@
namespace esphome {
namespace ld2410 {
class ResetButton : public button::Button, public Parented<LD2410Component> {
class FactoryResetButton : public button::Button, public Parented<LD2410Component> {
public:
ResetButton() = default;
FactoryResetButton() = default;
protected:
void press_action() override;
@@ -1,9 +0,0 @@
#include "reset_button.h"
namespace esphome {
namespace ld2410 {
void ResetButton::press_action() { this->parent_->factory_reset(); }
} // namespace ld2410
} // namespace esphome
File diff suppressed because it is too large Load Diff
+46 -41
View File
@@ -29,45 +29,48 @@
namespace esphome {
namespace ld2410 {
static const uint8_t MAX_LINE_LENGTH = 46; // Max characters for serial buffer
static const uint8_t TOTAL_GATES = 9; // Total number of gates supported by the LD2410
class LD2410Component : public Component, public uart::UARTDevice {
#ifdef USE_SENSOR
SUB_SENSOR(moving_target_distance)
SUB_SENSOR(still_target_distance)
SUB_SENSOR(moving_target_energy)
SUB_SENSOR(still_target_energy)
SUB_SENSOR(light)
SUB_SENSOR(detection_distance)
#endif
#ifdef USE_BINARY_SENSOR
SUB_BINARY_SENSOR(target)
SUB_BINARY_SENSOR(out_pin_presence_status)
SUB_BINARY_SENSOR(moving_target)
SUB_BINARY_SENSOR(still_target)
SUB_BINARY_SENSOR(out_pin_presence_status)
SUB_BINARY_SENSOR(target)
#endif
#ifdef USE_SENSOR
SUB_SENSOR(light)
SUB_SENSOR(detection_distance)
SUB_SENSOR(moving_target_distance)
SUB_SENSOR(moving_target_energy)
SUB_SENSOR(still_target_distance)
SUB_SENSOR(still_target_energy)
#endif
#ifdef USE_TEXT_SENSOR
SUB_TEXT_SENSOR(version)
SUB_TEXT_SENSOR(mac)
#endif
#ifdef USE_NUMBER
SUB_NUMBER(light_threshold)
SUB_NUMBER(max_move_distance_gate)
SUB_NUMBER(max_still_distance_gate)
SUB_NUMBER(timeout)
#endif
#ifdef USE_SELECT
SUB_SELECT(distance_resolution)
SUB_SELECT(baud_rate)
SUB_SELECT(distance_resolution)
SUB_SELECT(light_function)
SUB_SELECT(out_pin_level)
#endif
#ifdef USE_SWITCH
SUB_SWITCH(engineering_mode)
SUB_SWITCH(bluetooth)
SUB_SWITCH(engineering_mode)
#endif
#ifdef USE_BUTTON
SUB_BUTTON(reset)
SUB_BUTTON(restart)
SUB_BUTTON(factory_reset)
SUB_BUTTON(query)
#endif
#ifdef USE_NUMBER
SUB_NUMBER(max_still_distance_gate)
SUB_NUMBER(max_move_distance_gate)
SUB_NUMBER(timeout)
SUB_NUMBER(light_threshold)
SUB_BUTTON(restart)
#endif
public:
@@ -76,14 +79,14 @@ class LD2410Component : public Component, public uart::UARTDevice {
void loop() override;
void set_light_out_control();
#ifdef USE_NUMBER
void set_gate_still_threshold_number(int gate, number::Number *n);
void set_gate_move_threshold_number(int gate, number::Number *n);
void set_gate_still_threshold_number(uint8_t gate, number::Number *n);
void set_gate_move_threshold_number(uint8_t gate, number::Number *n);
void set_max_distances_timeout();
void set_gate_threshold(uint8_t gate);
#endif
#ifdef USE_SENSOR
void set_gate_move_sensor(int gate, sensor::Sensor *s);
void set_gate_still_sensor(int gate, sensor::Sensor *s);
void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s);
void set_gate_still_sensor(uint8_t gate, sensor::Sensor *s);
#endif
void set_throttle(uint16_t value) { this->throttle_ = value; };
void set_bluetooth_password(const std::string &password);
@@ -96,33 +99,35 @@ class LD2410Component : public Component, public uart::UARTDevice {
void factory_reset();
protected:
void send_command_(uint8_t command_str, const uint8_t *command_value, int command_value_len);
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
void set_config_mode_(bool enable);
void handle_periodic_data_(uint8_t *buffer, int len);
bool handle_ack_data_(uint8_t *buffer, int len);
void readline_(int readch, uint8_t *buffer, int len);
void handle_periodic_data_();
bool handle_ack_data_();
void readline_(int readch);
void query_parameters_();
void get_version_();
void get_mac_();
void get_distance_resolution_();
void get_light_control_();
void query_light_control_();
void restart_();
int32_t last_periodic_millis_ = 0;
int32_t last_engineering_mode_change_millis_ = 0;
uint16_t throttle_;
float light_threshold_ = -1;
std::string version_;
std::string mac_;
std::string out_pin_level_;
std::string light_function_;
uint32_t last_periodic_millis_ = 0;
uint16_t throttle_ = 0;
uint8_t light_function_ = 0;
uint8_t light_threshold_ = 0;
uint8_t out_pin_level_ = 0;
uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
uint8_t buffer_data_[MAX_LINE_LENGTH];
uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0};
uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
bool bluetooth_on_{false};
#ifdef USE_NUMBER
std::vector<number::Number *> gate_still_threshold_numbers_ = std::vector<number::Number *>(9);
std::vector<number::Number *> gate_move_threshold_numbers_ = std::vector<number::Number *>(9);
std::vector<number::Number *> gate_move_threshold_numbers_ = std::vector<number::Number *>(TOTAL_GATES);
std::vector<number::Number *> gate_still_threshold_numbers_ = std::vector<number::Number *>(TOTAL_GATES);
#endif
#ifdef USE_SENSOR
std::vector<sensor::Sensor *> gate_still_sensors_ = std::vector<sensor::Sensor *>(9);
std::vector<sensor::Sensor *> gate_move_sensors_ = std::vector<sensor::Sensor *>(9);
std::vector<sensor::Sensor *> gate_move_sensors_ = std::vector<sensor::Sensor *>(TOTAL_GATES);
std::vector<sensor::Sensor *> gate_still_sensors_ = std::vector<sensor::Sensor *>(TOTAL_GATES);
#endif
};