diff --git a/library.properties b/library.properties
index 4d17a02..cbd24fc 100644
--- a/library.properties
+++ b/library.properties
@@ -6,4 +6,4 @@ sentence=Allows Arduino boards to control a variety of servo motors.
paragraph=This library can control a great number of servos.
It makes careful use of timers: the library can control 12 servos using only 1 timer.
On the Arduino Due you can control up to 60 servos.
category=Device Control
url=https://www.arduino.cc/reference/en/libraries/servo/
-architectures=avr,megaavr,sam,samd,nrf52,stm32f4,mbed,mbed_nano,mbed_portenta,mbed_rp2040,renesas,renesas_portenta,renesas_uno
+architectures=avr,megaavr,sam,samd,nrf52,stm32f4,mbed,mbed_nano,mbed_portenta,mbed_rp2040,renesas,renesas_portenta,renesas_uno,esp32
diff --git a/src/esp32/Servo.cpp b/src/esp32/Servo.cpp
index 043908a..5977711 100644
--- a/src/esp32/Servo.cpp
+++ b/src/esp32/Servo.cpp
@@ -34,26 +34,42 @@
class ServoImpl {
uint8_t pin;
+ uint8_t channel;
public:
- ServoImpl(const uint8_t _pin, const uint8_t _channel) : pin(_pin) {
- // Setup timer
+ ServoImpl(const uint8_t _pin, const uint8_t _channel) : pin(_pin), channel(_channel) {
+#if ESP_IDF_VERSION_MAJOR < 5
ledcSetup(_channel, (1000000 / REFRESH_INTERVAL), LEDC_MAX_BIT_WIDTH);
-
- // Attach timer to a LED pin
ledcAttachPin(pin, _channel);
+#else
+ ledcAttachChannel(pin, (1000000 / REFRESH_INTERVAL), LEDC_MAX_BIT_WIDTH, channel);
+#endif
}
~ServoImpl() {
+#if ESP_IDF_VERSION_MAJOR < 5
ledcDetachPin(pin);
+#else
+ ledcDetach(pin);
+#endif
}
- void set(const uint8_t _channel, const uint32_t duration_us) {
- ledcWrite(_channel, LEDC_US_TO_TICKS(duration_us));
+ void set(const uint32_t duration_us) {
+ ledcWrite(ref(), LEDC_US_TO_TICKS(duration_us));
}
- uint32_t get(const uint8_t _channel) const {
- return LEDC_TICKS_TO_US(ledcRead(_channel));
+ uint32_t get() const {
+ return LEDC_TICKS_TO_US(ledcRead(ref()));
+ }
+
+private:
+ // read/write channel argument changed to pin between 4.x and 5.x
+ uint8_t ref() const {
+#if ESP_IDF_VERSION_MAJOR < 5
+ return channel;
+#else
+ return pin;
+#endif
}
};
@@ -121,7 +137,7 @@ void Servo::writeMicroseconds(int value)
else if (value > SERVO_MAX())
value = SERVO_MAX();
- servos[this->servoIndex]->set(this->servoIndex, value);
+ servos[this->servoIndex]->set(value);
}
}
@@ -135,7 +151,7 @@ int Servo::readMicroseconds()
if (!servos[this->servoIndex]) {
return 0;
}
- return servos[this->servoIndex]->get(this->servoIndex);
+ return servos[this->servoIndex]->get();
}
bool Servo::attached()
diff --git a/src/esp32/ServoTimers.h b/src/esp32/ServoTimers.h
index 25e8561..16122b8 100644
--- a/src/esp32/ServoTimers.h
+++ b/src/esp32/ServoTimers.h
@@ -1,8 +1,12 @@
#define MAX_PWM_SERVOS 16
+#if ESP_IDF_VERSION_MAJOR < 5
#define LEDC_MAX_BIT_WIDTH SOC_LEDC_TIMER_BIT_WIDE_NUM
+#else
+#define LEDC_MAX_BIT_WIDTH SOC_LEDC_TIMER_BIT_WIDTH
+#endif
constexpr uint32_t BIT_RESOLUTION = (1 << LEDC_MAX_BIT_WIDTH) - 1;
#define LEDC_US_TO_TICKS(us) static_cast((us * BIT_RESOLUTION) / REFRESH_INTERVAL)
-#define LEDC_TICKS_TO_US(ticks) static_cast((ticks * REFRESH_INTERVAL) / BIT_RESOLUTION)
\ No newline at end of file
+#define LEDC_TICKS_TO_US(ticks) static_cast((ticks * REFRESH_INTERVAL) / BIT_RESOLUTION)