Skip to content

Commit b3be0df

Browse files
committed
Update
Detaching a pin will now occur when the pwm signal is low for jitterless detach. Fixed a few ambiguous overload attach functions. Required changing pin, ch, munUs and maxUs parameters to int and speed and ke parameters to double.
1 parent 1629e70 commit b3be0df

File tree

8 files changed

+162
-160
lines changed

8 files changed

+162
-160
lines changed

README.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,15 @@ myservo.write(pin, value, speed, ke)
212212

213213
##### Parameters
214214

215-
- **pin** The pin number which (if necessary) will be attached to the next free channel *(uint8_t)*
216-
- **value** This value is converted to the pwm duty. See above table for range and units *(float)
217-
- **speed** This value has units degrees/second (float). For example, if `speed` is set to 100 deg/s and the servo position value is changed from 0 to 180 deg, then the servo will take 1.8 sec (1800 ms) to complete its travel. Its motion (response) will be determined by `ke`,
215+
- **pin** The pin number which (if necessary) will be attached to the next free channel *(int)*
216+
- **value** This value is converted to the pwm duty. See above table for range and units *(double)
217+
- **speed** This value has units degrees/second (double). For example, if `speed` is set to 100 deg/s and the servo position value is changed from 0 to 180 deg, then the servo will take 1.8 sec (1800 ms) to complete its travel. Its motion (response) will be determined by `ke`,
218218
- **ke** Servo easing constant for a [Normalized Tunable Sigmoid](https://www.desmos.com/calculator/ejkcwglzd1). A `ke` value of 0.0 represents a linear response. As you increase `ke`, this increases the steepness of a sigmoid response. When `ke` is 1.0, normal "instantaneous" servo response is enabled and the speed parameter is ignored.
219219

220220
##### Returns
221221

222222
- If the servo easing constant `ke` is 1.0 (default) then the pwm duty value *(uint32_t)* is returned.
223-
- If `ke` is less than 1.0, then a normalized float value (0.0 to 1.0) is returned. This represents the programmed servo position from start to stop as it moves over time. When the returned value reaches 0.5, this represents both 50% travel and 50% time duration, no matter what easing constant is set.
223+
- If `ke` is less than 1.0, then a normalized double value (0.0 to 1.0) is returned. This represents the programmed servo position from start to stop as it moves over time. When the returned value reaches 0.5, this represents both 50% travel and 50% time duration, no matter what easing constant is set.
224224

225225
</details>
226226

@@ -256,7 +256,7 @@ myservo.read(pin)
256256

257257
##### Parameters
258258

259-
- **pin** The pin number *(uint8_t)
259+
- **pin** The pin number (int)
260260

261261
##### Returns
262262

@@ -280,7 +280,7 @@ myservo.readMicroseconds(pin)
280280

281281
##### Parameters
282282

283-
- **pin** The pin number *(uint8_t)
283+
- **pin** The pin number (int)
284284

285285
##### Returns
286286

@@ -313,15 +313,15 @@ myservo.attach(pin, ch, minUs, maxUs, speed, ke, invert) // as above with inver
313313

314314
##### Parameters
315315

316-
- **pin** The pin number *(uint8_t)*
316+
- **pin** The pin number *(int)*
317317

318-
- **ch** This optional parameter is used to attach the pin to a specific channel *(uint8_t)*)
318+
- **ch** This optional parameter is used to attach the pin to a specific channel *(int)*)
319319

320-
- **minUs** Minimum timer width in microseconds *(uint16_t)
320+
- **minUs** Minimum timer width in microseconds *(int)*
321321

322-
- **maxUs** Maximum timer width in microseconds *(uint16_t)*
322+
- **maxUs** Maximum timer width in microseconds *(int)*
323323

324-
- **speed** This servo easing parameter has units degrees/second (float). For example, if `speed` is set to 100 deg/s and the servo position value is changed from 0 to 180 deg, then the servo will take 1.8 sec (1800 ms) to complete its travel. Its motion (response) will be determined by `ke`,
324+
- **speed** This servo easing parameter has units degrees/second (double). For example, if `speed` is set to 100 deg/s and the servo position value is changed from 0 to 180 deg, then the servo will take 1.8 sec (1800 ms) to complete its travel. Its motion (response) will be determined by `ke`,
325325

326326
- **ke** Servo easing constant for a [Normalized Tunable Sigmoid](https://www.desmos.com/calculator/ejkcwglzd1). A `ke` value of 0.0 represents a linear response. As you increase `ke`, this increases the steepness of a sigmoid response. When `ke` is 1.0, normal "instantaneous" servo response is enabled and the speed parameter is ignored.
327327

@@ -357,7 +357,7 @@ myservo.attach(pin, ch) // attach to specified channel
357357

358358
##### Parameters
359359

360-
- **pin** The pin number *(uint8_t)
360+
- **pin** The pin number *(int)*
361361

362362
##### Returns
363363

@@ -384,7 +384,7 @@ myservo.attached(pin)
384384

385385
##### Parameters
386386

387-
- **pin** The pin number *(uint8_t)
387+
- **pin** The pin number *(int)*
388388

389389
##### Returns
390390

@@ -412,8 +412,8 @@ myservo.attachInvert(pin, ch); // attach to specified ch with inverted pwm
412412

413413
##### Parameters
414414

415-
- **pin** The pin number *(uint8_t)*
416-
- **ch** This optional parameter is used to attach the pin to a specific channel *(uint8_t)*)
415+
- **pin** The pin number *(int)*
416+
- **ch** This optional parameter is used to attach the pin to a specific channel *(int)*
417417

418418
##### Returns
419419

@@ -441,7 +441,7 @@ myservo.attachedPin(ch)
441441

442442
##### Parameters
443443

444-
- **pin** The pin number *(uint8_t)
444+
- **pin** The pin number *(int)*
445445

446446
##### Returns
447447

@@ -470,7 +470,7 @@ myservo.writePwm(pin, duty, frequency, resolution, phase)
470470

471471
##### Parameters
472472

473-
- **pin** The pin number which (if necessary) will be attached to the next free channel *(uint8_t)*
473+
- **pin** The pin number which (if necessary) will be attached to the next free channel *(int)*
474474
- **duty** This sets the pwm duty. The range is 0 to (2**resolution) - 1 *(uint32_t)*
475475
- **frequency** The pwm timer frequency (Hz). The frequency and resolution limits are interdependent *(uint32_t)*. For more details, see [Supported Range of Frequency and Duty Resolutions](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/ledc.html#ledc-api-supported-range-frequency-duty-resolution).
476476
- **resolution** The bit resolution of the pwm duty *(uint8_t)*
@@ -498,7 +498,7 @@ myservo.detachPin(pin)
498498

499499
##### Parameters
500500

501-
- **pin** The pin number *(uint8_t)*
501+
- **pin** The pin number *(int)*
502502

503503
##### Returns
504504

@@ -572,7 +572,7 @@ myservo.setFrequency(pin, frequency)
572572

573573
##### Parameters
574574

575-
- **pin** The pin number (uint8_t) If the pin is detached (free) and there's a free channel available, the pin will be attached to the first free channel that's found *(uint8_t)*
575+
- **pin** The pin number *(int)* If the pin is detached (free) and there's a free channel available, the pin will be attached to the first free channel that's found *(int)*
576576
- **frequency** The frequency in Hz. The default is 1000 Hz *(uint32_t)*
577577

578578
##### Returns
@@ -597,7 +597,7 @@ myservo.setResolution(pin, resolution)
597597

598598
##### Parameters
599599

600-
- **pin** The pin number (uint8_t) If the pin is detached (free) and there's a free channel available, the pin will be attached to the first free channel that's found *(uint8_t)*
600+
- **pin** The pin number *(int)* If the pin is detached (free) and there's a free channel available, the pin will be attached to the first free channel that's found *(int)*
601601
- **resolution** The PWM resolution can be set from 1-bit to 16-bit, default is 8-bit *(uint8_t)*
602602

603603
##### Returns
@@ -631,7 +631,7 @@ myservo.tone(pin, frequency, duration, interval)
631631

632632
##### Parameters
633633

634-
- **pin** The pin number which (if necessary) will be attached to the next free channel *(uint8_t)*
634+
- **pin** The pin number which (if necessary) will be attached to the next free channel *(int)*
635635
- **frequency** The tone frequency (Hz) with range 1-65535 *(uint16_t)*.
636636
- **duration** The duration in milliseconds with range 0-65535 *(uint16_t)*, where 0 is off (default) and 65535 is always on.
637637
- **interval** This optional parameter specifies the pause time in milliseconds before the next call to tone becomes ready. *(uint16_t)*, range 0-65535, default = 0.
@@ -666,7 +666,7 @@ pwm.note(pin, note, octave, duration, interval)
666666

667667
##### Parameters
668668

669-
- **pin** The pin number which (if necessary) will be attached to the next free channel *(uint8_t)*
669+
- **pin** The pin number which (if necessary) will be attached to the next free channel *(int)*
670670
- **note** The type is defined in [esp32-hal-ledc.h](https://github.com/espressif/arduino-esp32/blob/master/cores/esp32/esp32-hal-ledc.h) *(note_t)*.
671671
- **octave** There are 8 octaves available, 1 to 8 *(uint8_t)*
672672
- **duration** The duration in milliseconds with range 0-65535 *(uint16_t)*, where 0 is off (default) and 65535 is always on.

examples/Servo_Easing_Interrupt/Servo_Easing_Interrupt.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
#include <Servo.h>
3939

4040
const int servoPin = 8;
41-
volatile float ke = 0.0; // easing curve
42-
volatile float speed = 100; // speed control (degrees/second)
41+
volatile double ke = 0.0; // easing curve
42+
volatile double speed = 100; // speed control (degrees/second)
4343
volatile float pos = 90; // servo position (degrees)
4444
volatile float ye; // calculated servo position (normalized)
4545

examples/Servo_Easing_Position/Servo_Easing_Position.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ const int servoPin2 = 19;
1616
const int servoPin3 = 21;
1717

1818
// units in degrees per second
19-
float speed1 = 70.0;
20-
float speed2 = 140.0;
21-
float speed3 = 180.0;
19+
double speed1 = 70.0;
20+
double speed2 = 140.0;
21+
double speed3 = 180.0;
2222

2323
// When easing constant (ke) < 1.0, return value is normalized, when 1.0, returns pulse width (μs)
2424
// ke = 0.0 is linear, between 0.0 and 1.0 is tunable sigmoid, 1.0 is normal response
2525
// Normalized Tunable Sigmoid: https://www.desmos.com/calculator/ejkcwglzd1
26-
float ke1 = 0.0;
27-
float ke2 = 0.6;
28-
float ke3 = 0.8;
26+
double ke1 = 0.0;
27+
double ke2 = 0.6;
28+
double ke3 = 0.8;
2929

3030
// go to position (degrees)
3131
uint8_t pos1 = 90;

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"keywords": "pwm, servo, tone, esp32, analogWrite, esp32-s2, esp32-s3, esp32-c3, ledc",
44
"description": "ESP32 PWM, Servo, Easing and Tone. Smart GPIO pin management and advanced control features.",
55
"license": "MIT",
6-
"version": "5.0.0",
6+
"version": "5.0.1",
77
"frameworks": "arduino",
88
"platforms": "espressif32",
99
"repository": {

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP32 ESP32S2 AnalogWrite
2-
version=5.0.0
2+
version=5.0.1
33
author=David Lloyd
44
maintainer=David Lloyd <[email protected]>
55
sentence=ESP32 PWM, Servo, Easing and Tone.

src/Servo.h

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -16,153 +16,153 @@ class Servo
1616

1717
// servo
1818

19-
uint8_t attach(uint8_t pin) {
19+
uint8_t attach(int pin) {
2020
return pwm.attachServo(pin);
2121
};
2222

23-
uint8_t attach(uint8_t pin, bool invert) {
23+
uint8_t attach(int pin, bool invert) {
2424
return pwm.attachServo(pin, invert);
2525
};
2626

27-
uint8_t attach(uint8_t pin, uint8_t ch) {
27+
uint8_t attach(int pin, int ch) {
2828
return pwm.attachServo(pin, ch);
2929
};
3030

31-
uint8_t attach(uint8_t pin, uint8_t ch, bool invert) {
31+
uint8_t attach(int pin, int ch, bool invert) {
3232
return pwm.attachServo(pin, ch, invert);
3333
};
3434

35-
uint8_t attach(uint8_t pin, uint16_t minUs, uint16_t maxUs) {
35+
uint8_t attach(int pin, int minUs, int maxUs) {
3636
return pwm.attachServo(pin, minUs, maxUs);
3737
};
3838

39-
uint8_t attach(uint8_t pin, uint8_t ch, uint16_t minUs, uint16_t maxUs) {
39+
uint8_t attach(int pin, int ch, int minUs, int maxUs) {
4040
return pwm.attachServo(pin, ch, minUs, maxUs);
4141
};
4242

43-
uint8_t attach(uint8_t pin, uint8_t ch, uint16_t minUs, uint16_t maxUs, bool invert) {
43+
uint8_t attach(int pin, int ch, int minUs, int maxUs, bool invert) {
4444
return pwm.attachServo(pin, ch, minUs, maxUs, invert);
4545
};
4646

47-
uint8_t attach(uint8_t pin, uint16_t minUs, uint16_t maxUs, float speed, float ke) {
47+
uint8_t attach(int pin, int minUs, int maxUs, double speed, double ke) {
4848
return pwm.attachServo(pin, minUs, maxUs, speed, ke);
4949
};
5050

51-
uint8_t attach(uint8_t pin, uint8_t ch, uint16_t minUs, uint16_t maxUs, float speed, float ke) {
51+
uint8_t attach(int pin, int ch, int minUs, int maxUs, double speed, double ke) {
5252
return pwm.attachServo(pin, ch, minUs, maxUs, speed, ke);
5353
};
5454

55-
uint8_t attach(uint8_t pin, uint8_t ch, uint16_t minUs, uint16_t maxUs, float speed, float ke, bool invert) {
55+
uint8_t attach(int pin, int ch, int minUs, int maxUs, double speed, double ke, bool invert) {
5656
return pwm.attachServo(pin, ch, minUs, maxUs, speed, ke, invert);
5757
};
5858

59-
float read(uint8_t pin) {
59+
float read(int pin) {
6060
return pwm.read(pin);
6161
};
6262

63-
float readMicroseconds(uint8_t pin) {
63+
float readMicroseconds(int pin) {
6464
return pwm.readMicroseconds(pin);
6565
};
6666

67-
float write(uint8_t pin, float value) {
67+
float write(int pin, float value) {
6868
return pwm.writeServo(pin, value);
6969
};
7070

71-
float writeMicroseconds(uint8_t pin, float value) {
71+
float writeMicroseconds(int pin, float value) {
7272
return pwm.writeServo(pin, value);
7373
};
7474

75-
float write(uint8_t pin, float value, float speed, float ke) {
75+
float write(int pin, float value, double speed, double ke) {
7676
return pwm.writeServo(pin, value, speed, ke);
7777
};
7878

79-
float writeMicroseconds(uint8_t pin, float value, float speed, float ke) {
79+
float writeMicroseconds(int pin, float value, double speed, double ke) {
8080
return pwm.writeServo(pin, value, speed, ke);
8181
};
8282

8383
// common
8484

85-
uint8_t attached(uint8_t pin) { // check if pin is attached
85+
uint8_t attached(int pin) { // check if pin is attached
8686
return pwm.attached(pin);
8787
};
8888

89-
uint8_t attachedPin(uint8_t ch) { // get pin on specified channel
89+
uint8_t attachedPin(int ch) { // get pin on specified channel
9090
return pwm.attachedPin(ch);
9191
};
9292

93-
uint8_t firstFreeCh(void) { // get first free channel
93+
uint8_t firstFreeCh(void) { // get first free channel
9494
return pwm.firstFreeCh();
9595
};
9696

97-
void detach(uint8_t pin) { // detach pin
97+
void detach(int pin) { // detach pin
9898
pwm.detach(pin);
9999
};
100100

101-
bool detached(uint8_t pin) { // check if pin is detached
101+
bool detached(int pin) { // check if pin is detached
102102
return pwm.detached(pin);
103103
};
104104

105-
void pause(uint8_t ch = 255) { // pause timer on all or specified channel
105+
void pause(int ch = 255) { // pause timer on all or specified channel
106106
pwm.pause(ch);
107107
};
108108

109-
void resume(uint8_t ch = 255) { // resume timer on all or specified channel
109+
void resume(int ch = 255) { // resume timer on all or specified channel
110110
pwm.resume(ch);
111111
};
112112

113-
void printDebug(void) { // print the status of all channels
113+
void printDebug(void) { // print the status of all channels
114114
pwm.printDebug();
115115
};
116116

117-
float setFrequency(uint8_t pin, uint32_t frequency = 1000) {
117+
float setFrequency(int pin, uint32_t frequency = 1000) {
118118
return pwm.setFrequency(pin, frequency);
119119
};
120120

121-
uint8_t setResolution(uint8_t pin, uint8_t resolution = 10) {
121+
uint8_t setResolution(int pin, uint8_t resolution = 10) {
122122
return pwm.setResolution(pin, resolution);
123123
};
124124

125125
// pwm
126126

127-
float writePwm(uint8_t pin, uint32_t duty) {
127+
float writePwm(int pin, uint32_t duty) {
128128
return pwm.write(pin, duty);
129129
};
130130

131-
float writePwm(uint8_t pin, uint32_t duty, uint32_t frequency) {
131+
float writePwm(int pin, uint32_t duty, uint32_t frequency) {
132132
return pwm.write(pin, duty, frequency);
133133
};
134134

135-
float writePwm(uint8_t pin, uint32_t duty, uint32_t frequency, uint8_t resolution) {
135+
float writePwm(int pin, uint32_t duty, uint32_t frequency, uint8_t resolution) {
136136
return pwm.write(pin, duty, frequency, resolution);
137137
};
138138

139-
float writePwm(uint8_t pin, uint32_t duty, uint32_t frequency, uint8_t resolution, uint32_t phase) {
139+
float writePwm(int pin, uint32_t duty, uint32_t frequency, uint8_t resolution, uint32_t phase) {
140140
return pwm.write(pin, duty, frequency, resolution, phase);
141141
};
142142

143-
uint8_t attachPwm(uint8_t pin) { // attach pin to next free channel
143+
uint8_t attachPwm(int pin) { // attach pin to next free channel
144144
return pwm.attach(pin);
145145
};
146146

147-
uint8_t attachPwm(uint8_t pin, uint8_t ch) { // attach to specified channel
147+
uint8_t attachPwm(int pin, int ch) { // attach to specified channel
148148
return pwm.attach(pin, ch);
149149
};
150150

151-
uint8_t attachInvert(uint8_t pin) { // attach pin to next free channel with inverted pwm
151+
uint8_t attachInvert(int pin) { // attach pin to next free channel with inverted pwm
152152
return pwm.attachInvert(pin);
153153
};
154154

155-
uint8_t attachInvert(uint8_t pin, uint8_t ch) { // attach to specified ch with inverted pwm
155+
uint8_t attachInvert(int pin, int ch) { // attach to specified ch with inverted pwm
156156
return pwm.attachInvert(pin, ch);
157157
};
158158

159159
// tone and note
160160

161-
void tone(uint8_t pin, uint32_t frequency, uint16_t duration = 500, uint16_t interval = 0) {
161+
void tone(int pin, uint32_t frequency, uint16_t duration = 500, uint16_t interval = 0) {
162162
pwm.tone(pin, frequency, duration, interval);
163163
};
164164

165-
void note(uint8_t pin, note_t note, uint8_t octave, uint16_t duration, uint16_t interval) {
165+
void note(int pin, note_t note, uint8_t octave, uint16_t duration, uint16_t interval) {
166166
pwm.note(pin, note, octave, duration, interval);
167167
};
168168

0 commit comments

Comments
 (0)