Some fixes, added an ability to clear calibration

This commit is contained in:
Oleg Tarasov 2024-11-15 18:46:00 +03:00
parent 054728ec0b
commit 1bdeb405f2
2 changed files with 22 additions and 7 deletions

View file

@ -574,6 +574,7 @@ void ShellyDimmer::start_calibration() {
this->set_brightness_no_transition_(1); this->set_brightness_no_transition_(1);
// Init calibration data // Init calibration data
this->calibration_data_.fill(0);
this->calibration_measurements_.fill(0); this->calibration_measurements_.fill(0);
this->calibration_measurement_cnt_ = 0; this->calibration_measurement_cnt_ = 0;
this->calibration_step_ = -20; this->calibration_step_ = -20;
@ -646,11 +647,7 @@ void ShellyDimmer::complete_calibration_() {
value = remap(value, min, max, 0.0f, 1.0f); value = remap(value, min, max, 0.0f, 1.0f);
} }
if (this->rtc_.save(&this->calibration_data_)) { this->save_calibration_();
ESP_LOGD(TAG, "Saved calibration to flash");
} else {
ESP_LOGW(TAG, "Couldn't save calibration to flash");
}
ESP_LOGD(TAG, "Finished calibration. Values:"); ESP_LOGD(TAG, "Finished calibration. Values:");
for (float value : this->calibration_data_) { for (float value : this->calibration_data_) {
@ -659,6 +656,13 @@ void ShellyDimmer::complete_calibration_() {
this->set_brightness_no_transition_(1); this->set_brightness_no_transition_(1);
} }
void ShellyDimmer::save_calibration_() {
if (this->rtc_.save(&this->calibration_data_)) {
ESP_LOGD(TAG, "Saved calibration to flash");
} else {
ESP_LOGW(TAG, "Couldn't save calibration to flash");
}
}
void ShellyDimmer::set_brightness_no_transition_(float brightness) { void ShellyDimmer::set_brightness_no_transition_(float brightness) {
auto call = this->state_->make_call(); auto call = this->state_->make_call();
call.set_brightness(brightness); call.set_brightness(brightness);
@ -666,6 +670,10 @@ void ShellyDimmer::set_brightness_no_transition_(float brightness) {
call.set_state(true); call.set_state(true);
call.perform(); call.perform();
} }
void ShellyDimmer::clear_calibration() {
this->calibration_data_.fill(0);
this->save_calibration_();
}
} // namespace shelly_dimmer } // namespace shelly_dimmer
} // namespace esphome } // namespace esphome

View file

@ -50,8 +50,12 @@ class ShellyDimmer : public PollingComponent, public light::LightOutput, public
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { this->voltage_sensor_ = voltage_sensor; } void set_voltage_sensor(sensor::Sensor *voltage_sensor) { this->voltage_sensor_ = voltage_sensor; }
void set_current_sensor(sensor::Sensor *current_sensor) { this->current_sensor_ = current_sensor; } void set_current_sensor(sensor::Sensor *current_sensor) { this->current_sensor_ = current_sensor; }
/// Starts the calibration process.
void start_calibration(); void start_calibration();
/// Clears calibration values.
void clear_calibration();
protected: protected:
GPIOPin *pin_nrst_; GPIOPin *pin_nrst_;
GPIOPin *pin_boot0_; GPIOPin *pin_boot0_;
@ -132,10 +136,13 @@ class ShellyDimmer : public PollingComponent, public light::LightOutput, public
/// Complete a single calibration step averaging over accumulated measurements. /// Complete a single calibration step averaging over accumulated measurements.
void complete_calibration_step_(); void complete_calibration_step_();
// Complete the whole calibration process. /// Complete the whole calibration process.
void complete_calibration_(); void complete_calibration_();
// Set brightness with no transition during calibration. /// Saves calibration values to flash.
void save_calibration_();
/// Set brightness with no transition during calibration.
void set_brightness_no_transition_(float brightness); void set_brightness_no_transition_(float brightness);
}; };