@@ -48,9 +48,26 @@ static bool fade_initialized = false;
4848
4949static bool ledcDetachBus (void * bus ) {
5050 ledc_channel_handle_t * handle = (ledc_channel_handle_t * )bus ;
51- ledc_handle .used_channels &= ~(1UL << handle -> channel );
51+ bool channel_found = false;
52+ // Check if more pins are attached to the same ledc channel
53+ for (uint8_t i = 0 ; i < SOC_GPIO_PIN_COUNT ; i ++ ) {
54+ if (!perimanPinIsValid (i )) {
55+ continue ; //invalid pin, skip
56+ }
57+ peripheral_bus_type_t type = perimanGetPinBusType (i );
58+ if (type == ESP32_BUS_TYPE_LEDC ) {
59+ ledc_channel_handle_t * bus_check = (ledc_channel_handle_t * )perimanGetPinBus (i , ESP32_BUS_TYPE_LEDC );
60+ if (bus_check -> channel == handle -> channel ) {
61+ channel_found = true;
62+ break ;
63+ }
64+ }
65+ }
5266 pinMatrixOutDetach (handle -> pin , false, false);
5367 free (handle );
68+ if (!channel_found ) {
69+ ledc_handle .used_channels &= ~(1UL << handle -> channel );
70+ }
5471 if (ledc_handle .used_channels == 0 ) {
5572 ledc_fade_func_uninstall ();
5673 fade_initialized = false;
@@ -59,8 +76,8 @@ static bool ledcDetachBus(void *bus) {
5976}
6077
6178bool ledcAttachChannel (uint8_t pin , uint32_t freq , uint8_t resolution , uint8_t channel ) {
62- if (channel >= LEDC_CHANNELS || ledc_handle .used_channels & (1UL << channel )) {
63- log_e ("Channel %u is not available (maximum %u) or already used!" , channel , LEDC_CHANNELS );
79+ if (channel >= LEDC_CHANNELS ) { // || ledc_handle.used_channels & (1UL << channel)) {
80+ log_e ("Channel %u is not available (maximum %u) TODO: delete ( or already used) !" , channel , LEDC_CHANNELS );
6481 return false;
6582 }
6683 if (freq == 0 ) {
0 commit comments