diff --git a/tasmota/xsns_01_counter.ino b/tasmota/xsns_01_counter.ino index 2b0576b80..d87a88850 100644 --- a/tasmota/xsns_01_counter.ino +++ b/tasmota/xsns_01_counter.ino @@ -283,36 +283,40 @@ void SyncACDimmer(void) // Calulate additional or less clockcycles to move current phase position to should be position phaseShift_ClockCycles = (int32_t)((int32_t)phaseStart_ToBeClockCycles-(int32_t)phaseStart_ActualClockCycles)/100; - #ifdef ESP8266 - if ( ac_zero_cross_dimmer.PWM_ON[i] == 0 ) { - // because in LOOP calculate the timelag to fire PWM correctly with zero-cross - uint32_t timelag_ClockCycles = (ESP.getCycleCount() - ac_zero_cross_dimmer.currentPWMCycleCount[3])%ac_zero_cross_dimmer.tobe_cycle_timeClockCycles; - timelag_ClockCycles = ((phaseStart_ToBeClockCycles + ac_zero_cross_dimmer.tobe_cycle_timeClockCycles) - timelag_ClockCycles)%ac_zero_cross_dimmer.tobe_cycle_timeClockCycles; - delayMicroseconds(clockCyclesToMicroseconds(timelag_ClockCycles)); - ac_zero_cross_dimmer.PWM_ON[i]=true; - pinMode(Pin(GPIO_PWM1, i), OUTPUT); - } else { - ac_zero_cross_dimmer.currentShiftClockCycle[i] += phaseShift_ClockCycles > 5 ? 1 : (phaseShift_ClockCycles < -5 ? -1 : 0); - ac_zero_cross_dimmer.current_cycle_ClockCycles += ac_zero_cross_dimmer.currentShiftClockCycle[i]+phaseShift_ClockCycles; - } + if ( ac_zero_cross_dimmer.PWM_ON[i] == 0 ) { + // because in LOOP calculate the timelag to fire PWM correctly with zero-cross + uint32_t timelag_ClockCycles = (ESP.getCycleCount() - ac_zero_cross_dimmer.currentPWMCycleCount[3])%ac_zero_cross_dimmer.tobe_cycle_timeClockCycles; + timelag_ClockCycles = ((phaseStart_ToBeClockCycles + ac_zero_cross_dimmer.tobe_cycle_timeClockCycles) - timelag_ClockCycles)%ac_zero_cross_dimmer.tobe_cycle_timeClockCycles; - // Find the first GPIO being generated by checking GCC's find-first-set (returns 1 + the bit of the first 1 in an int32_t - startWaveformClockCycles(Pin(GPIO_PWM1, i), ac_zero_cross_dimmer.high, ac_zero_cross_dimmer.current_cycle_ClockCycles - ac_zero_cross_dimmer.high, 0, -1, 0, true); - #endif // ESP8266 - #ifdef ESP32 - analogWrite(Pin(GPIO_PWM1, i), 5); - #endif // ESP32 + delayMicroseconds(clockCyclesToMicroseconds(timelag_ClockCycles)); + ac_zero_cross_dimmer.PWM_ON[i]=true; + pinMode(Pin(GPIO_PWM1, i), OUTPUT); + } else { + ac_zero_cross_dimmer.currentShiftClockCycle[i] += phaseShift_ClockCycles > 5 ? 1 : (phaseShift_ClockCycles < -5 ? -1 : 0); + ac_zero_cross_dimmer.current_cycle_ClockCycles += ac_zero_cross_dimmer.currentShiftClockCycle[i]+phaseShift_ClockCycles; + } +#ifdef ESP8266 + // Find the first GPIO being generated by checking GCC's find-first-set (returns 1 + the bit of the first 1 in an int32_t + startWaveformClockCycles(Pin(GPIO_PWM1, i), ac_zero_cross_dimmer.high, ac_zero_cross_dimmer.current_cycle_ClockCycles - ac_zero_cross_dimmer.high, 0, -1, 0, true); +#endif // ESP8266 +#ifdef ESP32 + double esp32freq = 1000000.0 / clockCyclesToMicroseconds(ac_zero_cross_dimmer.current_cycle_ClockCycles); + ledcSetup(i, esp32freq, 10); + ledcAttachPin(Pin(GPIO_PWM1, i), i); + ledcWrite(i, 5); + +#endif // ESP32 AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("CNT: [%d], shift: %d, dimm_time_CCs %d, phaseShift_CCs %d, currentPWMcylce: %lu, current_cycle_CC: %lu, lastcc %lu, currentSteps %lu, currDIM %lu, last delta:%lu"), i, ac_zero_cross_dimmer.currentShiftClockCycle[i], phaseStart_ToBeClockCycles,phaseShift_ClockCycles,ac_zero_cross_dimmer.currentPWMCycleCount[i],ac_zero_cross_dimmer.current_cycle_ClockCycles , ac_zero_cross_dimmer.lastCycleCount, ac_zero_cross_dimmer.currentSteps, Light.fade_cur_10[i],phaseStart_ActualClockCycles); // Light fading //AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("CNT: [%d], curr: %d, final: %d, fading: %d, phase-shift: %d, ON/OFF: %d"),i, Light.fade_cur_10[i], Light.fade_start_10[i], Light.fade_running, phaseStart_ToBeClockCycles,ac_zero_cross_dimmer.PWM_ON[i]); - } - } - } -} + } // do sync onchannel + } // loop on counter + } // zero cross detected +} // end SyncACDimmer #endif //USE_AC_ZERO_CROSS_DIMMER /*********************************************************************************************\