This commit is contained in:
CptSkippy 2024-05-27 00:05:02 -07:00
parent 41f73a8086
commit 1ad85619e0
3 changed files with 153 additions and 190 deletions

View file

@ -10,11 +10,8 @@ static const uint8_t LD2415H_CMD_SET_SPEED_ANGLE_SENSE[] = {0x43, 0x46, 0x01,
static const uint8_t LD2415H_CMD_SET_MODE_RATE_UOM[] = {0x43, 0x46, 0x02, 0x01, 0x01, 0x00, 0x0d, 0x0a}; static const uint8_t LD2415H_CMD_SET_MODE_RATE_UOM[] = {0x43, 0x46, 0x02, 0x01, 0x01, 0x00, 0x0d, 0x0a};
static const uint8_t LD2415H_CMD_SET_ANTI_VIB_COMP[] = {0x43, 0x46, 0x03, 0x05, 0x00, 0x00, 0x0d, 0x0a}; static const uint8_t LD2415H_CMD_SET_ANTI_VIB_COMP[] = {0x43, 0x46, 0x03, 0x05, 0x00, 0x00, 0x0d, 0x0a};
static const uint8_t LD2415H_CMD_SET_RELAY_DURATION_SPEED[] = {0x43, 0x46, 0x04, 0x03, 0x01, 0x00, 0x0d, 0x0a}; static const uint8_t LD2415H_CMD_SET_RELAY_DURATION_SPEED[] = {0x43, 0x46, 0x04, 0x03, 0x01, 0x00, 0x0d, 0x0a};
static const uint8_t LD2415H_CMD_GET_CONFIG[] = {0x43, 0x46, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; static const uint8_t LD2415H_CMD_GET_CONFIG[] = {0x43, 0x46, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
/* TODO ::
* Create controls that expose settings
*/
LD2415HComponent::LD2415HComponent() LD2415HComponent::LD2415HComponent()
: cmd_speed_angle_sense_{0x43, 0x46, 0x01, 0x01, 0x00, 0x05, 0x0d, 0x0a}, : cmd_speed_angle_sense_{0x43, 0x46, 0x01, 0x01, 0x00, 0x05, 0x0d, 0x0a},
@ -23,11 +20,10 @@ LD2415HComponent::LD2415HComponent()
cmd_relay_duration_speed_{0x43, 0x46, 0x04, 0x03, 0x01, 0x00, 0x0d, 0x0a}, cmd_relay_duration_speed_{0x43, 0x46, 0x04, 0x03, 0x01, 0x00, 0x0d, 0x0a},
cmd_config_{0x43, 0x46, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} {} cmd_config_{0x43, 0x46, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} {}
void LD2415HComponent::setup() { void LD2415HComponent::setup() {
// This triggers current sensor configurations to be dumped // This triggers current sensor configurations to be dumped
//this->issue_command_(LD2415H_CMD_GET_CONFIG, sizeof(LD2415H_CMD_GET_CONFIG));
this->update_config_ = true; this->update_config_ = true;
#ifdef USE_NUMBER #ifdef USE_NUMBER
this->min_speed_threshold_number_->publish_state(this->min_speed_threshold_); this->min_speed_threshold_number_->publish_state(this->min_speed_threshold_);
this->compensation_angle_number_->publish_state(this->compensation_angle_); this->compensation_angle_number_->publish_state(this->compensation_angle_);
@ -55,9 +51,6 @@ void LD2415HComponent::dump_config() {
ESP_LOGCONFIG(TAG, " Relay Trigger Duration: %u", this->relay_trigger_duration_); ESP_LOGCONFIG(TAG, " Relay Trigger Duration: %u", this->relay_trigger_duration_);
ESP_LOGCONFIG(TAG, " Relay Trigger Speed: %u KPH", this->relay_trigger_speed_); ESP_LOGCONFIG(TAG, " Relay Trigger Speed: %u KPH", this->relay_trigger_speed_);
ESP_LOGCONFIG(TAG, " Negotiation Mode: %s", NegotiationMode_to_s_(this->negotiation_mode_)); ESP_LOGCONFIG(TAG, " Negotiation Mode: %s", NegotiationMode_to_s_(this->negotiation_mode_));
//LOG_SELECT(TAG, " Sample Rate", this->sample_rate_);
//LOG_SELECT(TAG, " Tracking Mode", this->tracking_mode_);
} }
void LD2415HComponent::loop() { void LD2415HComponent::loop() {
@ -143,9 +136,7 @@ void LD2415HComponent::set_tracking_mode(TrackingMode mode) {
this->update_mode_rate_uom_ = true; this->update_mode_rate_uom_ = true;
} }
void LD2415HComponent::set_tracking_mode(uint8_t mode) { void LD2415HComponent::set_tracking_mode(uint8_t mode) { this->set_tracking_mode(i_to_TrackingMode_(mode)); }
this->set_tracking_mode(i_to_TrackingMode_(mode));
}
void LD2415HComponent::set_sample_rate(const std::string &state) { void LD2415HComponent::set_sample_rate(const std::string &state) {
uint8_t rate = SAMPLE_RATE_STR_TO_INT.at(state); uint8_t rate = SAMPLE_RATE_STR_TO_INT.at(state);
@ -252,8 +243,7 @@ void LD2415HComponent::parse_config_() {
char *token = strtok(this->response_buffer_, delim); char *token = strtok(this->response_buffer_, delim);
while (token != NULL) while (token != NULL) {
{
if (std::strlen(token) != token_len) { if (std::strlen(token) != token_len) {
ESP_LOGE(TAG, "Configuration key length invalid."); ESP_LOGE(TAG, "Configuration key length invalid.");
break; break;
@ -285,7 +275,6 @@ void LD2415HComponent::parse_config_() {
ESP_LOGCONFIG(TAG, " Relay Trigger Duration: %u", this->relay_trigger_duration_); ESP_LOGCONFIG(TAG, " Relay Trigger Duration: %u", this->relay_trigger_duration_);
ESP_LOGCONFIG(TAG, " Relay Trigger Speed: %u KPH", this->relay_trigger_speed_); ESP_LOGCONFIG(TAG, " Relay Trigger Speed: %u KPH", this->relay_trigger_speed_);
ESP_LOGCONFIG(TAG, " Negotiation Mode: %s", NegotiationMode_to_s_(this->negotiation_mode_)); ESP_LOGCONFIG(TAG, " Negotiation Mode: %s", NegotiationMode_to_s_(this->negotiation_mode_));
} }
void LD2415HComponent::parse_firmware_() { void LD2415HComponent::parse_firmware_() {
@ -377,9 +366,7 @@ void LD2415HComponent::parse_config_param_(char* key, char* value) {
TrackingMode LD2415HComponent::i_to_TrackingMode_(uint8_t value) { TrackingMode LD2415HComponent::i_to_TrackingMode_(uint8_t value) {
TrackingMode u = TrackingMode(value); TrackingMode u = TrackingMode(value);
ESP_LOGD(TAG, "i_to_TrackingMode_: %i, %i", value, static_cast<uint8_t>(u)); switch (u) {
switch (u)
{
case TrackingMode::APPROACHING_AND_RETREATING: case TrackingMode::APPROACHING_AND_RETREATING:
return TrackingMode::APPROACHING_AND_RETREATING; return TrackingMode::APPROACHING_AND_RETREATING;
case TrackingMode::APPROACHING: case TrackingMode::APPROACHING:
@ -394,8 +381,7 @@ TrackingMode LD2415HComponent::i_to_TrackingMode_(uint8_t value) {
UnitOfMeasure LD2415HComponent::i_to_UnitOfMeasure_(uint8_t value) { UnitOfMeasure LD2415HComponent::i_to_UnitOfMeasure_(uint8_t value) {
UnitOfMeasure u = UnitOfMeasure(value); UnitOfMeasure u = UnitOfMeasure(value);
switch (u) switch (u) {
{
case UnitOfMeasure::MPS: case UnitOfMeasure::MPS:
return UnitOfMeasure::MPS; return UnitOfMeasure::MPS;
case UnitOfMeasure::MPH: case UnitOfMeasure::MPH:
@ -411,8 +397,7 @@ UnitOfMeasure LD2415HComponent::i_to_UnitOfMeasure_(uint8_t value) {
NegotiationMode LD2415HComponent::i_to_NegotiationMode_(uint8_t value) { NegotiationMode LD2415HComponent::i_to_NegotiationMode_(uint8_t value) {
NegotiationMode u = NegotiationMode(value); NegotiationMode u = NegotiationMode(value);
switch (u) switch (u) {
{
case NegotiationMode::CUSTOM_AGREEMENT: case NegotiationMode::CUSTOM_AGREEMENT:
return NegotiationMode::CUSTOM_AGREEMENT; return NegotiationMode::CUSTOM_AGREEMENT;
case NegotiationMode::STANDARD_PROTOCOL: case NegotiationMode::STANDARD_PROTOCOL:
@ -424,8 +409,7 @@ NegotiationMode LD2415HComponent::i_to_NegotiationMode_(uint8_t value) {
} }
const char *LD2415HComponent::TrackingMode_to_s_(TrackingMode value) { const char *LD2415HComponent::TrackingMode_to_s_(TrackingMode value) {
switch (value) switch (value) {
{
case TrackingMode::APPROACHING_AND_RETREATING: case TrackingMode::APPROACHING_AND_RETREATING:
return "APPROACHING_AND_RETREATING"; return "APPROACHING_AND_RETREATING";
case TrackingMode::APPROACHING: case TrackingMode::APPROACHING:
@ -437,8 +421,7 @@ const char* LD2415HComponent::TrackingMode_to_s_(TrackingMode value) {
} }
const char *LD2415HComponent::UnitOfMeasure_to_s_(UnitOfMeasure value) { const char *LD2415HComponent::UnitOfMeasure_to_s_(UnitOfMeasure value) {
switch (value) switch (value) {
{
case UnitOfMeasure::MPS: case UnitOfMeasure::MPS:
return "MPS"; return "MPS";
case UnitOfMeasure::MPH: case UnitOfMeasure::MPH:
@ -450,8 +433,7 @@ const char* LD2415HComponent::UnitOfMeasure_to_s_(UnitOfMeasure value) {
} }
const char *LD2415HComponent::NegotiationMode_to_s_(NegotiationMode value) { const char *LD2415HComponent::NegotiationMode_to_s_(NegotiationMode value) {
switch (value) switch (value) {
{
case NegotiationMode::CUSTOM_AGREEMENT: case NegotiationMode::CUSTOM_AGREEMENT:
return "CUSTOM_AGREEMENT"; return "CUSTOM_AGREEMENT";
case NegotiationMode::STANDARD_PROTOCOL: case NegotiationMode::STANDARD_PROTOCOL:

View file

@ -14,40 +14,21 @@
namespace esphome { namespace esphome {
namespace ld2415h { namespace ld2415h {
enum NegotiationMode : uint8_t{ enum NegotiationMode : uint8_t { CUSTOM_AGREEMENT = 0x01, STANDARD_PROTOCOL = 0x02 };
CUSTOM_AGREEMENT = 0x01,
STANDARD_PROTOCOL = 0x02
};
enum SampleRateStructure : uint8_t { enum SampleRateStructure : uint8_t { SAMPLE_RATE_22FPS = 0x00, SAMPLE_RATE_11FPS = 0x01, SAMPLE_RATE_6FPS = 0x02 };
SAMPLE_RATE_22FPS = 0x00,
SAMPLE_RATE_11FPS = 0x01,
SAMPLE_RATE_6FPS = 0x02
};
static const std::map<std::string, uint8_t> SAMPLE_RATE_STR_TO_INT{ static const std::map<std::string, uint8_t> SAMPLE_RATE_STR_TO_INT{
{"~22 fps", SAMPLE_RATE_22FPS}, {"~22 fps", SAMPLE_RATE_22FPS}, {"~11 fps", SAMPLE_RATE_11FPS}, {"~6 fps", SAMPLE_RATE_6FPS}};
{"~11 fps", SAMPLE_RATE_11FPS},
{"~6 fps", SAMPLE_RATE_6FPS}
};
enum TrackingMode : uint8_t { enum TrackingMode : uint8_t { APPROACHING_AND_RETREATING = 0x00, APPROACHING = 0x01, RETREATING = 0x02 };
APPROACHING_AND_RETREATING = 0x00,
APPROACHING = 0x01,
RETREATING = 0x02
};
static const std::map<std::string, uint8_t> TRACKING_MODE_STR_TO_INT{ static const std::map<std::string, uint8_t> TRACKING_MODE_STR_TO_INT{
{"Approaching and Restreating", APPROACHING_AND_RETREATING}, {"Approaching and Restreating", APPROACHING_AND_RETREATING},
{"Approaching", APPROACHING}, {"Approaching", APPROACHING},
{"Restreating", RETREATING} {"Restreating", RETREATING}};
};
enum UnitOfMeasure : uint8_t { enum UnitOfMeasure : uint8_t { KPH = 0x00, MPH = 0x01, MPS = 0x02 };
KPH = 0x00,
MPH = 0x01,
MPS = 0x02
};
class LD2415HListener { class LD2415HListener {
public: public:

View file

@ -12,5 +12,5 @@ void LD2415HSensor::dump_config() {
LOG_SENSOR(" ", "Speed", this->speed_sensor_); LOG_SENSOR(" ", "Speed", this->speed_sensor_);
} }
} // namespace ld2415H } // namespace ld2415h
} // namespace esphome } // namespace esphome