From 7b19149f615178d2dce09351e8f02bb7b3491859 Mon Sep 17 00:00:00 2001 From: sen <126383355+screek-workshop@users.noreply.github.com> Date: Mon, 1 May 2023 18:21:15 +0800 Subject: [PATCH] Fix the daylight unit issue, fix the log back display issue of startup banble. --- .../screek-mod-components/ld2410/ld2410.cpp | 20 +++++++++---------- 1u/yaml/screek-mod-components/ld2410/ld2410.h | 11 ---------- .../screek-mod-components/ld2410/sensor.py | 10 ++++++++-- .../screek-humen-dectet-1w.yaml | 0 .../screek-mod-components/ld2410/__init__.py | 0 .../ld2410/binary_sensor.py | 0 .../screek-mod-components/ld2410/ld2410.cpp | 20 +++++++++---------- .../screek-mod-components/ld2410/ld2410.h | 11 ---------- .../screek-mod-components/ld2410/sensor.py | 10 ++++++++-- 9 files changed, 34 insertions(+), 48 deletions(-) rename 1w/yaml/{sun-light-reader => }/screek-humen-dectet-1w.yaml (100%) rename 1w/yaml/{sun-light-reader => }/screek-mod-components/ld2410/__init__.py (100%) rename 1w/yaml/{sun-light-reader => }/screek-mod-components/ld2410/binary_sensor.py (100%) rename 1w/yaml/{sun-light-reader => }/screek-mod-components/ld2410/ld2410.cpp (96%) rename 1w/yaml/{sun-light-reader => }/screek-mod-components/ld2410/ld2410.h (91%) rename 1w/yaml/{sun-light-reader => }/screek-mod-components/ld2410/sensor.py (94%) diff --git a/1u/yaml/screek-mod-components/ld2410/ld2410.cpp b/1u/yaml/screek-mod-components/ld2410/ld2410.cpp index 30c71f0..5dfd4bc 100644 --- a/1u/yaml/screek-mod-components/ld2410/ld2410.cpp +++ b/1u/yaml/screek-mod-components/ld2410/ld2410.cpp @@ -22,8 +22,7 @@ void LD2410Component::dump_config() { LOG_SENSOR(" ", "Still Energy", this->still_target_energy_sensor_); LOG_SENSOR(" ", "Detection Distance", this->detection_distance_sensor_); - // 回显光线模块设置(23年3月13日_17时28分_) - LOG_SENSOR(" ", "Light", this->light_sensor_); + LOG_SENSOR(" ", "Light", this->light_sensor_); #endif this->set_config_mode_(true); this->get_version_(); @@ -36,7 +35,7 @@ void LD2410Component::setup() { ESP_LOGCONFIG(TAG, "Setting up LD2410..."); // ESP_LOGCONFIG(TAG, "Apply screek patch..."); this->set_config_mode_(true); - // 使用蓝牙配置,取消这里的参数定义选项。(23年3月10日_20时50分_) + /* this->set_max_distances_timeout_(this->max_move_distance_, this->max_still_distance_, this->timeout_); // Configure Gates sensitivity @@ -53,8 +52,6 @@ void LD2410Component::setup() { this->get_version_(); this->set_config_mode_(false); - // 开启工程模式!(23年3月13日_18时03分_) - // 因为会自动开启,这里就不太合适继续需要了。 // this->factory_mode(true); ESP_LOGCONFIG(TAG, "Firmware Version : %u.%u.%u%u%u%u", this->version_[0], this->version_[1], this->version_[2], @@ -71,7 +68,6 @@ void LD2410Component::loop() { } } -// 发送命令给雷达!(23年3月13日_14时46分_) void LD2410Component::send_command_(uint8_t command, uint8_t *command_value, int command_value_len) { // lastCommandSuccess->publish_state(false); @@ -100,7 +96,6 @@ void LD2410Component::send_command_(uint8_t command, uint8_t *command_value, int delay(50); // NOLINT } -// https://cdn.arduino.cc/reference/en/language/functions/math/map/ long map(long x, long in_min, long in_max, long out_min, long out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } @@ -186,7 +181,7 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { if (this->light_sensor_ != nullptr) { int data_type = buffer[6]; int new_light = -1; - if (data_type == 0x01){ // 0x01 = factory mode + if (data_type == 0x01){ // 0x01 = 工程模式! new_light = buffer[37]; new_light = map(new_light, 85, 255, 0, 100); @@ -196,10 +191,14 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { if (new_light > 100){ new_light = 100; } + + ESP_LOGD(TAG,"LD2410 Sun Light: %d%%", new_light); }else{ int32_t now_millis = millis(); + if (now_millis - last_change_fatory_mode_millis > 2000){ ESP_LOGD(TAG,"Normal mode no light, change to factory mode"); + this->factory_mode(true); last_change_fatory_mode_millis = now_millis; } @@ -357,7 +356,6 @@ void LD2410Component::set_gate_threshold_(uint8_t gate, uint8_t motionsens, uint this->send_command_(CMD_GATE_SENS, value, 18); } -// 额外增加的命令到这里去(23年3月13日_14时57分_) void LD2410Component::factoryReset() { this->set_config_mode_(true); @@ -387,9 +385,9 @@ void LD2410Component::ble_control(bool enable) if (enable){ cmd_value[0] = 0x01; - ESP_LOGD(TAG, "Disable BLE..."); - }else{ ESP_LOGD(TAG, "Enable BLE..."); + }else{ + ESP_LOGD(TAG, "Disable BLE..."); } this -> send_command_(CMD_BLE_CONF, cmd_value, 2); diff --git a/1u/yaml/screek-mod-components/ld2410/ld2410.h b/1u/yaml/screek-mod-components/ld2410/ld2410.h index 0336811..1d0f8e1 100644 --- a/1u/yaml/screek-mod-components/ld2410/ld2410.h +++ b/1u/yaml/screek-mod-components/ld2410/ld2410.h @@ -112,25 +112,14 @@ class LD2410Component : public Component, public uart::UARTDevice { int32_t last_periodic_millis = millis(); - /// @brief 上次改变工程模式的时间,用来防止改变频率太高了。(23年3月13日_18时07分_) int32_t last_change_fatory_mode_millis = 0; - // 增加一些额外的命令,比如重启,开关蓝牙。(23年3月13日_14时44分_) - // https://github.com/rain931215/ESPHome-LD2410/blob/main/ld2410_uart.h - - /// 恢复出厂设置!(23年3月13日_14时48分_) void factoryReset(); - // 重启(23年3月13日_14时48分_) void reboot(); - /// @brief 蓝牙开关 - /// (23年3月13日_15时22分_) void ble_control(bool enable); - /// @brief 开关工程模式 - /// @param enable 开关 - /// 用来快速切换工程模式用的。(23年3月13日_17时59分_) void factory_mode(bool enable); protected: diff --git a/1u/yaml/screek-mod-components/ld2410/sensor.py b/1u/yaml/screek-mod-components/ld2410/sensor.py index 1a118f5..c5dc934 100644 --- a/1u/yaml/screek-mod-components/ld2410/sensor.py +++ b/1u/yaml/screek-mod-components/ld2410/sensor.py @@ -6,9 +6,11 @@ from esphome.const import ( DEVICE_CLASS_ENERGY, UNIT_CENTIMETER, UNIT_PERCENT, - DEVICE_CLASS_ILLUMINANCE, + STATE_CLASS_NONE, + ICON_BRIGHTNESS_5, UNIT_EMPTY, UNIT_LUX, + ) from . import CONF_LD2410_ID, LD2410Component @@ -37,8 +39,11 @@ CONFIG_SCHEMA = { cv.Optional(CONF_DETECTION_DISTANCE): sensor.sensor_schema( device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_CENTIMETER ), + cv.Optional(CONF_LIGHT): sensor.sensor_schema( - device_class=DEVICE_CLASS_ILLUMINANCE, unit_of_measurement=UNIT_PERCENT + device_class=STATE_CLASS_NONE, + icon=ICON_BRIGHTNESS_5, + unit_of_measurement=UNIT_PERCENT ), } @@ -59,6 +64,7 @@ async def to_code(config): if CONF_DETECTION_DISTANCE in config: sens = await sensor.new_sensor(config[CONF_DETECTION_DISTANCE]) cg.add(ld2410_component.set_detection_distance_sensor(sens)) + if CONF_LIGHT in config: sens = await sensor.new_sensor(config[CONF_LIGHT]) cg.add(ld2410_component.set_light_sensor(sens)) \ No newline at end of file diff --git a/1w/yaml/sun-light-reader/screek-humen-dectet-1w.yaml b/1w/yaml/screek-humen-dectet-1w.yaml similarity index 100% rename from 1w/yaml/sun-light-reader/screek-humen-dectet-1w.yaml rename to 1w/yaml/screek-humen-dectet-1w.yaml diff --git a/1w/yaml/sun-light-reader/screek-mod-components/ld2410/__init__.py b/1w/yaml/screek-mod-components/ld2410/__init__.py similarity index 100% rename from 1w/yaml/sun-light-reader/screek-mod-components/ld2410/__init__.py rename to 1w/yaml/screek-mod-components/ld2410/__init__.py diff --git a/1w/yaml/sun-light-reader/screek-mod-components/ld2410/binary_sensor.py b/1w/yaml/screek-mod-components/ld2410/binary_sensor.py similarity index 100% rename from 1w/yaml/sun-light-reader/screek-mod-components/ld2410/binary_sensor.py rename to 1w/yaml/screek-mod-components/ld2410/binary_sensor.py diff --git a/1w/yaml/sun-light-reader/screek-mod-components/ld2410/ld2410.cpp b/1w/yaml/screek-mod-components/ld2410/ld2410.cpp similarity index 96% rename from 1w/yaml/sun-light-reader/screek-mod-components/ld2410/ld2410.cpp rename to 1w/yaml/screek-mod-components/ld2410/ld2410.cpp index 30c71f0..5dfd4bc 100644 --- a/1w/yaml/sun-light-reader/screek-mod-components/ld2410/ld2410.cpp +++ b/1w/yaml/screek-mod-components/ld2410/ld2410.cpp @@ -22,8 +22,7 @@ void LD2410Component::dump_config() { LOG_SENSOR(" ", "Still Energy", this->still_target_energy_sensor_); LOG_SENSOR(" ", "Detection Distance", this->detection_distance_sensor_); - // 回显光线模块设置(23年3月13日_17时28分_) - LOG_SENSOR(" ", "Light", this->light_sensor_); + LOG_SENSOR(" ", "Light", this->light_sensor_); #endif this->set_config_mode_(true); this->get_version_(); @@ -36,7 +35,7 @@ void LD2410Component::setup() { ESP_LOGCONFIG(TAG, "Setting up LD2410..."); // ESP_LOGCONFIG(TAG, "Apply screek patch..."); this->set_config_mode_(true); - // 使用蓝牙配置,取消这里的参数定义选项。(23年3月10日_20时50分_) + /* this->set_max_distances_timeout_(this->max_move_distance_, this->max_still_distance_, this->timeout_); // Configure Gates sensitivity @@ -53,8 +52,6 @@ void LD2410Component::setup() { this->get_version_(); this->set_config_mode_(false); - // 开启工程模式!(23年3月13日_18时03分_) - // 因为会自动开启,这里就不太合适继续需要了。 // this->factory_mode(true); ESP_LOGCONFIG(TAG, "Firmware Version : %u.%u.%u%u%u%u", this->version_[0], this->version_[1], this->version_[2], @@ -71,7 +68,6 @@ void LD2410Component::loop() { } } -// 发送命令给雷达!(23年3月13日_14时46分_) void LD2410Component::send_command_(uint8_t command, uint8_t *command_value, int command_value_len) { // lastCommandSuccess->publish_state(false); @@ -100,7 +96,6 @@ void LD2410Component::send_command_(uint8_t command, uint8_t *command_value, int delay(50); // NOLINT } -// https://cdn.arduino.cc/reference/en/language/functions/math/map/ long map(long x, long in_min, long in_max, long out_min, long out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } @@ -186,7 +181,7 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { if (this->light_sensor_ != nullptr) { int data_type = buffer[6]; int new_light = -1; - if (data_type == 0x01){ // 0x01 = factory mode + if (data_type == 0x01){ // 0x01 = 工程模式! new_light = buffer[37]; new_light = map(new_light, 85, 255, 0, 100); @@ -196,10 +191,14 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) { if (new_light > 100){ new_light = 100; } + + ESP_LOGD(TAG,"LD2410 Sun Light: %d%%", new_light); }else{ int32_t now_millis = millis(); + if (now_millis - last_change_fatory_mode_millis > 2000){ ESP_LOGD(TAG,"Normal mode no light, change to factory mode"); + this->factory_mode(true); last_change_fatory_mode_millis = now_millis; } @@ -357,7 +356,6 @@ void LD2410Component::set_gate_threshold_(uint8_t gate, uint8_t motionsens, uint this->send_command_(CMD_GATE_SENS, value, 18); } -// 额外增加的命令到这里去(23年3月13日_14时57分_) void LD2410Component::factoryReset() { this->set_config_mode_(true); @@ -387,9 +385,9 @@ void LD2410Component::ble_control(bool enable) if (enable){ cmd_value[0] = 0x01; - ESP_LOGD(TAG, "Disable BLE..."); - }else{ ESP_LOGD(TAG, "Enable BLE..."); + }else{ + ESP_LOGD(TAG, "Disable BLE..."); } this -> send_command_(CMD_BLE_CONF, cmd_value, 2); diff --git a/1w/yaml/sun-light-reader/screek-mod-components/ld2410/ld2410.h b/1w/yaml/screek-mod-components/ld2410/ld2410.h similarity index 91% rename from 1w/yaml/sun-light-reader/screek-mod-components/ld2410/ld2410.h rename to 1w/yaml/screek-mod-components/ld2410/ld2410.h index 0336811..1d0f8e1 100644 --- a/1w/yaml/sun-light-reader/screek-mod-components/ld2410/ld2410.h +++ b/1w/yaml/screek-mod-components/ld2410/ld2410.h @@ -112,25 +112,14 @@ class LD2410Component : public Component, public uart::UARTDevice { int32_t last_periodic_millis = millis(); - /// @brief 上次改变工程模式的时间,用来防止改变频率太高了。(23年3月13日_18时07分_) int32_t last_change_fatory_mode_millis = 0; - // 增加一些额外的命令,比如重启,开关蓝牙。(23年3月13日_14时44分_) - // https://github.com/rain931215/ESPHome-LD2410/blob/main/ld2410_uart.h - - /// 恢复出厂设置!(23年3月13日_14时48分_) void factoryReset(); - // 重启(23年3月13日_14时48分_) void reboot(); - /// @brief 蓝牙开关 - /// (23年3月13日_15时22分_) void ble_control(bool enable); - /// @brief 开关工程模式 - /// @param enable 开关 - /// 用来快速切换工程模式用的。(23年3月13日_17时59分_) void factory_mode(bool enable); protected: diff --git a/1w/yaml/sun-light-reader/screek-mod-components/ld2410/sensor.py b/1w/yaml/screek-mod-components/ld2410/sensor.py similarity index 94% rename from 1w/yaml/sun-light-reader/screek-mod-components/ld2410/sensor.py rename to 1w/yaml/screek-mod-components/ld2410/sensor.py index 1a118f5..c5dc934 100644 --- a/1w/yaml/sun-light-reader/screek-mod-components/ld2410/sensor.py +++ b/1w/yaml/screek-mod-components/ld2410/sensor.py @@ -6,9 +6,11 @@ from esphome.const import ( DEVICE_CLASS_ENERGY, UNIT_CENTIMETER, UNIT_PERCENT, - DEVICE_CLASS_ILLUMINANCE, + STATE_CLASS_NONE, + ICON_BRIGHTNESS_5, UNIT_EMPTY, UNIT_LUX, + ) from . import CONF_LD2410_ID, LD2410Component @@ -37,8 +39,11 @@ CONFIG_SCHEMA = { cv.Optional(CONF_DETECTION_DISTANCE): sensor.sensor_schema( device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_CENTIMETER ), + cv.Optional(CONF_LIGHT): sensor.sensor_schema( - device_class=DEVICE_CLASS_ILLUMINANCE, unit_of_measurement=UNIT_PERCENT + device_class=STATE_CLASS_NONE, + icon=ICON_BRIGHTNESS_5, + unit_of_measurement=UNIT_PERCENT ), } @@ -59,6 +64,7 @@ async def to_code(config): if CONF_DETECTION_DISTANCE in config: sens = await sensor.new_sensor(config[CONF_DETECTION_DISTANCE]) cg.add(ld2410_component.set_detection_distance_sensor(sens)) + if CONF_LIGHT in config: sens = await sensor.new_sensor(config[CONF_LIGHT]) cg.add(ld2410_component.set_light_sensor(sens)) \ No newline at end of file