Add turn_on/off trigger to slow_pwm (#2921)

Co-authored-by: Jesse Hills <3060199+jesserockz@users.noreply.github.com>
This commit is contained in:
Martin
2022-01-09 19:49:57 +01:00
committed by GitHub
parent 15fe049a99
commit 6b773553fc
3 changed files with 99 additions and 15 deletions
@@ -7,10 +7,31 @@ namespace slow_pwm {
static const char *const TAG = "output.slow_pwm";
void SlowPWMOutput::setup() {
this->pin_->setup();
if (this->pin_)
this->pin_->setup();
this->turn_off();
}
/// turn on/off the configured output
void SlowPWMOutput::set_output_state_(bool new_state) {
if (this->pin_) {
this->pin_->digital_write(new_state);
}
if (new_state != current_state_) {
if (this->state_change_trigger_) {
this->state_change_trigger_->trigger(new_state);
}
if (new_state) {
if (this->turn_on_trigger_)
this->turn_on_trigger_->trigger();
} else {
if (this->turn_off_trigger_)
this->turn_off_trigger_->trigger();
}
current_state_ = new_state;
}
}
void SlowPWMOutput::loop() {
uint32_t now = millis();
float scaled_state = this->state_ * this->period_;
@@ -21,20 +42,24 @@ void SlowPWMOutput::loop() {
}
if (scaled_state > now - this->period_start_time_) {
this->pin_->digital_write(true);
this->set_output_state_(true);
} else {
this->pin_->digital_write(false);
this->set_output_state_(false);
}
}
void SlowPWMOutput::dump_config() {
ESP_LOGCONFIG(TAG, "Slow PWM Output:");
LOG_PIN(" Pin: ", this->pin_);
if (this->state_change_trigger_)
ESP_LOGCONFIG(TAG, " State change automation configured");
if (this->turn_on_trigger_)
ESP_LOGCONFIG(TAG, " Turn on automation configured");
if (this->turn_off_trigger_)
ESP_LOGCONFIG(TAG, " Turn off automation configured");
ESP_LOGCONFIG(TAG, " Period: %d ms", this->period_);
LOG_FLOAT_OUTPUT(this);
}
void SlowPWMOutput::write_state(float state) { this->state_ = state; }
} // namespace slow_pwm
} // namespace esphome