Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Simplify and fix countdown display skipping seconds #94

Merged
merged 9 commits into from
Jul 16, 2020

Conversation

robinstewart
Copy link
Contributor

@robinstewart robinstewart commented Jul 16, 2020

I was able to fix #90 and also simplify the tick timer code by using round() instead of floor(). Since the UI only allows timers to be set to an integer number of seconds, the exact "seconds remaining" is always near an integer boundary. We can simply use the round() function to make sure the nearest integer is displayed, regardless of whether the timer ends up firing slightly before or slightly after.

Here is a typical run when I uncomment the print statement:

9.992520928382874
8.992309927940369
7.992242932319641
6.992233991622925
5.991078019142151
4.992653012275696
3.9753549098968506
2.979225993156433
1.9741169214248657
0.9946630001068115
-0.010595083236694336

Fixes #90
Closes #93
Closes #92
Closes #91

* energy:
  Add a pre-timer to ensure that each tick of the countdown clock happens just after a seconds boundary. This ensures that the displayed number of seconds remaining is both accurate and robust to small system delays that can cause timers to fire a bit later than scheduled.
  Make countdown timer precise to about 1/30 of a second.
  Track window visibility and disable drawing and clock updates while the window is closed or hidden. Reduces CPU usage while in the background by at least 20x compared to previous version of the app.
  Improve battery life by setting a timer tolerance and updating the display less often.
@luckman212
Copy link

@robinstewart I like your round() solution. Very nice! Tested & works for me. I even tested it with tolerances up to 0.25 with no missed ticks. But, I don't think those extra milliseconds of CPU time saved are worth it — 0.03 is good 👍

@luckman212 luckman212 mentioned this pull request Jul 16, 2020
Timer/MVClockView.swift Outdated Show resolved Hide resolved
@karbassi
Copy link
Collaborator

@robinstewart looks good to me as well.

Merging

Fixes #90
Closes #93
Closes #92
Closes #91

@karbassi karbassi merged commit a5063ef into michaelvillar:master Jul 16, 2020
@robinstewart
Copy link
Contributor Author

Thanks!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Timer skips seconds
3 participants