Sigma delta fix (#4911)

This commit is contained in:
PlainTechEnthusiast 2023-07-17 20:49:04 -04:00 committed by Jesse Hills
parent f840eee1b7
commit 036e14ab7f
No known key found for this signature in database
GPG key ID: BEAAE804EFD8E83A
2 changed files with 63 additions and 25 deletions

View file

@ -0,0 +1,57 @@
#include "sigma_delta_output.h"
#include "esphome/core/log.h"
namespace esphome {
namespace sigma_delta_output {
static const char *const TAG = "output.sigma_delta";
void SigmaDeltaOutput::setup() {
if (this->pin_)
this->pin_->setup();
}
void SigmaDeltaOutput::dump_config() {
ESP_LOGCONFIG(TAG, "Sigma Delta 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");
}
LOG_UPDATE_INTERVAL(this);
LOG_FLOAT_OUTPUT(this);
}
void SigmaDeltaOutput::update() {
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();
}
}
}
} // namespace sigma_delta_output
} // namespace esphome

View file

@ -1,9 +1,12 @@
#pragma once #pragma once
#include "esphome/core/automation.h"
#include "esphome/core/component.h" #include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/components/output/float_output.h" #include "esphome/components/output/float_output.h"
namespace esphome { namespace esphome {
namespace sigma_delta_output { namespace sigma_delta_output {
class SigmaDeltaOutput : public PollingComponent, public output::FloatOutput { class SigmaDeltaOutput : public PollingComponent, public output::FloatOutput {
public: public:
Trigger<> *get_turn_on_trigger() { 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 set_pin(GPIOPin *pin) { this->pin_ = pin; };
void write_state(float state) override { this->state_ = state; } void write_state(float state) override { this->state_ = state; }
void update() override { void setup() override;
this->accum_ += this->state_; void dump_config() override;
const bool next_value = this->accum_ > 0; void update() override;
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();
}
}
}
protected: protected:
GPIOPin *pin_{nullptr}; GPIOPin *pin_{nullptr};