diff --git a/tasmota/core_esp8266_wiring_pwm.cpp b/tasmota/core_esp8266_wiring_pwm.cpp index d81928a5d..d879a0001 100644 --- a/tasmota/core_esp8266_wiring_pwm.cpp +++ b/tasmota/core_esp8266_wiring_pwm.cpp @@ -29,15 +29,21 @@ extern "C" { static uint32_t analogMap = 0; -static int32_t analogScale = PWMRANGE; +static int32_t analogScale = 255; // Match upstream default, breaking change from 2.x.x static uint16_t analogFreq = 1000; extern void __analogWriteRange(uint32_t range) { - if (range > 0) { + if ((range >= 15) && (range <= 65535)) { analogScale = range; } } +extern void __analogWriteResolution(int res) { + if ((res >= 4) && (res <= 16)) { + analogScale = (1 << res) - 1; + } +} + extern void __analogWriteFreq(uint32_t freq) { if (freq < 40) { analogFreq = 40; @@ -61,6 +67,10 @@ extern void __analogWrite(uint8_t pin, int val) { val = analogScale; } + // Per the Arduino docs at https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/ + // val: the duty cycle: between 0 (always off) and 255 (always on). + // So if val = 0 we have digitalWrite(LOW), if we have val==range we have digitalWrite(HIGH) + if (analogMap & 1UL << pin) { analogMap &= ~(1 << pin); } @@ -79,6 +89,7 @@ extern void __analogWrite(uint8_t pin, int val) { extern void analogWrite(uint8_t pin, int val) __attribute__((weak, alias("__analogWrite"))); extern void analogWriteFreq(uint32_t freq) __attribute__((weak, alias("__analogWriteFreq"))); extern void analogWriteRange(uint32_t range) __attribute__((weak, alias("__analogWriteRange"))); +extern void analogWriteResolution(int res) __attribute__((weak, alias("__analogWriteResolution"))); };