- Notifications
You must be signed in to change notification settings - Fork 14
Switch to time.monotonic_ns() when it's available #10
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
Changes from 6 commits
Commits
Show all changes
12 commits Select commit Hold shift + click to select a range
ef6951f Add some tests
tgs daea743 Keep internal interval in a convenient unit #9
tgs 596708b Correct wording of the intro
tgs 741932c Add doc section about whether the debouncer will work forever
tgs 33ba756 Nicer names and some comments
tgs 5e099ac Comments and add test of interval getter
tgs ab19bce Merge branch 'master' of https://github.com/adafruit/Adafruit_Circuit…
tgs e9b8614 Update and also rename duration/time to ticks when measured in ticks
tgs 71f24d9 Fix line length issue
tgs f060e9a Add instructions for using the tests
tgs 3de8835 Merge branch 'master' into use-monotonic-ns
kattni 481d615 black
tgs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import sys | ||
| import time | ||
| import adafruit_debouncer | ||
| | ||
| | ||
| def _true(): | ||
| return True | ||
| def _false(): | ||
| return False | ||
| | ||
| | ||
| def assertEqual(a, b): | ||
| assert a == b, "Want %r, got %r" % (a, b) | ||
| | ||
| | ||
| def test_simple(): | ||
| db = adafruit_debouncer.Debouncer(_false) | ||
| assertEqual(db.value, False) | ||
| | ||
| db.function = _true | ||
| db.update() | ||
| assertEqual(db.value, False) | ||
| time.sleep(0.02) | ||
| db.update() | ||
| assertEqual(db.value, True) | ||
| assertEqual(db.rose, True) | ||
| assertEqual(db.fell, False) | ||
| | ||
| db.function = _false | ||
| db.update() | ||
| assertEqual(db.value, True) | ||
| assertEqual(db.fell, False) | ||
| assertEqual(db.rose, False) | ||
| time.sleep(0.02) | ||
| db.update() | ||
| assertEqual(db.value, False) | ||
| assertEqual(db.rose, False) | ||
| assertEqual(db.fell, True) | ||
| | ||
| | ||
| def test_interval_is_the_same(): | ||
| db = adafruit_debouncer.Debouncer(_false, interval=0.25) | ||
| assertEqual(db.value, False) | ||
| db.update() | ||
| db.function = _true | ||
| db.update() | ||
| | ||
| time.sleep(0.1) # longer than default interval | ||
| db.update() | ||
| assertEqual(db.value, False) | ||
| | ||
| time.sleep(0.2) # 0.1 + 0.2 > 0.25 | ||
| db.update() | ||
| assertEqual(db.value, True) | ||
| assertEqual(db.rose, True) | ||
| assertEqual(db.interval, 0.25) | ||
| | ||
| | ||
| def test_setting_interval(): | ||
| # Check that setting the interval does change the time the debouncer waits | ||
| db = adafruit_debouncer.Debouncer(_false, interval=0.01) | ||
| db.update() | ||
| | ||
| # set the interval to a longer time, sleep for a time between | ||
| # the two interval settings, and assert that the value hasn't changed. | ||
| | ||
| db.function = _true | ||
| db.interval = 0.2 | ||
| db.update() | ||
| assert db.interval - 0.2 < 0.00001, "interval is not consistent" | ||
| time.sleep(0.11) | ||
| db.update() | ||
| | ||
| assertEqual(db.value, False) | ||
| assertEqual(db.rose, False) | ||
| assertEqual(db.fell, False) | ||
| | ||
| # and then once the whole time has passed make sure it did change | ||
| time.sleep(0.11) | ||
| db.update() | ||
| assertEqual(db.value, True) | ||
| assertEqual(db.rose, True) | ||
| assertEqual(db.fell, False) | ||
| | ||
| | ||
| def run(): | ||
| passes = 0 | ||
| fails = 0 | ||
| for name, test in locals().items(): | ||
| if name.startswith('test_') and callable(test): | ||
| try: | ||
| print() | ||
| print(name) | ||
| test() | ||
| print("PASS") | ||
| passes += 1 | ||
| except Exception as e: | ||
| sys.print_exception(e) | ||
| print("FAIL") | ||
| fails += 1 | ||
| | ||
| print(passes, "passed,", fails, "failed") | ||
| if passes and not fails: | ||
| print(r""" | ||
| ________ | ||
| < YATTA! > | ||
| -------- | ||
| \ ^__^ | ||
| \ (oo)\_______ | ||
| (__)\ )\/\ | ||
| ||----w | | ||
| || ||""") | ||
| | ||
| | ||
| if __name__ == '__main__': | ||
| run() |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.