Skip to content

Commit

Permalink
Merge pull request #8 from adafruit/try-time-ticksms
Browse files Browse the repository at this point in the history
try time.ticks_ms
  • Loading branch information
dhalbert authored Oct 15, 2022
2 parents 5436a0f + 335f993 commit 7832bbb
Showing 1 changed file with 49 additions and 24 deletions.
73 changes: 49 additions & 24 deletions adafruit_ticks.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
#
# - supervisor.ticks_ms is present. This will be the case starting in CP7.0
#
# - time.ticks_ms is present. This is the case for MicroPython & for the "unix
# port" of CircuitPython, used for some automated testing.
#
# - time.monotonic_ns is present, and works. This is the case on most
# Express boards in CP6.x, and most host computer versions of Python.
#
Expand All @@ -52,30 +55,9 @@
try:
from supervisor import ticks_ms # pylint: disable=unused-import
except (ImportError, NameError):
try:
from time import monotonic_ns as _monotonic_ns

_monotonic_ns() # Check that monotonic_ns is usable

def ticks_ms() -> int:
"""Return the time in milliseconds since an unspecified moment,
wrapping after 2**29ms.
The wrap value was chosen so that it is always possible to add or
subtract two `ticks_ms` values without overflow on a board without
long ints (or without allocating any long integer objects, on
boards with long ints).
This ticks value comes from a low-accuracy clock internal to the
microcontroller, just like `time.monotonic`. Due to its low
accuracy and the fact that it "wraps around" every few days, it is
intended for working with short term events like advancing an LED
animation, not for long term events like counting down the time
until a holiday."""
return (_monotonic_ns() // 1_000_000) & _TICKS_MAX
import time

except (ImportError, NameError, NotImplementedError):
from time import monotonic as _monotonic
if _ticks_ms := getattr(time, "ticks_ms", None):

def ticks_ms() -> int:
"""Return the time in milliseconds since an unspecified moment,
Expand All @@ -92,7 +74,50 @@ def ticks_ms() -> int:
intended for working with short term events like advancing an LED
animation, not for long term events like counting down the time
until a holiday."""
return int(_monotonic() * 1000) & _TICKS_MAX
return _ticks_ms() & _TICKS_MAX # pylint: disable=not-callable

else:
try:
from time import monotonic_ns as _monotonic_ns

_monotonic_ns() # Check that monotonic_ns is usable

def ticks_ms() -> int:
"""Return the time in milliseconds since an unspecified moment,
wrapping after 2**29ms.
The wrap value was chosen so that it is always possible to add or
subtract two `ticks_ms` values without overflow on a board without
long ints (or without allocating any long integer objects, on
boards with long ints).
This ticks value comes from a low-accuracy clock internal to the
microcontroller, just like `time.monotonic`. Due to its low
accuracy and the fact that it "wraps around" every few days, it is
intended for working with short term events like advancing an LED
animation, not for long term events like counting down the time
until a holiday."""
return (_monotonic_ns() // 1_000_000) & _TICKS_MAX

except (ImportError, NameError, NotImplementedError):
from time import monotonic as _monotonic

def ticks_ms() -> int:
"""Return the time in milliseconds since an unspecified moment,
wrapping after 2**29ms.
The wrap value was chosen so that it is always possible to add or
subtract two `ticks_ms` values without overflow on a board without
long ints (or without allocating any long integer objects, on
boards with long ints).
This ticks value comes from a low-accuracy clock internal to the
microcontroller, just like `time.monotonic`. Due to its low
accuracy and the fact that it "wraps around" every few days, it is
intended for working with short term events like advancing an LED
animation, not for long term events like counting down the time
until a holiday."""
return int(_monotonic() * 1000) & _TICKS_MAX


def ticks_add(ticks: int, delta: int) -> int:
Expand Down

0 comments on commit 7832bbb

Please sign in to comment.