8#include <core_esp8266_waveform.h>
17static const char *
const TAG =
"ac_dimmer";
28static constexpr uint32_t GATE_ENABLE_TIME = 50;
32static constexpr uint32_t TIMER_FREQUENCY_HZ = 1000000;
34static constexpr uint64_t TIMER_INTERVAL_US = 50;
46 if (this->
value == 65535 || this->
value == 0) {
63 return this->enable_time_us - time_since_zc;
80 uint32_t min_dt_us = 1000;
82 for (
auto *dimmer : all_dimmers) {
83 if (dimmer ==
nullptr) {
87 uint32_t res = dimmer->timer_intr(now);
88 if (res != 0 && res < min_dt_us)
103 if (cycle_time > 5000) {
111 if (this->
value == 65535) {
119 }
else if (this->
value == 0) {
150 for (
auto *dimmer : all_dimmers) {
151 if (dimmer ==
nullptr)
162static HWTimer *dimmer_timer =
nullptr;
170 auto setup_zero_cross_pin =
true;
172 for (
auto &all_dimmer : all_dimmers) {
173 if (all_dimmer ==
nullptr) {
174 all_dimmer = &this->
store_;
177 if (all_dimmer->zero_cross_pin_number == this->zero_cross_pin_->get_pin()) {
178 setup_zero_cross_pin =
false;
189 if (setup_zero_cross_pin) {
207 timer_alarm(dimmer_timer, TIMER_INTERVAL_US,
true, 0);
212 state = std::acos(1 - (2 *
state)) / std::numbers::pi;
213 auto new_value =
static_cast<uint16_t
>(roundf(
state * 65535));
222 " Min Power: %.1f%%\n"
223 " Init with half cycle: %s",
225 LOG_PIN(
" Output Pin: ", this->
gate_pin_);
228 ESP_LOGCONFIG(TAG,
" Method: leading pulse");
230 ESP_LOGCONFIG(TAG,
" Method: leading");
232 ESP_LOGCONFIG(TAG,
" Method: trailing");
235 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
void timer_attach_interrupt(HWTimer *timer, voidFuncPtr user_func)
void timer_alarm(HWTimer *timer, uint64_t alarm_value, bool autoreload, uint64_t reload_count)
uint32_t IRAM_ATTR HOT timer_interrupt()
Run timer interrupt code and return in how many µs the next event is expected.
HWTimer * timer_begin(uint32_t frequency)
@ DIM_METHOD_LEADING_PULSE
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)