Sigma delta fix (#4911)

This commit is contained in:
PlainTechEnthusiast
2023-07-17 20:49:04 -04:00
committed by GitHub
parent 3cfe1e3083
commit b0e286972d
2 changed files with 63 additions and 25 deletions
@@ -1,9 +1,12 @@
#pragma once
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/components/output/float_output.h"
namespace esphome {
namespace sigma_delta_output {
class SigmaDeltaOutput : public PollingComponent, public output::FloatOutput {
public:
Trigger<> *get_turn_on_trigger() {
@@ -25,31 +28,9 @@ class SigmaDeltaOutput : public PollingComponent, public output::FloatOutput {
void set_pin(GPIOPin *pin) { this->pin_ = pin; };
void write_state(float state) override { this->state_ = state; }
void update() override {
this->accum_ += this->state_;
const bool next_value = this->accum_ > 0;
if (next_value) {
this->accum_ -= 1.;
}
if (next_value != this->value_) {
this->value_ = next_value;
if (this->pin_) {
this->pin_->digital_write(next_value);
}
if (this->state_change_trigger_) {
this->state_change_trigger_->trigger(next_value);
}
if (next_value && this->turn_on_trigger_) {
this->turn_on_trigger_->trigger();
} else if (!next_value && this->turn_off_trigger_) {
this->turn_off_trigger_->trigger();
}
}
}
void setup() override;
void dump_config() override;
void update() override;
protected:
GPIOPin *pin_{nullptr};