@@ -215,26 +215,32 @@ static int cnt_channel = LEDC_CHANNELS;
215
215
static uint8_t analog_resolution = 8 ;
216
216
static int analog_frequency = 1000 ;
217
217
void analogWrite (uint8_t pin , int value ) {
218
- // Use ledc hardware for internal pins
219
- if (pin < SOC_GPIO_PIN_COUNT ) {
220
- if (pin_to_channel [pin ] == 0 ) {
221
- if (!cnt_channel ) {
222
- log_e ("No more analogWrite channels available! You can have maximum %u" , LEDC_CHANNELS );
223
- return ;
224
- }
225
- if (ledcSetup (cnt_channel - 1 , analog_frequency , analog_resolution ) == 0 ){
226
- log_e ("analogWrite setup failed (freq = %u, resolution = %u). Try setting different resolution or frequency" );
227
- return ;
228
- }
229
- ledcAttachPin (pin , cnt_channel - 1 );
230
- pin_to_channel [pin ] = cnt_channel -- ;
218
+ // Use ledc hardware for internal pins
219
+ if (pin < SOC_GPIO_PIN_COUNT ) {
220
+ int8_t channel = -1 ;
221
+ if (pin_to_channel [pin ] == 0 ) {
222
+ if (!cnt_channel ) {
223
+ log_e ("No more analogWrite channels available! You can have maximum %u" , LEDC_CHANNELS );
224
+ return ;
225
+ }
226
+ cnt_channel -- ;
227
+ channel = cnt_channel ;
228
+ } else {
229
+ channel = analogGetChannel (pin );
230
+ }
231
+ log_v ("GPIO %d - Using Channel %d, Value = %d" , pin , channel , value );
232
+ if (ledcSetup (channel , analog_frequency , analog_resolution ) == 0 ){
233
+ log_e ("analogWrite setup failed (freq = %u, resolution = %u). Try setting different resolution or frequency" );
234
+ return ;
235
+ }
236
+ ledcAttachPin (pin , channel );
237
+ pin_to_channel [pin ] = channel ;
238
+ ledcWrite (channel , value );
231
239
}
232
- ledcWrite (pin_to_channel [pin ] - 1 , value );
233
- }
234
240
}
235
241
236
242
int8_t analogGetChannel (uint8_t pin ) {
237
- return pin_to_channel [pin ] - 1 ;
243
+ return pin_to_channel [pin ];
238
244
}
239
245
240
246
void analogWriteFrequency (uint32_t freq ) {
0 commit comments