mirror of
https://github.com/esphome/esphome.git
synced 2025-09-26 15:12:21 +01:00
Cleanup dashboard JS (#491)
* Cleanup dashboard JS * Add vscode * Save start_mark/end_mark * Updates * Updates * Remove need for cv.nameable It's a bit hacky but removes so much bloat from integrations * Add enum helper * Document APIs, and Improvements * Fixes * Fixes * Update PULL_REQUEST_TEMPLATE.md * Updates * Updates * Updates
This commit is contained in:
@@ -25,6 +25,8 @@ void ESP8266PWM::dump_config() {
|
||||
LOG_FLOAT_OUTPUT(this);
|
||||
}
|
||||
void HOT ESP8266PWM::write_state(float state) {
|
||||
this->last_output_ = state;
|
||||
|
||||
// Also check pin inversion
|
||||
if (this->pin_->is_inverted()) {
|
||||
state = 1.0f - state;
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/esphal.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/output/float_output.h"
|
||||
|
||||
namespace esphome {
|
||||
@@ -9,9 +10,13 @@ namespace esp8266_pwm {
|
||||
|
||||
class ESP8266PWM : public output::FloatOutput, public Component {
|
||||
public:
|
||||
explicit ESP8266PWM(GPIOPin *pin) : pin_(pin) {}
|
||||
|
||||
void set_pin(GPIOPin *pin) { pin_ = pin; }
|
||||
void set_frequency(float frequency) { this->frequency_ = frequency; }
|
||||
/// Dynamically update frequency
|
||||
void update_frequency(float frequency) {
|
||||
this->set_frequency(frequency);
|
||||
this->write_state(this->last_output_);
|
||||
}
|
||||
|
||||
/// Initialize pin
|
||||
void setup() override;
|
||||
@@ -24,6 +29,22 @@ class ESP8266PWM : public output::FloatOutput, public Component {
|
||||
|
||||
GPIOPin *pin_;
|
||||
float frequency_{1000.0};
|
||||
/// Cache last output level for dynamic frequency updating
|
||||
float last_output_{0.0};
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetFrequencyAction : public Action<Ts...> {
|
||||
public:
|
||||
SetFrequencyAction(ESP8266PWM *parent) : parent_(parent) {}
|
||||
TEMPLATABLE_VALUE(float, frequency);
|
||||
|
||||
void play(Ts... x) {
|
||||
float freq = this->frequency_.value(x...);
|
||||
this->parent_->update_frequency(freq);
|
||||
}
|
||||
|
||||
protected:
|
||||
ESP8266PWM *parent_;
|
||||
};
|
||||
|
||||
} // namespace esp8266_pwm
|
||||
|
@@ -1,4 +1,4 @@
|
||||
from esphome import pins
|
||||
from esphome import pins, automation
|
||||
from esphome.components import output
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
@@ -15,18 +15,34 @@ def valid_pwm_pin(value):
|
||||
|
||||
esp8266_pwm_ns = cg.esphome_ns.namespace('esp8266_pwm')
|
||||
ESP8266PWM = esp8266_pwm_ns.class_('ESP8266PWM', output.FloatOutput, cg.Component)
|
||||
SetFrequencyAction = esp8266_pwm_ns.class_('SetFrequencyAction', automation.Action)
|
||||
validate_frequency = cv.All(cv.frequency, cv.Range(min=1.0e-6))
|
||||
|
||||
CONFIG_SCHEMA = output.FLOAT_OUTPUT_SCHEMA.extend({
|
||||
cv.Required(CONF_ID): cv.declare_variable_id(ESP8266PWM),
|
||||
cv.Required(CONF_ID): cv.declare_id(ESP8266PWM),
|
||||
cv.Required(CONF_PIN): cv.All(pins.internal_gpio_output_pin_schema, valid_pwm_pin),
|
||||
cv.Optional(CONF_FREQUENCY, default='1kHz'): cv.All(cv.frequency, cv.Range(min=1.0e-6)),
|
||||
cv.Optional(CONF_FREQUENCY, default='1kHz'): validate_frequency,
|
||||
}).extend(cv.COMPONENT_SCHEMA)
|
||||
|
||||
|
||||
def to_code(config):
|
||||
pin = yield cg.gpio_pin_expression(config[CONF_PIN])
|
||||
var = cg.new_Pvariable(config[CONF_ID], pin)
|
||||
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_frequency(config[CONF_FREQUENCY]))
|
||||
|
||||
|
||||
@automation.register_action('output.esp8266_pwm.set_frequency', SetFrequencyAction, cv.Schema({
|
||||
cv.Required(CONF_ID): cv.use_id(ESP8266PWM),
|
||||
cv.Required(CONF_FREQUENCY): cv.templatable(validate_frequency),
|
||||
}))
|
||||
def esp8266_set_frequency_to_code(config, action_id, template_arg, args):
|
||||
paren = yield cg.get_variable(config[CONF_ID])
|
||||
var = cg.new_Pvariable(action_id, template_arg, paren)
|
||||
template_ = yield cg.templatable(config[CONF_FREQUENCY], args, float)
|
||||
cg.add(var.set_frequency(template_))
|
||||
yield var
|
||||
|
Reference in New Issue
Block a user