-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpfmd.py
executable file
·278 lines (207 loc) · 6.04 KB
/
pfmd.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/python
import serlcd
import si4713
import RPi.GPIO as GPIO
# for getting timestamps
import timeit
from time import sleep, gmtime, strftime
import random
import subprocess
import os
import atexit
import select
import ConfigParser
import glib
from pyudev import Context, Monitor
import pyudev
from pyudev.glib import GUDevMonitorObserver as MonitorObserver
import threading
# for finding files in usb device
import fnmatch
import os.path
import re
import glib, gio, gobject
###### HIER GEHTS LOS!!!
# configure hardware
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
BTN_UP = 20
BTN_DWN = 13
BTN_SET = 5
GPIO.setup(BTN_UP, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(BTN_DWN, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(BTN_SET, GPIO.IN, pull_up_down = GPIO.PUD_UP)
LED_ON = 0
LED_TX = 1
RST_LINE = 24 # new board
# load config file
config = ConfigParser.ConfigParser()
config.read("/home/pi/pfm.conf")
# define runtime constants
locked = True
pin_target = config.get('keylock', 'pin')
pin_pos = 0
pin_input = [0,0,0,0]
print pin_target
# initially open the fifo for writing
mplayer_fifo = open("/home/pi/.mplayer_control", "w")
# initialize display
lcd = serlcd.SerLCD()
# initialize radio
radio = si4713.Si4713(RST_LINE)
# pyudev helpers
usb_added = False
# pin lock functions
def pinlock():
global locked
build_pin_screen()
while locked == True:
pass
# hide cursor when done
lcd.show_cursor(False)
def build_pin_screen():
# prepare screen for pin input
lcd.clear()
lcd.cursor(1,3)
lcd.write('Unlock Pocket FM')
pin_pos_offset = 7
for i in range(4):
lcd.cursor(3,pin_pos_offset+i*2)
lcd.write(str(pin_input[i]))
# enable cursor for input
lcd.cursor(3,pin_pos_offset)
lcd.show_cursor()
def update_pin_screen():
pin_pos_offset = 7
for i in range(4):
lcd.cursor(3,pin_pos_offset+i*2)
lcd.write(str(pin_input[i]))
# enable cursor for input
lcd.cursor(3,pin_pos_offset+pin_pos*2)
def increase_digit(pin):
if pin_input[pin_pos] < 9:
pin_input[pin_pos] += 1
else:
pin_input[pin_pos] = 0
update_pin_screen()
def decrease_digit(pin):
if pin_input[pin_pos] > 0:
pin_input[pin_pos] -= 1
else:
pin_input[pin_pos] = 9
update_pin_screen()
def set_digit(pin):
global pin_pos, pin_input, pin_target, locked
# next digit
if pin_pos < 3:
pin_pos += 1
update_pin_screen()
else:
# compare strings
pin = "".join( map(str, pin_input) )
if pin == pin_target:
pin_success()
else:
pin_wrong()
# reset and cycle
pin_pos = 0
pin_input = [0,0,0,0]
update_pin_screen()
def pin_success():
global locked
lcd.clear()
lcd.cursor(2, 2)
lcd.show_cursor(False)
lcd.write('Unlock successful!')
sleep(3)
locked = False
def pin_wrong():
lcd.clear()
lcd.cursor(2, 2)
lcd.write('Wrong pin entered')
lcd.cursor(3, 2)
lcd.write('Try again...')
sleep(1)
build_pin_screen()
# initially bound inputs to pinlock
GPIO.add_event_detect(BTN_UP, GPIO.RISING, callback = increase_digit, bouncetime = 100)
GPIO.add_event_detect(BTN_DWN, GPIO.RISING, callback = set_digit, bouncetime = 100)
GPIO.add_event_detect(BTN_SET, GPIO.RISING, callback = set_digit, bouncetime = 100)
# Handling USB events
def add_to_playlist(files_array, playlist_file):
playlist = playlist_file
usb_path = '/media/usb'
f = open(playlist, 'w')
for item in files_array:
f.write("%s/%s\n" % (usb_path,item))
f.close()
return playlist
def list_audio_files(dir):
print "Scan dir: " + dir
audio_files = []
for ROOT,DIR,FILES in os.walk(dir):
for file in FILES:
if file.endswith(('.mp3','.MP3')) and not file.startswith(('.')):
audio_files.append(file)
return audio_files
def device_event(observer, action, device):
global usb_added
dev_type = device.get('DEVTYPE')
proc = None
# there are two events, another for usb_interface - drop it
if dev_type == 'usb_device':
if action == 'add' and usb_added == False:
usb_added = True
print 'USB attached. Play files from USB.'
mount_point = '/media/usb' # always mounted there get_mount_point(dev_name)
while len(os.listdir(mount_point)) == 0:
# wait for usb to mount
print "wait for it..."
sleep(.1)
# create playlist in writable area (/media/)
playlist_file = '/media/usb-playlist.txt'
files_array = list_audio_files('/media/usb')
add_to_playlist(files_array, playlist_file)
# send command to mplayer fifo
cmd = "loadlist %s\n" % playlist_file
print(cmd)
mplayer_fifo.write(cmd)
sleep(0.1)
mplayer_fifo.flush()
elif action == 'remove' and usb_added == True:
usb_added = False
print 'USB removed. Switch to local playlist'
mplayer_fifo.write("loadlist /home/pi/audio/playlist.txt\n")
sleep(0.1)
mplayer_fifo.flush()
# Main program loop
def update_loop():
#print "In the loop. Wait 10s"
sleep(1)
return True
# Initial program
try:
if locked == True:
print "Pinlock is active"
pinlock()
lcd.clear()
lcd.write('Pocket FM 2.0')
lcd.cursor(2,1)
lcd.write('F: 106,4 Mhz')
lcd.cursor(3,1)
lcd.write('S: SAT')
# enter digital mode for radio
radio.init_radio()
context = Context()
monitor = Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
observer = MonitorObserver(monitor)
observer.connect('device-event', device_event)
monitor.start()
gobject.threads_init()
glib.idle_add(update_loop)
glib.MainLoop().run()
finally:
# close the fifo
mplayer_fifo.close()
GPIO.cleanup()