mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[lps22] Replace set_retry with set_interval to avoid heap allocation
set_retry internally does a std::make_shared<RetryArgs>() heap allocation on every invocation. Replace with set_interval + countdown counter which avoids this entirely.
This commit is contained in:
@@ -38,22 +38,29 @@ void LPS22Component::dump_config() {
|
|||||||
LOG_UPDATE_INTERVAL(this);
|
LOG_UPDATE_INTERVAL(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static constexpr uint32_t INTERVAL_READ = 0;
|
||||||
|
|
||||||
void LPS22Component::update() {
|
void LPS22Component::update() {
|
||||||
uint8_t value = 0x00;
|
uint8_t value = 0x00;
|
||||||
this->read_register(CTRL_REG2, &value, 1);
|
this->read_register(CTRL_REG2, &value, 1);
|
||||||
value |= CTRL_REG2_ONE_SHOT_MASK;
|
value |= CTRL_REG2_ONE_SHOT_MASK;
|
||||||
this->write_register(CTRL_REG2, &value, 1);
|
this->write_register(CTRL_REG2, &value, 1);
|
||||||
this->set_retry(READ_INTERVAL, READ_ATTEMPTS, [this](uint8_t _) { return this->try_read_(); });
|
this->read_attempts_remaining_ = READ_ATTEMPTS;
|
||||||
|
this->set_interval(INTERVAL_READ, READ_INTERVAL, [this]() { this->try_read_(); });
|
||||||
}
|
}
|
||||||
|
|
||||||
RetryResult LPS22Component::try_read_() {
|
void LPS22Component::try_read_() {
|
||||||
uint8_t value = 0x00;
|
uint8_t value = 0x00;
|
||||||
this->read_register(STATUS, &value, 1);
|
this->read_register(STATUS, &value, 1);
|
||||||
const uint8_t expected_status_mask = STATUS_T_DA_MASK | STATUS_P_DA_MASK;
|
const uint8_t expected_status_mask = STATUS_T_DA_MASK | STATUS_P_DA_MASK;
|
||||||
if ((value & expected_status_mask) != expected_status_mask) {
|
if ((value & expected_status_mask) != expected_status_mask) {
|
||||||
ESP_LOGD(TAG, "STATUS not ready: %x", value);
|
ESP_LOGD(TAG, "STATUS not ready: %x", value);
|
||||||
return RetryResult::RETRY;
|
if (--this->read_attempts_remaining_ == 0) {
|
||||||
|
this->cancel_interval(INTERVAL_READ);
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this->cancel_interval(INTERVAL_READ);
|
||||||
|
|
||||||
if (this->temperature_sensor_ != nullptr) {
|
if (this->temperature_sensor_ != nullptr) {
|
||||||
uint8_t t_buf[2]{0};
|
uint8_t t_buf[2]{0};
|
||||||
@@ -68,7 +75,6 @@ RetryResult LPS22Component::try_read_() {
|
|||||||
uint32_t p_lsb = encode_uint24(p_buf[2], p_buf[1], p_buf[0]);
|
uint32_t p_lsb = encode_uint24(p_buf[2], p_buf[1], p_buf[0]);
|
||||||
this->pressure_sensor_->publish_state(PRESSURE_SCALE * static_cast<float>(p_lsb));
|
this->pressure_sensor_->publish_state(PRESSURE_SCALE * static_cast<float>(p_lsb));
|
||||||
}
|
}
|
||||||
return RetryResult::DONE;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace lps22
|
} // namespace lps22
|
||||||
|
|||||||
@@ -17,10 +17,11 @@ class LPS22Component : public sensor::Sensor, public PollingComponent, public i2
|
|||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
void try_read_();
|
||||||
|
|
||||||
sensor::Sensor *temperature_sensor_{nullptr};
|
sensor::Sensor *temperature_sensor_{nullptr};
|
||||||
sensor::Sensor *pressure_sensor_{nullptr};
|
sensor::Sensor *pressure_sensor_{nullptr};
|
||||||
|
uint8_t read_attempts_remaining_{0};
|
||||||
RetryResult try_read_();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace lps22
|
} // namespace lps22
|
||||||
|
|||||||
Reference in New Issue
Block a user