Skip to content

Update adafruit_debouncer.py #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 14, 2020
Merged

Update adafruit_debouncer.py #24

merged 2 commits into from
Sep 14, 2020

Conversation

dglaude
Copy link
Contributor

@dglaude dglaude commented Sep 12, 2020

Testing if time.monotonic_ns() is really implemented.
Supposed to solve #22
Works for me on PyPortal.

(not parsed with black or pylint, just copy and paste from my version in Mu)

Testing if time.monotonic_ns() is really implemented.
Supposed to solve #22 
Works for me on PyPortal.
@dglaude
Copy link
Contributor Author

dglaude commented Sep 12, 2020

The test failure is an import issue "detected" by the documentation system.
I can not really reproduce that, and that is not an issue on my PyRuler that does not have an implementation of monotonic_ms and use the exception to get to the other branch.
This code would need to be tested on hardware that has monotonic_ms and investigate the documentation error message.

It's a happy coincidence that the documentation build caught this problem. 👍 

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.
@jepler
Copy link
Contributor

jepler commented Sep 13, 2020

Thanks @dglaude !

It's a happy coincidence that the documentation build caught this problem. 👍

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.

Please give my updated version a test if possible.

@dglaude
Copy link
Contributor Author

dglaude commented Sep 13, 2020

You are right, that fixed the documentation too.

I tested, but for me it give the same result because on my hardware it raise NotImplementedError.

To have a full coverage test it would need a platform/setup/config that:

  1. raise NotImplementedError
  2. raise AttributeError
  3. succeed and raise no exception

I am pretty sure about (1) and (3). Not sure about AttributeError and how to get that.

@jepler
Copy link
Contributor

jepler commented Sep 14, 2020

Isn't it a 5.3 board without long ints that raises AttributeError (or ImportError, if the name is not found by an 'import ... from' statement) and 6.0 raises NotImplementedError?

@jepler
Copy link
Contributor

jepler commented Sep 14, 2020

To phrase it more positively, I think it is adequate to test with a matrix of 4 configurations, probably requiring two boards: 5.3 and 6; with and without long ints.

@dglaude
Copy link
Contributor Author

dglaude commented Sep 14, 2020

So here are my result on a board with M0.
Maybe it need to be done on another board with M4 or anything else.

Special version of the library with some print code:

try:
    time.monotonic_ns()
    TICKS_PER_SEC = 1_000_000_000
    MONOTONIC_TICKS = time.monotonic_ns
    print("time.monotonic_ns")
except (AttributeError, NotImplementedError) as err:
    TICKS_PER_SEC = 1
    MONOTONIC_TICKS = time.monotonic
    print(err)
    print("time.monotonic")

Special test main.py code:

print("Before import.")
from adafruit_debouncer import Debouncer
print("After import.")
while True:
    pass
print("You should never reach here.")

Result with 5.3.1 on Commander 8086 board:

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
main.py output:
Before import.
'module' object has no attribute 'monotonic_ns'
time.monotonic
After import.

Result with 6.0.0-alpha3+ on Commander 8086 board:

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
main.py output:
Before import.
No long integer support
time.monotonic
After import.

@dglaude
Copy link
Contributor Author

dglaude commented Sep 14, 2020

Ok, so tested on Itsy Bitsy M4 Express, both with a 5.3.0-rc0 and the latest "night build" and it give:

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
main.py output:
Before import.
time.monotonic_ns
After import.

So I guess all the usecase are tested.

@jepler jepler merged commit 3569774 into adafruit:master Sep 14, 2020
@jepler
Copy link
Contributor

jepler commented Sep 14, 2020

Thank you for doing that additional testing.

@dglaude dglaude deleted the patch-1 branch September 14, 2020 19:35
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Sep 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants