bugfix: reattachInterrupt() pass wrong frequency value to setFrequency() by RushOnline · Pull Request #19 · khoih-prog/ESP8266TimerInterrupt (original) (raw)
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