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

Commit f85307b

Browse files
authored
v1.2.0 to fix multiple-definitions linker error
### Releases v1.2.0 1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories 2. Add feature to select among highest, medium or lowest accuracy for Timers for shortest, medium or longest time 3. Fix reattachInterrupt() bug. Check [bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19](khoih-prog/ESP8266TimerInterrupt#19) 4. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project 5. Improve accuracy by using `double`, instead of `uint32_t` for `dutycycle`, `period`. Check [Change Duty Cycle #1](#1 (comment)) 6. Update examples accordingly
1 parent a47bbab commit f85307b

19 files changed

+1131
-856
lines changed

CONTRIBUTING.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p
1414

1515
Please ensure to specify the following:
1616

17-
* Arduino IDE version (e.g. 1.8.16) or Platform.io version
17+
* Arduino IDE version (e.g. 1.8.19) or Platform.io version
1818
* `ESP8266` Core Version (e.g. ESP8266 core v3.0.2)
1919
* Contextual information (e.g. what you were trying to achieve)
2020
* Simplest possible steps to reproduce
@@ -26,11 +26,11 @@ Please ensure to specify the following:
2626
### Example
2727

2828
```
29-
Arduino IDE version: 1.8.16
30-
ESP8266 core v3.0.2
31-
ESP8266_NODEMCU_ESP12E Module
29+
Arduino IDE version: v1.8.19
30+
ESP8266 Core Version v3.0.2
31+
ESP8266_NODEMCU
3232
OS: Ubuntu 20.04 LTS
33-
Linux xy-Inspiron-3593 5.4.0-86-generic #97-Ubuntu SMP Fri Sep 17 19:19:40 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
33+
Linux xy-Inspiron-3593 5.4.0-96-generic #109-Ubuntu SMP Wed Jan 12 16:49:16 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
3434
3535
Context:
3636
I encountered a crash while using TimerInterrupt.

README.md

+143-142
Large diffs are not rendered by default.

changelog.md

+10
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
## Table of Contents
1313

1414
* [Changelog](#changelog)
15+
* [Releases v1.2.0](#releases-v120)
1516
* [Releases v1.1.0](#releases-v110)
1617
* [Releases v1.0.0](#releases-v100)
1718

@@ -20,6 +21,15 @@
2021

2122
## Changelog
2223

24+
### Releases v1.2.0
25+
26+
1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
27+
2. Add feature to select among highest, medium or lowest accuracy for Timers for shortest, medium or longest time
28+
3. Fix reattachInterrupt() bug. Check [bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19](https://github.com/khoih-prog/ESP8266TimerInterrupt/pull/19)
29+
4. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project
30+
5. Improve accuracy by using `double`, instead of `uint32_t` for `dutycycle`, `period`. Check [Change Duty Cycle #1](https://github.com/khoih-prog/ESP8266_PWM/issues/1#issuecomment-1024969658)
31+
6. Update examples accordingly
32+
2333
### Releases v1.1.0
2434

2535
1. Add functions to modify PWM settings on-the-fly

examples/ISR_16_PWMs_Array/ISR_16_PWMs_Array.ino

+15-8
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@
2626
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
2727
#define _PWM_LOGLEVEL_ 4
2828

29-
#define USING_MICROS_RESOLUTION true //false
29+
#define USING_MICROS_RESOLUTION true //false
3030

31+
// Select a Timer Clock
32+
#define USING_TIM_DIV1 true // for shortest and most accurate timer
33+
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
34+
#define USING_TIM_DIV256 false // for longest timer but least accurate. Default
35+
36+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
3137
#include "ESP8266_PWM.h"
3238

3339
#ifndef LED_BUILTIN
@@ -85,24 +91,25 @@ uint32_t PWM_Pin[NUMBER_ISR_PWMS] =
8591
};
8692

8793
// You can assign any interval for any timer here, in microseconds
88-
uint32_t PWM_Period[NUMBER_ISR_PWMS] =
94+
double PWM_Period[NUMBER_ISR_PWMS] =
8995
{
90-
1000000L, 500000L, 333333L, 250000L, 200000L, 166667L, 142857L, 125000L
96+
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.667, 142857.143, 125000.0
9197
};
9298

9399

94100
// You can assign any interval for any timer here, in Hz
95-
uint32_t PWM_Freq[NUMBER_ISR_PWMS] =
101+
double PWM_Freq[NUMBER_ISR_PWMS] =
96102
{
97-
1, 2, 3, 4, 5, 6, 7, 8
103+
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0
98104
};
99105

100-
// You can assign any interval for any timer here, in milliseconds
101-
uint32_t PWM_DutyCycle[NUMBER_ISR_PWMS] =
106+
// You can assign any interval for any timer here, in Microseconds
107+
double PWM_DutyCycle[NUMBER_ISR_PWMS] =
102108
{
103-
5, 10, 20, 30, 40, 45, 50, 55
109+
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0
104110
};
105111

112+
106113
typedef void (*irqCallback) ();
107114

108115

examples/ISR_16_PWMs_Array_Complex/ISR_16_PWMs_Array_Complex.ino

+33-27
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@
3030
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
3131
#define _PWM_LOGLEVEL_ 4
3232

33-
#define USING_MICROS_RESOLUTION true //false
33+
#define USING_MICROS_RESOLUTION true //false
3434

35+
// Select a Timer Clock
36+
#define USING_TIM_DIV1 true // for shortest and most accurate timer
37+
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
38+
#define USING_TIM_DIV256 false // for longest timer but least accurate. Default
39+
40+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
3541
#include "ESP8266_PWM.h"
3642

3743
#include <SimpleTimer.h> // https://github.com/jfturcot/SimpleTimer
@@ -97,12 +103,12 @@ typedef struct
97103
irqCallback irqCallbackStopFunc;
98104

99105
#if USING_PWM_FREQUENCY
100-
uint32_t PWM_Freq;
106+
double PWM_Freq;
101107
#else
102-
uint32_t PWM_Period;
108+
double PWM_Period;
103109
#endif
104110

105-
uint32_t PWM_DutyCycle;
111+
double PWM_DutyCycle;
106112

107113
uint64_t deltaMicrosStart;
108114
uint64_t previousMicrosStart;
@@ -134,22 +140,22 @@ uint32_t PWM_Pin[NUMBER_ISR_PWMS] =
134140
};
135141

136142
// You can assign any interval for any timer here, in microseconds
137-
uint32_t PWM_Period[NUMBER_ISR_PWMS] =
143+
double PWM_Period[NUMBER_ISR_PWMS] =
138144
{
139-
1000000L, 500000L, 333333L, 250000L, 200000L, 166667L, 142857L, 125000L
145+
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.667, 142857.143, 125000.0
140146
};
141147

142148

143149
// You can assign any interval for any timer here, in Hz
144-
uint32_t PWM_Freq[NUMBER_ISR_PWMS] =
150+
double PWM_Freq[NUMBER_ISR_PWMS] =
145151
{
146-
1, 2, 3, 4, 5, 6, 7, 8
152+
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0
147153
};
148154

149155
// You can assign any interval for any timer here, in Microseconds
150-
uint32_t PWM_DutyCycle[NUMBER_ISR_PWMS] =
156+
double PWM_DutyCycle[NUMBER_ISR_PWMS] =
151157
{
152-
5, 10, 20, 30, 40, 45, 50, 55
158+
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0
153159
};
154160

155161
///////////////////////////////////
@@ -269,29 +275,29 @@ void doingSomethingStop7()
269275
ISR_PWM_Data curISR_PWM_Data[NUMBER_ISR_PWMS] =
270276
{
271277
//pin, irqCallbackStartFunc, irqCallbackStopFunc, PWM_Period, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
272-
{ PIN_D0, doingSomethingStart0, doingSomethingStop0, 1, 5, 0, 0, 0, 0 },
273-
{ PIN_D1, doingSomethingStart1, doingSomethingStop1, 2, 10, 0, 0, 0, 0 },
274-
{ LED_BUILTIN, doingSomethingStart2, doingSomethingStop2, 3, 20, 0, 0, 0, 0 },
275-
{ PIN_D3, doingSomethingStart3, doingSomethingStop3, 4, 30, 0, 0, 0, 0 },
276-
{ PIN_D4, doingSomethingStart4, doingSomethingStop4, 5, 40, 0, 0, 0, 0 },
277-
{ PIN_D5, doingSomethingStart5, doingSomethingStop5, 6, 45, 0, 0, 0, 0 },
278-
{ PIN_D6, doingSomethingStart6, doingSomethingStop6, 7, 50, 0, 0, 0, 0 },
279-
{ PIN_D7, doingSomethingStart7, doingSomethingStop7, 8, 55, 0, 0, 0, 0 },
278+
{ PIN_D0, doingSomethingStart0, doingSomethingStop0, 1.0, 5.0, 0, 0, 0, 0 },
279+
{ PIN_D1, doingSomethingStart1, doingSomethingStop1, 2.0, 10.0, 0, 0, 0, 0 },
280+
{ LED_BUILTIN, doingSomethingStart2, doingSomethingStop2, 3.0, 20.0, 0, 0, 0, 0 },
281+
{ PIN_D3, doingSomethingStart3, doingSomethingStop3, 4.0, 30.0, 0, 0, 0, 0 },
282+
{ PIN_D4, doingSomethingStart4, doingSomethingStop4, 5.0, 40.0, 0, 0, 0, 0 },
283+
{ PIN_D5, doingSomethingStart5, doingSomethingStop5, 6.0, 45.0, 0, 0, 0, 0 },
284+
{ PIN_D6, doingSomethingStart6, doingSomethingStop6, 7.0, 50.0, 0, 0, 0, 0 },
285+
{ PIN_D7, doingSomethingStart7, doingSomethingStop7, 8.0, 55.0, 0, 0, 0, 0 },
280286
};
281287

282288
#else // #if USING_PWM_FREQUENCY
283289

284290
ISR_PWM_Data curISR_PWM_Data[NUMBER_ISR_PWMS] =
285291
{
286292
//irqCallbackStartFunc, PWM_Period, deltaMicrosStart, previousMicrosStart, deltaMicrosStop, previousMicrosStop
287-
{ PIN_D0, doingSomethingStart0, doingSomethingStop0, 1000000L, 5, 0, 0, 0, 0 },
288-
{ PIN_D1, doingSomethingStart1, doingSomethingStop1, 500000L, 10, 0, 0, 0, 0 },
289-
{ LED_BUILTIN, doingSomethingStart2, doingSomethingStop2, 333333L, 20, 0, 0, 0, 0 },
290-
{ PIN_D3, doingSomethingStart3, doingSomethingStop3, 250000L, 30, 0, 0, 0, 0 },
291-
{ PIN_D4, doingSomethingStart4, doingSomethingStop4, 200000L, 40, 0, 0, 0, 0 },
292-
{ PIN_D5, doingSomethingStart5, doingSomethingStop5, 166667L, 45, 0, 0, 0, 0 },
293-
{ PIN_D6, doingSomethingStart6, doingSomethingStop6, 142857L, 50, 0, 0, 0, 0 },
294-
{ PIN_D7, doingSomethingStart7, doingSomethingStop7, 125000L, 55, 0, 0, 0, 0 },
293+
{ PIN_D0, doingSomethingStart0, doingSomethingStop0, 1000000.0, 5.0, 0, 0, 0, 0 },
294+
{ PIN_D1, doingSomethingStart1, doingSomethingStop1, 500000.0, 10.0, 0, 0, 0, 0 },
295+
{ LED_BUILTIN, doingSomethingStart2, doingSomethingStop2, 333333,333, 20.0, 0, 0, 0, 0 },
296+
{ PIN_D3, doingSomethingStart3, doingSomethingStop3, 250000.0, 30.0, 0, 0, 0, 0 },
297+
{ PIN_D4, doingSomethingStart4, doingSomethingStop4, 200000.0, 40.0, 0, 0, 0, 0 },
298+
{ PIN_D5, doingSomethingStart5, doingSomethingStop5, 166666.667, 45.0, 0, 0, 0, 0 },
299+
{ PIN_D6, doingSomethingStart6, doingSomethingStop6, 142857.143, 50.0, 0, 0, 0, 0 },
300+
{ PIN_D7, doingSomethingStart7, doingSomethingStop7, 125000.0, 55.0, 0, 0, 0, 0 },
295301
};
296302

297303
#endif // #if USING_PWM_FREQUENCY
@@ -376,7 +382,7 @@ void simpleTimerdoingSomething2s()
376382
Serial.print(F("PWM Channel : ")); Serial.print(i);
377383

378384
#if USING_PWM_FREQUENCY
379-
Serial.print(1000000 / PWM_Freq[i]);
385+
Serial.print(1000000.0 / PWM_Freq[i]);
380386
#else
381387
Serial.print(PWM_Period[i]);
382388
#endif

examples/ISR_16_PWMs_Array_Simple/ISR_16_PWMs_Array_Simple.ino

+15-8
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@
2626
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
2727
#define _PWM_LOGLEVEL_ 4
2828

29-
#define USING_MICROS_RESOLUTION true //false
29+
#define USING_MICROS_RESOLUTION true //false
3030

31+
// Select a Timer Clock
32+
#define USING_TIM_DIV1 true // for shortest and most accurate timer
33+
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
34+
#define USING_TIM_DIV256 false // for longest timer but least accurate. Default
35+
36+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
3137
#include "ESP8266_PWM.h"
3238

3339
#ifndef LED_BUILTIN
@@ -85,21 +91,22 @@ uint32_t PWM_Pin[NUMBER_ISR_PWMS] =
8591
};
8692

8793
// You can assign any interval for any timer here, in microseconds
88-
uint32_t PWM_Period[NUMBER_ISR_PWMS] =
94+
double PWM_Period[NUMBER_ISR_PWMS] =
8995
{
90-
1000000L, 500000L, 333333L, 250000L, 200000L, 166667L, 142857L, 125000L
96+
1000000.0, 500000.0, 333333.333, 250000.0, 200000.0, 166666.667, 142857.143, 125000.0
9197
};
9298

99+
93100
// You can assign any interval for any timer here, in Hz
94-
uint32_t PWM_Freq[NUMBER_ISR_PWMS] =
101+
double PWM_Freq[NUMBER_ISR_PWMS] =
95102
{
96-
1, 2, 3, 4, 5, 6, 7, 8
103+
1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0
97104
};
98105

99-
// You can assign any interval for any timer here, in milliseconds
100-
uint32_t PWM_DutyCycle[NUMBER_ISR_PWMS] =
106+
// You can assign any interval for any timer here, in Microseconds
107+
double PWM_DutyCycle[NUMBER_ISR_PWMS] =
101108
{
102-
5, 10, 20, 30, 40, 45, 50, 55
109+
5.0, 10.0, 20.0, 30.0, 40.0, 45.0, 50.0, 55.0
103110
};
104111

105112
////////////////////////////////////////////////

examples/ISR_Changing_PWM/ISR_Changing_PWM.ino

+12-6
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@
1818
*****************************************************************************************************************************/
1919

2020
#if !defined(ESP8266)
21-
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
21+
#error This code is designed to run on ESP8266 and ESP8266-based boards! Please check your Tools->Board setting.
2222
#endif
2323

2424
// These define's must be placed at the beginning before #include "ESP8266_PWM.h"
2525
// _PWM_LOGLEVEL_ from 0 to 4
2626
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
2727
#define _PWM_LOGLEVEL_ 4
2828

29-
#define USING_MICROS_RESOLUTION true //false
29+
#define USING_MICROS_RESOLUTION true //false
3030

31+
// Select a Timer Clock
32+
#define USING_TIM_DIV1 true // for shortest and most accurate timer
33+
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
34+
#define USING_TIM_DIV256 false // for longest timer but least accurate. Default
35+
36+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
3137
#include "ESP8266_PWM.h"
3238

3339
#ifndef LED_BUILTIN
@@ -85,14 +91,14 @@ double PWM_Freq1 = 1.0f;
8591
double PWM_Freq2 = 2.0f;
8692

8793
// You can assign any interval for any timer here, in microseconds
88-
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
94+
double PWM_Period1 = 1000000.0 / PWM_Freq1;
8995
// You can assign any interval for any timer here, in microseconds
90-
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;
96+
double PWM_Period2 = 1000000.0 / PWM_Freq2;
9197

9298
// You can assign any duty_cycle for any PWM here, from 0-100
93-
uint32_t PWM_DutyCycle1 = 50;
99+
double PWM_DutyCycle1 = 50.0;
94100
// You can assign any duty_cycle for any PWM here, from 0-100
95-
uint32_t PWM_DutyCycle2 = 90;
101+
double PWM_DutyCycle2 = 90.0;
96102

97103
// Channel number used to identify associated channel
98104
int channelNum;

examples/ISR_Modify_PWM/ISR_Modify_PWM.ino

+11-5
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@
2626
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
2727
#define _PWM_LOGLEVEL_ 4
2828

29-
#define USING_MICROS_RESOLUTION true //false
29+
#define USING_MICROS_RESOLUTION true //false
3030

31+
// Select a Timer Clock
32+
#define USING_TIM_DIV1 true // for shortest and most accurate timer
33+
#define USING_TIM_DIV16 false // for medium time and medium accurate timer
34+
#define USING_TIM_DIV256 false // for longest timer but least accurate. Default
35+
36+
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
3137
#include "ESP8266_PWM.h"
3238

3339
#ifndef LED_BUILTIN
@@ -64,14 +70,14 @@ double PWM_Freq1 = 1.0f;
6470
double PWM_Freq2 = 2.0f;
6571

6672
// You can assign any interval for any timer here, in microseconds
67-
uint32_t PWM_Period1 = 1000000 / PWM_Freq1;
73+
double PWM_Period1 = 1000000.0 / PWM_Freq1;
6874
// You can assign any interval for any timer here, in microseconds
69-
uint32_t PWM_Period2 = 1000000 / PWM_Freq2;
75+
double PWM_Period2 = 1000000.0 / PWM_Freq2;
7076

7177
// You can assign any duty_cycle for any PWM here, from 0-100
72-
uint32_t PWM_DutyCycle1 = 10;
78+
double PWM_DutyCycle1 = 10.0;
7379
// You can assign any duty_cycle for any PWM here, from 0-100
74-
uint32_t PWM_DutyCycle2 = 90;
80+
double PWM_DutyCycle2 = 90.0;
7581

7682
// Channel number used to identify associated channel
7783
int channelNum;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/****************************************************************************************************************************
2+
multiFileProject.cpp
3+
4+
For ESP8266 boards
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_PWM
8+
Licensed under MIT license
9+
*****************************************************************************************************************************/
10+
11+
// To demo how to include files in multi-file Projects
12+
13+
#include "multiFileProject.h"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/****************************************************************************************************************************
2+
multiFileProject.h
3+
4+
For ESP8266 boards
5+
Written by Khoi Hoang
6+
7+
Built by Khoi Hoang https://github.com/khoih-prog/ESP8266_PWM
8+
Licensed under MIT license
9+
*****************************************************************************************************************************/
10+
11+
// To demo how to include files in multi-file Projects
12+
13+
#pragma once
14+
15+
// Can be included as many times as necessary, without `Multiple Definitions` Linker Error
16+
#include "ESP8266_PWM.hpp"

0 commit comments

Comments
 (0)