From 74aca2137b22b3c9f650cf7e9595cada06c2f611 Mon Sep 17 00:00:00 2001 From: Nick Whyte Date: Sun, 8 Dec 2019 03:15:04 +1100 Subject: [PATCH] Add duty cycle output component (#894) * Add duty cycle output component * cleanup + tests * format * duty_cycle -> slow_pwm * . * clang-format * ESP_LOGD -> ESPLOGVV Co-Authored-By: Otto Winter --- esphome/components/slow_pwm/__init__.py | 0 esphome/components/slow_pwm/output.py | 29 ++++++++++++++ .../components/slow_pwm/slow_pwm_output.cpp | 40 +++++++++++++++++++ esphome/components/slow_pwm/slow_pwm_output.h | 32 +++++++++++++++ esphome/const.py | 1 + tests/test1.yaml | 4 ++ 6 files changed, 106 insertions(+) create mode 100644 esphome/components/slow_pwm/__init__.py create mode 100644 esphome/components/slow_pwm/output.py create mode 100644 esphome/components/slow_pwm/slow_pwm_output.cpp create mode 100644 esphome/components/slow_pwm/slow_pwm_output.h diff --git a/esphome/components/slow_pwm/__init__.py b/esphome/components/slow_pwm/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/esphome/components/slow_pwm/output.py b/esphome/components/slow_pwm/output.py new file mode 100644 index 0000000000..f7b26a953a --- /dev/null +++ b/esphome/components/slow_pwm/output.py @@ -0,0 +1,29 @@ +from esphome import pins, core +from esphome.components import output +import esphome.config_validation as cv +import esphome.codegen as cg +from esphome.const import CONF_ID, CONF_PIN, CONF_PERIOD + +slow_pwm_ns = cg.esphome_ns.namespace("slow_pwm") +SlowPWMOutput = slow_pwm_ns.class_("SlowPWMOutput", output.FloatOutput, cg.Component) + +CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend( + { + cv.Required(CONF_ID): cv.declare_id(SlowPWMOutput), + cv.Required(CONF_PIN): pins.gpio_output_pin_schema, + cv.Required(CONF_PERIOD): cv.All( + cv.positive_time_period_milliseconds, + cv.Range(min=core.TimePeriod(milliseconds=100)), + ), + } +).extend(cv.COMPONENT_SCHEMA) + + +def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + yield cg.register_component(var, config) + yield output.register_output(var, config) + + pin = yield cg.gpio_pin_expression(config[CONF_PIN]) + cg.add(var.set_pin(pin)) + cg.add(var.set_period(config[CONF_PERIOD])) diff --git a/esphome/components/slow_pwm/slow_pwm_output.cpp b/esphome/components/slow_pwm/slow_pwm_output.cpp new file mode 100644 index 0000000000..04a0d86bf7 --- /dev/null +++ b/esphome/components/slow_pwm/slow_pwm_output.cpp @@ -0,0 +1,40 @@ +#include "slow_pwm_output.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace slow_pwm { + +static const char *TAG = "output.slow_pwm"; + +void SlowPWMOutput::setup() { + this->pin_->setup(); + this->turn_off(); +} + +void SlowPWMOutput::loop() { + unsigned long now = millis(); + float scaled_state = this->state_ * this->period_; + + if (now - this->period_start_time_ > this->period_) { + ESP_LOGVV(TAG, "End of period. State: %f, Scaled state: %f", this->state_, scaled_state); + this->period_start_time_ += this->period_; + } + + if (scaled_state > now - this->period_start_time_) { + this->pin_->digital_write(true); + } else { + this->pin_->digital_write(false); + } +} + +void SlowPWMOutput::dump_config() { + ESP_LOGCONFIG(TAG, "Slow PWM Output:"); + LOG_PIN(" Pin: ", this->pin_); + 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 diff --git a/esphome/components/slow_pwm/slow_pwm_output.h b/esphome/components/slow_pwm/slow_pwm_output.h new file mode 100644 index 0000000000..4a2c1d0a14 --- /dev/null +++ b/esphome/components/slow_pwm/slow_pwm_output.h @@ -0,0 +1,32 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/core/esphal.h" +#include "esphome/components/output/float_output.h" + +namespace esphome { +namespace slow_pwm { + +class SlowPWMOutput : public output::FloatOutput, public Component { + public: + void set_pin(GPIOPin *pin) { pin_ = pin; }; + void set_period(unsigned int period) { period_ = period; }; + + /// Initialize pin + void setup() override; + void dump_config() override; + /// HARDWARE setup_priority + float get_setup_priority() const override { return setup_priority::HARDWARE; } + + protected: + void write_state(float state) override; + void loop() override; + + GPIOPin *pin_; + float state_{0}; + unsigned int period_start_time_{0}; + unsigned int period_{5000}; +}; + +} // namespace slow_pwm +} // namespace esphome diff --git a/esphome/const.py b/esphome/const.py index e225862e70..0790a49552 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -317,6 +317,7 @@ CONF_PASSWORD = 'password' CONF_PAYLOAD = 'payload' CONF_PAYLOAD_AVAILABLE = 'payload_available' CONF_PAYLOAD_NOT_AVAILABLE = 'payload_not_available' +CONF_PERIOD = 'period' CONF_PHASE_BALANCER = 'phase_balancer' CONF_PIN = 'pin' CONF_PIN_A = 'pin_a' diff --git a/tests/test1.yaml b/tests/test1.yaml index b7f3d04b40..1080339b67 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -995,6 +995,10 @@ output: - platform: my9231 id: my_5 channel: 5 + - platform: slow_pwm + id: id24 + pin: GPIO26 + period: 15s light: - platform: binary