summaryrefslogtreecommitdiff
path: root/src/ticks.py
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2023-05-22 08:40:47 +0200
committerSébastien Dailly <sebastien@dailly.me>2023-05-22 08:40:47 +0200
commit597b007333d8ec0d9cfd29e6941fcbe57379108a (patch)
tree0cf87e1ac487e7deb91acf7f2bec70bd4dd06703 /src/ticks.py
Initial commit
Diffstat (limited to 'src/ticks.py')
-rw-r--r--src/ticks.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/ticks.py b/src/ticks.py
new file mode 100644
index 0000000..ba6831a
--- /dev/null
+++ b/src/ticks.py
@@ -0,0 +1,21 @@
+from supervisor import ticks_ms
+
+# See the example at
+# https://docs.circuitpython.org/en/latest/shared-bindings/supervisor/index.html?#supervisor.ticks_ms
+_TICKS_PERIOD = const(1<<29)
+_TICKS_MAX = const(_TICKS_PERIOD-1)
+_TICKS_HALFPERIOD = const(_TICKS_PERIOD//2)
+
+def add(ticks, delta):
+ "Add a delta to a base number of ticks, performing wraparound at 2**29ms."
+ return (ticks + delta) % _TICKS_PERIOD
+
+def ticks_diff(ticks1, ticks2):
+ "Compute the signed difference between two ticks values, assuming that they are within 2**28 ticks"
+ diff = (ticks1 - ticks2) & _TICKS_MAX
+ diff = ((diff + _TICKS_HALFPERIOD) & _TICKS_MAX) - _TICKS_HALFPERIOD
+ return diff
+
+def less(ticks):
+ "Return true iff ticks1 is less than ticks2, assuming that they are within 2**28 ticks"
+ return ticks_diff(ticks_ms(), ticks) < 0