From efe1efb84e7aa1b712fb0d1918aa868a9e815dde Mon Sep 17 00:00:00 2001 From: Adam Bogocz Date: Sat, 16 Nov 2024 14:49:41 +0000 Subject: [PATCH] [ac_dimmer] : Support for ANY_EDGE interrupt of zero-crossing signal --- esphome/components/ac_dimmer/ac_dimmer.cpp | 6 ++++-- esphome/components/ac_dimmer/ac_dimmer.h | 4 ++++ esphome/components/ac_dimmer/output.py | 17 ++++++++++++++--- tests/components/ac_dimmer/test.esp32-ard.yaml | 1 + .../components/ac_dimmer/test.esp32-c3-ard.yaml | 1 + 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/esphome/components/ac_dimmer/ac_dimmer.cpp b/esphome/components/ac_dimmer/ac_dimmer.cpp index 5361814340..5c67111d21 100644 --- a/esphome/components/ac_dimmer/ac_dimmer.cpp +++ b/esphome/components/ac_dimmer/ac_dimmer.cpp @@ -182,8 +182,10 @@ void AcDimmer::setup() { if (setup_zero_cross_pin) { this->zero_cross_pin_->setup(); this->store_.zero_cross_pin = this->zero_cross_pin_->to_isr(); - this->zero_cross_pin_->attach_interrupt(&AcDimmerDataStore::s_gpio_intr, &this->store_, - gpio::INTERRUPT_ANY_EDGE); + this->zero_cross_pin_->attach_interrupt( + &AcDimmerDataStore::s_gpio_intr, &this->store_, + this->zero_crossing_type_ == ZeroCrossingType::ZERO_CROSSING_TYPE_RISING_EDGE ? gpio::INTERRUPT_RISING_EDGE + : gpio::INTERRUPT_ANY_EDGE); } #ifdef USE_ESP8266 diff --git a/esphome/components/ac_dimmer/ac_dimmer.h b/esphome/components/ac_dimmer/ac_dimmer.h index fd1bbc28db..0630b04051 100644 --- a/esphome/components/ac_dimmer/ac_dimmer.h +++ b/esphome/components/ac_dimmer/ac_dimmer.h @@ -11,6 +11,8 @@ namespace ac_dimmer { enum DimMethod { DIM_METHOD_LEADING_PULSE = 0, DIM_METHOD_LEADING, DIM_METHOD_TRAILING }; +enum ZeroCrossingType { ZERO_CROSSING_TYPE_RISING_EDGE, ZERO_CROSSING_TYPE_ANY_EDGE }; + struct AcDimmerDataStore { /// Zero-cross pin ISRInternalGPIOPin zero_cross_pin; @@ -53,6 +55,7 @@ class AcDimmer : public output::FloatOutput, public Component { void set_zero_cross_pin(InternalGPIOPin *zero_cross_pin) { zero_cross_pin_ = zero_cross_pin; } void set_init_with_half_cycle(bool init_with_half_cycle) { init_with_half_cycle_ = init_with_half_cycle; } void set_method(DimMethod method) { method_ = method; } + void set_zero_crossing_type(ZeroCrossingType zero_crossing_type) { this->zero_crossing_type_ = zero_crossing_type; } protected: void write_state(float state) override; @@ -62,6 +65,7 @@ class AcDimmer : public output::FloatOutput, public Component { AcDimmerDataStore store_; bool init_with_half_cycle_; DimMethod method_; + ZeroCrossingType zero_crossing_type_; }; } // namespace ac_dimmer diff --git a/esphome/components/ac_dimmer/output.py b/esphome/components/ac_dimmer/output.py index c39fc382b6..2e6d313233 100644 --- a/esphome/components/ac_dimmer/output.py +++ b/esphome/components/ac_dimmer/output.py @@ -1,8 +1,8 @@ -import esphome.codegen as cg -import esphome.config_validation as cv from esphome import pins +import esphome.codegen as cg from esphome.components import output -from esphome.const import CONF_ID, CONF_MIN_POWER, CONF_METHOD +import esphome.config_validation as cv +from esphome.const import CONF_ID, CONF_METHOD, CONF_MIN_POWER CODEOWNERS = ["@glmnet"] @@ -16,8 +16,15 @@ DIM_METHODS = { "TRAILING": DimMethod.DIM_METHOD_TRAILING, } +ZeroCrossingType = ac_dimmer_ns.enum("ZeroCrossingType") +ZC_TYPES = { + "RISING_EDGE": ZeroCrossingType.ZERO_CROSSING_TYPE_RISING_EDGE, + "ANY_EDGE": ZeroCrossingType.ZERO_CROSSING_TYPE_ANY_EDGE, +} + CONF_GATE_PIN = "gate_pin" CONF_ZERO_CROSS_PIN = "zero_cross_pin" +CONF_ZERO_CROSS_TYPE = "zero_cross_type" CONF_INIT_WITH_HALF_CYCLE = "init_with_half_cycle" CONFIG_SCHEMA = cv.All( output.FLOAT_OUTPUT_SCHEMA.extend( @@ -29,6 +36,9 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_METHOD, default="leading pulse"): cv.enum( DIM_METHODS, upper=True, space="_" ), + cv.Optional(CONF_ZERO_CROSS_TYPE, default="RISING_EDGE"): cv.enum( + ZC_TYPES, upper=True + ), } ).extend(cv.COMPONENT_SCHEMA), cv.only_with_arduino, @@ -50,3 +60,4 @@ async def to_code(config): cg.add(var.set_zero_cross_pin(pin)) cg.add(var.set_init_with_half_cycle(config[CONF_INIT_WITH_HALF_CYCLE])) cg.add(var.set_method(config[CONF_METHOD])) + cg.add(var.set_zero_crossing_type(config[CONF_ZERO_CROSS_TYPE])) diff --git a/tests/components/ac_dimmer/test.esp32-ard.yaml b/tests/components/ac_dimmer/test.esp32-ard.yaml index cc17201666..3460fdb05a 100644 --- a/tests/components/ac_dimmer/test.esp32-ard.yaml +++ b/tests/components/ac_dimmer/test.esp32-ard.yaml @@ -5,3 +5,4 @@ output: number: 12 zero_cross_pin: number: 13 + zero_cross_type: ANY_EDGE diff --git a/tests/components/ac_dimmer/test.esp32-c3-ard.yaml b/tests/components/ac_dimmer/test.esp32-c3-ard.yaml index f411d376be..6d5be47171 100644 --- a/tests/components/ac_dimmer/test.esp32-c3-ard.yaml +++ b/tests/components/ac_dimmer/test.esp32-c3-ard.yaml @@ -5,3 +5,4 @@ output: number: 5 zero_cross_pin: number: 6 + zero_cross_type: RISING_EDGE