Skip to content

Commit 643afc9

Browse files
Allow passing debounce_threshold in keypad.py (#1044)
* Expose debounce_threshold in keypad.py
1 parent 9ecce75 commit 643afc9

File tree

3 files changed

+19
-59
lines changed

3 files changed

+19
-59
lines changed

docs/en/scanners.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ need to swap it out with an alternative scanner.
1111
## Keypad Scanners
1212
The scanners in `kmk.scanners.keypad` wrap the `keypad` module that ships with
1313
CircuitPython and support the same configuration and tuning options as their
14-
upstream. You can find out more in the [CircuitPython
14+
upstream.
15+
16+
**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
17+
18+
You can find out more in the [CircuitPython
1519
documentation](https://docs.circuitpython.org/en/latest/shared-bindings/keypad/index.html).
1620

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

@@ -68,7 +73,8 @@ class MyKeyboard(KMKKeyboard):
6873
# optional arguments with defaults:
6974
value_when_pressed=False,
7075
pull=True,
71-
interval=0.02, # Debounce time in floating point seconds
76+
interval=0.02, # Matrix sampling interval in ms
77+
debounce_threshold=None, # Number of samples needed to change state, values greater than 1 enable debouncing. Only applicable for CircuitPython >= 9.2.0
7278
max_events=64
7379
)
7480
```
@@ -94,7 +100,8 @@ class MyKeyboard(KMKKeyboard):
94100
# optional arguments with defaults:
95101
value_to_latch=True, # 74HC165: True, CD4021: False
96102
value_when_pressed=False,
97-
interval=0.02, # Debounce time in floating point seconds
103+
interval=0.02, # Matrix sampling interval in ms
104+
debounce_threshold=None, # Number of samples needed to change state, values greater than 1 enable debouncing. Only applicable for CircuitPython >= 9.2.0
98105
max_events=64
99106
)
100107
```

kmk/scanners/keypad.py

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import keypad
22

3-
from kmk.scanners import DiodeOrientation, Scanner
3+
from kmk.scanners import Scanner
44

55

66
class KeypadScanner(Scanner):
@@ -36,22 +36,8 @@ class MatrixScanner(KeypadScanner):
3636
:param direction: The diode orientation of the matrix.
3737
'''
3838

39-
def __init__(
40-
self,
41-
row_pins,
42-
column_pins,
43-
*,
44-
columns_to_anodes=DiodeOrientation.COL2ROW,
45-
interval=0.02,
46-
max_events=64,
47-
):
48-
self.keypad = keypad.KeyMatrix(
49-
row_pins,
50-
column_pins,
51-
columns_to_anodes=(columns_to_anodes == DiodeOrientation.COL2ROW),
52-
interval=interval,
53-
max_events=max_events,
54-
)
39+
def __init__(self, *args, **kwargs):
40+
self.keypad = keypad.Keys(*args, **kwargs)
5541
super().__init__()
5642

5743

@@ -62,46 +48,12 @@ class KeysScanner(KeypadScanner):
6248
:param pins: An array of arrays of CircuitPython Pin objects, such that pins[r][c] is the pin for row r, column c.
6349
'''
6450

65-
def __init__(
66-
self,
67-
pins,
68-
*,
69-
value_when_pressed=False,
70-
pull=True,
71-
interval=0.02,
72-
max_events=64,
73-
):
74-
self.keypad = keypad.Keys(
75-
pins,
76-
value_when_pressed=value_when_pressed,
77-
pull=pull,
78-
interval=interval,
79-
max_events=max_events,
80-
)
51+
def __init__(self, *args, **kwargs):
52+
self.keypad = keypad.Keys(*args, **kwargs)
8153
super().__init__()
8254

8355

8456
class ShiftRegisterKeys(KeypadScanner):
85-
def __init__(
86-
self,
87-
*,
88-
clock,
89-
data,
90-
latch,
91-
value_to_latch=True,
92-
key_count,
93-
value_when_pressed=False,
94-
interval=0.02,
95-
max_events=64,
96-
):
97-
self.keypad = keypad.ShiftRegisterKeys(
98-
clock=clock,
99-
data=data,
100-
latch=latch,
101-
value_to_latch=value_to_latch,
102-
key_count=key_count,
103-
value_when_pressed=value_when_pressed,
104-
interval=interval,
105-
max_events=max_events,
106-
)
57+
def __init__(self, *args, **kwargs):
58+
self.keypad = keypad.Keys(*args, **kwargs)
10759
super().__init__()

util/aspell.en.pws

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Crkbd
3333
Crowboard
3434
Ctrl
3535
Cygwin
36+
debounce
3637
DFU
3738
DISCOVERABLE
3839
DIY

0 commit comments

Comments
 (0)