@@ -48,9 +48,26 @@ static bool fade_initialized = false;
48
48
49
49
static bool ledcDetachBus (void * bus ) {
50
50
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
+ }
52
66
pinMatrixOutDetach (handle -> pin , false, false);
53
67
free (handle );
68
+ if (!channel_found ) {
69
+ ledc_handle .used_channels &= ~(1UL << handle -> channel );
70
+ }
54
71
if (ledc_handle .used_channels == 0 ) {
55
72
ledc_fade_func_uninstall ();
56
73
fade_initialized = false;
@@ -59,8 +76,8 @@ static bool ledcDetachBus(void *bus) {
59
76
}
60
77
61
78
bool 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 );
64
81
return false;
65
82
}
66
83
if (freq == 0 ) {
0 commit comments