Add getRemaining method#18
Conversation
Aasim-A
left a comment
There was a problem hiding this comment.
Hi, thanks for the PR, I've left some comments, as for the version bump, you can bump it to 2.3.0
src/AsyncTimer.cpp
Outdated
| for (unsigned short i = 0; i < m_maxArrayLength; i++) | ||
| if (m_callsArray[i].id == id) | ||
| return m_callsArray[i].timestamp + m_callsArray[i].delayByMs - now; | ||
| return -1; |
There was a problem hiding this comment.
return 0 instead of -1 because unsigned long doesn't support negatives
There was a problem hiding this comment.
Also beware that now might happen to be after m_callsArray[i].timestamp + m_callsArray[i].delayByMs just because the code so far has been executing synchronously long enough to not allow the library to execute the scheduled callback on time (no callback is guaranteed to be executed at its scheduled time exactly).
Simple example would be if the callback has one more second before it has to execute but the current code takes three seconds before it completes, and at the end of those three seconds .getRemaining() will return -2
There was a problem hiding this comment.
Good point, a fix to this problem is to keep the now variable and check if now is bigger than the sum of the timestamp and the delay, if it is bigger, return 0, otherwise subtract now from it and return the result
There was a problem hiding this comment.
Thanks for the pointers. I'll update the code
README.md
Outdated
| ```c++ | ||
| AsyncTimer t; | ||
|
|
||
| // This timeout will never run |
README.md
Outdated
|
|
||
| ## getRemaining | ||
|
|
||
| Gets the number of milliseconds remaining in a timer. Returns -1 on timer not found. |
There was a problem hiding this comment.
Returns 0 instead of -1
Change double space after period to single space
src/AsyncTimer.cpp
Outdated
| } | ||
|
|
||
| unsigned long AsyncTimer::getRemaining(unsigned short id) { | ||
| unsigned long now = millis(); |
There was a problem hiding this comment.
Since now is only used once, it should be omitted and replaced by millis() call
src/AsyncTimer.cpp
Outdated
|
|
||
| unsigned long AsyncTimer::getRemaining(unsigned short id) { | ||
| unsigned long now = millis(); | ||
| for (unsigned short i = 0; i < m_maxArrayLength; i++) |
There was a problem hiding this comment.
Add curly braces to for loop for consistency with other code
src/AsyncTimer.h
Outdated
| void reset(unsigned short id); | ||
| void cancel(unsigned short id); | ||
| void cancelAll(bool includeIntervals = true); | ||
| unsigned long getRemaining(unsigned short id); |
There was a problem hiding this comment.
Move this line to be under setInterval line
src/AsyncTimer.cpp
Outdated
| } | ||
| } | ||
|
|
||
| unsigned long AsyncTimer::getRemaining(unsigned short id) { |
There was a problem hiding this comment.
Move this function declaration to be under setInterval function
README.md
Outdated
| t.cancelAll(false); | ||
| ``` | ||
|
|
||
| ## getRemaining |
There was a problem hiding this comment.
Move this function to be under setInterval
* Handle the case where `now` is after `m_callsArray[i].timestamp` + `m_callsArray[i].delayByMs` * Re-order `getRemaining` to be after `setInterval` in `README.md`, `AsyncTimer.h`, `AsyncTimer.cpp` * `getRemaining` returns `0` if timer not found
|
LGTM! |
This addresses feature request #17
I want to be able to report how much time is left to the next interval on a timer. I would like to write code
If you're okay with the code, would you like me to add in a version bump to 2.3.0?