Unverified Commit 2173373d authored by Ricardo Lipas Augusto's avatar Ricardo Lipas Augusto Committed by GitHub

ledc.c: Fix analogWrite() last channel available verification (#8509)

* Fix analogWrite channel available verification

The last channel allocated is number 0, which conflicted with the value given to an uninitialized pin, giving the "No more analogWrite channels available!" error when trying to use it
Pins are now given the value -1 to indicate that they are not used so channel 0 can be used without errors.

* Fix incorrect array initialization

Keeping array of zeros for `pin_to_channel` and shifting stored channel
values by +1 to keep the pin with channel 0 from being interpreted as unused.

ref: https://github.com/espressif/arduino-esp32/pull/8509#issuecomment-1676103452
parent e2c4799b
......@@ -234,13 +234,13 @@ void analogWrite(uint8_t pin, int value) {
return;
}
ledcAttachPin(pin, channel);
pin_to_channel[pin] = channel;
pin_to_channel[pin] = channel + 1;
ledcWrite(channel, value);
}
}
int8_t analogGetChannel(uint8_t pin) {
return pin_to_channel[pin];
return pin_to_channel[pin] - 1;
}
void analogWriteFrequency(uint32_t freq) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment