-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathradio.py
executable file
·385 lines (296 loc) · 7.67 KB
/
radio.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# A simple internet radio for RaspberryPI
# Based on mpd/mpc and the Character LCD Plate by Adafruit
#
# The basic navigation code is based on lcdmenu.py by Alan Aufderheide
#
# Copyright (c) 2013 Olav Schettler
# Open source. MIT license
#
import signal
import sys
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
import re
import shlex, subprocess
from time import strftime, sleep
from unidecode import unidecode
DEBUG = False
def fixed_length(str, length):
'Truncate and pad str to length'
return ('{:<%d}' % length).format(str[:length])
class Node:
'''
Base class for nodes in a hierarchical navigation tree
'''
def __init__(self, text):
self.mark = '-'
self.parent = None
self.text = text
def into(self):
pass
def __repr__(self):
return 'node:' + self.text
class Timer(Node):
def __init__(self):
self.mark = '-'
self.parent = None
def gettext(self):
print "TT"
return strftime('%H:%M:%S %d.%m')
text = property(gettext)
class Folder(Node):
def __init__(self, text, items=[]):
Node.__init__(self, text)
self.mark = '>'
self.setItems(items)
def setItems(self, items):
self.items = items
for item in self.items:
item.parent = self
class Playlists(Folder):
def __init__(self, radio):
Folder.__init__(self, 'Playlists')
self.radio = radio
def into(self):
print "into", repr(self)
self.setItems([
Playlist(playlist, self.radio) for playlist in self.radio.command('mpc lsplaylists')
])
class FinishException(Exception):
pass
class App:
'''
Base class of applications and applets
'''
ROWS = 2
COLS = 16
def __init__(self, lcd, folder):
self.lcd = lcd
self.folder = folder
self.top = 0
self.selected = 0
def display(self):
if self.top > len(self.folder.items) - self.ROWS:
self.top = len(self.folder.items) - self.ROWS
if self.top < 0:
self.top = 0
if DEBUG:
print '------------------'
str = ''
for row in range(self.top, self.top + self.ROWS):
if row > self.top:
str += '\n'
if row < len(self.folder.items):
if row == self.selected:
line = self.folder.items[row].mark
else:
line = ' '
line = fixed_length(line + self.folder.items[row].text, self.COLS)
str += line
if DEBUG:
print('|' + line + '|')
if DEBUG:
print '------------------'
self.lcd.home()
self.lcd.message(str)
def up(self):
if self.selected == 0:
return
elif self.selected > self.top:
self.selected -= 1
else:
self.top -= 1
self.selected -= 1
def down(self):
if self.selected + 1 == len(self.folder.items):
return
elif self.selected < self.top + self.ROWS - 1:
self.selected += 1
else:
self.top += 1
self.selected += 1
def left(self):
if not isinstance(self.folder.parent, Folder):
return
# find the current in the parent
itemno = 0
index = 0
for item in self.folder.parent.items:
if self.folder == item:
if DEBUG:
print 'foundit:', item
index = itemno
else:
itemno += 1
if index < len(self.folder.parent.items):
self.folder = self.folder.parent
self.top = index
self.selected = index
else:
self.folder = self.folder.parent
self.top = 0
self.selected = 0
def right(self):
if isinstance(self.folder.items[self.selected], Folder):
self.folder = self.folder.items[self.selected]
self.top = 0
self.selected = 0
self.folder.into()
elif isinstance(self.folder.items[self.selected], Applet):
self.folder.items[self.selected].run()
def select(self):
if isinstance(self.folder.items[self.selected], Applet):
self.folder.items[self.selected].run()
def command(self, cmd):
print shlex.split(cmd)
result = subprocess.check_output(
shlex.split(cmd), stderr=subprocess.STDOUT
)
result = result.rstrip().split('\n')
print cmd, '-->', result
return result
def tick(self):
'''
In case variable information is displayed, refresh every second
'''
if self.ticks % 10 == 0:
self.display()
def handlesignal(self, signum, frame):
self.lcd.clear()
self.lcd.backlight(Adafruit_CharLCDPlate.OFF)
sys.exit(0)
def run(self):
'''
Basic event loop of the application
'''
'catch shutdown'
signal.signal(signal.SIGTERM, self.handlesignal)
self.ticks = 0
self.display()
last_buttons = None
while True:
self.tick()
self.ticks += 1
sleep(0.1)
buttons = self.lcd.buttons()
if last_buttons == buttons:
continue
last_buttons = buttons
try:
if (self.lcd.buttonPressed(self.lcd.LEFT)):
self.left()
self.display()
if (self.lcd.buttonPressed(self.lcd.UP)):
self.up()
self.display()
if (self.lcd.buttonPressed(self.lcd.DOWN)):
self.down()
self.display()
if (self.lcd.buttonPressed(self.lcd.RIGHT)):
self.right()
self.display()
if (self.lcd.buttonPressed(self.lcd.SELECT)):
self.select()
self.display()
except FinishException:
return
class Radio(App):
'''
The application.
'''
def __init__(self):
self.command('mpc stop')
App.__init__(self,
Adafruit_CharLCDPlate(),
Folder('Pauls iRadio', (
Playlists(self),
Folder('Settings', (
Node(self.command('hostname -I')[0]),
Timer(),
)),
))
)
class Applet(App):
def __init__(self, text, app):
self.mark = '*'
self.parent = None
self.text = text
self.app = app
self.lcd = app.lcd
def command(self, cmd):
return self.app.command(cmd)
def left(self):
return
def right(self):
return
def up(self):
return
def down(self):
return
def select(self):
return
class Playlist(Applet):
volumes = (0, 60, 70, 80, 85, 90, 95, 100)
def display(self):
self.lines = (
unidecode(self.command('mpc -f %name% current')[0].split(',', 1)[0]),
unidecode(self.command('mpc -f %title% current')[0]),
)
self.volume = int(re.search(r'\d+',
self.command('mpc volume')[0]
).group())
self.dir = 'L'
self.shift = 0
def tick(self):
if self.ticks % 5 != 0:
return
if self.lines[0] == '':
self.command('mpc volume 70')
self.display()
return
str = ''
str += fixed_length(self.lines[0], self.COLS)
str += '\n' + fixed_length(self.lines[1][self.shift:], self.COLS)
if DEBUG:
print '------------------'
for line in str.split('\n'):
print '|' + line + '|'
print '------------------'
self.lcd.home()
self.lcd.message(str)
if self.dir == 'L':
if self.shift + self.COLS < len(self.lines[1]):
self.shift += 1
else:
self.dir = 'R'
else:
if self.shift > 0:
self.shift -= 1
else:
self.display()
def run(self):
self.command('mpc clear')
self.command('mpc load ' + self.text)
self.command('mpc play')
Applet.run(self)
def left(self):
'Return from applet'
raise FinishException
def up(self):
try:
pos = self.volumes.index(self.volume)
except:
pos = 0
if pos < len(self.volumes) - 1:
self.command('mpc volume %d' % self.volumes[pos + 1])
def down(self):
try:
pos = self.volumes.index(self.volume)
except:
pos = 0
if pos > 0:
self.command('mpc volume %d' % self.volumes[pos - 1])
if __name__ == '__main__':
Radio().run()