mirror of
https://github.com/esphome/esphome.git
synced 2024-11-10 01:07:45 +01:00
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:
parent
15fe049a99
commit
6b773553fc
3 changed files with 99 additions and 15 deletions
|
@ -2,15 +2,37 @@ from esphome import pins, core
|
||||||
from esphome.components import output
|
from esphome.components import output
|
||||||
import esphome.config_validation as cv
|
import esphome.config_validation as cv
|
||||||
import esphome.codegen as cg
|
import esphome.codegen as cg
|
||||||
from esphome.const import CONF_ID, CONF_PIN, CONF_PERIOD
|
from esphome import automation
|
||||||
|
from esphome.const import (
|
||||||
|
CONF_ID,
|
||||||
|
CONF_PIN,
|
||||||
|
CONF_PERIOD,
|
||||||
|
CONF_TURN_ON_ACTION,
|
||||||
|
CONF_TURN_OFF_ACTION,
|
||||||
|
)
|
||||||
|
|
||||||
slow_pwm_ns = cg.esphome_ns.namespace("slow_pwm")
|
slow_pwm_ns = cg.esphome_ns.namespace("slow_pwm")
|
||||||
SlowPWMOutput = slow_pwm_ns.class_("SlowPWMOutput", output.FloatOutput, cg.Component)
|
SlowPWMOutput = slow_pwm_ns.class_("SlowPWMOutput", output.FloatOutput, cg.Component)
|
||||||
|
|
||||||
|
CONF_STATE_CHANGE_ACTION = "state_change_action"
|
||||||
|
|
||||||
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
cv.Required(CONF_ID): cv.declare_id(SlowPWMOutput),
|
cv.Required(CONF_ID): cv.declare_id(SlowPWMOutput),
|
||||||
cv.Required(CONF_PIN): pins.gpio_output_pin_schema,
|
cv.Optional(CONF_PIN): pins.gpio_output_pin_schema,
|
||||||
|
cv.Inclusive(
|
||||||
|
CONF_TURN_ON_ACTION,
|
||||||
|
"on_off",
|
||||||
|
f"{CONF_TURN_ON_ACTION} and {CONF_TURN_OFF_ACTION} must both be defined",
|
||||||
|
): automation.validate_automation(single=True),
|
||||||
|
cv.Inclusive(
|
||||||
|
CONF_TURN_OFF_ACTION,
|
||||||
|
"on_off",
|
||||||
|
f"{CONF_TURN_ON_ACTION} and {CONF_TURN_OFF_ACTION} must both be defined",
|
||||||
|
): automation.validate_automation(single=True),
|
||||||
|
cv.Optional(CONF_STATE_CHANGE_ACTION): automation.validate_automation(
|
||||||
|
single=True
|
||||||
|
),
|
||||||
cv.Required(CONF_PERIOD): cv.All(
|
cv.Required(CONF_PERIOD): cv.All(
|
||||||
cv.positive_time_period_milliseconds,
|
cv.positive_time_period_milliseconds,
|
||||||
cv.Range(min=core.TimePeriod(milliseconds=100)),
|
cv.Range(min=core.TimePeriod(milliseconds=100)),
|
||||||
|
@ -23,7 +45,21 @@ async def to_code(config):
|
||||||
var = cg.new_Pvariable(config[CONF_ID])
|
var = cg.new_Pvariable(config[CONF_ID])
|
||||||
await cg.register_component(var, config)
|
await cg.register_component(var, config)
|
||||||
await output.register_output(var, config)
|
await output.register_output(var, config)
|
||||||
|
if CONF_PIN in config:
|
||||||
pin = await cg.gpio_pin_expression(config[CONF_PIN])
|
pin = await cg.gpio_pin_expression(config[CONF_PIN])
|
||||||
cg.add(var.set_pin(pin))
|
cg.add(var.set_pin(pin))
|
||||||
|
if CONF_STATE_CHANGE_ACTION in config:
|
||||||
|
await automation.build_automation(
|
||||||
|
var.get_state_change_trigger(),
|
||||||
|
[(bool, "state")],
|
||||||
|
config[CONF_STATE_CHANGE_ACTION],
|
||||||
|
)
|
||||||
|
if CONF_TURN_ON_ACTION in config:
|
||||||
|
await automation.build_automation(
|
||||||
|
var.get_turn_on_trigger(), [], config[CONF_TURN_ON_ACTION]
|
||||||
|
)
|
||||||
|
await automation.build_automation(
|
||||||
|
var.get_turn_off_trigger(), [], config[CONF_TURN_OFF_ACTION]
|
||||||
|
)
|
||||||
|
|
||||||
cg.add(var.set_period(config[CONF_PERIOD]))
|
cg.add(var.set_period(config[CONF_PERIOD]))
|
||||||
|
|
|
@ -7,10 +7,31 @@ namespace slow_pwm {
|
||||||
static const char *const TAG = "output.slow_pwm";
|
static const char *const TAG = "output.slow_pwm";
|
||||||
|
|
||||||
void SlowPWMOutput::setup() {
|
void SlowPWMOutput::setup() {
|
||||||
|
if (this->pin_)
|
||||||
this->pin_->setup();
|
this->pin_->setup();
|
||||||
this->turn_off();
|
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() {
|
void SlowPWMOutput::loop() {
|
||||||
uint32_t now = millis();
|
uint32_t now = millis();
|
||||||
float scaled_state = this->state_ * this->period_;
|
float scaled_state = this->state_ * this->period_;
|
||||||
|
@ -21,20 +42,24 @@ void SlowPWMOutput::loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (scaled_state > now - this->period_start_time_) {
|
if (scaled_state > now - this->period_start_time_) {
|
||||||
this->pin_->digital_write(true);
|
this->set_output_state_(true);
|
||||||
} else {
|
} else {
|
||||||
this->pin_->digital_write(false);
|
this->set_output_state_(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SlowPWMOutput::dump_config() {
|
void SlowPWMOutput::dump_config() {
|
||||||
ESP_LOGCONFIG(TAG, "Slow PWM Output:");
|
ESP_LOGCONFIG(TAG, "Slow PWM Output:");
|
||||||
LOG_PIN(" Pin: ", this->pin_);
|
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_);
|
ESP_LOGCONFIG(TAG, " Period: %d ms", this->period_);
|
||||||
LOG_FLOAT_OUTPUT(this);
|
LOG_FLOAT_OUTPUT(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SlowPWMOutput::write_state(float state) { this->state_ = state; }
|
|
||||||
|
|
||||||
} // namespace slow_pwm
|
} // namespace slow_pwm
|
||||||
} // namespace esphome
|
} // namespace esphome
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#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/core/hal.h"
|
||||||
#include "esphome/components/output/float_output.h"
|
#include "esphome/components/output/float_output.h"
|
||||||
|
@ -11,19 +11,42 @@ class SlowPWMOutput : public output::FloatOutput, public Component {
|
||||||
public:
|
public:
|
||||||
void set_pin(GPIOPin *pin) { pin_ = pin; };
|
void set_pin(GPIOPin *pin) { pin_ = pin; };
|
||||||
void set_period(unsigned int period) { period_ = period; };
|
void set_period(unsigned int period) { period_ = period; };
|
||||||
|
|
||||||
/// Initialize pin
|
/// Initialize pin
|
||||||
void setup() override;
|
void setup() override;
|
||||||
void dump_config() override;
|
void dump_config() override;
|
||||||
/// HARDWARE setup_priority
|
/// HARDWARE setup_priority
|
||||||
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
float get_setup_priority() const override { return setup_priority::HARDWARE; }
|
||||||
|
|
||||||
protected:
|
Trigger<> *get_turn_on_trigger() {
|
||||||
void write_state(float state) override;
|
// Lazy create
|
||||||
void loop() override;
|
if (!this->turn_on_trigger_)
|
||||||
|
this->turn_on_trigger_ = make_unique<Trigger<>>();
|
||||||
|
return this->turn_on_trigger_.get();
|
||||||
|
}
|
||||||
|
Trigger<> *get_turn_off_trigger() {
|
||||||
|
if (!this->turn_off_trigger_)
|
||||||
|
this->turn_off_trigger_ = make_unique<Trigger<>>();
|
||||||
|
return this->turn_off_trigger_.get();
|
||||||
|
}
|
||||||
|
|
||||||
GPIOPin *pin_;
|
Trigger<bool> *get_state_change_trigger() {
|
||||||
|
if (!this->state_change_trigger_)
|
||||||
|
this->state_change_trigger_ = make_unique<Trigger<bool>>();
|
||||||
|
return this->state_change_trigger_.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void loop() override;
|
||||||
|
void write_state(float state) override { state_ = state; }
|
||||||
|
/// turn on/off the configured output
|
||||||
|
void set_output_state_(bool state);
|
||||||
|
|
||||||
|
GPIOPin *pin_{nullptr};
|
||||||
|
std::unique_ptr<Trigger<>> turn_on_trigger_{nullptr};
|
||||||
|
std::unique_ptr<Trigger<>> turn_off_trigger_{nullptr};
|
||||||
|
std::unique_ptr<Trigger<bool>> state_change_trigger_{nullptr};
|
||||||
float state_{0};
|
float state_{0};
|
||||||
|
bool current_state_{false};
|
||||||
unsigned int period_start_time_{0};
|
unsigned int period_start_time_{0};
|
||||||
unsigned int period_{5000};
|
unsigned int period_{5000};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue