-
Notifications
You must be signed in to change notification settings - Fork 28
/
keypadd.py
151 lines (129 loc) · 4.83 KB
/
keypadd.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# #####################################################
# Read 3x4 matrix keypad and toggle byte in armed.txt
#
# Arming and disarming logic by Jeff Highsmith
# June 2013
#
# Keypad-reading library written by Chris Crumpacker
# May 2013
# http://crumpspot.blogspot.com/2013/05/using-3x4-matrix-keypad-with-raspberry.html
#
# main structure is adapted from Bandono's
# matrixQPI which is wiringPi based.
# https://github.com/bandono/matrixQPi?source=cc
# #####################################################
import RPi.GPIO as GPIO
import time
import subprocess
class keypad():
# CONSTANTS
KEYPAD = [
[1,2,3],
[4,5,6],
[7,8,9],
["*",0,"#"]
]
ROW = [27,23,24,25]
COLUMN = [4,17,22]
def __init__(self):
GPIO.setmode(GPIO.BCM)
def getKey(self):
# Set all columns as output low
for j in range(len(self.COLUMN)):
GPIO.setup(self.COLUMN[j], GPIO.OUT)
GPIO.output(self.COLUMN[j], GPIO.LOW)
# Set all rows as input
for i in range(len(self.ROW)):
GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Scan rows for pushed key/button
# A valid key press should set "rowVal" between 0 and 3.
rowVal = -1
for i in range(len(self.ROW)):
tmpRead = GPIO.input(self.ROW[i])
if tmpRead == 0:
rowVal = i
# if rowVal is not 0 thru 3 then no button was pressed and we can exit
if rowVal < 0 or rowVal > 3:
self.exit()
return
# Convert columns to input
for j in range(len(self.COLUMN)):
GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
# Switch the i-th row found from scan to output
GPIO.setup(self.ROW[rowVal], GPIO.OUT)
GPIO.output(self.ROW[rowVal], GPIO.HIGH)
# Scan columns for still-pushed key/button
# A valid key press should set "colVal" between 0 and 2.
colVal = -1
for j in range(len(self.COLUMN)):
tmpRead = GPIO.input(self.COLUMN[j])
if tmpRead == 1:
colVal=j
# if colVal is not 0 thru 2 then no button was pressed and we can exit
if colVal < 0 or colVal > 2:
self.exit()
return
# Return the value of the key pressed
self.exit()
return self.KEYPAD[rowVal][colVal]
def exit(self):
# Reinitialize all rows and columns as input at exit
for i in range(len(self.ROW)):
GPIO.setup(self.ROW[i], GPIO.IN, pull_up_down=GPIO.PUD_UP)
for j in range(len(self.COLUMN)):
GPIO.setup(self.COLUMN[j], GPIO.IN, pull_up_down=GPIO.PUD_UP)
if __name__ == '__main__':
# Initialize the keypad class
kp = keypad()
attempt = "0000"
passcode = "1912"
haltcode = "5764"
with open("/home/pi/Alarm/armed.txt", "r+") as fo:
fo.seek(0, 0)
fo.write("0")
fo.closed
GPIO.setmode(GPIO.BCM)
GPIO.setup(10, GPIO.OUT) #Green
GPIO.output(10, GPIO.HIGH)
GPIO.setup(9, GPIO.OUT) #Red
GPIO.output(9, GPIO.LOW)
GPIO.setup(7, GPIO.OUT) #Flashing Light
GPIO.output(7, GPIO.LOW)
subprocess.call("mpg123 /home/pi/Alarm/ready.mp3", shell=True)
# Loop while waiting for a keypress
while True:
digit = None
while digit == None:
digit = kp.getKey()
# Print the result
print digit
attempt = (attempt[1:] + str(digit))
print attempt
if (attempt == passcode):
with open("/home/pi/Alarm/armed.txt", "r+") as fo:
fo.seek(0, 0)
status = fo.read(1)
fo.closed
if (status == "1"):
#system was armed, disarm it
with open("/home/pi/Alarm/armed.txt", "r+") as fo:
fo.seek(0, 0)
fo.write("0")
fo.closed
GPIO.output(10, GPIO.HIGH) #Green LED On
GPIO.output(9, GPIO.LOW) #Red LED off
GPIO.output(7, GPIO.LOW)
subprocess.call("mpg123 /home/pi/Alarm/disarmed.mp3", shell=True)
else:
GPIO.output(10, GPIO.LOW) #Green LED Off
GPIO.output(9, GPIO.HIGH) #Red LED on
subprocess.call("mpg123 /home/pi/Alarm/armed.mp3", shell=True)
time.sleep(10)
with open("/home/pi/Alarm/armed.txt", "r+") as fo:
fo.seek(0, 0)
fo.write("1")
fo.closed
elif (attempt == haltcode):
subprocess.call("mpg123 /home/pi/Alarm/shutdown.mp3", shell=True)
subprocess.call("halt", shell=True)
time.sleep(0.5)