-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkeypad.py
67 lines (54 loc) · 1.52 KB
/
keypad.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
from micropython import const
from machine import Pin
from utime import sleep
PIN_R1 = const(2)
PIN_R2 = const(4)
PIN_R3 = const(5)
PIN_R4 = const(18)
PIN_C1 = const(19)
PIN_C2 = const(21)
PIN_C3 = const(22)
PIN_C4 = const(23)
def scan_keypad(row: int, col: int) -> int:
"""
enable keypad rows and verify cols
:param row: integer for row number
:param col: integer for col number
:return: integer
"""
global row_pins
global col_pins
row_pins[row].value(1)
if col_pins[col].value():
status = 1
else:
status = 0
row_pins[row].value(0)
return status
if __name__ == '__main__':
keys = [
['1', '2', '3', 'A'],
['4', '5', '6', 'B'],
['7', '8', '9', 'C'],
['*', '0', '#', 'D']
]
row_pins = [
Pin(PIN_R1, mode=Pin.OUT, value=0),
Pin(PIN_R2, mode=Pin.OUT, value=0),
Pin(PIN_R3, mode=Pin.OUT, value=0),
Pin(PIN_R4, mode=Pin.OUT, value=0)
]
col_pins = [
Pin(PIN_C1, mode=Pin.IN, pull=Pin.PULL_DOWN),
Pin(PIN_C2, mode=Pin.IN, pull=Pin.PULL_DOWN),
Pin(PIN_C3, mode=Pin.IN, pull=Pin.PULL_DOWN),
Pin(PIN_C4, mode=Pin.IN, pull=Pin.PULL_DOWN)
]
print('Please press any key on Keypad:')
while True:
for row_item in range(4):
for col_item in range(4):
value = scan_keypad(row_item, col_item)
if value == 1:
print(f"[INFO] Key {keys[row_item][col_item]} pressed")
sleep(0.25)