| 12 | // for very old IDF versions. We use a weak symbol so it auto-disables on newer platforms. |
| 13 | #if !ESP_IDF_VERSION_4_OR_HIGHER |
| 14 | FL_LINK_WEAK void analogWrite(uint8_t pin, int value) { |
| 15 | // Setup PWM channel for the pin if not already done |
| 16 | static bool channels_setup[16] = {false}; // ESP32 has 16 PWM channels // okay static in header |
| 17 | static uint8_t channel_counter = 0; // okay static in header |
| 18 | |
| 19 | // Find or assign channel for this pin |
| 20 | static uint8_t pin_to_channel[40] = {255}; // ESP32 has up to 40 GPIO pins, 255 = unassigned |
| 21 | if (pin_to_channel[pin] == 255) { |
| 22 | pin_to_channel[pin] = channel_counter++; |
| 23 | if (channel_counter > 15) channel_counter = 0; // Wrap around |
| 24 | } |
| 25 | |
| 26 | uint8_t channel = pin_to_channel[pin]; |
| 27 | |
| 28 | // Setup channel if not already done |
| 29 | if (!channels_setup[channel]) { |
| 30 | ledcSetup(channel, 5000, 8); // 5kHz frequency, 8-bit resolution |
| 31 | ledcAttachPin(pin, channel); |
| 32 | channels_setup[channel] = true; |
| 33 | } |
| 34 | |
| 35 | // Write PWM value (0-255 for 8-bit resolution) |
| 36 | ledcWrite(channel, value); |
| 37 | } |
| 38 | #endif // !ESP_IDF_VERSION_4_OR_HIGHER |
| 39 | |
| 40 |
no outgoing calls
no test coverage detected