micropython-lib
micropython-lib copied to clipboard
stdlib/time: Add time package (wrapping utime) with monotonic function.
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.
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.