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

@ -215,26 +215,32 @@ static int cnt_channel = LEDC_CHANNELS;
static uint8_t analog_resolution = 8; static uint8_t analog_resolution = 8;
static int analog_frequency = 1000; 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) {
if (pin_to_channel[pin] == 0) { int8_t channel = -1;
if (!cnt_channel) { if (pin_to_channel[pin] == 0) {
log_e("No more analogWrite channels available! You can have maximum %u", LEDC_CHANNELS); if (!cnt_channel) {
return; log_e("No more analogWrite channels available! You can have maximum %u", LEDC_CHANNELS);
} return;
if(ledcSetup(cnt_channel - 1, analog_frequency, analog_resolution) == 0){ }
log_e("analogWrite setup failed (freq = %u, resolution = %u). Try setting different resolution or frequency"); cnt_channel--;
return; channel = cnt_channel;
} } else {
ledcAttachPin(pin, cnt_channel - 1); channel = analogGetChannel(pin);
pin_to_channel[pin] = cnt_channel--; }
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");
return;
}
ledcAttachPin(pin, 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) {