mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-05 20:38:27 +02:00
Refactor Sensirion Sensors (#3374)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -8,6 +8,7 @@ from esphome.const import (
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["i2c"]
|
||||
AUTO_LOAD = ["sensirion_common"]
|
||||
|
||||
sts3x_ns = cg.esphome_ns.namespace("sts3x")
|
||||
|
||||
|
||||
@@ -19,13 +19,13 @@ static const uint16_t STS3X_COMMAND_FETCH_DATA = 0xE000;
|
||||
|
||||
void STS3XComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up STS3x...");
|
||||
if (!this->write_command_(STS3X_COMMAND_READ_SERIAL_NUMBER)) {
|
||||
if (!this->write_command(STS3X_COMMAND_READ_SERIAL_NUMBER)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t raw_serial_number[2];
|
||||
if (!this->read_data_(raw_serial_number, 1)) {
|
||||
if (!this->read_data(raw_serial_number, 1)) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
@@ -46,16 +46,16 @@ float STS3XComponent::get_setup_priority() const { return setup_priority::DATA;
|
||||
void STS3XComponent::update() {
|
||||
if (this->status_has_warning()) {
|
||||
ESP_LOGD(TAG, "Retrying to reconnect the sensor.");
|
||||
this->write_command_(STS3X_COMMAND_SOFT_RESET);
|
||||
this->write_command(STS3X_COMMAND_SOFT_RESET);
|
||||
}
|
||||
if (!this->write_command_(STS3X_COMMAND_POLLING_H)) {
|
||||
if (!this->write_command(STS3X_COMMAND_POLLING_H)) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
this->set_timeout(50, [this]() {
|
||||
uint16_t raw_data[1];
|
||||
if (!this->read_data_(raw_data, 1)) {
|
||||
if (!this->read_data(raw_data, 1)) {
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
@@ -67,56 +67,5 @@ void STS3XComponent::update() {
|
||||
});
|
||||
}
|
||||
|
||||
bool STS3XComponent::write_command_(uint16_t command) {
|
||||
// Warning ugly, trick the I2Ccomponent base by setting register to the first 8 bit.
|
||||
return this->write_byte(command >> 8, command & 0xFF);
|
||||
}
|
||||
|
||||
uint8_t sts3x_crc(uint8_t data1, uint8_t data2) {
|
||||
uint8_t bit;
|
||||
uint8_t crc = 0xFF;
|
||||
|
||||
crc ^= data1;
|
||||
for (bit = 8; bit > 0; --bit) {
|
||||
if (crc & 0x80) {
|
||||
crc = (crc << 1) ^ 0x131;
|
||||
} else {
|
||||
crc = (crc << 1);
|
||||
}
|
||||
}
|
||||
|
||||
crc ^= data2;
|
||||
for (bit = 8; bit > 0; --bit) {
|
||||
if (crc & 0x80) {
|
||||
crc = (crc << 1) ^ 0x131;
|
||||
} else {
|
||||
crc = (crc << 1);
|
||||
}
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
|
||||
bool STS3XComponent::read_data_(uint16_t *data, uint8_t len) {
|
||||
const uint8_t num_bytes = len * 3;
|
||||
std::vector<uint8_t> buf(num_bytes);
|
||||
|
||||
if (this->read(buf.data(), num_bytes) != i2c::ERROR_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
const uint8_t j = 3 * i;
|
||||
uint8_t crc = sts3x_crc(buf[j], buf[j + 1]);
|
||||
if (crc != buf[j + 2]) {
|
||||
ESP_LOGE(TAG, "CRC8 Checksum invalid! 0x%02X != 0x%02X", buf[j + 2], crc);
|
||||
return false;
|
||||
}
|
||||
data[i] = (buf[j] << 8) | buf[j + 1];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace sts3x
|
||||
} // namespace esphome
|
||||
|
||||
@@ -2,22 +2,18 @@
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "esphome/components/sensirion_common/i2c_sensirion.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sts3x {
|
||||
|
||||
/// This class implements support for the ST3x-DIS family of temperature i2c sensors.
|
||||
class STS3XComponent : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice {
|
||||
class STS3XComponent : public sensor::Sensor, public PollingComponent, public sensirion_common::SensirionI2CDevice {
|
||||
public:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
void update() override;
|
||||
|
||||
protected:
|
||||
bool write_command_(uint16_t command);
|
||||
bool read_data_(uint16_t *data, uint8_t len);
|
||||
};
|
||||
|
||||
} // namespace sts3x
|
||||
|
||||
Reference in New Issue
Block a user