mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-28 04:07:47 +00:00
[PWM] Fix led flickering
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user