Skip to content

stdlib/time: Add time package (wrapping utime) with monotonic function. #502

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andrewleech
Copy link
Contributor

MicroPython replacement for time.monotonic suitable for timeouts / comparisons

CPython time.monotonic() → float returns the value (in fractional seconds) of a monotonic clock,
i.e. a clock that cannot go backwards. The clock is not affected by system clock updates.
The reference point of the returned value is undefined, so that only the difference between the
results of two calls is valid.

Most micropython ports have single-precision float for size / efficiency reasons, and some do not have
float support at all in hardware (so are very slow).
To support measurements of difference between two time points, time.ticks_ms() and time.ticks.diff()
are generally recommended, however this can complicate efforts to port existing libraries using
time.monotonic.

This library is intended to support being used as a drop-in replacement for many/most use cases of
time.monotonic. It will wrap the ticks functions and handle/hide the 32-bit rollover handling.

Note however if you convert the output of monotonic to int or float, eg float(monotonic()) then
comparisions between these value are not always valid becasuse they will wrap around back to zero
after a certain length of time. In other words, always do comparisons against the object returned
by monotonic() without type conversion.

See the test_monotonic.py unit test for examples of usage.



def monotonic(impl=int):
return TimeMS(ticks_ms(), impl=int)

Choose a reason for hiding this comment

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

should this be TimeMS(ticks_ms(), impl) with =int removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes it should thanks!

@andrewleech andrewleech changed the title micropython/monotonic: Add a wrapper replacement for time.monotonic(). stdlib/time: Add time package (wrapping utime) with monotonic function. Jul 11, 2022
@andrewleech
Copy link
Contributor Author

On review I've renamed this to micropython-lib/python-stdlib/time/time.py to act as a wrapper for utime - so this should now work fairly seamlessly for use porting other library with many/most use cases of monotonic. All other functions are provided directly from the utime package.

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