mirror of
https://github.com/esphome/esphome.git
synced 2024-11-21 22:48:10 +01:00
fix a01nyub data reading (#5882)
Co-authored-by: Samuel Sieb <samuel@sieb.net>
This commit is contained in:
parent
e271faa544
commit
29dcc4031f
1 changed files with 18 additions and 31 deletions
|
@ -8,50 +8,37 @@ namespace esphome {
|
||||||
namespace a01nyub {
|
namespace a01nyub {
|
||||||
|
|
||||||
static const char *const TAG = "a01nyub.sensor";
|
static const char *const TAG = "a01nyub.sensor";
|
||||||
static const uint8_t MAX_DATA_LENGTH_BYTES = 4;
|
|
||||||
|
|
||||||
void A01nyubComponent::loop() {
|
void A01nyubComponent::loop() {
|
||||||
uint8_t data;
|
uint8_t data;
|
||||||
while (this->available() > 0) {
|
while (this->available() > 0) {
|
||||||
if (this->read_byte(&data)) {
|
this->read_byte(&data);
|
||||||
buffer_.push_back(data);
|
if (this->buffer_.empty() && (data != 0xff))
|
||||||
|
continue;
|
||||||
|
buffer_.push_back(data);
|
||||||
|
if (this->buffer_.size() == 4)
|
||||||
this->check_buffer_();
|
this->check_buffer_();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void A01nyubComponent::check_buffer_() {
|
void A01nyubComponent::check_buffer_() {
|
||||||
if (this->buffer_.size() >= MAX_DATA_LENGTH_BYTES) {
|
uint8_t checksum = this->buffer_[0] + this->buffer_[1] + this->buffer_[2];
|
||||||
size_t i;
|
if (this->buffer_[3] == checksum) {
|
||||||
for (i = 0; i < this->buffer_.size(); i++) {
|
float distance = (this->buffer_[1] << 8) + this->buffer_[2];
|
||||||
// Look for the first packet
|
if (distance > 280) {
|
||||||
if (this->buffer_[i] == 0xFF) {
|
float meters = distance / 1000.0;
|
||||||
if (i + 1 + 3 < this->buffer_.size()) { // Packet is not complete
|
ESP_LOGV(TAG, "Distance from sensor: %f mm, %f m", distance, meters);
|
||||||
return; // Wait for completion
|
this->publish_state(meters);
|
||||||
}
|
} else {
|
||||||
|
ESP_LOGW(TAG, "Invalid data read from sensor: %s", format_hex_pretty(this->buffer_).c_str());
|
||||||
uint8_t checksum = (this->buffer_[i] + this->buffer_[i + 1] + this->buffer_[i + 2]) & 0xFF;
|
|
||||||
if (this->buffer_[i + 3] == checksum) {
|
|
||||||
float distance = (this->buffer_[i + 1] << 8) + this->buffer_[i + 2];
|
|
||||||
if (distance > 280) {
|
|
||||||
float meters = distance / 1000.0;
|
|
||||||
ESP_LOGV(TAG, "Distance from sensor: %f mm, %f m", distance, meters);
|
|
||||||
this->publish_state(meters);
|
|
||||||
} else {
|
|
||||||
ESP_LOGW(TAG, "Invalid data read from sensor: %s", format_hex_pretty(this->buffer_).c_str());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
this->buffer_.clear();
|
} else {
|
||||||
|
ESP_LOGW(TAG, "checksum failed: %02x != %02x", checksum, this->buffer_[3]);
|
||||||
}
|
}
|
||||||
|
this->buffer_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void A01nyubComponent::dump_config() {
|
void A01nyubComponent::dump_config() { LOG_SENSOR("", "A01nyub Sensor", this); }
|
||||||
ESP_LOGCONFIG(TAG, "A01nyub Sensor:");
|
|
||||||
LOG_SENSOR(" ", "Distance", this);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace a01nyub
|
} // namespace a01nyub
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
Loading…
Reference in a new issue