Fix the daylight unit issue, fix the log back display issue of startup banble.

This commit is contained in:
sen 2023-05-01 18:21:15 +08:00
parent 6d9ab93c3f
commit 7b19149f61
9 changed files with 34 additions and 48 deletions

View File

@ -22,8 +22,7 @@ void LD2410Component::dump_config() {
LOG_SENSOR(" ", "Still Energy", this->still_target_energy_sensor_); LOG_SENSOR(" ", "Still Energy", this->still_target_energy_sensor_);
LOG_SENSOR(" ", "Detection Distance", this->detection_distance_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 #endif
this->set_config_mode_(true); this->set_config_mode_(true);
this->get_version_(); this->get_version_();
@ -36,7 +35,7 @@ void LD2410Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up LD2410..."); ESP_LOGCONFIG(TAG, "Setting up LD2410...");
// ESP_LOGCONFIG(TAG, "Apply screek patch..."); // ESP_LOGCONFIG(TAG, "Apply screek patch...");
this->set_config_mode_(true); 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_); this->set_max_distances_timeout_(this->max_move_distance_, this->max_still_distance_, this->timeout_);
// Configure Gates sensitivity // Configure Gates sensitivity
@ -53,8 +52,6 @@ void LD2410Component::setup() {
this->get_version_(); this->get_version_();
this->set_config_mode_(false); this->set_config_mode_(false);
// 开启工程模式!(23年3月13日_18时03分_)
// 因为会自动开启,这里就不太合适继续需要了。
// this->factory_mode(true); // this->factory_mode(true);
ESP_LOGCONFIG(TAG, "Firmware Version : %u.%u.%u%u%u%u", this->version_[0], this->version_[1], this->version_[2], 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) { void LD2410Component::send_command_(uint8_t command, uint8_t *command_value, int command_value_len) {
// lastCommandSuccess->publish_state(false); // lastCommandSuccess->publish_state(false);
@ -100,7 +96,6 @@ void LD2410Component::send_command_(uint8_t command, uint8_t *command_value, int
delay(50); // NOLINT 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) { 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; 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) { if (this->light_sensor_ != nullptr) {
int data_type = buffer[6]; int data_type = buffer[6];
int new_light = -1; int new_light = -1;
if (data_type == 0x01){ // 0x01 = factory mode if (data_type == 0x01){ // 0x01 = 工程模式!
new_light = buffer[37]; new_light = buffer[37];
new_light = map(new_light, 85, 255, 0, 100); 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){ if (new_light > 100){
new_light = 100; new_light = 100;
} }
ESP_LOGD(TAG,"LD2410 Sun Light: %d%%", new_light);
}else{ }else{
int32_t now_millis = millis(); int32_t now_millis = millis();
if (now_millis - last_change_fatory_mode_millis > 2000){ if (now_millis - last_change_fatory_mode_millis > 2000){
ESP_LOGD(TAG,"Normal mode no light, change to factory mode"); ESP_LOGD(TAG,"Normal mode no light, change to factory mode");
this->factory_mode(true); this->factory_mode(true);
last_change_fatory_mode_millis = now_millis; 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); this->send_command_(CMD_GATE_SENS, value, 18);
} }
// 额外增加的命令到这里去(23年3月13日_14时57分_)
void LD2410Component::factoryReset() void LD2410Component::factoryReset()
{ {
this->set_config_mode_(true); this->set_config_mode_(true);
@ -387,9 +385,9 @@ void LD2410Component::ble_control(bool enable)
if (enable){ if (enable){
cmd_value[0] = 0x01; cmd_value[0] = 0x01;
ESP_LOGD(TAG, "Disable BLE...");
}else{
ESP_LOGD(TAG, "Enable BLE..."); ESP_LOGD(TAG, "Enable BLE...");
}else{
ESP_LOGD(TAG, "Disable BLE...");
} }
this -> send_command_(CMD_BLE_CONF, cmd_value, 2); this -> send_command_(CMD_BLE_CONF, cmd_value, 2);

View File

@ -112,25 +112,14 @@ class LD2410Component : public Component, public uart::UARTDevice {
int32_t last_periodic_millis = millis(); int32_t last_periodic_millis = millis();
/// @brief 上次改变工程模式的时间,用来防止改变频率太高了。(23年3月13日_18时07分_)
int32_t last_change_fatory_mode_millis = 0; 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(); void factoryReset();
// 重启(23年3月13日_14时48分_)
void reboot(); void reboot();
/// @brief 蓝牙开关
/// (23年3月13日_15时22分_)
void ble_control(bool enable); void ble_control(bool enable);
/// @brief 开关工程模式
/// @param enable 开关
/// 用来快速切换工程模式用的。(23年3月13日_17时59分_)
void factory_mode(bool enable); void factory_mode(bool enable);
protected: protected:

View File

@ -6,9 +6,11 @@ from esphome.const import (
DEVICE_CLASS_ENERGY, DEVICE_CLASS_ENERGY,
UNIT_CENTIMETER, UNIT_CENTIMETER,
UNIT_PERCENT, UNIT_PERCENT,
DEVICE_CLASS_ILLUMINANCE, STATE_CLASS_NONE,
ICON_BRIGHTNESS_5,
UNIT_EMPTY, UNIT_EMPTY,
UNIT_LUX, UNIT_LUX,
) )
from . import CONF_LD2410_ID, LD2410Component from . import CONF_LD2410_ID, LD2410Component
@ -37,8 +39,11 @@ CONFIG_SCHEMA = {
cv.Optional(CONF_DETECTION_DISTANCE): sensor.sensor_schema( cv.Optional(CONF_DETECTION_DISTANCE): sensor.sensor_schema(
device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_CENTIMETER device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_CENTIMETER
), ),
cv.Optional(CONF_LIGHT): sensor.sensor_schema( 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: if CONF_DETECTION_DISTANCE in config:
sens = await sensor.new_sensor(config[CONF_DETECTION_DISTANCE]) sens = await sensor.new_sensor(config[CONF_DETECTION_DISTANCE])
cg.add(ld2410_component.set_detection_distance_sensor(sens)) cg.add(ld2410_component.set_detection_distance_sensor(sens))
if CONF_LIGHT in config: if CONF_LIGHT in config:
sens = await sensor.new_sensor(config[CONF_LIGHT]) sens = await sensor.new_sensor(config[CONF_LIGHT])
cg.add(ld2410_component.set_light_sensor(sens)) cg.add(ld2410_component.set_light_sensor(sens))

View File

@ -22,8 +22,7 @@ void LD2410Component::dump_config() {
LOG_SENSOR(" ", "Still Energy", this->still_target_energy_sensor_); LOG_SENSOR(" ", "Still Energy", this->still_target_energy_sensor_);
LOG_SENSOR(" ", "Detection Distance", this->detection_distance_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 #endif
this->set_config_mode_(true); this->set_config_mode_(true);
this->get_version_(); this->get_version_();
@ -36,7 +35,7 @@ void LD2410Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up LD2410..."); ESP_LOGCONFIG(TAG, "Setting up LD2410...");
// ESP_LOGCONFIG(TAG, "Apply screek patch..."); // ESP_LOGCONFIG(TAG, "Apply screek patch...");
this->set_config_mode_(true); 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_); this->set_max_distances_timeout_(this->max_move_distance_, this->max_still_distance_, this->timeout_);
// Configure Gates sensitivity // Configure Gates sensitivity
@ -53,8 +52,6 @@ void LD2410Component::setup() {
this->get_version_(); this->get_version_();
this->set_config_mode_(false); this->set_config_mode_(false);
// 开启工程模式!(23年3月13日_18时03分_)
// 因为会自动开启,这里就不太合适继续需要了。
// this->factory_mode(true); // this->factory_mode(true);
ESP_LOGCONFIG(TAG, "Firmware Version : %u.%u.%u%u%u%u", this->version_[0], this->version_[1], this->version_[2], 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) { void LD2410Component::send_command_(uint8_t command, uint8_t *command_value, int command_value_len) {
// lastCommandSuccess->publish_state(false); // lastCommandSuccess->publish_state(false);
@ -100,7 +96,6 @@ void LD2410Component::send_command_(uint8_t command, uint8_t *command_value, int
delay(50); // NOLINT 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) { 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; 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) { if (this->light_sensor_ != nullptr) {
int data_type = buffer[6]; int data_type = buffer[6];
int new_light = -1; int new_light = -1;
if (data_type == 0x01){ // 0x01 = factory mode if (data_type == 0x01){ // 0x01 = 工程模式!
new_light = buffer[37]; new_light = buffer[37];
new_light = map(new_light, 85, 255, 0, 100); 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){ if (new_light > 100){
new_light = 100; new_light = 100;
} }
ESP_LOGD(TAG,"LD2410 Sun Light: %d%%", new_light);
}else{ }else{
int32_t now_millis = millis(); int32_t now_millis = millis();
if (now_millis - last_change_fatory_mode_millis > 2000){ if (now_millis - last_change_fatory_mode_millis > 2000){
ESP_LOGD(TAG,"Normal mode no light, change to factory mode"); ESP_LOGD(TAG,"Normal mode no light, change to factory mode");
this->factory_mode(true); this->factory_mode(true);
last_change_fatory_mode_millis = now_millis; 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); this->send_command_(CMD_GATE_SENS, value, 18);
} }
// 额外增加的命令到这里去(23年3月13日_14时57分_)
void LD2410Component::factoryReset() void LD2410Component::factoryReset()
{ {
this->set_config_mode_(true); this->set_config_mode_(true);
@ -387,9 +385,9 @@ void LD2410Component::ble_control(bool enable)
if (enable){ if (enable){
cmd_value[0] = 0x01; cmd_value[0] = 0x01;
ESP_LOGD(TAG, "Disable BLE...");
}else{
ESP_LOGD(TAG, "Enable BLE..."); ESP_LOGD(TAG, "Enable BLE...");
}else{
ESP_LOGD(TAG, "Disable BLE...");
} }
this -> send_command_(CMD_BLE_CONF, cmd_value, 2); this -> send_command_(CMD_BLE_CONF, cmd_value, 2);

View File

@ -112,25 +112,14 @@ class LD2410Component : public Component, public uart::UARTDevice {
int32_t last_periodic_millis = millis(); int32_t last_periodic_millis = millis();
/// @brief 上次改变工程模式的时间,用来防止改变频率太高了。(23年3月13日_18时07分_)
int32_t last_change_fatory_mode_millis = 0; 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(); void factoryReset();
// 重启(23年3月13日_14时48分_)
void reboot(); void reboot();
/// @brief 蓝牙开关
/// (23年3月13日_15时22分_)
void ble_control(bool enable); void ble_control(bool enable);
/// @brief 开关工程模式
/// @param enable 开关
/// 用来快速切换工程模式用的。(23年3月13日_17时59分_)
void factory_mode(bool enable); void factory_mode(bool enable);
protected: protected:

View File

@ -6,9 +6,11 @@ from esphome.const import (
DEVICE_CLASS_ENERGY, DEVICE_CLASS_ENERGY,
UNIT_CENTIMETER, UNIT_CENTIMETER,
UNIT_PERCENT, UNIT_PERCENT,
DEVICE_CLASS_ILLUMINANCE, STATE_CLASS_NONE,
ICON_BRIGHTNESS_5,
UNIT_EMPTY, UNIT_EMPTY,
UNIT_LUX, UNIT_LUX,
) )
from . import CONF_LD2410_ID, LD2410Component from . import CONF_LD2410_ID, LD2410Component
@ -37,8 +39,11 @@ CONFIG_SCHEMA = {
cv.Optional(CONF_DETECTION_DISTANCE): sensor.sensor_schema( cv.Optional(CONF_DETECTION_DISTANCE): sensor.sensor_schema(
device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_CENTIMETER device_class=DEVICE_CLASS_DISTANCE, unit_of_measurement=UNIT_CENTIMETER
), ),
cv.Optional(CONF_LIGHT): sensor.sensor_schema( 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: if CONF_DETECTION_DISTANCE in config:
sens = await sensor.new_sensor(config[CONF_DETECTION_DISTANCE]) sens = await sensor.new_sensor(config[CONF_DETECTION_DISTANCE])
cg.add(ld2410_component.set_detection_distance_sensor(sens)) cg.add(ld2410_component.set_detection_distance_sensor(sens))
if CONF_LIGHT in config: if CONF_LIGHT in config:
sens = await sensor.new_sensor(config[CONF_LIGHT]) sens = await sensor.new_sensor(config[CONF_LIGHT])
cg.add(ld2410_component.set_light_sensor(sens)) cg.add(ld2410_component.set_light_sensor(sens))