Skip to content

Commit

Permalink
feat(sen5x): FSM for read commands arbitration
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeInPolish committed May 8, 2024
1 parent 7428b4b commit c075183
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
18 changes: 16 additions & 2 deletions esphome/components/sen5x/sen5x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ static const uint16_t SEN5X_CMD_VOC_ALGORITHM_TUNING = 0x60D0;
void SEN5XComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up sen5x...");

this->fsm_ = IDLE;

// the sensor needs 1000 ms to enter the idle state
this->set_timeout(1000, [this]() {
// Check if measurement is ready before reading the value
Expand Down Expand Up @@ -331,8 +333,16 @@ void SEN5XComponent::update() {
this->update_measured_values_();

if (this->get_pm_number_concentration_and_tps_) {
// delay the extra reading to allow update_measured_values to complete.
this->set_timeout(30, [this]() { this->update_measured_pm_(); });
this->set_timeout(10, [this]() { this->trigger_update_measured_pm_(); });
}
}

void SEN5XComponent::trigger_update_measured_pm_() {
ESP_LOGD(TAG, "fsm: %d", this->fsm_);
if (this->fsm_ != VALUE_UPDATE_DONE) {
this->set_timeout(20, [this]() { this->trigger_update_measured_pm_(); });
} else if (this->fsm_ == VALUE_UPDATE_DONE) {
this->update_measured_pm_();
}
}

Expand Down Expand Up @@ -415,10 +425,12 @@ void SEN5XComponent::update_measured_pm_() {
this->pm_tps_sensor_->publish_state(pm_tps);

this->status_clear_warning();
this->fsm_ = IDLE;
});
}

void SEN5XComponent::update_measured_values_() {
this->fsm_ = VALUE_UPDATE_ONGOING;
if (!this->write_command(SEN5X_CMD_READ_MEASUREMENT)) {
this->status_set_warning();
ESP_LOGD(TAG, "write error read measurement (%d)", this->last_error_);
Expand Down Expand Up @@ -460,7 +472,9 @@ void SEN5XComponent::update_measured_values_() {
this->voc_sensor_->publish_state(voc);
if (this->nox_sensor_ != nullptr)
this->nox_sensor_->publish_state(nox);

this->status_clear_warning();
this->fsm_ = VALUE_UPDATE_DONE;
});
}

Expand Down
3 changes: 3 additions & 0 deletions esphome/components/sen5x/sen5x.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class SEN5XComponent : public PollingComponent, public sensirion_common::Sensiri
void write_voc_baseline_();
void update_measured_values_();
void update_measured_pm_();
void trigger_update_measured_pm_();
ERRORCODE error_code_;
bool initialized_{false};
sensor::Sensor *pm_1_0_sensor_{nullptr};
Expand Down Expand Up @@ -152,6 +153,8 @@ class SEN5XComponent : public PollingComponent, public sensirion_common::Sensiri
optional<TemperatureCompensation> temperature_compensation_;
// Driver state variables
bool get_pm_number_concentration_and_tps_;
enum FsmStates { IDLE, VALUE_UPDATE_ONGOING, VALUE_UPDATE_DONE };
FsmStates fsm_;
};

} // namespace sen5x
Expand Down

0 comments on commit c075183

Please sign in to comment.