-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeypad.py
116 lines (86 loc) · 2.53 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
107
108
109
110
111
112
113
114
115
116
import RPi.GPIO as GPIO
import time
L1 = 5
L2 = 6
L3 = 13
L4 = 19
C1 = 12
C2 = 16
C3 = 20
C4 = 21
pin_code = "1234"
user_input = ""
current_key = None
def keypress_callback(channel):
global current_key
if current_key is None:
current_key = channel
def setup_gpio():
GPIO.setmode(GPIO.BCM)
GPIO.setup(L1, GPIO.OUT)
GPIO.setup(L2, GPIO.OUT)
GPIO.setup(L3, GPIO.OUT)
GPIO.setup(L4, GPIO.OUT)
GPIO.setup(C1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(C1, GPIO.RISING, callback=keypress_callback)
GPIO.add_event_detect(C2, GPIO.RISING, callback=keypress_callback)
GPIO.add_event_detect(C3, GPIO.RISING, callback=keypress_callback)
GPIO.add_event_detect(C4, GPIO.RISING, callback=keypress_callback)
def set_state(state):
GPIO.output(L1, state)
GPIO.output(L2, state)
GPIO.output(L3, state)
GPIO.output(L4, state)
def get_code():
global user_input
pressed = False
GPIO.output(L3, GPIO.HIGH)
GPIO.output(L3, GPIO.LOW)
GPIO.output(L1, GPIO.HIGH)
if not pressed and GPIO.input(C4) == 1:
if user_input == pin_code:
print("yes")
else:
print("no")
pressed = True
GPIO.output(L3, GPIO.LOW)
if pressed:
user_input = ""
return pressed
def readLine(line, characters):
global user_input
GPIO.output(line, GPIO.HIGH)
if GPIO.input(C1) == 1:
user_input += characters[0]
if GPIO.input(C2) == 1:
user_input += characters[1]
if GPIO.input(C3) == 1:
user_input += characters[2]
if GPIO.input(C4) == 1:
user_input += characters[3]
GPIO.output(line, GPIO.LOW)
def main():
global current_key
try:
while True:
if current_key is None:
set_state(GPIO.HIGH)
if not GPIO.input(current_key):
current_key = None
else:
time.slep(0.1)
else:
if not get_code():
readLine(L1, ["1", "2", "3", "A"])
readLine(L2, ["4", "5", "6", "B"])
readLine(L3, ["7", "8", "9", "C"])
readLine(L4, ["*", "0", "#", "D"])
time.sleep(0.1)
except KeyboardInterrupt:
print("\nApplication stopped!")
if __name__ == "__main__":
setup_gpio()
main()