Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
* Fixes issue with integrating previous servo easing update,.
* Renames printConfig() function to printDebug()
* Updates examples and readme.
  • Loading branch information
Dlloydev committed Apr 2, 2023
1 parent 0d189be commit 8188a62
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 91 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ attach(pin, ch, minUs, defUs, maxUs, speed, ke, invert) // as above with invert
[Servo_Sweep_Inverted](https://wokwi.com/projects/351967394028061269)
![image](https://user-images.githubusercontent.com/63488701/209453287-4c1b0ba8-8e0e-42cd-9bef-55a7b2f1e8b4.png)
![image](https://user-images.githubusercontent.com/63488701/229374262-460e878e-81f1-4398-8ea5-60b02026a4cf.png)
##### Returns
Expand Down Expand Up @@ -498,7 +498,7 @@ pwm.setResolution(pin, resolution)



### printConfig()
### printDebug()

##### Description

Expand All @@ -507,7 +507,7 @@ This function prints the available PWM pins to choose from and a formatted outpu
**Syntax**

```c++
pwm.printConfig()
pwm.printDebug()
```

##### Parameters (optional)
Expand All @@ -518,9 +518,7 @@ pwm.printConfig()

- serial report on serial monitor

![image](https://user-images.githubusercontent.com/63488701/207452928-ca44457d-f807-439b-971e-927af4259e16.png)


![![image](https://user-images.githubusercontent.com/63488701/229374511-de75b97d-f91f-44d0-b103-0ca858d16727.png)

```
This Library is licensed under the MIT License
Expand Down
4 changes: 2 additions & 2 deletions examples/ESP32_3phase_10kHz/ESP32_3phase_10kHz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ void setup() {
pwm.write(pwmPin[i], (duty - deadtime), frequency, resolution, (phase[i] + shift[i]));
}
pwm.resume();
pwm.printConfig();
pwm.printDebug();
}

void loop() {
}
}
2 changes: 1 addition & 1 deletion examples/ESP32_3phase_40kHz/ESP32_3phase_40kHz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void setup() {
pwm.write(pwmPin[i], (duty - deadtime), frequency, resolution, (phase[i] + shift[i]));
}
pwm.resume();
pwm.printConfig();
pwm.printDebug();
}

void loop() {
Expand Down
4 changes: 2 additions & 2 deletions examples/ESP32_C3_3phase_10kHz/ESP32_C3_3phase_10kHz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ void setup() {
pwm.write(pwmPin[i], (duty - deadtime), frequency, resolution, (phase[i] + shift[i]));
}
pwm.resume();
pwm.printConfig();
pwm.printDebug();
}

void loop() {
}
}
4 changes: 2 additions & 2 deletions examples/ESP32_S2_3phase_10kHz/ESP32_S2_3phase_10kHz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ void setup() {
pwm.write(pwmPin[i], (duty - deadtime), frequency, resolution, (phase[i] + shift[i]));
}
pwm.resume();
pwm.printConfig();
pwm.printDebug();
}

void loop() {
}
}
2 changes: 1 addition & 1 deletion examples/ESP32_Sync2_300kHz/ESP32_Sync2_300kHz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void setup() {
pwm.write(pwm21, 64, 300000);
pwm.write(pwm19, 128, 300000);
pwm.resume();
pwm.printConfig();
pwm.printDebug();
}

void loop() {
Expand Down
2 changes: 1 addition & 1 deletion examples/ESP32_Sync8_20kHz/ESP32_Sync8_20kHz.ino
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void setup() {
pwm.write(pwmPin[i], duty[i], frequency, resolution, phase[i]);
}
pwm.resume();
pwm.printConfig();
pwm.printDebug();
}

void loop() {
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"keywords": "pwm, servo, tone, esp32, analogWrite, esp32-s2, esp32-s3, esp32-c3, ledc",
"description": "ESP32 PWM, Servo, Easing and Tone. Smart GPIO pin management and advanced control features.",
"license": "MIT",
"version": "4.3.2",
"version": "4.3.3",
"frameworks": "arduino",
"platforms": "espressif32",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP32 ESP32S2 AnalogWrite
version=4.3.2
version=4.3.3
author=David Lloyd
maintainer=David Lloyd <dlloydev@testcor.ca>
sentence=ESP32 PWM, Servo, Easing and Tone.
Expand Down
78 changes: 24 additions & 54 deletions src/pwmWrite.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************
ESP32 PWM, SERVO and TONE Library, Version 4.3.2
ESP32 PWM, SERVO and TONE Library, Version 4.3.3
by dlloydev https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite
This Library is licensed under the MIT License
*******************************************************************/
Expand Down Expand Up @@ -47,36 +47,28 @@ float Pwm::write(uint8_t pin, uint32_t duty, uint32_t frequency, uint8_t resolut

float Pwm::writeServo(uint8_t pin, float value, float speed, float ke) {
uint8_t ch = attached(pin);
bool ms = millis() >> 3 & 1;
if (ms && !mem[ch].pms) {
wr_servo(pin, value, speed, ke);
}
mem[ch].pms = ms;
wr_servo(pin, value, speed, ke);
return (ke < 1.0) ? mem[ch].ye : value; // normalized easing duty (0.0 - 1.0)
}

float Pwm::writeServo(uint8_t pin, float value) {
uint8_t ch = attached(pin);
bool ms = millis() >> 3 & 1;
if (ms && !mem[ch].pms) {
if (ch == 253) { // free channels exist
for (uint8_t c = 0; c < chMax; c++) {
if (mem[c].pin == 255 && ch == 253) { //first free ch
mem[c].pin = pin;
ch = c;
if (mem[ch].frequency < 40 || mem[ch].frequency > 900) mem[ch].frequency = 50;
if (mem[ch].resolution > widthMax) mem[ch].resolution = widthMax;
else if (mem[ch].resolution < 14 && widthMax == 20) mem[ch].resolution = 16;
else if (mem[ch].resolution < 14) mem[ch].resolution = 14;
ledcSetup(ch, mem[ch].frequency, mem[ch].resolution);
if (sync) pause(ch);
ledcAttachPin(pin, ch);
}
if (ch == 253) { // free channels exist
for (uint8_t c = 0; c < chMax; c++) {
if (mem[c].pin == 255 && ch == 253) { //first free ch
mem[c].pin = pin;
ch = c;
if (mem[ch].frequency < 40 || mem[ch].frequency > 900) mem[ch].frequency = 50;
if (mem[ch].resolution > widthMax) mem[ch].resolution = widthMax;
else if (mem[ch].resolution < 14 && widthMax == 20) mem[ch].resolution = 16;
else if (mem[ch].resolution < 14) mem[ch].resolution = 14;
ledcSetup(ch, mem[ch].frequency, mem[ch].resolution);
if (sync) pause(ch);
ledcAttachPin(pin, ch);
}
}
wr_servo(pin, value, mem[ch].speed, mem[ch].ke);
}
mem[ch].pms = ms;
wr_servo(pin, value, mem[ch].speed, mem[ch].ke);
return (mem[ch].ke < 1.0) ? mem[ch].ye : value; // normalized easing duty (0.0 - 1.0)
}

Expand Down Expand Up @@ -248,43 +240,19 @@ uint8_t Pwm::setResolution(uint8_t pin, uint8_t resolution) {
return mem[ch].resolution;
}

void Pwm::printConfig() {
Serial.print(F("PWM pins: "));
void Pwm::printDebug() {
Serial.printf("PWM pins:\n");
for (uint8_t i = 0; i < 48; i++) {
if ((pinMask >> i) & 1) {
Serial.print(i); Serial.print(F(", "));
Serial.printf("%d,", i);
}
}
Serial.println();
Serial.println();
Serial.println(F("Ch Pin Hz Res Duty Phase Servo"));
Serial.printf("\n\nCh Pin Hz Res Duty Servo Speed ke\n");
for (uint8_t ch = 0; ch < chMax; ch++) {
if (ch < 10) Serial.print(F(" "));
Serial.print(ch);
Serial.print(F(" "));
if (mem[ch].pin < 100) Serial.print(F(" "));
if (mem[ch].pin < 10) Serial.print(F(" "));
Serial.print(mem[ch].pin); Serial.print(F(" "));
if (mem[ch].frequency < 10000) Serial.print(F(" "));
if (mem[ch].frequency < 1000) Serial.print(F(" "));
if (mem[ch].frequency < 100) Serial.print(F(" "));
if (mem[ch].frequency < 10) Serial.print(F(" "));
Serial.print(mem[ch].frequency, 1); Serial.print(F(" "));
if (mem[ch].resolution < 10) Serial.print(F(" "));
Serial.print(mem[ch].resolution); Serial.print(F(" "));
if (mem[ch].duty < 1000) Serial.print(F(" "));
if (mem[ch].duty < 100) Serial.print(F(" "));
if (mem[ch].duty < 10) Serial.print(F(" "));
Serial.print(mem[ch].duty); Serial.print(F(" "));
if (mem[ch].phase < 1000) Serial.print(F(" "));
if (mem[ch].phase < 100) Serial.print(F(" "));
if (mem[ch].phase < 10) Serial.print(F(" "));
Serial.print(mem[ch].phase); Serial.print(F(" "));
Serial.print(mem[ch].servoMinUs); Serial.print(F(" "));
Serial.print(mem[ch].servoDefUs); Serial.print(F(" "));
Serial.print(mem[ch].servoMaxUs);
Serial.println();
Serial.printf ("%2d %3d %5.0f %2d %4d %d-%d-%d %5.1f %1.1f\n", ch, mem[ch].pin, mem[ch].frequency, mem[ch].resolution,
mem[ch].duty, mem[ch].servoMinUs, mem[ch].servoDefUs, mem[ch].servoMaxUs, mem[ch].speed, mem[ch].ke);
}
Serial.printf("\n");
}

/************************* private functions ***************************/
Expand Down Expand Up @@ -441,4 +409,6 @@ void Pwm::reset_fields(uint8_t ch) {
mem[ch].servoMinUs = 544;
mem[ch].servoDefUs = 1472;
mem[ch].servoMaxUs = 2400;
mem[ch].speed = 0;
mem[ch].ke = 1.0;
}
39 changes: 19 additions & 20 deletions src/pwmWrite.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,25 @@ class Pwm {
float ke; // servo easing constant (0.0 linear, >0 and <1 sigmoid, 1.0 no easing)
float te; // servo easing normalized elapsed time (0.0 start - 1.0 stop)
float ye; // servo easing normalized position (0.0 start - 1.0 stop)
bool pms; // servo easing previous ms value
} mem_t;

mem_t mem[16] = { // channel data (see above)
{255, 1000, 0, 8, 0, 0, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 0, 0, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 0, 1, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 0, 1, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 0, 2, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 0, 2, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 0, 3, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 0, 3, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 1, 0, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 1, 0, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 1, 1, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 1, 1, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 1, 2, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 1, 2, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 1, 3, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 },
{255, 1000, 0, 8, 1, 3, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0, 0 }
{255, 1000, 0, 8, 0, 0, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 0, 0, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 0, 1, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 0, 1, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 0, 2, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 0, 2, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 0, 3, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 0, 3, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 1, 0, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 1, 0, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 1, 1, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 1, 1, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 1, 2, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 1, 2, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 1, 3, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0},
{255, 1000, 0, 8, 1, 3, 0, 544, 1472, 2400, 0, 0, 0, 0, 0, 0, 0, 1.0, 0, 0}
};

// pwm
Expand All @@ -80,7 +79,7 @@ class Pwm {
float write(uint8_t pin, uint32_t duty, uint32_t frequency, uint8_t resolution);
float write(uint8_t pin, uint32_t duty, uint32_t frequency, uint8_t resolution, uint32_t phase);
uint8_t attach(uint8_t pin); // attach pin to next free channel
uint8_t attach(uint8_t pin, uint8_t ch, bool invert = false); // attach to specified ch with invert option
uint8_t attach(uint8_t pin, uint8_t ch, bool invert = false); // attach to specified ch with invert option

// servo
uint8_t attach(uint8_t pin, uint16_t minUs, uint16_t defUs, uint16_t maxUs);
Expand All @@ -103,14 +102,14 @@ class Pwm {
bool detached(uint8_t pin); // check if pin is detached
void pause(uint8_t ch = 255); // pause timer on all or specified channel
void resume(uint8_t ch = 255); // resume timer on all or specified channel
void printConfig(void); // print the status of all channels
void printDebug(void); // print the status of all channels
float setFrequency(uint8_t pin, uint32_t frequency = 1000);
uint8_t setResolution(uint8_t pin, uint8_t resolution = 10);

private:
float duty2deg(uint8_t ch, uint32_t duty, float countPerUs);
void ledc_attach_with_invert(uint8_t pin, uint8_t ch, bool invert = false);
void config_servo(uint8_t ch, uint16_t minUs, uint16_t defUs, uint16_t maxUs, float speed = 100, float ke = 1.0);
void config_servo(uint8_t ch, uint16_t minUs, uint16_t defUs, uint16_t maxUs, float speed = 0, float ke = 1.0);
void wr_servo(uint8_t pin, float value, float speed, float ke);
void wr_ch_pair(uint8_t ch, uint32_t frequency, uint8_t resolution);
void wr_duty(uint8_t ch, uint32_t duty);
Expand Down

0 comments on commit 8188a62

Please sign in to comment.