Skip to content

Commit 7832bbb

Browse files
authored
Merge pull request #8 from adafruit/try-time-ticksms
try time.ticks_ms
2 parents 5436a0f + 335f993 commit 7832bbb

File tree

1 file changed

+49
-24
lines changed

1 file changed

+49
-24
lines changed

adafruit_ticks.py

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
#
3636
# - supervisor.ticks_ms is present. This will be the case starting in CP7.0
3737
#
38+
# - time.ticks_ms is present. This is the case for MicroPython & for the "unix
39+
# port" of CircuitPython, used for some automated testing.
40+
#
3841
# - time.monotonic_ns is present, and works. This is the case on most
3942
# Express boards in CP6.x, and most host computer versions of Python.
4043
#
@@ -52,30 +55,9 @@
5255
try:
5356
from supervisor import ticks_ms # pylint: disable=unused-import
5457
except (ImportError, NameError):
55-
try:
56-
from time import monotonic_ns as _monotonic_ns
57-
58-
_monotonic_ns() # Check that monotonic_ns is usable
59-
60-
def ticks_ms() -> int:
61-
"""Return the time in milliseconds since an unspecified moment,
62-
wrapping after 2**29ms.
63-
64-
The wrap value was chosen so that it is always possible to add or
65-
subtract two `ticks_ms` values without overflow on a board without
66-
long ints (or without allocating any long integer objects, on
67-
boards with long ints).
68-
69-
This ticks value comes from a low-accuracy clock internal to the
70-
microcontroller, just like `time.monotonic`. Due to its low
71-
accuracy and the fact that it "wraps around" every few days, it is
72-
intended for working with short term events like advancing an LED
73-
animation, not for long term events like counting down the time
74-
until a holiday."""
75-
return (_monotonic_ns() // 1_000_000) & _TICKS_MAX
58+
import time
7659

77-
except (ImportError, NameError, NotImplementedError):
78-
from time import monotonic as _monotonic
60+
if _ticks_ms := getattr(time, "ticks_ms", None):
7961

8062
def ticks_ms() -> int:
8163
"""Return the time in milliseconds since an unspecified moment,
@@ -92,7 +74,50 @@ def ticks_ms() -> int:
9274
intended for working with short term events like advancing an LED
9375
animation, not for long term events like counting down the time
9476
until a holiday."""
95-
return int(_monotonic() * 1000) & _TICKS_MAX
77+
return _ticks_ms() & _TICKS_MAX # pylint: disable=not-callable
78+
79+
else:
80+
try:
81+
from time import monotonic_ns as _monotonic_ns
82+
83+
_monotonic_ns() # Check that monotonic_ns is usable
84+
85+
def ticks_ms() -> int:
86+
"""Return the time in milliseconds since an unspecified moment,
87+
wrapping after 2**29ms.
88+
89+
The wrap value was chosen so that it is always possible to add or
90+
subtract two `ticks_ms` values without overflow on a board without
91+
long ints (or without allocating any long integer objects, on
92+
boards with long ints).
93+
94+
This ticks value comes from a low-accuracy clock internal to the
95+
microcontroller, just like `time.monotonic`. Due to its low
96+
accuracy and the fact that it "wraps around" every few days, it is
97+
intended for working with short term events like advancing an LED
98+
animation, not for long term events like counting down the time
99+
until a holiday."""
100+
return (_monotonic_ns() // 1_000_000) & _TICKS_MAX
101+
102+
except (ImportError, NameError, NotImplementedError):
103+
from time import monotonic as _monotonic
104+
105+
def ticks_ms() -> int:
106+
"""Return the time in milliseconds since an unspecified moment,
107+
wrapping after 2**29ms.
108+
109+
The wrap value was chosen so that it is always possible to add or
110+
subtract two `ticks_ms` values without overflow on a board without
111+
long ints (or without allocating any long integer objects, on
112+
boards with long ints).
113+
114+
This ticks value comes from a low-accuracy clock internal to the
115+
microcontroller, just like `time.monotonic`. Due to its low
116+
accuracy and the fact that it "wraps around" every few days, it is
117+
intended for working with short term events like advancing an LED
118+
animation, not for long term events like counting down the time
119+
until a holiday."""
120+
return int(_monotonic() * 1000) & _TICKS_MAX
96121

97122

98123
def ticks_add(ticks: int, delta: int) -> int:

0 commit comments

Comments
 (0)