✨ ESP32 - Hardware PWM for fan, cutter, servos (#23802)
This commit is contained in:
committed by
Scott Lahteine
parent
c7a2ecc31a
commit
cc8e485e1c
@@ -31,20 +31,18 @@
|
||||
// so we only allocate servo channels up high to avoid side effects with regards to analogWrite (fans, leds, laser pwm etc.)
|
||||
int Servo::channel_next_free = 12;
|
||||
|
||||
Servo::Servo() {
|
||||
channel = channel_next_free++;
|
||||
}
|
||||
Servo::Servo() {}
|
||||
|
||||
int8_t Servo::attach(const int inPin) {
|
||||
if (channel >= CHANNEL_MAX_NUM) return -1;
|
||||
if (inPin > 0) pin = inPin;
|
||||
|
||||
ledcSetup(channel, 50, 16); // channel X, 50 Hz, 16-bit depth
|
||||
ledcAttachPin(pin, channel);
|
||||
return true;
|
||||
channel = get_pwm_channel(pin, 50u, 16u);
|
||||
return channel; // -1 if no PWM avail.
|
||||
}
|
||||
|
||||
void Servo::detach() { ledcDetachPin(pin); }
|
||||
// leave channel connected to servo - set duty to zero
|
||||
void Servo::detach() {
|
||||
if (channel >= 0) ledcWrite(channel, 0);
|
||||
}
|
||||
|
||||
int Servo::read() { return degrees; }
|
||||
|
||||
@@ -52,7 +50,7 @@ void Servo::write(int inDegrees) {
|
||||
degrees = constrain(inDegrees, MIN_ANGLE, MAX_ANGLE);
|
||||
int us = map(degrees, MIN_ANGLE, MAX_ANGLE, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
|
||||
int duty = map(us, 0, TAU_USEC, 0, MAX_COMPARE);
|
||||
ledcWrite(channel, duty);
|
||||
if (channel >= 0) ledcWrite(channel, duty); // don't save duty for servos!
|
||||
}
|
||||
|
||||
void Servo::move(const int value) {
|
||||
|
||||
Reference in New Issue
Block a user