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 int analog_frequency = 1000;
void analogWrite(uint8_t pin, int value) {
// Use ledc hardware for internal pins
if (pin < SOC_GPIO_PIN_COUNT) {
if (pin_to_channel[pin] == 0) {
if (!cnt_channel) {
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");
return;
}
ledcAttachPin(pin, cnt_channel - 1);
pin_to_channel[pin] = cnt_channel--;
// Use ledc hardware for internal pins
if (pin < SOC_GPIO_PIN_COUNT) {
int8_t channel = -1;
if (pin_to_channel[pin] == 0) {
if (!cnt_channel) {
log_e("No more analogWrite channels available! You can have maximum %u", LEDC_CHANNELS);
return;
}
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");
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) {
return pin_to_channel[pin] - 1;
return pin_to_channel[pin];
}
void analogWriteFrequency(uint32_t freq) {