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 <otto@otto-winter.com>
This commit is contained in:
Nick Whyte 2019-12-08 03:15:04 +11:00 committed by Otto Winter
parent d09dff3ae3
commit 74aca2137b
6 changed files with 106 additions and 0 deletions

View file

View file

@ -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]))

View file

@ -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

View file

@ -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

View file

@ -317,6 +317,7 @@ CONF_PASSWORD = 'password'
CONF_PAYLOAD = 'payload' CONF_PAYLOAD = 'payload'
CONF_PAYLOAD_AVAILABLE = 'payload_available' CONF_PAYLOAD_AVAILABLE = 'payload_available'
CONF_PAYLOAD_NOT_AVAILABLE = 'payload_not_available' CONF_PAYLOAD_NOT_AVAILABLE = 'payload_not_available'
CONF_PERIOD = 'period'
CONF_PHASE_BALANCER = 'phase_balancer' CONF_PHASE_BALANCER = 'phase_balancer'
CONF_PIN = 'pin' CONF_PIN = 'pin'
CONF_PIN_A = 'pin_a' CONF_PIN_A = 'pin_a'

View file

@ -995,6 +995,10 @@ output:
- platform: my9231 - platform: my9231
id: my_5 id: my_5
channel: 5 channel: 5
- platform: slow_pwm
id: id24
pin: GPIO26
period: 15s
light: light:
- platform: binary - platform: binary