-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypad.py
107 lines (83 loc) · 2.54 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
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
95
96
97
98
99
100
101
102
103
104
105
106
import time
import digitalio
import board
import adafruit_matrixkeypad
from gpiozero import LED
def blink_led(time_in_seconds, led):
"""
Blink the led for an amount of time
"""
led.on()
time.sleep(time_in_seconds)
led.off()
time.sleep(time_in_seconds)
def get_pressed_key(keys, led):
"""
If there are more than 1 keys pressed, then return nothing... and blink the led 3 times
else return the key and blink 1 time
"""
result = None
if keys is None or len(keys) > 1:
blink_led(0.1, led)
blink_led(0.1, led)
blink_led(0.1, led)
elif keys:
result = str(keys[0])
blink_led(0.1, led)
print(result)
return result
def check_code(code, door, led):
"""
Checks if the current code is equal to the predefined code
"""
if len(code) > 4:
door.off()
print("To many numbers entered, reset code")
blink_led(0.1, led)
blink_led(0.1, led)
blink_led(0.1, led)
elif code == "3266":
print("The right code was entered")
print("The door is unlocking")
door.on()
led.on()
return True
return False
def main():
"""
Main starting application
"""
# Membrane 3x4 matrix keypad on Raspberry Pi -
# https://www.adafruit.com/product/419
cols = [digitalio.DigitalInOut(x) for x in (board.D26, board.D20, board.D21)]
rows = [digitalio.DigitalInOut(x) for x in (board.D5, board.D6, board.D13, board.D19)]
# 3x4 matrix keypad on Raspberry Pi -
# rows and columns are mixed up for https://www.adafruit.com/product/3845
# cols = [digitalio.DigitalInOut(x) for x in (board.D13, board.D5, board.D26)]
# rows = [digitalio.DigitalInOut(x) for x in (board.D6, board.D21, board.D20, board.D19)]
keys = (("#", 0, "*"), (9, 8, 7), (6, 5, 4), (3, 2, 1))
keypad = adafruit_matrixkeypad.Matrix_Keypad(rows, cols, keys)
led = LED(17)
door = LED(4)
code = ""
while True:
keys = keypad.pressed_keys
key_pressed = get_pressed_key(keys, led)
if key_pressed is None:
continue
if key_pressed == "*":
code = ""
door.off()
led.off()
else:
code = code + key_pressed
if check_code(code, door, led):
print("Door is now open for 10 seconds")
time.sleep(10)
door.off()
led.off()
code = ""
print("Door is closed")
print("code: {}".format(code))
time.sleep(0.1)
main()