This repository has been archived by the owner on Sep 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keypad.py
123 lines (101 loc) · 2.9 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
117
118
119
120
121
122
123
import RPi.GPIO as GPIO
from constants import PASSCODE
import time
# Keyboard variables
L1 = 5
L2 = 6
L3 = 13
L4 = 19
C1 = 12
C2 = 16
C3 = 20
C4 = 21
GPIO.setwarnings(False)
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)
def read_keypad():
keys = [
["1", "2", "3", "A"],
["4", "5", "6", "B"],
["7", "8", "9", "C"],
["*", "0", "#", "D"]
]
lines = [L1, L2, L3, L4]
columns = [C1, C2, C3, C4]
output = ""
for i, line in enumerate(lines):
GPIO.output(line, GPIO.HIGH)
for j, column in enumerate(columns):
if GPIO.input(column) == 1:
output = keys[i][j]
GPIO.output(line, GPIO.LOW)
return output
def enter_passcode():
time.sleep(0.3)
input_code = ""
print("[VERIFY] Enter your 4-digit passcode:")
while len(input_code) < 4:
key = None
while not key:
key = read_keypad()
time.sleep(0.3)
if key:
print(f"[VERIFY] Key pressed: {key}")
if key.isdigit():
input_code += key
print(f"[VERIFY] Current input: {input_code}")
elif key == "*":
input_code = ""
print("[VERIFY] Input reset.")
return input_code
def enter_roll():
number = ""
while len(number) < 2:
print("[DD] ENTER NUMBER:")
key = None
while not key:
key = read_keypad()
time.sleep(0.2)
if key:
if key.isdigit():
number += key
print(f"[DD] CURRENT NUMBER: {number}")
elif key == "*":
number = ""
print("[DD] NUMBER RESET.")
return number
def roll_list():
numbers_list = []
while True:
two_digit_number = enter_roll()
numbers_list.append(two_digit_number)
print(f"\n[DD] ROLL ADDED: {two_digit_number}")
while True:
print("[DD] Press 'A' to add another number, 'B' to finish.")
choice = None
while not choice:
choice = read_keypad()
time.sleep(0.1)
if choice == "A":
print("[DD] Adding another roll.")
break
elif choice == "B":
print("[DD] Finished adding rolls.\n")
return numbers_list
else:
continue
def verify_passcode():
entered_code = enter_passcode()
if entered_code == PASSCODE:
print("[VERIFY] ACCESS GRANTED.")
return True
else:
print("[VERIFY] ACCESS DENIED.")
return False