FastLED 3.9.15
Loading...
Searching...
No Matches
compat.h
Go to the documentation of this file.
1
2
3#ifdef ARDUINO_ESP32_DEV
5
6#include "platforms/esp/esp_version.h"
7#include "driver/ledc.h"
8#include "esp32-hal-ledc.h"
9
10// Ancient versions of ESP32 on Arduino (IDF < 4.0) did not have analogWrite() defined.
11// IDF 4.4 and 5.0+ both have analogWrite() available, so this polyfill is only needed
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
14FL_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
17 static uint8_t channel_counter = 0;
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
41
42#endif // ESP32
#define FL_WEAK