rewrote response parsing and added support for distance and SNR sensors

This commit is contained in:
Robin Thoni 2024-06-24 08:42:55 +00:00 committed by GitHub
parent a2f02aba57
commit 3a8a3cd6e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -66,13 +66,41 @@ uint8_t ReadStateCommand::execute(DfrobotSen0395Component *parent) {
this->parent_ = parent; this->parent_ = parent;
if (this->parent_->read_message_()) { if (this->parent_->read_message_()) {
std::string message(this->parent_->read_buffer_); std::string message(this->parent_->read_buffer_);
if (message.rfind("$JYBSS,0, , , *") != std::string::npos) { std::vector<std::string> params;
this->parent_->set_detected_(false); 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); this->parent_->set_active(true);
return 1; // Command done return 1; // Command done
} else if (message.rfind("$JYBSS,1, , , *") != std::string::npos) { } else if (params.size() == 8 && params[0] == "$JYRPO") {
this->parent_->set_detected_(true); const auto target_count = std::atoi(params[1].c_str());
this->parent_->set_active(true); 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 return 1; // Command done
} }
} }