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

Commit de7814b

Browse files
authored
v1.2.1 to replace double with float
### Releases v1.2.1 1. Use `float` for `DutyCycle` and `Freq`, `uint32_t` for `period`. 2. Optimize code by not calculation in ISR
1 parent 682cb6d commit de7814b

File tree

23 files changed

+306
-276
lines changed

23 files changed

+306
-276
lines changed

README.md

Lines changed: 152 additions & 131 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.1](#releases-v121)
1516
* [Releases v1.2.0](#releases-v120)
1617
* [Releases v1.1.0](#releases-v110)
1718
* [Initial Releases v1.0.0](#Initial-Releases-v100)
@@ -21,6 +22,11 @@
2122

2223
## Changelog
2324

25+
### Releases v1.2.1
26+
27+
1. Use `float` for `DutyCycle` and `Freq`, `uint32_t` for `period`.
28+
2. Optimize code by not calculation in ISR
29+
2430
### Releases v1.2.0
2531

2632
1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories

examples/SAMD21/ISR_4_PWMs_Array/ISR_4_PWMs_Array.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,19 @@ uint32_t PWM_Pin[] =
8989
};
9090

9191
// You can assign any interval for any timer here, in microseconds
92-
double PWM_Period[] =
92+
uint32_t PWM_Period[] =
9393
{
94-
1000000.0, 500000.0, 333333.333, 50000.0
94+
1000000, 500000, 250000, 200000
9595
};
9696

9797
// You can assign any interval for any timer here, in Hz
98-
double PWM_Freq[] =
98+
float PWM_Freq[] =
9999
{
100-
1.0, 2.0, 3.0, 4.0
100+
1.0, 2.0, 4.0, 5.0
101101
};
102102

103103
// You can assign any interval for any timer here, in milliseconds
104-
double PWM_DutyCycle[] =
104+
float PWM_DutyCycle[] =
105105
{
106106
40.00, 45.00, 50.00, 55.00
107107
};
@@ -160,7 +160,7 @@ void setup()
160160
// You can use up to 16 timer for each ISR_PWM
161161
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
162162
{
163-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
163+
//void setPWM(uint32_t pin, float frequency, float dutycycle
164164
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
165165

166166
#if USING_PWM_FREQUENCY

examples/SAMD21/ISR_4_PWMs_Array_Complex/ISR_4_PWMs_Array_Complex.ino

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,12 @@ typedef struct
110110
irqCallback irqCallbackStopFunc;
111111

112112
#if USING_PWM_FREQUENCY
113-
double PWM_Freq;
113+
float PWM_Freq;
114114
#else
115-
double PWM_Period;
115+
uint32_t PWM_Period;
116116
#endif
117117

118-
double PWM_DutyCycle;
118+
float PWM_DutyCycle;
119119

120120
uint64_t deltaMicrosStart;
121121
uint64_t previousMicrosStart;
@@ -148,19 +148,19 @@ uint32_t PWM_Pin[] =
148148
};
149149

150150
// You can assign any interval for any timer here, in microseconds
151-
double PWM_Period[] =
151+
uint32_t PWM_Period[] =
152152
{
153-
1000000.0, 500000.0, 333333.333, 50000.0
153+
1000000, 500000, 250000, 200000
154154
};
155155

156156
// You can assign any interval for any timer here, in Hz
157-
double PWM_Freq[] =
157+
float PWM_Freq[] =
158158
{
159-
1.0, 2.0, 3.0, 4.0
159+
1.0, 2.0, 4.0, 5.0
160160
};
161161

162162
// You can assign any interval for any timer here, in milliseconds
163-
double PWM_DutyCycle[] =
163+
float PWM_DutyCycle[] =
164164
{
165165
40.00, 45.00, 50.00, 55.00
166166
};
@@ -243,19 +243,19 @@ void doingSomethingStop3()
243243
// pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Freq, PWM_DutyCycle, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
244244
{ LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1.0, 5.0, 0, 0, 0, 0 },
245245
{ PIN_D2, doingSomethingStart1, doingSomethingStop1, 2.0, 10.0, 0, 0, 0, 0 },
246-
{ PIN_D7, doingSomethingStart2, doingSomethingStop2, 15.0, 20.0, 0, 0, 0, 0 },
247-
{ PIN_D8, doingSomethingStart3, doingSomethingStop3, 20.0, 30.0, 0, 0, 0, 0 },
246+
{ PIN_D7, doingSomethingStart2, doingSomethingStop2, 4.0, 20.0, 0, 0, 0, 0 },
247+
{ PIN_D8, doingSomethingStart3, doingSomethingStop3, 5.0, 30.0, 0, 0, 0, 0 },
248248
};
249249

250250
#else // #if USING_PWM_FREQUENCY
251251

252252
ISR_PWM_Data curISR_PWM_Data[] =
253253
{
254254
// pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Period, PWM_DutyCycle, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
255-
{ LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1000000.0, 5.0, 0, 0, 0, 0 },
256-
{ PIN_D2, doingSomethingStart5, doingSomethingStop1, 166666.667, 10.0, 0, 0, 0, 0 },
257-
{ PIN_D7, doingSomethingStart2, doingSomethingStop2, 66666.667, 20.0, 0, 0, 0, 0 },
258-
{ PIN_D8, doingSomethingStart3, doingSomethingStop3, 50000.0, 30.0, 0, 0, 0, 0 },
255+
{ LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1000000, 5.0, 0, 0, 0, 0 },
256+
{ PIN_D2, doingSomethingStart5, doingSomethingStop1, 500000, 10.0, 0, 0, 0, 0 },
257+
{ PIN_D7, doingSomethingStart2, doingSomethingStop2, 250000, 20.0, 0, 0, 0, 0 },
258+
{ PIN_D8, doingSomethingStart3, doingSomethingStop3, 200000, 30.0, 0, 0, 0, 0 },
259259
};
260260

261261
#endif // #if USING_PWM_FREQUENCY

examples/SAMD21/ISR_4_PWMs_Array_Simple/ISR_4_PWMs_Array_Simple.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,19 @@ uint32_t PWM_Pin[] =
9999
};
100100

101101
// You can assign any interval for any timer here, in microseconds
102-
double PWM_Period[] =
102+
uint32_t PWM_Period[] =
103103
{
104-
1000000.0, 500000.0, 333333.333, 50000.0
104+
1000000, 500000, 250000, 200000
105105
};
106106

107107
// You can assign any interval for any timer here, in Hz
108-
double PWM_Freq[] =
108+
float PWM_Freq[] =
109109
{
110-
1.0, 2.0, 3.0, 4.0
110+
1.0, 2.0, 4.0, 5.0
111111
};
112112

113113
// You can assign any interval for any timer here, in milliseconds
114-
double PWM_DutyCycle[] =
114+
float PWM_DutyCycle[] =
115115
{
116116
40.00, 45.00, 50.00, 55.00
117117
};

examples/SAMD21/ISR_Changing_PWM/ISR_Changing_PWM.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,19 @@ void TimerHandler()
8282
uint32_t PWM_Pin = LED_BUILTIN;
8383

8484
// You can assign any interval for any timer here, in Hz
85-
double PWM_Freq1 = 1.0f;
85+
float PWM_Freq1 = 1.0f;
8686
// You can assign any interval for any timer here, in Hz
87-
double PWM_Freq2 = 2.0f;
87+
float PWM_Freq2 = 2.0f;
8888

8989
// You can assign any interval for any timer here, in microseconds
90-
double PWM_Period1 = 1000000.0 / PWM_Freq1;
90+
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
9191
// You can assign any interval for any timer here, in microseconds
92-
double PWM_Period2 = 1000000.0 / PWM_Freq2;
92+
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;
9393

9494
// You can assign any duty_cycle for any PWM here, from 0-100
95-
double PWM_DutyCycle1 = 50.0;
95+
float PWM_DutyCycle1 = 50.0;
9696
// You can assign any duty_cycle for any PWM here, from 0-100
97-
double PWM_DutyCycle2 = 90.0;
97+
float PWM_DutyCycle2 = 90.0;
9898

9999
// Channel number used to identify associated channel
100100
int8_t channelNum;

examples/SAMD21/ISR_Modify_PWM/ISR_Modify_PWM.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,19 @@ void TimerHandler()
8282
uint32_t PWM_Pin = LED_BUILTIN;
8383

8484
// You can assign any interval for any timer here, in Hz
85-
double PWM_Freq1 = 1.0f;
85+
float PWM_Freq1 = 1.0f;
8686
// You can assign any interval for any timer here, in Hz
87-
double PWM_Freq2 = 2.0f;
87+
float PWM_Freq2 = 2.0f;
8888

8989
// You can assign any interval for any timer here, in microseconds
90-
double PWM_Period1 = 1000000.0 / PWM_Freq1;
90+
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
9191
// You can assign any interval for any timer here, in microseconds
92-
double PWM_Period2 = 1000000.0 / PWM_Freq2;
92+
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;
9393

9494
// You can assign any duty_cycle for any PWM here, from 0-100
95-
double PWM_DutyCycle1 = 50.0;
95+
float PWM_DutyCycle1 = 50.0;
9696
// You can assign any duty_cycle for any PWM here, from 0-100
97-
double PWM_DutyCycle2 = 90.0;
97+
float PWM_DutyCycle2 = 90.0;
9898

9999
// Channel number used to identify associated channel
100100
int channelNum;

examples/SAMD21/multiFileProject/multiFileProject.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#error This code is designed to run on SAMD21 platform! Please check your Tools->Board setting.
2020
#endif
2121

22-
#define SAMD_SLOW_PWM_VERSION_MIN_TARGET "SAMD_Slow_PWM v1.2.0"
23-
#define SAMD_SLOW_PWM_VERSION_MIN 1002000
22+
#define SAMD_SLOW_PWM_VERSION_MIN_TARGET "SAMD_Slow_PWM v1.2.1"
23+
#define SAMD_SLOW_PWM_VERSION_MIN 1002001
2424

2525
#include "multiFileProject.h"
2626

examples/SAMD51/ISR_16_PWMs_Array/ISR_16_PWMs_Array.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,21 @@ uint32_t PWM_Pin[] =
9797
};
9898

9999
// You can assign any interval for any timer here, in microseconds
100-
double PWM_Period[] =
100+
uint32_t PWM_Period[] =
101101
{
102-
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.666, 142857.143, 125000.0,
103-
111111.111, 100000.0, 66666.666, 50000.0, 40000.0, 33333.333, 25000.0, 20000.0
102+
10000000, 5000000, 333333, 250000, 200000, 166667, 142857, 125000,
103+
111111, 100000, 66667, 50000, 40000, 33333, 25000, 20000
104104
};
105105

106106
// You can assign any interval for any timer here, in Hz
107-
double PWM_Freq[] =
107+
float PWM_Freq[] =
108108
{
109109
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
110110
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
111111
};
112112

113113
// You can assign any interval for any timer here, in milliseconds
114-
double PWM_DutyCycle[] =
114+
float PWM_DutyCycle[] =
115115
{
116116
5.00, 10.00, 20.00, 30.00, 40.00, 45.00, 50.00, 55.00,
117117
60.00, 65.00, 70.00, 75.00, 80.00, 85.00, 90.00, 95.00
@@ -222,7 +222,7 @@ void setup()
222222
// You can use up to 16 timer for each ISR_PWM
223223
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
224224
{
225-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
225+
//void setPWM(uint32_t pin, float frequency, float dutycycle
226226
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
227227

228228
#if USING_PWM_FREQUENCY

examples/SAMD51/ISR_16_PWMs_Array_Complex/ISR_16_PWMs_Array_Complex.ino

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,12 @@ typedef struct
107107
irqCallback irqCallbackStopFunc;
108108

109109
#if USING_PWM_FREQUENCY
110-
double PWM_Freq;
110+
float PWM_Freq;
111111
#else
112-
double PWM_Period;
112+
uint32_t PWM_Period;
113113
#endif
114114

115-
double PWM_DutyCycle;
115+
float PWM_DutyCycle;
116116

117117
uint64_t deltaMicrosStart;
118118
uint64_t previousMicrosStart;
@@ -146,21 +146,21 @@ uint32_t PWM_Pin[] =
146146
};
147147

148148
// You can assign any interval for any timer here, in microseconds
149-
double PWM_Period[] =
149+
uint32_t PWM_Period[] =
150150
{
151-
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.666, 142857.143, 125000.0,
152-
111111.111, 100000.0, 66666.666, 50000.0, 40000.0, 33333.333, 25000.0, 20000.0
151+
10000000, 5000000, 333333, 250000, 200000, 166667, 142857, 125000,
152+
111111, 100000, 66667, 50000, 40000, 33333, 25000, 20000
153153
};
154154

155155
// You can assign any interval for any timer here, in Hz
156-
double PWM_Freq[] =
156+
float PWM_Freq[] =
157157
{
158158
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
159159
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
160160
};
161161

162162
// You can assign any interval for any timer here, in milliseconds
163-
double PWM_DutyCycle[] =
163+
float PWM_DutyCycle[] =
164164
{
165165
5.00, 10.00, 20.00, 30.00, 40.00, 45.00, 50.00, 55.00,
166166
60.00, 65.00, 70.00, 75.00, 80.00, 85.00, 90.00, 95.00
@@ -383,22 +383,22 @@ void doingSomethingStop15()
383383
ISR_PWM_Data curISR_PWM_Data[] =
384384
{
385385
// pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Period, PWM_DutyCycle, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
386-
{ LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1000000.0, 5.0, 0, 0, 0, 0 },
387-
{ LED_BLUE, doingSomethingStart1, doingSomethingStop1, 500000.0, 10.0, 0, 0, 0, 0 },
388-
{ LED_RED, doingSomethingStart2, doingSomethingStop2, 333333.333, 20.0, 0, 0, 0, 0 },
389-
{ PIN_D0, doingSomethingStart3, doingSomethingStop3, 250000.0, 30.0, 0, 0, 0, 0 },
390-
{ PIN_D1, doingSomethingStart4, doingSomethingStop4, 200000.0, 40.0, 0, 0, 0, 0 },
391-
{ PIN_D2, doingSomethingStart5, doingSomethingStop5, 166667.667, 45.0, 0, 0, 0, 0 },
392-
{ PIN_D3, doingSomethingStart6, doingSomethingStop6, 142857.143, 50.0, 0, 0, 0, 0 },
393-
{ PIN_D4, doingSomethingStart7, doingSomethingStop7, 125000.0, 55.0, 0, 0, 0, 0 },
394-
{ PIN_D5, doingSomethingStart8, doingSomethingStop8, 111111.111, 60.0, 0, 0, 0, 0 },
395-
{ PIN_D6, doingSomethingStart9, doingSomethingStop9, 100000.0, 65.0, 0, 0, 0, 0 },
396-
{ PIN_D7, doingSomethingStart10, doingSomethingStop10, 66666.667, 70.0, 0, 0, 0, 0 },
397-
{ PIN_D8, doingSomethingStart11, doingSomethingStop11, 50000.0, 75.0, 0, 0, 0, 0 },
398-
{ PIN_D9, doingSomethingStart12, doingSomethingStop12, 40000.0, 80.0, 0, 0, 0, 0 },
399-
{ PIN_D10, doingSomethingStart13, doingSomethingStop13, 33333.333, 85.0, 0, 0, 0, 0 },
400-
{ PIN_D11, doingSomethingStart14, doingSomethingStop14, 25000.0, 90.0, 0, 0, 0, 0 },
401-
{ PIN_D12, doingSomethingStart15, doingSomethingStop15, 20000.0, 95.0, 0, 0, 0, 0 }
386+
{ LED_BUILTIN, doingSomethingStart0, doingSomethingStop0, 1000000, 5.0, 0, 0, 0, 0 },
387+
{ LED_BLUE, doingSomethingStart1, doingSomethingStop1, 500000, 10.0, 0, 0, 0, 0 },
388+
{ LED_RED, doingSomethingStart2, doingSomethingStop2, 333333, 20.0, 0, 0, 0, 0 },
389+
{ PIN_D0, doingSomethingStart3, doingSomethingStop3, 250000, 30.0, 0, 0, 0, 0 },
390+
{ PIN_D1, doingSomethingStart4, doingSomethingStop4, 200000, 40.0, 0, 0, 0, 0 },
391+
{ PIN_D2, doingSomethingStart5, doingSomethingStop5, 166667, 45.0, 0, 0, 0, 0 },
392+
{ PIN_D3, doingSomethingStart6, doingSomethingStop6, 142857, 50.0, 0, 0, 0, 0 },
393+
{ PIN_D4, doingSomethingStart7, doingSomethingStop7, 125000, 55.0, 0, 0, 0, 0 },
394+
{ PIN_D5, doingSomethingStart8, doingSomethingStop8, 111111, 60.0, 0, 0, 0, 0 },
395+
{ PIN_D6, doingSomethingStart9, doingSomethingStop9, 100000, 65.0, 0, 0, 0, 0 },
396+
{ PIN_D7, doingSomethingStart10, doingSomethingStop10, 66667, 70.0, 0, 0, 0, 0 },
397+
{ PIN_D8, doingSomethingStart11, doingSomethingStop11, 50000, 75.0, 0, 0, 0, 0 },
398+
{ PIN_D9, doingSomethingStart12, doingSomethingStop12, 40000, 80.0, 0, 0, 0, 0 },
399+
{ PIN_D10, doingSomethingStart13, doingSomethingStop13, 33333, 85.0, 0, 0, 0, 0 },
400+
{ PIN_D11, doingSomethingStart14, doingSomethingStop14, 25000, 90.0, 0, 0, 0, 0 },
401+
{ PIN_D12, doingSomethingStart15, doingSomethingStop15, 20000, 95.0, 0, 0, 0, 0 }
402402
};
403403

404404
#endif // #if USING_PWM_FREQUENCY
@@ -536,7 +536,7 @@ void setup()
536536
curISR_PWM_Data[i].previousMicrosStart = startMicros;
537537
//ISR_PWM.setInterval(curISR_PWM_Data[i].PWM_Period, curISR_PWM_Data[i].irqCallbackStartFunc);
538538

539-
//void setPWM(uint32_t pin, uint32_t frequency, uint32_t dutycycle
539+
//void setPWM(uint32_t pin, float frequency, float dutycycle
540540
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
541541

542542
#if USING_PWM_FREQUENCY

0 commit comments

Comments
 (0)