mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-06-13 16:13:32 +02:00
Refactor Sensirion Sensors (#3374)
Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
@@ -7,55 +7,50 @@ namespace esphome {
|
||||
namespace sdp3x {
|
||||
|
||||
static const char *const TAG = "sdp3x.sensor";
|
||||
static const uint8_t SDP3X_SOFT_RESET[2] = {0x00, 0x06};
|
||||
static const uint8_t SDP3X_READ_ID1[2] = {0x36, 0x7C};
|
||||
static const uint8_t SDP3X_READ_ID2[2] = {0xE1, 0x02};
|
||||
static const uint8_t SDP3X_START_DP_AVG[2] = {0x36, 0x15};
|
||||
static const uint8_t SDP3X_START_MASS_FLOW_AVG[2] = {0x36, 0x03};
|
||||
static const uint8_t SDP3X_STOP_MEAS[2] = {0x3F, 0xF9};
|
||||
static const uint16_t SDP3X_SOFT_RESET = 0x0006;
|
||||
static const uint16_t SDP3X_READ_ID1 = 0x367C;
|
||||
static const uint16_t SDP3X_READ_ID2 = 0xE102;
|
||||
static const uint16_t SDP3X_START_DP_AVG = 0x3615;
|
||||
static const uint16_t SDP3X_START_MASS_FLOW_AVG = 0x3603;
|
||||
static const uint16_t SDP3X_STOP_MEAS = 0x3FF9;
|
||||
|
||||
void SDP3XComponent::update() { this->read_pressure_(); }
|
||||
|
||||
void SDP3XComponent::setup() {
|
||||
ESP_LOGD(TAG, "Setting up SDP3X...");
|
||||
|
||||
if (this->write(SDP3X_STOP_MEAS, 2) != i2c::ERROR_OK) {
|
||||
if (!this->write_command(SDP3X_STOP_MEAS)) {
|
||||
ESP_LOGW(TAG, "Stop SDP3X failed!"); // This sometimes fails for no good reason
|
||||
}
|
||||
|
||||
if (this->write(SDP3X_SOFT_RESET, 2) != i2c::ERROR_OK) {
|
||||
if (!this->write_command(SDP3X_SOFT_RESET)) {
|
||||
ESP_LOGW(TAG, "Soft Reset SDP3X failed!"); // This sometimes fails for no good reason
|
||||
}
|
||||
|
||||
this->set_timeout(20, [this] {
|
||||
if (this->write(SDP3X_READ_ID1, 2) != i2c::ERROR_OK) {
|
||||
if (!this->write_command(SDP3X_READ_ID1)) {
|
||||
ESP_LOGE(TAG, "Read ID1 SDP3X failed!");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
if (this->write(SDP3X_READ_ID2, 2) != i2c::ERROR_OK) {
|
||||
if (!this->write_command(SDP3X_READ_ID2)) {
|
||||
ESP_LOGE(TAG, "Read ID2 SDP3X failed!");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t data[18];
|
||||
if (this->read(data, 18) != i2c::ERROR_OK) {
|
||||
uint16_t data[6];
|
||||
if (this->read_data(data, 6) != i2c::ERROR_OK) {
|
||||
ESP_LOGE(TAG, "Read ID SDP3X failed!");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
if (!(check_crc_(&data[0], 2, data[2]) && check_crc_(&data[3], 2, data[5]))) {
|
||||
ESP_LOGE(TAG, "CRC ID SDP3X failed!");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
// SDP8xx
|
||||
// ref:
|
||||
// https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/8_Differential_Pressure/Datasheets/Sensirion_Differential_Pressure_Datasheet_SDP8xx_Digital.pdf
|
||||
if (data[2] == 0x02) {
|
||||
switch (data[3]) {
|
||||
if (data[1] >> 8 == 0x02) {
|
||||
switch (data[1] & 0xFF) {
|
||||
case 0x01: // SDP800-500Pa
|
||||
ESP_LOGCONFIG(TAG, "Sensor is SDP800-500Pa");
|
||||
break;
|
||||
@@ -75,15 +70,16 @@ void SDP3XComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Sensor is SDP810-125Pa");
|
||||
break;
|
||||
}
|
||||
} else if (data[2] == 0x01) {
|
||||
if (data[3] == 0x01) {
|
||||
} else if (data[1] >> 8 == 0x01) {
|
||||
if ((data[1] & 0xFF) == 0x01) {
|
||||
ESP_LOGCONFIG(TAG, "Sensor is SDP31-500Pa");
|
||||
} else if (data[3] == 0x02) {
|
||||
} else if ((data[1] & 0xFF) == 0x02) {
|
||||
ESP_LOGCONFIG(TAG, "Sensor is SDP32-125Pa");
|
||||
}
|
||||
}
|
||||
|
||||
if (this->write(measurement_mode_ == DP_AVG ? SDP3X_START_DP_AVG : SDP3X_START_MASS_FLOW_AVG, 2) != i2c::ERROR_OK) {
|
||||
if (this->write_command(measurement_mode_ == DP_AVG ? SDP3X_START_DP_AVG : SDP3X_START_MASS_FLOW_AVG) !=
|
||||
i2c::ERROR_OK) {
|
||||
ESP_LOGE(TAG, "Start Measurements SDP3X failed!");
|
||||
this->mark_failed();
|
||||
return;
|
||||
@@ -101,22 +97,16 @@ void SDP3XComponent::dump_config() {
|
||||
}
|
||||
|
||||
void SDP3XComponent::read_pressure_() {
|
||||
uint8_t data[9];
|
||||
if (this->read(data, 9) != i2c::ERROR_OK) {
|
||||
uint16_t data[3];
|
||||
if (this->read_data(data, 3) != i2c::ERROR_OK) {
|
||||
ESP_LOGW(TAG, "Couldn't read SDP3X data!");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!(check_crc_(&data[0], 2, data[2]) && check_crc_(&data[3], 2, data[5]) && check_crc_(&data[6], 2, data[8]))) {
|
||||
ESP_LOGW(TAG, "Invalid SDP3X data!");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
int16_t pressure_raw = encode_uint16(data[0], data[1]);
|
||||
int16_t temperature_raw = encode_uint16(data[3], data[4]);
|
||||
int16_t scale_factor_raw = encode_uint16(data[6], data[7]);
|
||||
int16_t pressure_raw = data[0];
|
||||
int16_t temperature_raw = data[1];
|
||||
int16_t scale_factor_raw = data[2];
|
||||
// scale factor is in Pa - convert to hPa
|
||||
float pressure = pressure_raw / (scale_factor_raw * 100.0f);
|
||||
ESP_LOGV(TAG, "Got raw pressure=%d, raw scale factor =%d, raw temperature=%d ", pressure_raw, scale_factor_raw,
|
||||
@@ -129,26 +119,5 @@ void SDP3XComponent::read_pressure_() {
|
||||
|
||||
float SDP3XComponent::get_setup_priority() const { return setup_priority::DATA; }
|
||||
|
||||
// Check CRC function from SDP3X sample code provided by sensirion
|
||||
// Returns true if a checksum is OK
|
||||
bool SDP3XComponent::check_crc_(const uint8_t data[], uint8_t size, uint8_t checksum) {
|
||||
uint8_t crc = 0xFF;
|
||||
|
||||
// calculates 8-Bit checksum with given polynomial 0x31 (x^8 + x^5 + x^4 + 1)
|
||||
for (int i = 0; i < size; i++) {
|
||||
crc ^= (data[i]);
|
||||
for (uint8_t bit = 8; bit > 0; --bit) {
|
||||
if (crc & 0x80) {
|
||||
crc = (crc << 1) ^ 0x31;
|
||||
} else {
|
||||
crc = (crc << 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// verify checksum
|
||||
return (crc == checksum);
|
||||
}
|
||||
|
||||
} // namespace sdp3x
|
||||
} // namespace esphome
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
|
||||
#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 sdp3x {
|
||||
|
||||
enum MeasurementMode { MASS_FLOW_AVG, DP_AVG };
|
||||
|
||||
class SDP3XComponent : public PollingComponent, public i2c::I2CDevice, public sensor::Sensor {
|
||||
class SDP3XComponent : public PollingComponent, public sensirion_common::SensirionI2CDevice, public sensor::Sensor {
|
||||
public:
|
||||
/// Schedule temperature+pressure readings.
|
||||
void update() override;
|
||||
@@ -23,8 +23,6 @@ class SDP3XComponent : public PollingComponent, public i2c::I2CDevice, public se
|
||||
protected:
|
||||
/// Internal method to read the pressure from the component after it has been scheduled.
|
||||
void read_pressure_();
|
||||
|
||||
bool check_crc_(const uint8_t data[], uint8_t size, uint8_t checksum);
|
||||
MeasurementMode measurement_mode_;
|
||||
};
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.components import sensirion_common
|
||||
from esphome.const import (
|
||||
DEVICE_CLASS_PRESSURE,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
@@ -8,10 +9,13 @@ from esphome.const import (
|
||||
)
|
||||
|
||||
DEPENDENCIES = ["i2c"]
|
||||
AUTO_LOAD = ["sensirion_common"]
|
||||
CODEOWNERS = ["@Azimath"]
|
||||
|
||||
sdp3x_ns = cg.esphome_ns.namespace("sdp3x")
|
||||
SDP3XComponent = sdp3x_ns.class_("SDP3XComponent", cg.PollingComponent, i2c.I2CDevice)
|
||||
SDP3XComponent = sdp3x_ns.class_(
|
||||
"SDP3XComponent", cg.PollingComponent, sensirion_common.SensirionI2CDevice
|
||||
)
|
||||
|
||||
|
||||
MeasurementMode = sdp3x_ns.enum("MeasurementMode")
|
||||
|
||||
Reference in New Issue
Block a user