[PWM] Fix led flickering

This commit is contained in:
chromoxdor
2026-02-27 13:51:26 +01:00
parent f7d0c83104
commit e725092984
+15 -5
View File
@@ -170,28 +170,38 @@ void detachLedChannel(int pin)
# endif // if ESP_IDF_VERSION_MAJOR < 5
uint32_t analogWriteESP32(int pin, int value, uint32_t frequency)
uint32_t analogWriteESP32(int pin, uint16_t value, uint32_t frequency)
{
static int lastPin = -1;
static uint32_t lastFreq = 0;
static int8_t ledChannel = -1;
if (value == 0) {
detachLedChannel(pin);
return 0;
}
// find existing channel if this pin has been used before
uint8_t resolution = 10;
uint8_t resolution = 16;
value = adapt_ledc_frequency_resolution_duty(frequency, resolution, value);
int8_t ledChannel = attachLedChannel(pin, frequency, resolution);
// Only update frequency & attach channel if pin/freq changed
if ((lastPin != pin) || (lastFreq != frequency)) {
value = adapt_ledc_frequency_resolution_duty(frequency, resolution, value);
ledChannel = attachLedChannel(pin, frequency, resolution);
lastPin = pin;
lastFreq = frequency;
}
if (ledChannel != -1) {
# if ESP_IDF_VERSION_MAJOR < 5
ledcWrite(ledChannel, value);
return ledChannelFreq[ledChannel];
# else // if ESP_IDF_VERSION_MAJOR < 5
ledcWrite(pin, value);
ledcWrite(pin, value);
return ledcReadFreq(pin);
# endif // if ESP_IDF_VERSION_MAJOR < 5
}
return 0;
}