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

Commit 56685a9

Browse files
authored
v1.2.3 to replace double with float
### Releases v1.2.3 1. Use `float` for `DutyCycle` and `Freq`, `uint32_t` for `period`. 2. Optimize code by not calculation in ISR
1 parent 80b3b93 commit 56685a9

File tree

16 files changed

+215
-194
lines changed

16 files changed

+215
-194
lines changed

README.md

Lines changed: 110 additions & 101 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.3](#releases-v123)
1516
* [Releases v1.2.2](#releases-v122)
1617
* [Releases v1.2.1](#releases-v121)
1718
* [Releases v1.2.0](#releases-v120)
@@ -23,6 +24,11 @@
2324

2425
## Changelog
2526

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

2834
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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,26 +88,26 @@ void IRAM_ATTR TimerHandler()
8888
//////////////////////////////////////////////////////
8989

9090
// You can assign pins here. Be carefull to select good pin to use or crash
91-
uint32_t PWM_Pin[NUMBER_ISR_PWMS] =
91+
uint32_t PWM_Pin[] =
9292
{
9393
PIN_D0, PIN_D1, LED_BUILTIN, PIN_D3, PIN_D4, PIN_D5, PIN_D6, PIN_D7
9494
};
9595

9696
// You can assign any interval for any timer here, in microseconds
97-
double PWM_Period[NUMBER_ISR_PWMS] =
97+
uint32_t PWM_Period[] =
9898
{
99-
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.667, 142857.143, 125000.0
99+
1000000, 500000, 333333, 250000, 200000, 166667, 142857, 125000
100100
};
101101

102102

103103
// You can assign any interval for any timer here, in Hz
104-
double PWM_Freq[NUMBER_ISR_PWMS] =
104+
float PWM_Freq[] =
105105
{
106106
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0
107107
};
108108

109109
// You can assign any interval for any timer here, in Microseconds
110-
double PWM_DutyCycle[NUMBER_ISR_PWMS] =
110+
float PWM_DutyCycle[] =
111111
{
112112
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0
113113
};
@@ -152,7 +152,7 @@ void doingSomething7()
152152
}
153153

154154

155-
irqCallback irqCallbackStartFunc[NUMBER_ISR_PWMS] =
155+
irqCallback irqCallbackStartFunc[] =
156156
{
157157
doingSomething0, doingSomething1, doingSomething2, doingSomething3,
158158
doingSomething4, doingSomething5, doingSomething6, doingSomething7
@@ -186,7 +186,7 @@ void setup()
186186
// You can use up to 16 timer for each ISR_PWM
187187
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
188188
{
189-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
189+
//void setPWM(uint32_t pin, float frequency, float dutycycle
190190
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
191191

192192
#if USING_PWM_FREQUENCY

examples/ISR_16_PWMs_Array_Complex/ISR_16_PWMs_Array_Complex.ino

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ typedef struct
106106
irqCallback irqCallbackStopFunc;
107107

108108
#if USING_PWM_FREQUENCY
109-
double PWM_Freq;
109+
float PWM_Freq;
110110
#else
111-
double PWM_Period;
111+
uint32_t PWM_Period;
112112
#endif
113113

114-
double PWM_DutyCycle;
114+
float PWM_DutyCycle;
115115

116116
uint64_t deltaMicrosStart;
117117
uint64_t previousMicrosStart;
@@ -130,33 +130,32 @@ void doingSomethingStop(int index);
130130

131131
#else // #if USE_COMPLEX_STRUCT
132132

133-
volatile unsigned long deltaMicrosStart [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
134-
volatile unsigned long previousMicrosStart [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
133+
volatile unsigned long deltaMicrosStart [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
134+
volatile unsigned long previousMicrosStart [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
135135

136-
volatile unsigned long deltaMicrosStop [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
137-
volatile unsigned long previousMicrosStop [NUMBER_ISR_PWMS] = { 0, 0, 0, 0, 0, 0, 0, 0 };
136+
volatile unsigned long deltaMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
137+
volatile unsigned long previousMicrosStop [] = { 0, 0, 0, 0, 0, 0, 0, 0 };
138138

139139
// You can assign pins here. Be carefull to select good pin to use or crash
140-
uint32_t PWM_Pin[NUMBER_ISR_PWMS] =
140+
uint32_t PWM_Pin[] =
141141
{
142142
PIN_D0, PIN_D1, LED_BUILTIN, PIN_D3, PIN_D4, PIN_D5, PIN_D6, PIN_D7
143143
};
144144

145145
// You can assign any interval for any timer here, in microseconds
146-
double PWM_Period[NUMBER_ISR_PWMS] =
146+
uint32_t PWM_Period[] =
147147
{
148-
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.667, 142857.143, 125000.0
148+
1000000, 500000, 333333, 250000, 200000, 166667, 142857, 125000
149149
};
150150

151-
152151
// You can assign any interval for any timer here, in Hz
153-
double PWM_Freq[NUMBER_ISR_PWMS] =
152+
float PWM_Freq[] =
154153
{
155154
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0
156155
};
157156

158157
// You can assign any interval for any timer here, in Microseconds
159-
double PWM_DutyCycle[NUMBER_ISR_PWMS] =
158+
float PWM_DutyCycle[] =
160159
{
161160
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0
162161
};
@@ -275,9 +274,9 @@ void doingSomethingStop7()
275274

276275
#if USING_PWM_FREQUENCY
277276

278-
ISR_PWM_Data curISR_PWM_Data[NUMBER_ISR_PWMS] =
277+
ISR_PWM_Data curISR_PWM_Data[] =
279278
{
280-
//pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Period, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
279+
//pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Freq, PWM_DC, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
281280
{ PIN_D0, doingSomethingStart0, doingSomethingStop0, 1.0, 5.0, 0, 0, 0, 0 },
282281
{ PIN_D1, doingSomethingStart1, doingSomethingStop1, 2.0, 10.0, 0, 0, 0, 0 },
283282
{ LED_BUILTIN, doingSomethingStart2, doingSomethingStop2, 3.0, 20.0, 0, 0, 0, 0 },
@@ -290,17 +289,17 @@ void doingSomethingStop7()
290289

291290
#else // #if USING_PWM_FREQUENCY
292291

293-
ISR_PWM_Data curISR_PWM_Data[NUMBER_ISR_PWMS] =
292+
ISR_PWM_Data curISR_PWM_Data[] =
294293
{
295-
//irqCallbackStartFunc, PWM_Period, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
296-
{ PIN_D0, doingSomethingStart0, doingSomethingStop0, 1000000.0, 5.0, 0, 0, 0, 0 },
297-
{ PIN_D1, doingSomethingStart1, doingSomethingStop1, 500000.0, 10.0, 0, 0, 0, 0 },
298-
{ LED_BUILTIN, doingSomethingStart2, doingSomethingStop2, 333333,333, 20.0, 0, 0, 0, 0 },
299-
{ PIN_D3, doingSomethingStart3, doingSomethingStop3, 250000.0, 30.0, 0, 0, 0, 0 },
300-
{ PIN_D4, doingSomethingStart4, doingSomethingStop4, 200000.0, 40.0, 0, 0, 0, 0 },
301-
{ PIN_D5, doingSomethingStart5, doingSomethingStop5, 166666.667, 45.0, 0, 0, 0, 0 },
302-
{ PIN_D6, doingSomethingStart6, doingSomethingStop6, 142857.143, 50.0, 0, 0, 0, 0 },
303-
{ PIN_D7, doingSomethingStart7, doingSomethingStop7, 125000.0, 55.0, 0, 0, 0, 0 },
294+
//pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Period, PWM_DC, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
295+
{ PIN_D0, doingSomethingStart0, doingSomethingStop0, 1000000, 5.0, 0, 0, 0, 0 },
296+
{ PIN_D1, doingSomethingStart1, doingSomethingStop1, 500000, 10.0, 0, 0, 0, 0 },
297+
{ LED_BUILTIN, doingSomethingStart2, doingSomethingStop2, 333333, 20.0, 0, 0, 0, 0 },
298+
{ PIN_D3, doingSomethingStart3, doingSomethingStop3, 250000, 30.0, 0, 0, 0, 0 },
299+
{ PIN_D4, doingSomethingStart4, doingSomethingStop4, 200000, 40.0, 0, 0, 0, 0 },
300+
{ PIN_D5, doingSomethingStart5, doingSomethingStop5, 166667, 45.0, 0, 0, 0, 0 },
301+
{ PIN_D6, doingSomethingStart6, doingSomethingStop6, 142857, 50.0, 0, 0, 0, 0 },
302+
{ PIN_D7, doingSomethingStart7, doingSomethingStop7, 125000 55.0, 0, 0, 0, 0 },
304303
};
305304

306305
#endif // #if USING_PWM_FREQUENCY
@@ -325,13 +324,13 @@ void doingSomethingStop(int index)
325324

326325
#else // #if USE_COMPLEX_STRUCT
327326

328-
irqCallback irqCallbackStartFunc[NUMBER_ISR_PWMS] =
327+
irqCallback irqCallbackStartFunc[] =
329328
{
330329
doingSomethingStart0, doingSomethingStart1, doingSomethingStart2, doingSomethingStart3,
331330
doingSomethingStart4, doingSomethingStart5, doingSomethingStart6, doingSomethingStart7
332331
};
333332

334-
irqCallback irqCallbackStopFunc[NUMBER_ISR_PWMS] =
333+
irqCallback irqCallbackStopFunc[] =
335334
{
336335
doingSomethingStop0, doingSomethingStop1, doingSomethingStop2, doingSomethingStop3,
337336
doingSomethingStop4, doingSomethingStop5, doingSomethingStop6, doingSomethingStop7
@@ -435,7 +434,7 @@ void setup()
435434
curISR_PWM_Data[i].previousMicrosStart = startMicros;
436435
//ISR_PWM.setInterval(curISR_PWM_Data[i].PWM_Period, curISR_PWM_Data[i].irqCallbackStartFunc);
437436

438-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
437+
//void setPWM(uint32_t pin, float frequency, float dutycycle
439438
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
440439

441440
#if USING_PWM_FREQUENCY

examples/ISR_16_PWMs_Array_Simple/ISR_16_PWMs_Array_Simple.ino

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,20 +94,19 @@ uint32_t PWM_Pin[NUMBER_ISR_PWMS] =
9494
};
9595

9696
// You can assign any interval for any timer here, in microseconds
97-
double PWM_Period[NUMBER_ISR_PWMS] =
97+
uint32_t PWM_Period[] =
9898
{
99-
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.667, 142857.143, 125000.0
99+
1000000, 500000, 333333, 250000, 200000, 166667, 142857, 125000
100100
};
101101

102-
103102
// You can assign any interval for any timer here, in Hz
104-
double PWM_Freq[NUMBER_ISR_PWMS] =
103+
float PWM_Freq[] =
105104
{
106105
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0
107106
};
108107

109108
// You can assign any interval for any timer here, in Microseconds
110-
double PWM_DutyCycle[NUMBER_ISR_PWMS] =
109+
float PWM_DutyCycle[] =
111110
{
112111
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0
113112
};
@@ -140,7 +139,7 @@ void setup()
140139
// You can use up to 16 timer for each ISR_PWM
141140
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
142141
{
143-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
142+
//void setPWM(uint32_t pin, float frequency, float dutycycle
144143
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
145144

146145
#if USING_PWM_FREQUENCY

examples/ISR_Changing_PWM/ISR_Changing_PWM.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,19 @@ void IRAM_ATTR TimerHandler()
8989
uint32_t PWM_Pin = LED_BUILTIN;
9090

9191
// You can assign any interval for any timer here, in Hz
92-
double PWM_Freq1 = 1.0f;
92+
float PWM_Freq1 = 1.0f;
9393
// You can assign any interval for any timer here, in Hz
94-
double PWM_Freq2 = 2.0f;
94+
float PWM_Freq2 = 2.0f;
9595

9696
// You can assign any interval for any timer here, in microseconds
97-
double PWM_Period1 = 1000000.0 / PWM_Freq1;
97+
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
9898
// You can assign any interval for any timer here, in microseconds
99-
double PWM_Period2 = 1000000.0 / PWM_Freq2;
99+
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;
100100

101101
// You can assign any duty_cycle for any PWM here, from 0-100
102-
double PWM_DutyCycle1 = 50.0;
102+
float PWM_DutyCycle1 = 50.0;
103103
// You can assign any duty_cycle for any PWM here, from 0-100
104-
double PWM_DutyCycle2 = 90.0;
104+
float PWM_DutyCycle2 = 90.0;
105105

106106
// Channel number used to identify associated channel
107107
int channelNum;
@@ -130,7 +130,7 @@ void setup()
130130

131131
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
132132
// You can use up to 16 timer for each ISR_PWM
133-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
133+
//void setPWM(uint32_t pin, float frequency, float dutycycle
134134
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
135135
}
136136

examples/ISR_Modify_PWM/ISR_Modify_PWM.ino

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,19 @@ void IRAM_ATTR TimerHandler()
6868
uint32_t PWM_Pin = LED_BUILTIN;
6969

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

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

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

8585
// Channel number used to identify associated channel
8686
int channelNum;
@@ -109,7 +109,7 @@ void setup()
109109

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

@@ -135,8 +135,8 @@ void changePWM()
135135
{
136136
static uint8_t count = 1;
137137

138-
double PWM_Freq;
139-
uint32_t PWM_DutyCycle;
138+
float PWM_Freq;
139+
float PWM_DutyCycle;
140140

141141
if (count++ % 2)
142142
{

examples/multiFileProject/multiFileProject.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
1515
#endif
1616

17-
#define ESP8266_PWM_VERSION_MIN_TARGET "ESP8266_PWM v1.2.2"
18-
#define ESP8266_PWM_VERSION_MIN 1002002
17+
#define ESP8266_PWM_VERSION_MIN_TARGET "ESP8266_PWM v1.2.3"
18+
#define ESP8266_PWM_VERSION_MIN 1002003
1919

2020
#include "multiFileProject.h"
2121

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ESP8266_PWM",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
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, ESP8266, synchronized-PWM, on-the-fly",
55
"description": "This library enables you to use Interrupt from Hardware Timers on an ESP8266-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=ESP8266_PWM
2-
version=1.2.2
2+
version=1.2.3
33
author=Khoi Hoang <[email protected]>
44
maintainer=Khoi Hoang <[email protected]>
55
sentence=TThis library enables you to use Interrupt from Hardware Timers on an ESP8266-based board to create and output PWM to pins.

0 commit comments

Comments
 (0)