Skip to content
This repository was archived by the owner on Jan 29, 2023. It is now read-only.

Commit 305f829

Browse files
authored
v1.2.2 to replace double with float
### Releases v1.2.2 1. Use `float` for `DutyCycle` and `Freq`, `uint32_t` for `period`. 2. Optimize code by not calculation in ISR
1 parent 20dff48 commit 305f829

File tree

15 files changed

+245
-221
lines changed

15 files changed

+245
-221
lines changed

README.md

Lines changed: 150 additions & 140 deletions
Large diffs are not rendered by default.

changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
## Table of Contents
1313

1414
* [Changelog](#changelog)
15+
* [Releases v1.2.2](#releases-v122)
1516
* [Releases v1.2.1](#releases-v121)
1617
* [Releases v1.2.0](#releases-v120)
1718
* [Releases v1.1.1](#releases-v111)
@@ -24,6 +25,11 @@
2425

2526
## Changelog
2627

28+
### Releases v1.2.2
29+
30+
1. Use `float` for `DutyCycle` and `Freq`, `uint32_t` for `period`.
31+
2. Optimize code by not calculation in ISR
32+
2733
### Releases v1.2.1
2834

2935
1. DutyCycle to be optionally updated at the end current PWM period instead of immediately. Check [DutyCycle to be updated at the end current PWM period #2](https://github.com/khoih-prog/ESP8266_PWM/issues/2)

examples/ISR_16_PWMs_Array/ISR_16_PWMs_Array.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,21 @@ uint32_t PWM_Pin[] =
123123
#endif
124124

125125
// You can assign any interval for any timer here, in microseconds
126-
double PWM_Period[] =
126+
uint32_t PWM_Period[] =
127127
{
128-
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.666, 142857.143, 125000.0,
129-
111111.111, 100000.0, 66666.666, 50000.0, 40000.0, 33333.333, 25000.0, 20000.0
128+
1000000, 500000, 333333, 250000, 200000, 166667, 142857, 125000,
129+
111111, 100000, 66667, 50000, 40000, 33333, 25000, 20000
130130
};
131131

132132
// You can assign any interval for any timer here, in Hz
133-
double PWM_Freq[] =
133+
float PWM_Freq[] =
134134
{
135135
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
136136
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
137137
};
138138

139139
// You can assign any interval for any timer here, in milliseconds
140-
double PWM_DutyCycle[] =
140+
float PWM_DutyCycle[] =
141141
{
142142
5.00, 10.00, 20.00, 30.00, 40.00, 45.00, 50.00, 55.00,
143143
60.00, 65.00, 70.00, 75.00, 80.00, 85.00, 90.00, 95.00
@@ -249,7 +249,7 @@ void setup()
249249
// You can use up to 16 timer for each ISR_PWM
250250
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
251251
{
252-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
252+
//void setPWM(uint32_t pin, float frequency, float dutycycle
253253
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
254254

255255
#if USING_PWM_FREQUENCY

examples/ISR_16_PWMs_Array_Complex/ISR_16_PWMs_Array_Complex.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,21 @@ uint32_t PWM_Pin[] =
132132
#endif
133133

134134
// You can assign any interval for any timer here, in microseconds
135-
double PWM_Period[] =
135+
uint32_t PWM_Period[] =
136136
{
137-
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.666, 142857.143, 125000.0,
138-
111111.111, 100000.0, 66666.666, 50000.0, 40000.0, 33333.333, 25000.0, 20000.0
137+
1000000, 500000, 333333, 250000, 200000, 166667, 142857, 125000,
138+
111111, 100000, 66667, 50000, 40000, 33333, 25000, 20000
139139
};
140140

141141
// You can assign any interval for any timer here, in Hz
142-
double PWM_Freq[] =
142+
float PWM_Freq[] =
143143
{
144144
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
145145
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
146146
};
147147

148148
// You can assign any interval for any timer here, in milliseconds
149-
double PWM_DutyCycle[] =
149+
float PWM_DutyCycle[] =
150150
{
151151
5.00, 10.00, 20.00, 30.00, 40.00, 45.00, 50.00, 55.00,
152152
60.00, 65.00, 70.00, 75.00, 80.00, 85.00, 90.00, 95.00

examples/ISR_16_PWMs_Array_Simple/ISR_16_PWMs_Array_Simple.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,21 @@ uint32_t PWM_Pin[] =
123123
#endif
124124

125125
// You can assign any interval for any timer here, in microseconds
126-
double PWM_Period[] =
126+
uint32_t PWM_Period[] =
127127
{
128-
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.666, 142857.143, 125000.0,
129-
111111.111, 100000.0, 66666.666, 50000.0, 40000.0, 33333.333, 25000.0, 20000.0
128+
1000000, 500000, 333333, 250000, 200000, 166667, 142857, 125000,
129+
111111, 100000, 66667, 50000, 40000, 33333, 25000, 20000
130130
};
131131

132132
// You can assign any interval for any timer here, in Hz
133-
double PWM_Freq[] =
133+
float PWM_Freq[] =
134134
{
135135
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
136136
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
137137
};
138138

139139
// You can assign any interval for any timer here, in milliseconds
140-
double PWM_DutyCycle[] =
140+
float PWM_DutyCycle[] =
141141
{
142142
5.00, 10.00, 20.00, 30.00, 40.00, 45.00, 50.00, 55.00,
143143
60.00, 65.00, 70.00, 75.00, 80.00, 85.00, 90.00, 95.00
@@ -170,7 +170,7 @@ void setup()
170170
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
171171
//for (uint16_t i = 0; i < 1; i++)
172172
{
173-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
173+
//void setPWM(uint32_t pin, float frequency, float dutycycle
174174
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
175175

176176
#if USING_PWM_FREQUENCY

examples/ISR_Changing_PWM/ISR_Changing_PWM.ino

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,19 @@ bool IRAM_ATTR TimerHandler(void * timerNo)
6969
uint32_t PWM_Pin = LED_BUILTIN;
7070

7171
// You can assign any interval for any timer here, in Hz
72-
double PWM_Freq1 = 1.0f;
72+
float PWM_Freq1 = 1.0f;
7373
// You can assign any interval for any timer here, in Hz
74-
double PWM_Freq2 = 2.0f;
74+
float PWM_Freq2 = 2.0f;
7575

7676
// You can assign any interval for any timer here, in microseconds
77-
double PWM_Period1 = 1000000.0 / PWM_Freq1;
77+
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
7878
// You can assign any interval for any timer here, in microseconds
79-
double PWM_Period2 = 1000000.0 / PWM_Freq2;
79+
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;
8080

8181
// You can assign any duty_cycle for any PWM here, from 0-100
82-
double PWM_DutyCycle1 = 50.0;
82+
float PWM_DutyCycle1 = 50.0;
8383
// You can assign any duty_cycle for any PWM here, from 0-100
84-
double PWM_DutyCycle2 = 90.0;
84+
float PWM_DutyCycle2 = 90.0;
8585

8686
// Channel number used to identify associated channel
8787
int channelNum;
@@ -135,7 +135,7 @@ void loop()
135135

136136
delay(10000);
137137

138-
ISR_PWM.deleteChannel((unsigned) channelNum);
138+
ISR_PWM.deleteChannel((uint8_t) channelNum);
139139

140140
Serial.print(F("Using PWM Freq = ")); Serial.print(PWM_Freq2); Serial.print(F(", PWM DutyCycle = ")); Serial.println(PWM_DutyCycle2);
141141

@@ -156,5 +156,5 @@ void loop()
156156

157157
delay(10000);
158158

159-
ISR_PWM.deleteChannel((unsigned) channelNum);
159+
ISR_PWM.deleteChannel((uint8_t) channelNum);
160160
}

examples/ISR_Modify_PWM/ISR_Modify_PWM.ino

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,19 @@ bool IRAM_ATTR TimerHandler(void * timerNo)
7070
uint32_t PWM_Pin = LED_BUILTIN;
7171

7272
// You can assign any interval for any timer here, in Hz
73-
double PWM_Freq1 = 1.0f;
73+
float PWM_Freq1 = 1.0f;
7474
// You can assign any interval for any timer here, in Hz
75-
double PWM_Freq2 = 2.0f;
75+
float PWM_Freq2 = 2.0f;
7676

7777
// You can assign any interval for any timer here, in microseconds
78-
double PWM_Period1 = 1000000.0 / PWM_Freq1;
78+
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
7979
// You can assign any interval for any timer here, in microseconds
80-
double PWM_Period2 = 1000000.0 / PWM_Freq2;
80+
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;
8181

8282
// You can assign any duty_cycle for any PWM here, from 0-100
83-
double PWM_DutyCycle1 = 10.0;
83+
float PWM_DutyCycle1 = 10.0;
8484
// You can assign any duty_cycle for any PWM here, from 0-100
85-
double PWM_DutyCycle2 = 90.0;
85+
float PWM_DutyCycle2 = 90.0;
8686

8787
// Channel number used to identify associated channel
8888
int channelNum;
@@ -111,7 +111,7 @@ void setup()
111111

112112
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
113113
// You can use up to 16 timer for each ISR_PWM
114-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
114+
//void setPWM(uint32_t pin, float frequency, float dutycycle
115115
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
116116
Serial.print(F("Using PWM Freq = ")); Serial.print(PWM_Freq1); Serial.print(F(", PWM DutyCycle = ")); Serial.println(PWM_DutyCycle1);
117117

@@ -137,8 +137,8 @@ void changePWM()
137137
{
138138
static uint8_t count = 1;
139139

140-
double PWM_Freq;
141-
uint32_t PWM_DutyCycle;
140+
float PWM_Freq;
141+
float PWM_DutyCycle;
142142

143143
if (count++ % 2)
144144
{

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ESP32_PWM",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
44
"keywords": "timing, device, control, pwm, timer, interrupt, hardware, isr, isr-based, hardware-timer, isr-timer, isr-based-timer, mission-critical, accuracy, precise, non-blocking, esp32, esp32-s2, esp32-c3, synchronized-PWM, on-the-fly",
55
"description": "This library enables you to use Interrupt from Hardware Timers on an ESP32, ESP32_S2 or ESP32_C3-based board to create and output PWM to pins. It now supports 16 ISR-based synchronized PWM channels, while consuming only 1 Hardware Timer. PWM interval can be very long (uint32_t millisecs). The most important feature is they're ISR-based PWM channels. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. These hardware PWM channels, using interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to measure some data requiring better accuracy. Now you can change the PWM settings on-the-fly",
66
"authors":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP32_PWM
2-
version=1.2.1
2+
version=1.2.2
33
author=Khoi Hoang <[email protected]>
44
maintainer=Khoi Hoang <[email protected]>
55
sentence=This library enables you to use Interrupt from Hardware Timers on an ESP32, ESP32_S2 or ESP32_C3-based board to create and output PWM to pins.

src/ESP32_PWM.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
Therefore, their executions are not blocked by bad-behaving functions / tasks.
2222
This important feature is absolutely necessary for mission-critical tasks.
2323
24-
Version: 1.2.1
24+
Version: 1.2.2
2525
2626
Version Modified By Date Comments
2727
------- ----------- ---------- -----------
@@ -31,6 +31,7 @@
3131
1.1.1 K Hoang 09/11/2021 Fix examples to not use GPIO1/TX0 for core v2.0.1+
3232
1.2.0 K Hoang 29/01/2022 Fix multiple-definitions linker error. Improve accuracy. Fix bug
3333
1.2.1 K Hoang 30/01/2022 DutyCycle to be updated at the end current PWM period
34+
1.2.2 K Hoang 01/02/2022 Use float for DutyCycle and Freq, uint32_t for period. Optimize code
3435
*****************************************************************************************************************************/
3536

3637
#pragma once

0 commit comments

Comments
 (0)