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

bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19

Merged
merged 1 commit into from Jan 18, 2022
Merged

bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19

merged 1 commit into from Jan 18, 2022

Conversation

RushOnline
Copy link
Contributor

Signed-off-by: Evgeniy Nepomniashchiy rush.zlo@gmail.com

@RushOnline
Copy link
Contributor Author

Now I can control stepper driver faster and more precisely within my project based on esphhome. Thank you!

@khoih-prog
Copy link
Owner

Hi @RushOnline

Thanks for the bug-fixing PR.

It's so good that you figure out the bug, as

_timerCount = _frequency / frequency  => frequency = _frequency / _timerCount

I should have stored frequency as a class member, but this fix saves some memory without the need to do so.

@khoih-prog khoih-prog merged commit 7f86f9c into khoih-prog:master Jan 18, 2022
@RushOnline
Copy link
Contributor Author

I should have stored frequency as a class member, but this fix saves some memory without the need to do so.

I've do it this way to minify changes only. To save a memory we can do following changes:

diff --git a/src/ESP8266TimerInterrupt.h b/src/ESP8266TimerInterrupt.h
index 06f941f..d2409d2 100644
--- a/src/ESP8266TimerInterrupt.h
+++ b/src/ESP8266TimerInterrupt.h
@@ -102,14 +102,12 @@ class ESP8266TimerInterrupt
   private:
     timer_callback  _callback;        // pointer to the callback function
     float           _frequency;       // Timer frequency
-    uint32_t        _timerCount;      // count to activate timer
 
   public:
 
     ESP8266TimerInterrupt()
     {
       _frequency  = 0;
-      _timerCount = 0;
       _callback   = NULL;
     };
 
@@ -121,10 +119,10 @@ class ESP8266TimerInterrupt
 
       // ESP8266 only has one usable timer1, max count is only 8,388,607. So to get longer time, we use max available 256 divider
       // Will use later if very low frequency is needed.
-      _frequency  = 80000000 / 256;
-      _timerCount = (uint32_t) _frequency / frequency;
+      _frequency  = frequency;
       _callback   = callback;
 
+      uint32_t _timerCount = (uint32_t) 80000000 / 256 / _frequency;
       if ( _timerCount > MAX_ESP8266_COUNT)
       {
         _timerCount = MAX_ESP8266_COUNT;
@@ -179,8 +177,8 @@ class ESP8266TimerInterrupt
     // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely
     void reattachInterrupt()
     {
-      if ( (_frequency != 0) && (_timerCount != 0) && (_callback != NULL) )
-        setFrequency(_frequency / _timerCount, _callback);
+      if ( (_frequency != 0) && (_callback != NULL) )
+        setFrequency(_frequency, _callback);
     }
 
     // Duration (in milliseconds). Duration = 0 or not specified => run indefinitely

@RushOnline
Copy link
Contributor Author

Warning: this patch is not tested yet. Local time is 00:39 and my rollerblinds must be closed :)

@khoih-prog
Copy link
Owner

I've just already rewritten the library to add more features and fix that issue. Will release within several hours.

khoih-prog added a commit that referenced this pull request Jan 18, 2022
### Releases v1.5.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](#19)
khoih-prog added a commit that referenced this pull request Jan 19, 2022
### Releases v1.5.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](#19)
4. Update examples
khoih-prog added a commit that referenced this pull request Jan 19, 2022
### Releases v1.5.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](#19)
4. Update examples
@khoih-prog
Copy link
Owner

Hi @RushOnline

The new ESP8266TimerInterrupt releases v1.5.0 has just been published. Your contribution is noted in Contributions and Thanks

Please test the new feature and check if there is any more and new bug.

// Select a Timer Clock
#define USING_TIM_DIV1                false           // for shortest and most accurate timer
#define USING_TIM_DIV16               false           // for medium time and medium accurate timer
#define USING_TIM_DIV256              true            // for longest timer but least accurate. Default

Best Regards,


Releases v1.5.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
  4. Update examples

khoih-prog added a commit to khoih-prog/STM32_TimerInterrupt that referenced this pull request Jan 20, 2022
### Releases v1.3.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Fix reattachInterrupt() bug. Check [bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19](khoih-prog/ESP8266TimerInterrupt#19)
3. Update `Packages_Patches`
khoih-prog added a commit to khoih-prog/NRF52_TimerInterrupt that referenced this pull request Jan 22, 2022
### Releases v1.4.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project.
3. Fix `reattachInterrupt()` bug. Check [bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19](khoih-prog/ESP8266TimerInterrupt#19)
4. Add support to Adafruit `NRF52840_LED_GLASSES`
5. Optimize library code by using `reference-passing` instead of `value-passing`
6. Update all examples
7. Update `Packages_Patches`
khoih-prog added a commit to khoih-prog/NRF52_TimerInterrupt that referenced this pull request Jan 22, 2022
### Releases v1.4.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project.
3. Fix `reattachInterrupt()` bug. Check [bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19](khoih-prog/ESP8266TimerInterrupt#19)
4. Add support to Adafruit `NRF52840_LED_GLASSES`
5. Optimize library code by using `reference-passing` instead of `value-passing`
6. Update all examples
7. Update `Packages_Patches`
khoih-prog added a commit to khoih-prog/NRF52_MBED_TimerInterrupt that referenced this pull request Jan 22, 2022
### Releases v1.4.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project.
3. Fix `reattachInterrupt()` bug. Check [bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19](khoih-prog/ESP8266TimerInterrupt#19)
4. Optimize library code by using `reference-passing` instead of `value-passing`
5. Update all examples
khoih-prog added a commit to khoih-prog/NRF52_MBED_TimerInterrupt that referenced this pull request Jan 22, 2022
### Releases v1.4.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project.
3. Fix `reattachInterrupt()` bug. Check [bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19](khoih-prog/ESP8266TimerInterrupt#19)
4. Optimize library code by using `reference-passing` instead of `value-passing`
5. Update all examples
khoih-prog added a commit to khoih-prog/NRF52_MBED_TimerInterrupt that referenced this pull request Jan 22, 2022
### Releases v1.4.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project.
3. Fix `reattachInterrupt()` bug. Check [bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19](khoih-prog/ESP8266TimerInterrupt#19)
4. Optimize library code by using `reference-passing` instead of `value-passing`
5. Update all examples
khoih-prog added a commit to khoih-prog/Portenta_H7_TimerInterrupt that referenced this pull request Jan 23, 2022
### Releases v1.4.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project.
3. Fix `reattachInterrupt()` bug. Check [bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19](khoih-prog/ESP8266TimerInterrupt#19)
4. Optimize library code by using `reference-passing` instead of `value-passing`
5. Update all examples
6. Update `Packages_Patches`
khoih-prog added a commit to khoih-prog/Portenta_H7_TimerInterrupt that referenced this pull request Jan 23, 2022
### Releases v1.4.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project.
3. Fix `reattachInterrupt()` bug. Check [bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19](khoih-prog/ESP8266TimerInterrupt#19)
4. Optimize library code by using `reference-passing` instead of `value-passing`
5. Update all examples
6. Update `Packages_Patches`
khoih-prog added a commit to khoih-prog/ESP8266_PWM that referenced this pull request Jan 29, 2022
### 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
khoih-prog added a commit to khoih-prog/ESP8266_PWM that referenced this pull request Jan 29, 2022
### 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
khoih-prog added a commit to khoih-prog/ESP8266_PWM that referenced this pull request Jan 29, 2022
### 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
khoih-prog added a commit to khoih-prog/ESP8266_PWM that referenced this pull request Jan 29, 2022
### 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
khoih-prog added a commit to khoih-prog/STM32_Slow_PWM that referenced this pull request Jan 30, 2022
### Releases v1.2.0

1. Fix `multiple-definitions` linker error. Drop `src_cpp` and `src_h` directories
2. Add example [multiFileProject](examples/multiFileProject) to demo for multiple-file project
3. Improve accuracy by using `double`, instead of `uint32_t` for `dutycycle`, `period`
4. Optimize library code by using `reference-passing` instead of `value-passing`
5. Fix reattachInterrupt() bug. Check [bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() #19](khoih-prog/ESP8266TimerInterrupt#19)
6. Update examples accordingly
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants