Merge pull request #8833 from stefanbode/patch-14

Add support for AC Dimmer #8789
This commit is contained in:
Theo Arends 2020-07-03 10:48:14 +02:00 committed by GitHub
commit 911dc64e99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 2 deletions

View File

@ -48,10 +48,12 @@ extern void __analogWriteFreq(uint32_t freq) {
}
}
extern void __analogWrite(uint8_t pin, int val) {
if (pin > 16) {
return;
}
uint32_t analogPeriod = microsecondsToClockCycles(1000000UL) / analogFreq;
if (val < 0) {
val = 0;
@ -80,4 +82,4 @@ extern void analogWriteRange(uint32_t range) __attribute__((weak, alias("__analo
};
#endif // ESP8266
#endif // ESP8266

View File

@ -42,6 +42,8 @@ void DomoticzTempHumPressureSensor(float temp, float hum, float baro = -1);
char* ToHex_P(const unsigned char * in, size_t insz, char * out, size_t outsz, char inbetween = '\0');
extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack, uint32_t stack_end);
extern "C" void resetPins();
extern "C" int startWaveformClockCycles(uint8_t pin, uint32_t highCcys, uint32_t lowCcys,
uint32_t runTimeCcys, int8_t alignPhase, uint32_t phaseOffsetCcys, bool autoPwm);
#ifdef ESP32

View File

@ -44,6 +44,9 @@ struct COUNTER {
bool any_counter = false;
} Counter;
uint32_t last_cycle;
uint32_t cycle_time;
#ifndef ARDUINO_ESP8266_RELEASE_2_3_0 // Fix core 2.5.x ISR not in IRAM Exception
void CounterUpdate(uint8_t index) ICACHE_RAM_ATTR;
void CounterUpdate1(void) ICACHE_RAM_ATTR;
@ -75,7 +78,40 @@ void CounterUpdate(uint8_t index)
Counter.timer_low_high[index] = time;
Counter.pin_state ^= (1<<index);
// do not count on rising edge
if bitRead(Counter.pin_state, index) return;
if bitRead(Counter.pin_state, index) {
// PWMfrequency 100
// restart PWM each second (german 50Hz has to up to 0.01% deviation)
// set COUNTERDEBOUNCELOW 1 to catch the raising edge
// Zero-HIGH is typical 2ms
if (RtcSettings.pulse_counter[index]%100 == 0 && PinUsed(GPIO_PWM1, index)) {
const uint32_t current_cycle = ESP.getCycleCount();
// stop pwm on PIN to start in Sync with rising edge
// calculate timeoffset to fire PWM
uint16_t dimm_time= 10000 * (100 - light_state.getDimmer(index)) / Settings.pwm_frequency;
digitalWrite(Pin(GPIO_PWM1, index), LOW);
// 1000µs to ensure not to fire on the next sinus wave
if (dimm_time < (1000000 / Settings.pwm_frequency)-1000) {
delayMicroseconds(dimm_time);
// fire small PWM signal to start TRIAC/SSR. Kill on next
// zero phase automatic.
// calculate actual cycle time and adapt frequency im milli Hz steps
// add 100.000 cpu ticks to ensure right step calculation
uint32_t steps = (current_cycle-last_cycle+100000)/(clockCyclesPerMicrosecond() * 10000);
cycle_time = (current_cycle-last_cycle)/steps;
#ifdef ESP8266
pinMode(Pin(GPIO_PWM1, index), OUTPUT);
uint32_t high = (cycle_time * 5) / 1023;
uint32_t low = cycle_time - high;
// 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, index), high, low, 0, -1, 0, true);
#else
analogWrite(Pin(GPIO_PWM1, index), 5);
#endif
}
last_cycle = current_cycle;
}
return;
}
}
debounce_time = time - Counter.timer[index];