Skip to content
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

Allow passing debounce_threshold in keypad.py #1044

Merged
merged 18 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions docs/en/scanners.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ need to swap it out with an alternative scanner.
## Keypad Scanners
The scanners in `kmk.scanners.keypad` wrap the `keypad` module that ships with
CircuitPython and support the same configuration and tuning options as their
upstream. You can find out more in the [CircuitPython
upstream.

**Some users may need to tweak debounce parameters**, `interval=0.01,debounce_threshold=5` is a good starting point. `debounce_threshold` is only applicable for CircuitPython >= 9.2.0
xs5871 marked this conversation as resolved.
Show resolved Hide resolved

You can find out more in the [CircuitPython
documentation](https://docs.circuitpython.org/en/latest/shared-bindings/keypad/index.html).

### keypad MatrixScanner
Expand All @@ -30,7 +34,8 @@ class MyKeyboard(KMKKeyboard):
row_pins=self.row_pins,
# optional arguments with defaults:
columns_to_anodes=DiodeOrientation.COL2ROW,
interval=0.02, # Debounce time in floating point seconds
interval=0.02, # Matrix sampling interval in ms
debounce_threshold=None, # Number of samples needed to change state, values greater than 1 enable debouncing. Only applicable for CircuitPython >= 9.2.0
max_events=64
)

Expand Down Expand Up @@ -68,7 +73,8 @@ class MyKeyboard(KMKKeyboard):
# optional arguments with defaults:
value_when_pressed=False,
pull=True,
interval=0.02, # Debounce time in floating point seconds
interval=0.02, # Matrix sampling interval in ms
debounce_threshold=None, # Number of samples needed to change state, values greater than 1 enable debouncing. Only applicable for CircuitPython >= 9.2.0
max_events=64
)
```
Expand All @@ -94,7 +100,8 @@ class MyKeyboard(KMKKeyboard):
# optional arguments with defaults:
value_to_latch=True, # 74HC165: True, CD4021: False
value_when_pressed=False,
interval=0.02, # Debounce time in floating point seconds
interval=0.02, # Matrix sampling interval in ms
debounce_threshold=None, # Number of samples needed to change state, values greater than 1 enable debouncing. Only applicable for CircuitPython >= 9.2.0
max_events=64
)
```
Expand Down
77 changes: 55 additions & 22 deletions kmk/scanners/keypad.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,26 @@ def __init__(
*,
columns_to_anodes=DiodeOrientation.COL2ROW,
interval=0.02,
debounce_threshold=None,
max_events=64,
):
self.keypad = keypad.KeyMatrix(
args = [
row_pins,
column_pins,
columns_to_anodes=(columns_to_anodes == DiodeOrientation.COL2ROW),
interval=interval,
max_events=max_events,
)
]
for i in args:
if i is None:
args.pop(i)
kwargs = {
'columns_to_anodes': columns_to_anodes,
'interval': interval,
'debounce_threshold': debounce_threshold,
'max_events': max_events,
}
for key, value in kwargs.items():
if value is None:
kwargs.pop(key)
self.keypad = keypad.Keys(*args, **kwargs)
Copy link
Collaborator

@xs5871 xs5871 Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh dear. That is much too complicated for my taste. Do we want backwards compatibility with CP8?

  1. No: none if this is necessary
  2. Yes: Since we're not doing any validation nor any abstraction, just use pokemon arguments:
def __init__(self, *args, **kwargs):
    self.keypad = keypad.Keys(*args, **kwargs)

style sidenote: in any case, this screams comprehension:

args = [v for v in args if v is not None]
kwargs = {k:v for k,v in kwargs.items() if v is not None}

Copy link
Member Author

@regicidalplutophage regicidalplutophage Nov 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't go with comprehension because init had explicit arguments. I do like the pokemon arguments though, not sure why it wasn't that in the first place -- referring both to my PR and the main branch.


Right, the too complicated stuff came from the place of wanting to provide non-upstream defaults then have the ability to None-out those defaults if it has to work in earlier versions of circuitpython.

super().__init__()


Expand All @@ -69,15 +80,26 @@ def __init__(
value_when_pressed=False,
pull=True,
interval=0.02,
debounce_threshold=None,
max_events=64,
):
self.keypad = keypad.Keys(
args = [
pins,
value_when_pressed=value_when_pressed,
pull=pull,
interval=interval,
max_events=max_events,
)
]
for i in args:
if i is None:
args.pop(i)
kwargs = {
'value_when_pressed': value_when_pressed,
'pull': pull,
'interval': interval,
'debounce_threshold': debounce_threshold,
'max_events': max_events,
}
for key, value in kwargs.items():
if value is None:
kwargs.pop(key)
self.keypad = keypad.Keys(*args, **kwargs)
super().__init__()


Expand All @@ -88,20 +110,31 @@ def __init__(
clock,
data,
latch,
value_to_latch=True,
key_count,
value_to_latch=True,
value_when_pressed=False,
interval=0.02,
debounce_threshold=None,
max_events=64,
):
self.keypad = keypad.ShiftRegisterKeys(
clock=clock,
data=data,
latch=latch,
value_to_latch=value_to_latch,
key_count=key_count,
value_when_pressed=value_when_pressed,
interval=interval,
max_events=max_events,
)
args = [
clock,
data,
latch,
key_count,
]
for i in args:
if i is None:
args.pop(i)
kwargs = {
'value_to_latch': value_to_latch,
'value_when_pressed': value_when_pressed,
'interval': interval,
'debounce_threshold': debounce_threshold,
'max_events': max_events,
}
for key, value in kwargs.items():
if value is None:
kwargs.pop(key)
self.keypad = keypad.Keys(*args, **kwargs)
super().__init__()
1 change: 1 addition & 0 deletions util/aspell.en.pws
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Crkbd
Crowboard
Ctrl
Cygwin
debounce
DFU
DISCOVERABLE
DIY
Expand Down