mirror of
https://github.com/esphome/esphome.git
synced 2024-12-11 14:49:20 +01:00
rewrote response parsing and added support for distance and SNR sensors
This commit is contained in:
parent
a2f02aba57
commit
3a8a3cd6e0
@ -66,13 +66,41 @@ uint8_t ReadStateCommand::execute(DfrobotSen0395Component *parent) {
|
||||
this->parent_ = parent;
|
||||
if (this->parent_->read_message_()) {
|
||||
std::string message(this->parent_->read_buffer_);
|
||||
if (message.rfind("$JYBSS,0, , , *") != std::string::npos) {
|
||||
this->parent_->set_detected_(false);
|
||||
std::vector<std::string> params;
|
||||
size_t pos = 0;
|
||||
std::string token;
|
||||
while ((pos = message.find(",")) != std::string::npos) {
|
||||
token = message.substr(0, pos);
|
||||
params.push_back(token);
|
||||
message.erase(0, pos + 1);
|
||||
}
|
||||
params.push_back(message);
|
||||
|
||||
if (params.size() == 5 && params[0] == "$JYBSS") {
|
||||
this->parent_->set_detected_(params[1] == "1");
|
||||
if (params[1] != "1") {
|
||||
for (int ti = 1; ti <= TARGET_COUNT; ++ti) {
|
||||
this->parent_->set_detected_target_distance_(ti, NAN);
|
||||
this->parent_->set_detected_target_snr_(ti, NAN);
|
||||
}
|
||||
}
|
||||
this->parent_->set_active(true);
|
||||
return 1; // Command done
|
||||
} else if (message.rfind("$JYBSS,1, , , *") != std::string::npos) {
|
||||
this->parent_->set_detected_(true);
|
||||
this->parent_->set_active(true);
|
||||
} else if (params.size() == 8 && params[0] == "$JYRPO") {
|
||||
const auto target_count = std::atoi(params[1].c_str());
|
||||
const auto target = std::atoi(params[2].c_str());
|
||||
const auto dist = std::atof(params[3].c_str());
|
||||
const auto snr = std::atof(params[5].c_str());
|
||||
this->parent_->set_detected_target_distance_(target, dist);
|
||||
this->parent_->set_detected_target_snr_(target, snr);
|
||||
|
||||
if (target == target_count) {
|
||||
for (int ti = target + 1; ti <= TARGET_COUNT; ++ti) {
|
||||
this->parent_->set_detected_target_distance_(ti, NAN);
|
||||
this->parent_->set_detected_target_snr_(ti, NAN);
|
||||
}
|
||||
}
|
||||
|
||||
return 1; // Command done
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user