1
1
#!/usr/bin/env python3
2
2
# -*- coding: utf-8 -*-
3
3
4
- import os
5
4
import time
6
- # import pifacedigitalio as pfio
7
- from RPi import GPIO
8
5
import queue
9
6
from threading import Thread , RLock
10
- import pygame
11
7
8
+ import mpv
9
+ from RPi import GPIO
12
10
from ldap_interface import authenticate
13
11
12
+
14
13
NUMERIC_KEYS = ['0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' ]
15
14
16
- BOUNCE_TIME = 300 # in milliseconds
17
- STALE_TIMEOUT = 30 # in seconds
18
- timeouts = {'1' : BOUNCE_TIME , '2' : BOUNCE_TIME , '3' : BOUNCE_TIME , '4' : BOUNCE_TIME ,
15
+ BOUNCE_TIME = 300 # in milliseconds
16
+ STALE_TIMEOUT = 30 # in seconds
17
+ timeouts = {'1' : BOUNCE_TIME , '2' : BOUNCE_TIME , '3' : BOUNCE_TIME , '4' : BOUNCE_TIME ,
19
18
'5' : BOUNCE_TIME , '6' : BOUNCE_TIME , '7' : BOUNCE_TIME , '8' : BOUNCE_TIME ,
20
- '9' : BOUNCE_TIME , '0' : BOUNCE_TIME , 'A' : BOUNCE_TIME , 'B' : BOUNCE_TIME ,
19
+ '9' : BOUNCE_TIME , '0' : BOUNCE_TIME , 'A' : BOUNCE_TIME , 'B' : BOUNCE_TIME ,
21
20
'C' : BOUNCE_TIME , 'D' : BOUNCE_TIME , 'E' : BOUNCE_TIME , 'F' : BOUNCE_TIME }
22
21
23
22
24
23
q = queue .Queue ()
25
24
lock = RLock ()
26
25
27
- # for piface
28
- # ROWS = [2, 3, 4, 5]
29
- # COLS = [0, 1, 2, 3]
30
-
31
26
COLS = [15 , 13 , 11 , 7 ]
32
27
ROWS = [12 , 16 , 18 , 22 ]
33
28
34
29
OPEN_PIN = 26
35
30
36
31
# preinit to avoid sound lag
37
- pygame .mixer .pre_init (44100 , - 16 , 2 , 2048 ) # setup mixer to avoid sound lag
38
- pygame .mixer .init ()
39
- beep = pygame .mixer .Sound ('/home/pi/raspberrylock/beep.wav' )
40
- fail = pygame .mixer .Sound ('/home/pi/raspberrylock/fail.wav' )
41
- success = pygame .mixer .Sound ('/home/pi/raspberrylock/success.wav' )
42
-
43
- def init_piface ():
44
- pfio .init ()
45
- pfio .digital_write_pullup (0 , 1 )
46
- pfio .digital_write_pullup (1 , 1 )
47
- pfio .digital_write_pullup (2 , 1 )
48
- pfio .digital_write_pullup (3 , 1 )
49
-
32
+ MPV = mpv .MPV ()
33
+ beep = '/opt/raspberrylock/beep.wav'
34
+ fail = '/opt/raspberrylock/fail.wav'
35
+ success = '/opt/raspberrylock/success.wav'
36
+
37
+ state = 0
38
+ uid = ''
39
+ pin = ''
40
+ reset_timer = STALE_TIMEOUT
41
+
42
+
50
43
def init_gpios ():
51
44
GPIO .setmode (GPIO .BOARD )
52
45
GPIO .setup (OPEN_PIN , GPIO .OUT )
@@ -56,80 +49,83 @@ def init_gpios():
56
49
for pin in COLS :
57
50
GPIO .setup (pin , GPIO .IN , pull_up_down = GPIO .PUD_DOWN )
58
51
52
+
59
53
def decode_keypad (measurements ):
60
54
"""
61
55
x1 = (0,0,1,0)
62
56
"""
63
- layout = [['C' , 'D' , 'E' , 'F' ], ['B' , '9' , '6' , '3' ],
57
+ layout = [['C' , 'D' , 'E' , 'F' ], ['B' , '9' , '6' , '3' ],
64
58
['0' , '8' , '5' , '2' ], ['A' , '7' , '4' , '1' ]]
65
59
for y_index , y_row in enumerate (measurements ):
66
60
for x_index , x_state in enumerate (y_row ):
67
61
if x_state > 0 :
68
62
return layout [y_index ][x_index ]
69
63
64
+
70
65
def decrease_timeouts (timeouts ):
71
66
for k , v in timeouts .items ():
72
67
if v > 0 :
73
68
timeouts [k ] = v - 1
74
69
return timeouts
75
70
71
+
76
72
def collect_measurements ():
77
73
"""
78
74
"""
79
75
pin_state = []
80
76
for y_pin in ROWS :
81
- # pfio.digital_write(y_pin, 1)
82
77
GPIO .output (y_pin , 1 )
83
78
x_pin_states = []
84
79
for x_pin in COLS :
85
- #pin_in = pfio.digital_read(x_pin)
86
80
pin_in = GPIO .input (x_pin )
87
81
# print("{}x{} = {}".format(y_pin, x_pin, pin_in))
88
82
x_pin_states .append (pin_in )
89
- # pfio.digital_write(y_pin, 0)
90
83
GPIO .output (y_pin , 0 )
91
84
pin_state .append (x_pin_states )
92
85
return pin_state
93
86
87
+
94
88
def read_keypad ():
95
89
decrease_timeouts (timeouts )
96
90
key = decode_keypad (collect_measurements ())
97
91
if key :
98
92
if timeouts [key ] > 0 :
99
93
return None
100
94
else :
101
- beep . play ()
95
+ play (beep )
102
96
timeouts [key ] = BOUNCE_TIME
103
97
return key
104
98
else :
105
99
return None
106
100
107
101
108
- state = 0
109
- uid = ''
110
- pin = ''
111
- reset_timer = STALE_TIMEOUT
112
102
113
103
def reset_state ():
114
- print ("reset state" )
115
104
global state , uid , pin
105
+
106
+ print ("reset state" )
116
107
state = 0
117
108
uid = ''
118
109
pin = ''
119
110
111
+
120
112
def timeout_reset_state ():
121
113
global reset_timer
114
+
122
115
while True :
123
116
reset_timer -= 1
124
117
if reset_timer <= 0 :
125
118
reset_state ()
126
119
reset_timer = STALE_TIMEOUT
127
120
time .sleep (1.0 )
128
121
122
+
129
123
def control_loop ():
130
124
global reset_timer
131
125
global state , uid , pin
126
+
132
127
reset_state ()
128
+
133
129
# Main state machine.
134
130
# Expects the user to enter her UID, then PIN like this:
135
131
# [A] 2903 [A] 123456 A
@@ -172,32 +168,28 @@ def control_loop():
172
168
pin += key
173
169
continue
174
170
elif key == 'C' :
175
- reset_state ()
171
+ reset_state ()
176
172
continue
177
173
elif key == 'A' :
178
174
t = Thread (target = open_if_correct , args = (uid , pin ))
179
175
t .start ()
180
176
reset_state ()
181
177
continue
182
178
179
+
183
180
def open_if_correct (uid , pin ):
184
181
print ('checking ldap ...' )
185
182
if authenticate (uid , pin ):
186
- success . play ()
183
+ play (success )
187
184
with lock :
188
185
GPIO .output (OPEN_PIN , 1 )
189
- #pfio.digital_write(6, 1)
190
- # pfio.digital_write(1, 1)
191
186
time .sleep (10 )
192
187
GPIO .output (OPEN_PIN , 0 )
193
- # pfio.digital_write(6, 0)
194
- # pfio.digital_write(1, 0)
195
188
else :
196
- fail . play ()
189
+ play (fail )
197
190
with lock :
198
- #pfio.digital_write(7, 1)
199
191
time .sleep (2 )
200
- # pfio.digital_write(7, 0)
192
+
201
193
202
194
def keypad_loop ():
203
195
while True :
@@ -206,8 +198,8 @@ def keypad_loop():
206
198
if key :
207
199
q .put (key )
208
200
201
+
209
202
def main ():
210
- # pfio.init()
211
203
#os.nice(10)
212
204
init_gpios ()
213
205
control_thread = Thread (target = control_loop )
@@ -217,8 +209,10 @@ def main():
217
209
timeout_thread = Thread (target = timeout_reset_state )
218
210
timeout_thread .start ()
219
211
212
+
220
213
if __name__ == '__main__' :
221
214
try :
222
215
main ()
223
216
except KeyboardInterrupt :
224
217
GPIO .cleanup ()
218
+
0 commit comments