Fixes analogWrite (#8137)

* Fixes analogWrite

* sets cnt_channel index

* fixes TAB alligment
This commit is contained in:
Rodrigo Garcia 2023-05-03 13:46:12 -03:00 committed by GitHub
parent 85d179c63c
commit a7bd6c9bc5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -217,24 +217,30 @@ static int analog_frequency = 1000;
void analogWrite(uint8_t pin, int value) { void analogWrite(uint8_t pin, int value) {
// Use ledc hardware for internal pins // Use ledc hardware for internal pins
if (pin < SOC_GPIO_PIN_COUNT) { if (pin < SOC_GPIO_PIN_COUNT) {
int8_t channel = -1;
if (pin_to_channel[pin] == 0) { if (pin_to_channel[pin] == 0) {
if (!cnt_channel) { if (!cnt_channel) {
log_e("No more analogWrite channels available! You can have maximum %u", LEDC_CHANNELS); log_e("No more analogWrite channels available! You can have maximum %u", LEDC_CHANNELS);
return; return;
} }
if(ledcSetup(cnt_channel - 1, analog_frequency, analog_resolution) == 0){ cnt_channel--;
channel = cnt_channel;
} else {
channel = analogGetChannel(pin);
}
log_v("GPIO %d - Using Channel %d, Value = %d", pin, channel, value);
if(ledcSetup(channel, analog_frequency, analog_resolution) == 0){
log_e("analogWrite setup failed (freq = %u, resolution = %u). Try setting different resolution or frequency"); log_e("analogWrite setup failed (freq = %u, resolution = %u). Try setting different resolution or frequency");
return; return;
} }
ledcAttachPin(pin, cnt_channel - 1); ledcAttachPin(pin, channel);
pin_to_channel[pin] = cnt_channel--; pin_to_channel[pin] = channel;
} ledcWrite(channel, value);
ledcWrite(pin_to_channel[pin] - 1, value);
} }
} }
int8_t analogGetChannel(uint8_t pin) { int8_t analogGetChannel(uint8_t pin) {
return pin_to_channel[pin] - 1; return pin_to_channel[pin];
} }
void analogWriteFrequency(uint32_t freq) { void analogWriteFrequency(uint32_t freq) {