9 #include <core_esp8266_waveform.h> 11 #ifdef USE_ESP32_FRAMEWORK_ARDUINO 12 #include <esp32-hal-timer.h> 18 static const char *
const TAG =
"ac_dimmer";
29 static 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) {
143 for (
auto *dimmer : all_dimmers) {
144 if (dimmer ==
nullptr)
155 static hw_timer_t *dimmer_timer =
nullptr;
163 auto setup_zero_cross_pin =
true;
165 for (
auto &all_dimmer : all_dimmers) {
166 if (all_dimmer ==
nullptr) {
167 all_dimmer = &this->store_;
170 if (all_dimmer->zero_cross_pin_number == this->zero_cross_pin_->get_pin()) {
171 setup_zero_cross_pin =
false;
175 this->gate_pin_->setup();
176 this->store_.gate_pin = this->gate_pin_->to_isr();
177 this->store_.zero_cross_pin_number = this->zero_cross_pin_->get_pin();
178 this->store_.min_power =
static_cast<uint16_t
>(this->min_power_ * 1000);
179 this->min_power_ = 0;
180 this->store_.method = this->method_;
182 if (setup_zero_cross_pin) {
183 this->zero_cross_pin_->setup();
184 this->store_.zero_cross_pin = this->zero_cross_pin_->to_isr();
196 dimmer_timer = timerBegin(0, 80,
true);
201 timerAlarmWrite(dimmer_timer, 50,
true);
202 timerAlarmEnable(dimmer_timer);
206 state = std::acos(1 - (2 * state)) / 3.14159;
207 auto new_value =
static_cast<uint16_t
>(roundf(state * 65535));
208 if (new_value != 0 && this->store_.value == 0)
209 this->store_.init_cycle = this->init_with_half_cycle_;
210 this->store_.value = new_value;
213 ESP_LOGCONFIG(TAG,
"AcDimmer:");
214 LOG_PIN(
" Output Pin: ", this->gate_pin_);
215 LOG_PIN(
" Zero-Cross Pin: ", this->zero_cross_pin_);
216 ESP_LOGCONFIG(TAG,
" Min Power: %.1f%%", this->store_.min_power / 10.0f);
217 ESP_LOGCONFIG(TAG,
" Init with half cycle: %s", YESNO(this->init_with_half_cycle_));
219 ESP_LOGCONFIG(TAG,
" Method: leading pulse");
221 ESP_LOGCONFIG(TAG,
" Method: leading");
223 ESP_LOGCONFIG(TAG,
" Method: trailing");
226 LOG_FLOAT_OUTPUT(
this);
227 ESP_LOGV(TAG,
" Estimated Frequency: %.3fHz", 1e6f / this->store_.cycle_time_us / 2);
233 #endif // USE_ARDUINO uint32_t IRAM_ATTR HOT timer_interrupt()
Run timer interrupt code and return in how many µs the next event is expected.
uint32_t enable_time_us
Time since last ZC pulse to enable gate pin. 0 means not set.
uint32_t cycle_time_us
Time between the last two ZC pulses.
uint32_t crossed_zero_at
Time (in micros()) of last ZC signal.
uint32_t IRAM_ATTR HOT micros()
uint16_t min_power
Minimum power for activation.
DimMethod method
Dimmer method.
bool init_cycle
Set to send the first half ac cycle complete.
void write_state(float state) override
static void s_gpio_intr(AcDimmerDataStore *store)
static void s_timer_intr()
uint32_t timer_intr(uint32_t now)
Function called from timer interrupt Input is current time in microseconds (micros()) Returns when ne...
uint16_t value
Value of the dimmer - 0 to 65535.
ISRInternalGPIOPin gate_pin
Output pin to write to.
void dump_config() override
Implementation of SPI Controller mode.
uint32_t disable_time_us
Time since last ZC pulse to disable gate pin. 0 means no disable.
void digital_write(bool value)
void gpio_intr()
GPIO interrupt routine, called when ZC pin triggers.
uint8_t zero_cross_pin_number
Zero-cross pin number - used to share ZC pin across multiple dimmers.