-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathadafruit_matrixkeypad.py
94 lines (75 loc) · 2.89 KB
/
adafruit_matrixkeypad.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# SPDX-FileCopyrightText: 2018 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_matrixkeypad`
====================================================
CircuitPython library for matrix keypads
* Author(s): ladyada
Implementation Notes
--------------------
**Hardware:**
* Flexible 3x4 Matrix Keypad <https://www.adafruit.com/product/419>
* Phone-style 3x4 Matrix Keypad <https://www.adafruit.com/product/1824>
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
"""
# imports
from digitalio import Direction, Pull
# Since the board may or may not have access to the typing library we need
# to have this in a try/except to enable type
try:
from typing import List
from digitalio import DigitalInOut
except ImportError:
pass
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MatrixKeypad.git"
class Matrix_Keypad:
"""Driver for passive matrix keypads - any size"""
def __init__(
self,
row_pins: List[DigitalInOut],
col_pins: List[DigitalInOut],
keys: List[List],
) -> None:
"""
Initialise the driver with the correct size and key list.
:param list row_pins: a list of DigitalInOut objects corresponding to the rows
:param list col_pins: a list of DigitalInOut objects corresponding to the colums
:param list keys: a list of lists that has the corresponding symbols for each key
"""
if len(keys) != len(row_pins):
raise RuntimeError("Key name matrix doesn't match # of colums")
for row in keys:
if len(row) != len(col_pins):
raise RuntimeError("Key name matrix doesn't match # of rows")
self.row_pins = row_pins
self.col_pins = col_pins
self.keys = keys
@property
def pressed_keys(self) -> List:
"""
An array containing all detected keys that are pressed from the initalized
list-of-lists passed in during creation
:return: a list of keys that are pressed
"""
# make a list of all the keys that are detected
pressed = []
# set all pins pins to be inputs w/pullups
for pin in self.row_pins + self.col_pins:
pin.direction = Direction.INPUT
pin.pull = Pull.UP
for row, row_pin in enumerate(self.row_pins):
# set one row low at a time
row_pin.direction = Direction.OUTPUT
row_pin.value = False
# check the column pins, which ones are pulled down
for col, val in enumerate(self.col_pins):
if not val.value:
pressed.append(self.keys[row][col])
# reset the pin to be an input
row_pin.direction = Direction.INPUT
row_pin.pull = Pull.UP
return pressed