From 650517dad02b3bc4250152ad7c5811287bc2d7c5 Mon Sep 17 00:00:00 2001 From: David Glaude Date: Sat, 12 Sep 2020 22:17:18 +0200 Subject: [PATCH 1/2] Update adafruit_debouncer.py Testing if time.monotonic_ns() is really implemented. Supposed to solve #22 Works for me on PyPortal. --- adafruit_debouncer.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/adafruit_debouncer.py b/adafruit_debouncer.py index 8ff911a..5db66b7 100644 --- a/adafruit_debouncer.py +++ b/adafruit_debouncer.py @@ -62,12 +62,13 @@ _UNSTABLE_STATE = const(0x02) _CHANGED_STATE = const(0x04) -# Find out whether the current CircuitPython supports time.monotonic_ns(), +# Find out whether the current CircuitPython really supports time.monotonic_ns(), # which doesn't have the accuracy limitation. -if hasattr(time, "monotonic_ns"): +try: + time.monotonic_ns() TICKS_PER_SEC = 1_000_000_000 MONOTONIC_TICKS = time.monotonic_ns -else: +except (ImportError, NotImplementedError): TICKS_PER_SEC = 1 MONOTONIC_TICKS = time.monotonic From 62eb98241af1f6705c5e545ccfc1f6533cd6dd7b Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 13 Sep 2020 15:34:23 -0500 Subject: [PATCH 2/2] Correct exception specification It's a happy coincidence that the documentation build caught this problem. :+1: The code to adapt to both `monotonic_ns` and `monotonic` is structured a bit differently than the LED animation library, which is not a problem. However, when monotonic_ns is not available, the line `time.monotonic_ns()` creates an AttributeError, so we need to catch that. Since there's no `import` inside this block, we do _NOT_ need to catch `ImportError`. At any rate, that's my quick analysis of what has occurred here. I did not do any testing. I think that if I'm correct it will fix the documentation building problem, as well as the functionality. --- adafruit_debouncer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_debouncer.py b/adafruit_debouncer.py index 5db66b7..391779b 100644 --- a/adafruit_debouncer.py +++ b/adafruit_debouncer.py @@ -68,7 +68,7 @@ time.monotonic_ns() TICKS_PER_SEC = 1_000_000_000 MONOTONIC_TICKS = time.monotonic_ns -except (ImportError, NotImplementedError): +except (AttributeError, NotImplementedError): TICKS_PER_SEC = 1 MONOTONIC_TICKS = time.monotonic