35
35
#
36
36
# - supervisor.ticks_ms is present. This will be the case starting in CP7.0
37
37
#
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
+ #
38
41
# - time.monotonic_ns is present, and works. This is the case on most
39
42
# Express boards in CP6.x, and most host computer versions of Python.
40
43
#
52
55
try :
53
56
from supervisor import ticks_ms # pylint: disable=unused-import
54
57
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
76
59
77
- except (ImportError , NameError , NotImplementedError ):
78
- from time import monotonic as _monotonic
60
+ if _ticks_ms := getattr (time , "ticks_ms" , None ):
79
61
80
62
def ticks_ms () -> int :
81
63
"""Return the time in milliseconds since an unspecified moment,
@@ -92,7 +74,50 @@ def ticks_ms() -> int:
92
74
intended for working with short term events like advancing an LED
93
75
animation, not for long term events like counting down the time
94
76
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
96
121
97
122
98
123
def ticks_add (ticks : int , delta : int ) -> int :
0 commit comments