9#include <core_esp8266_waveform.h>
11#ifdef USE_ESP32_FRAMEWORK_ARDUINO
12#include <esp32-hal-timer.h>
18static const char *
const TAG =
"ac_dimmer";
29static const uint32_t GATE_ENABLE_TIME = 50;
40 if (this->
value == 65535 || this->
value == 0) {
57 return this->enable_time_us - time_since_zc;
74 uint32_t min_dt_us = 1000;
76 for (
auto *dimmer : all_dimmers) {
77 if (dimmer ==
nullptr) {
81 uint32_t res = dimmer->timer_intr(now);
82 if (res != 0 && res < min_dt_us)
97 if (cycle_time > 5000) {
105 if (this->
value == 65535) {
113 }
else if (this->
value == 0) {
144 for (
auto *dimmer : all_dimmers) {
145 if (dimmer ==
nullptr)
156static hw_timer_t *dimmer_timer =
nullptr;
164 auto setup_zero_cross_pin =
true;
166 for (
auto &all_dimmer : all_dimmers) {
167 if (all_dimmer ==
nullptr) {
168 all_dimmer = &this->
store_;
171 if (all_dimmer->zero_cross_pin_number == this->zero_cross_pin_->get_pin()) {
172 setup_zero_cross_pin =
false;
183 if (setup_zero_cross_pin) {
197 dimmer_timer = timerBegin(0, 80,
true);
202 timerAlarmWrite(dimmer_timer, 50,
true);
203 timerAlarmEnable(dimmer_timer);
208 auto new_value =
static_cast<uint16_t
>(roundf(
state * 65535));
214 ESP_LOGCONFIG(TAG,
"AcDimmer:");
215 LOG_PIN(
" Output Pin: ", this->
gate_pin_);
217 ESP_LOGCONFIG(TAG,
" Min Power: %.1f%%", this->
store_.
min_power / 10.0f);
220 ESP_LOGCONFIG(TAG,
" Method: leading pulse");
222 ESP_LOGCONFIG(TAG,
" Method: leading");
224 ESP_LOGCONFIG(TAG,
" Method: trailing");
227 LOG_FLOAT_OUTPUT(
this);
void digital_write(bool value)
virtual uint8_t get_pin() const =0
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
virtual ISRInternalGPIOPin to_isr() const =0
bool init_with_half_cycle_
void write_state(float state) override
InternalGPIOPin * gate_pin_
InternalGPIOPin * zero_cross_pin_
void dump_config() override
uint32_t IRAM_ATTR HOT timer_interrupt()
Run timer interrupt code and return in how many µs the next event is expected.
@ DIM_METHOD_LEADING_PULSE
Providing packet encoding functions for exchanging data with a remote host.
uint32_t IRAM_ATTR HOT micros()
uint32_t cycle_time_us
Time between the last two ZC pulses.
static void s_timer_intr()
bool init_cycle
Set to send the first half ac cycle complete.
uint32_t disable_time_us
Time since last ZC pulse to disable gate pin. 0 means no disable.
uint32_t enable_time_us
Time since last ZC pulse to enable gate pin. 0 means not set.
uint16_t min_power
Minimum power for activation.
uint32_t crossed_zero_at
Time (in micros()) of last ZC signal.
uint8_t zero_cross_pin_number
Zero-cross pin number - used to share ZC pin across multiple dimmers.
uint16_t value
Value of the dimmer - 0 to 65535.
uint32_t timer_intr(uint32_t now)
Function called from timer interrupt Input is current time in microseconds (micros()) Returns when ne...
ISRInternalGPIOPin gate_pin
Output pin to write to.
ISRInternalGPIOPin zero_cross_pin
Zero-cross pin.
DimMethod method
Dimmer method.
void gpio_intr()
GPIO interrupt routine, called when ZC pin triggers.
static void s_gpio_intr(AcDimmerDataStore *store)