mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-28 04:07:47 +00:00
[PWM] using ledcRead()
This commit is contained in:
@@ -158,8 +158,12 @@ int8_t attachLedChannel(int pin, uint32_t frequency, uint8_t resolution)
|
||||
if (frequency == 0) {
|
||||
frequency = ESPEASY_PWM_DEFAULT_FREQUENCY;
|
||||
}
|
||||
ledcDetach(pin); // See: https://github.com/espressif/arduino-esp32/issues/9212
|
||||
return ledcAttach(pin, frequency, resolution) ? 0 : -1;
|
||||
|
||||
if (ledcRead(pin) == 0) {
|
||||
ledcDetach(pin); // See: https://github.com/espressif/arduino-esp32/issues/9212
|
||||
return ledcAttach(pin, frequency, resolution) ? 0 : -1;
|
||||
}
|
||||
else { return 0; }
|
||||
}
|
||||
|
||||
void detachLedChannel(int pin) { ledcDetach(pin); }
|
||||
@@ -168,38 +172,23 @@ void detachLedChannel(int pin) { ledcDetach(pin); }
|
||||
|
||||
uint32_t analogWriteESP32(int pin, int value, uint32_t frequency)
|
||||
{
|
||||
static int8_t ledChannel[GPIO_NUM_MAX];
|
||||
static bool initialized = false;
|
||||
|
||||
if (!initialized) {
|
||||
for (int i = 0; i < GPIO_NUM_MAX; ++i) {
|
||||
ledChannel[i] = -1;
|
||||
}
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
if ((value == 0)) {
|
||||
if (value == 0) {
|
||||
detachLedChannel(pin);
|
||||
ledChannel[pin] = -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// find existing channel if this pin has been used before
|
||||
uint8_t resolution = 10;
|
||||
|
||||
value = adapt_ledc_frequency_resolution_duty(frequency, resolution, value);
|
||||
int8_t ledChannel = attachLedChannel(pin, frequency, resolution);
|
||||
|
||||
// Only update frequency & attach channel if pin/freq changed
|
||||
if (ledChannel[pin] == -1) {
|
||||
|
||||
ledChannel[pin] = attachLedChannel(pin, frequency, resolution);
|
||||
}
|
||||
|
||||
if (ledChannel[pin] != -1) {
|
||||
if (ledChannel != -1) {
|
||||
# if ESP_IDF_VERSION_MAJOR < 5
|
||||
ledcWrite(ledChannel[pin], value);
|
||||
return ledChannelFreq[ledChannel[pin]];
|
||||
ledcWrite(ledChannel, value);
|
||||
return ledChannelFreq[ledChannel];
|
||||
# else // if ESP_IDF_VERSION_MAJOR < 5
|
||||
ledcWrite(pin, value);
|
||||
ledcWrite(pin, value);
|
||||
return ledcReadFreq(pin);
|
||||
# endif // if ESP_IDF_VERSION_MAJOR < 5
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user