Fix pressure compensation in SCD4X (#4357)

This commit is contained in:
Carlos Garcia Saura 2023-02-02 01:22:23 +01:00 committed by GitHub
parent e847766514
commit e3d9c44bdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 8 deletions

View File

@ -149,9 +149,9 @@ void SCD4XComponent::update() {
}
if (this->ambient_pressure_source_ != nullptr) {
float pressure = this->ambient_pressure_source_->state / 1000.0f;
float pressure = this->ambient_pressure_source_->state;
if (!std::isnan(pressure)) {
set_ambient_pressure_compensation(this->ambient_pressure_source_->state / 1000.0f);
set_ambient_pressure_compensation(pressure);
}
}
@ -254,12 +254,15 @@ bool SCD4XComponent::factory_reset() {
return true;
}
// Note pressure in bar here. Convert to hPa
void SCD4XComponent::set_ambient_pressure_compensation(float pressure_in_bar) {
void SCD4XComponent::set_ambient_pressure_compensation(float pressure_in_hpa) {
ambient_pressure_compensation_ = true;
uint16_t new_ambient_pressure = (uint16_t)(pressure_in_bar * 1000);
// remove millibar from comparison to avoid frequent updates +/- 10 millibar doesn't matter
if (initialized_ && (new_ambient_pressure / 10 != ambient_pressure_ / 10)) {
uint16_t new_ambient_pressure = (uint16_t) pressure_in_hpa;
if (!initialized_) {
ambient_pressure_ = new_ambient_pressure;
return;
}
// Only send pressure value if it has changed since last update
if (new_ambient_pressure != ambient_pressure_) {
update_ambient_pressure_compensation_(new_ambient_pressure);
ambient_pressure_ = new_ambient_pressure;
} else {

View File

@ -26,7 +26,7 @@ class SCD4XComponent : public PollingComponent, public sensirion_common::Sensiri
void set_automatic_self_calibration(bool asc) { enable_asc_ = asc; }
void set_altitude_compensation(uint16_t altitude) { altitude_compensation_ = altitude; }
void set_ambient_pressure_compensation(float pressure_in_bar);
void set_ambient_pressure_compensation(float pressure_in_hpa);
void set_ambient_pressure_source(sensor::Sensor *pressure) { ambient_pressure_source_ = pressure; }
void set_temperature_offset(float offset) { temperature_offset_ = offset; };