Skip to content

Add getRemaining method#18

Merged
Aasim-A merged 4 commits intoAasim-A:masterfrom
oxo42:getRemaining
Mar 1, 2022
Merged

Add getRemaining method#18
Aasim-A merged 4 commits intoAasim-A:masterfrom
oxo42:getRemaining

Conversation

@oxo42
Copy link
Contributor

@oxo42 oxo42 commented Feb 25, 2022

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

unsigned long remaining = t.getRemaining(timerId);
Serial.print("Time remaining: ");
Serial.println(remaining);

If you're okay with the code, would you like me to add in a version bump to 2.3.0?

Copy link
Owner

@Aasim-A Aasim-A left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for the PR, I've left some comments, as for the version bump, you can bump it to 2.3.0

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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return 0 instead of -1 because unsigned long doesn't support negatives

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointers. I'll update the code

README.md Outdated
```c++
AsyncTimer t;

// This timeout will never run
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this comment

README.md Outdated

## getRemaining

Gets the number of milliseconds remaining in a timer. Returns -1 on timer not found.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns 0 instead of -1
Change double space after period to single space

}

unsigned long AsyncTimer::getRemaining(unsigned short id) {
unsigned long now = millis();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since now is only used once, it should be omitted and replaced by millis() call


unsigned long AsyncTimer::getRemaining(unsigned short id) {
unsigned long now = millis();
for (unsigned short i = 0; i < m_maxArrayLength; i++)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this line to be under setInterval line

}
}

unsigned long AsyncTimer::getRemaining(unsigned short id) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this function declaration to be under setInterval function

README.md Outdated
t.cancelAll(false);
```

## getRemaining
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this function to be under setInterval

oxo42 and others added 3 commits February 28, 2022 22:52
* 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
@Aasim-A Aasim-A merged commit d56fca9 into Aasim-A:master Mar 1, 2022
@Aasim-A
Copy link
Owner

Aasim-A commented Mar 1, 2022

LGTM!
Thanks again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants