@@ -222,9 +222,12 @@ void analogWrite(uint8_t pin, int value) {
222
222
log_e ("No more analogWrite channels available! You can have maximum %u" , LEDC_CHANNELS );
223
223
return ;
224
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 );
225
230
pin_to_channel [pin ] = cnt_channel -- ;
226
- ledcSetup (cnt_channel , analog_frequency , analog_resolution );
227
- ledcAttachPin (pin , cnt_channel );
228
231
}
229
232
ledcWrite (pin_to_channel [pin ] - 1 , value );
230
233
}
@@ -237,7 +240,10 @@ int8_t analogGetChannel(uint8_t pin) {
237
240
void analogWriteFrequency (uint32_t freq ) {
238
241
if (cnt_channel != LEDC_CHANNELS ) {
239
242
for (int channel = LEDC_CHANNELS - 1 ; channel >= cnt_channel ; channel -- ) {
240
- ledcChangeFrequency (channel , freq , analog_resolution );
243
+ if (ledcChangeFrequency (channel , freq , analog_resolution ) == 0 ){
244
+ log_e ("analogWrite frequency cant be set due to selected resolution! Try to adjust resolution first" );
245
+ return ;
246
+ }
241
247
}
242
248
}
243
249
analog_frequency = freq ;
@@ -250,7 +256,10 @@ void analogWriteResolution(uint8_t bits) {
250
256
}
251
257
if (cnt_channel != LEDC_CHANNELS ) {
252
258
for (int channel = LEDC_CHANNELS - 1 ; channel >= cnt_channel ; channel -- ) {
253
- ledcChangeFrequency (channel , analog_frequency , bits );
259
+ if (ledcChangeFrequency (channel , analog_frequency , bits ) == 0 ){
260
+ log_e ("analogWrite resolution cant be set due to selected frequency! Try to adjust frequency first" );
261
+ return ;
262
+ }
254
263
}
255
264
}
256
265
analog_resolution = bits ;
0 commit comments