[scd4x] Memory optimization (#9358)

This commit is contained in:
Keith Burzinski 2025-07-06 22:54:19 -05:00 committed by GitHub
parent e49b89a051
commit 364b6ca8d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 15 deletions

View File

@ -58,7 +58,7 @@ void SCD4XComponent::setup() {
} }
// If pressure compensation available use it, else use altitude // If pressure compensation available use it, else use altitude
if (this->ambient_pressure_compensation_) { if (this->ambient_pressure_) {
if (!this->update_ambient_pressure_compensation_(this->ambient_pressure_)) { if (!this->update_ambient_pressure_compensation_(this->ambient_pressure_)) {
ESP_LOGE(TAG, "Error setting ambient pressure compensation"); ESP_LOGE(TAG, "Error setting ambient pressure compensation");
this->error_code_ = MEASUREMENT_INIT_FAILED; this->error_code_ = MEASUREMENT_INIT_FAILED;
@ -137,7 +137,7 @@ void SCD4XComponent::dump_config() {
ESP_LOGCONFIG(TAG, " Dynamic ambient pressure compensation using '%s'", ESP_LOGCONFIG(TAG, " Dynamic ambient pressure compensation using '%s'",
this->ambient_pressure_source_->get_name().c_str()); this->ambient_pressure_source_->get_name().c_str());
} else { } else {
if (this->ambient_pressure_compensation_) { if (this->ambient_pressure_) {
ESP_LOGCONFIG(TAG, ESP_LOGCONFIG(TAG,
" Altitude compensation disabled\n" " Altitude compensation disabled\n"
" Ambient pressure compensation: %dmBar", " Ambient pressure compensation: %dmBar",
@ -230,7 +230,7 @@ bool SCD4XComponent::perform_forced_calibration(uint16_t current_co2_concentrati
// frc takes 400 ms // frc takes 400 ms
// because this method will be used very rarly // because this method will be used very rarly
// the simple approach with delay is ok // the simple approach with delay is ok
delay(400); // NOLINT' delay(400); // NOLINT
if (!this->start_measurement_()) { if (!this->start_measurement_()) {
return false; return false;
} else { } else {
@ -267,8 +267,7 @@ bool SCD4XComponent::factory_reset() {
} }
void SCD4XComponent::set_ambient_pressure_compensation(float pressure_in_hpa) { void SCD4XComponent::set_ambient_pressure_compensation(float pressure_in_hpa) {
ambient_pressure_compensation_ = true; uint16_t new_ambient_pressure = static_cast<uint16_t>(pressure_in_hpa);
uint16_t new_ambient_pressure = (uint16_t) pressure_in_hpa;
if (!this->initialized_) { if (!this->initialized_) {
this->ambient_pressure_ = new_ambient_pressure; this->ambient_pressure_ = new_ambient_pressure;
return; return;

View File

@ -46,19 +46,17 @@ class SCD4XComponent : public PollingComponent, public sensirion_common::Sensiri
bool update_ambient_pressure_compensation_(uint16_t pressure_in_hpa); bool update_ambient_pressure_compensation_(uint16_t pressure_in_hpa);
bool start_measurement_(); bool start_measurement_();
uint16_t altitude_compensation_;
uint16_t ambient_pressure_;
bool initialized_{false};
bool ambient_pressure_compensation_;
bool enable_asc_;
float temperature_offset_;
ErrorCode error_code_;
MeasurementMode measurement_mode_{PERIODIC};
sensor::Sensor *co2_sensor_{nullptr}; sensor::Sensor *co2_sensor_{nullptr};
sensor::Sensor *temperature_sensor_{nullptr}; sensor::Sensor *temperature_sensor_{nullptr};
sensor::Sensor *humidity_sensor_{nullptr}; sensor::Sensor *humidity_sensor_{nullptr};
// used for compensation sensor::Sensor *ambient_pressure_source_{nullptr}; // used for compensation
sensor::Sensor *ambient_pressure_source_{nullptr}; float temperature_offset_;
uint16_t altitude_compensation_{0};
uint16_t ambient_pressure_{0}; // Per datasheet, valid values are 700 to 1200 hPa; 0 is a valid sentinel value
bool initialized_{false};
bool enable_asc_{false};
ErrorCode error_code_;
MeasurementMode measurement_mode_{PERIODIC};
}; };
} // namespace scd4x } // namespace scd4x