forked from bobrathbone/piradio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathretro_radio.py
688 lines (572 loc) · 18.2 KB
/
retro_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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
#!/usr/bin/env python
#
# Raspberry Pi Internet Radio
# Rotary encoder version with no LCD display for Retro Radio conversion
# $Id: retro_radio.py,v 1.18 2016/07/23 16:13:10 bob Exp $
#
# Author : Bob Rathbone
# Site : http://www.bobrathbone.com
#
# This program uses Music Player Daemon 'mpd'and it's client 'mpc'
# See http://mpd.wikia.com/wiki/Music_Player_Daemon_Wiki
#
#
# License: GNU V3, See https://www.gnu.org/copyleft/gpl.html
#
# Disclaimer: Software is provided as is and absolutly no warranties are implied or given.
# The authors shall not be liable for any loss or damage however caused.
#
import os
import RPi.GPIO as GPIO
import signal
import subprocess
import sys
import time
import string
import datetime
from time import strftime
import shutil
import atexit
import traceback
# Class imports
from radio_daemon import Daemon
from radio_class import Radio
from log_class import Log
from rotary_class import RotaryEncoder
from rotary_class_alternative import RotaryEncoderAlternative
from status_led_class import StatusLed
from menu_switch_class import MenuSwitch
# To use GPIO 14 and 15 (Serial RX/TX)
# Remove references to /dev/ttyAMA0 from /boot/cmdline.txt and /etc/inittab
UP = 0
DOWN = 1
GREEN_LED = 27 # Normal operation
BLUE_LED = 22 # Initialisation or loading operation
RED_LED = 23 # Error occured
CurrentStationFile = "/var/lib/radiod/current_station"
CurrentTrackFile = "/var/lib/radiod/current_track"
CurrentFile = CurrentStationFile
PlaylistsDirectory = "/var/lib/mpd/playlists/"
log = Log()
radio = Radio()
statusLed = None
volumeknob = None
tunerknob = None
menu_switch = None
menu_switch_value = 0
# Signal SIGTERM handler
def signalHandler(signal,frame):
global log
radio.execCommand("umount /media > /dev/null 2>&1")
radio.execCommand("umount /share > /dev/null 2>&1")
pid = os.getpid()
log.message("Radio stopped, PID " + str(pid), log.INFO)
GPIO.cleanup()
sys.exit(0)
# Menu switch event handler
def menu_swich_event(switch):
global menu_switch_value
global menu_switch
global log,radio
time.sleep(0.1)
value = menu_switch.get()
if value != menu_switch_value:
message = "Menu switch " + str(switch) + " value " + str(value)
log.message(message , log.INFO)
menu_switch_value = value
return
# Daemon class
class MyDaemon(Daemon):
def run(self):
global CurrentFile
global menu_switch_value
global volumeknob,tunerknob,statusLed,menu_switch
menu_settle = 5 # Allow menu switch to settle
log.init('radio')
signal.signal(signal.SIGTERM,signalHandler)
statusLed = StatusLed(RED_LED,BLUE_LED,GREEN_LED)
statusLed.set(StatusLed.BUSY)
progcall = str(sys.argv)
log.message('Radio running pid ' + str(os.getpid()), log.INFO)
log.message("Radio " + progcall + " daemon version " + radio.getVersion(), log.INFO)
log.message("GPIO version " + str(GPIO.VERSION), log.INFO)
hostname = exec_cmd('hostname')
ipaddr = exec_cmd('hostname -I')
# Display daemon pid on the LCD
message = "Radio pid " + str(os.getpid())
# Wait for the IP network
ipaddr = ""
waiting4network = True
count = 10
while waiting4network:
ipaddr = exec_cmd('hostname -I')
time.sleep(1)
count -= 1
if (count < 0) or (len(ipaddr) > 1):
waiting4network = False
if len(ipaddr) < 1:
log.message("No IP network", log.INFO)
statusLed.set(StatusLed.ERROR)
time.sleep(1)
else:
log.message("IP " + ipaddr, log.INFO)
time.sleep(2)
log.message("Starting MPD", log.INFO)
radio.start()
log.message("MPD started", log.INFO)
radio.setLastMode(radio.MODE_OPTIONS)
radio.setLastOption(radio.RELOADLIB)
mpd_version = radio.execMpcCommand("version")
log.message(mpd_version, log.INFO)
time.sleep(1)
reload(radio)
radio.play(get_stored_id(CurrentFile))
log.message("Current ID = " + str(radio.getCurrentID()), log.INFO)
# Get rotary switches configuration
up_switch = radio.getSwitchGpio("up_switch")
down_switch = radio.getSwitchGpio("down_switch")
left_switch = radio.getSwitchGpio("left_switch")
right_switch = radio.getSwitchGpio("right_switch")
menu_switch = radio.getSwitchGpio("menu_switch")
mute_switch = radio.getSwitchGpio("mute_switch")
boardrevision = radio.getBoardRevision()
if radio.getRotaryClass() is radio.ROTARY_STANDARD:
boardrevision = radio.getBoardRevision()
volumeknob = RotaryEncoder(left_switch,right_switch,mute_switch,volume_event,boardrevision)
tunerknob = RotaryEncoder(up_switch,down_switch,menu_switch,tuner_event,boardrevision)
elif radio.getRotaryClass() is radio.ROTARY_ALTERNATIVE:
volumeknob = RotaryEncoderAlternative(left_switch,right_switch,mute_switch,volume_event,boardrevision)
tunerknob = RotaryEncoderAlternative(up_switch,down_switch,menu_switch,tuner_event,boardrevision)
menu_switch = MenuSwitch(24,8,7,menu_swich_event)
log.message("Running" , log.INFO)
statusLed.set(StatusLed.NORMAL)
# Main processing loop
count = 0
while True:
display_mode = radio.getDisplayMode()
if not radio.getReload():
if display_mode is radio.MODE_SEARCH:
statusLed.set(StatusLed.SELECT)
else:
statusLed.set(StatusLed.NORMAL)
switch = radio.getSwitch()
if switch > 0:
get_switch_states(radio,volumeknob,tunerknob)
radio.setSwitch(0)
if menu_switch_value > 0:
menu_settle -= 1
if menu_settle < 0:
setMenu(radio,menu_switch_value)
menu_settle = 5
else:
time.sleep(0.2)
dateFormat = radio.getDateFormat()
todaysdate = strftime(dateFormat)
# Check for IP address
ipaddr = exec_cmd('hostname -I')
if len(ipaddr) < 1 and radio.getSource() != radio.PLAYER:
statusLed.set(StatusLed.ERROR)
# Shutdown command issued
if display_mode == radio.MODE_SHUTDOWN:
while True:
# Not an error just shutting down
statusLed.set(StatusLed.ERROR)
radio.execCommand("shutdown -h now")
time.sleep(1)
elif display_mode == radio.MODE_TIME:
if radio.getReload():
log.message("Reload ", log.DEBUG)
reload(radio)
radio.setReload(False)
# Check state (pause or play)
checkState(radio)
if radio.volumeChanged():
time.sleep(0.1)
time.sleep(0.1)
def status(self):
# Get the pid from the pidfile
try:
pf = file(self.pidfile,'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if not pid:
message = "radiod status: not running"
log.message(message, log.INFO)
print message
else:
message = "radiod running pid " + str(pid)
log.message(message, log.INFO)
print message
return
# End of class overrides
# Call back routine for the volume control knob
def volume_event(event):
global radio
global volumeknob
switch = 0
# Get rotary switches configuration
left_switch = radio.getSwitchGpio("left_switch")
right_switch = radio.getSwitchGpio("right_switch")
mute_switch = radio.getSwitchGpio("mute_switch")
ButtonNotPressed = volumeknob.getSwitchState(mute_switch)
# Suppress events if volume button pressed
if ButtonNotPressed:
radio.incrementEvent()
if event == RotaryEncoder.CLOCKWISE:
switch = right_switch
elif event == RotaryEncoder.ANTICLOCKWISE:
switch = left_switch
if event == RotaryEncoder.BUTTONDOWN:
switch = mute_switch
radio.setSwitch(switch)
return
# Call back routine for the tuner control knob
def tuner_event(event):
global radio
global tunerknob
switch = 0
# Get rotary switches configuration
up_switch = radio.getSwitchGpio("up_switch")
down_switch = radio.getSwitchGpio("down_switch")
menu_switch = radio.getSwitchGpio("menu_switch")
ButtonNotPressed = tunerknob.getSwitchState(menu_switch)
# Suppress events if tuner button pressed
if ButtonNotPressed:
radio.incrementEvent()
if event == RotaryEncoder.CLOCKWISE:
switch = up_switch
elif event == RotaryEncoder.ANTICLOCKWISE:
switch = radio.getSwitchGpio("down_switch")
if event == RotaryEncoder.BUTTONDOWN:
switch = menu_switch
radio.setSwitch(switch)
return
# Check switch states
def get_switch_states(radio,volumeknob,tunerknob):
switch = radio.getSwitch()
display_mode = radio.getDisplayMode()
input_source = radio.getSource()
events = radio.getEvents()
option = radio.getOption()
# Get rotary switches configuration
up_switch = radio.getSwitchGpio("up_switch")
down_switch = radio.getSwitchGpio("down_switch")
left_switch = radio.getSwitchGpio("left_switch")
right_switch = radio.getSwitchGpio("right_switch")
menu_switch = radio.getSwitchGpio("menu_switch")
mute_switch = radio.getSwitchGpio("mute_switch")
if switch == menu_switch:
log.message("MENU switch mode=" + str(display_mode), log.DEBUG)
radio.unmute()
display_mode = display_mode + 1
# This radio version doesn't use RSS or IP information display
if display_mode >= radio.MODE_RSS:
display_mode = radio.MODE_TIME
radio.setDisplayMode(display_mode)
log.message("New mode " + radio.getDisplayModeString()+
"(" + str(display_mode) + ")", log.DEBUG)
# Shutdown if menu button held for > 3 seconds
MenuSwitch = tunerknob.getSwitchState(menu_switch)
log.message("switch state=" + str(MenuSwitch), log.DEBUG)
count = 15
while MenuSwitch == 0:
time.sleep(0.2)
MenuSwitch = tunerknob.getSwitchState(menu_switch)
count = count - 1
if count < 0:
statusLed.set(StatusLed.ERROR)
log.message("Shutdown", log.DEBUG)
MenuSwitch = 1
radio.setDisplayMode(radio.MODE_SHUTDOWN)
if radio.getUpdateLibrary():
update_library(radio)
radio.setDisplayMode(radio.MODE_TIME)
elif radio.getReload():
source = radio.getSource()
log.message("Reload " + str(source), log.INFO)
reload(radio)
radio.setReload(False)
radio.setDisplayMode(radio.MODE_TIME)
elif radio.optionChanged():
log.message("optionChanged", log.DEBUG)
if radio.alarmActive() and not radio.getTimer() \
and (option == radio.ALARMSETHOURS or option == radio.ALARMSETMINS):
radio.setDisplayMode(radio.MODE_SLEEP)
radio.mute()
else:
radio.setDisplayMode(radio.MODE_TIME)
radio.optionChangedFalse()
elif radio.loadNew():
log.message("Load new search=" + str(radio.getSearchIndex()), log.DEBUG)
radio.playNew(radio.getSearchIndex())
radio.setDisplayMode(radio.MODE_TIME)
time.sleep(0.2)
elif switch == up_switch:
if display_mode != radio.MODE_SLEEP:
log.message("UP switch display_mode " + str(display_mode), log.DEBUG)
if radio.muted():
radio.unmute()
if display_mode == radio.MODE_SOURCE:
radio.toggleSource()
radio.setReload(True)
time.sleep(0.2)
radio.getEvents() # Clear any events
elif display_mode == radio.MODE_SEARCH:
statusLed.set(StatusLed.BUSY)
radio.getNext(UP)
elif display_mode == radio.MODE_OPTIONS:
radio.cycleOptions(UP)
else:
statusLed.set(StatusLed.BUSY)
radio.channelUp()
if display_mode == radio.MODE_RSS:
radio.setDisplayMode(radio.MODE_TIME)
elif switch == down_switch:
log.message("DOWN switch display_mode " + str(display_mode), log.DEBUG)
if display_mode != radio.MODE_SLEEP:
if radio.muted():
radio.unmute()
if display_mode == radio.MODE_SOURCE:
radio.toggleSource()
radio.setReload(True)
time.sleep(0.2)
radio.getEvents() # Clear any events
elif display_mode == radio.MODE_SEARCH:
statusLed.set(StatusLed.BUSY)
radio.getNext(DOWN)
elif display_mode == radio.MODE_OPTIONS:
radio.cycleOptions(DOWN)
else:
statusLed.set(StatusLed.BUSY)
radio.channelDown()
if display_mode == radio.MODE_RSS:
radio.setDisplayMode(radio.MODE_TIME)
elif switch == left_switch:
log.message("LEFT switch" ,log.DEBUG)
if display_mode != radio.MODE_SLEEP:
if display_mode == radio.MODE_OPTIONS:
#toggle_option(radio,DOWN)
radio.changeOption(DOWN)
elif display_mode == radio.MODE_SEARCH and input_source == radio.PLAYER:
statusLed.set(StatusLed.BUSY)
radio.findNextArtist(DOWN)
else:
# Set the volume by the number of rotary encoder events
log.message("events=" + str(radio.getEvents()), log.DEBUG)
statusLed.set(StatusLed.BUSY)
if events > 1:
volAdjust = events/2
else:
volAdjust = events
if radio.muted():
radio.unmute()
volume = radio.getVolume()
while volAdjust > 0:
volume -= 1
if volume < 0:
volume = 0
radio.setVolume(volume)
volAdjust -= 1
statusLed.set(StatusLed.NORMAL)
elif switch == right_switch:
log.message("RIGHT switch" ,log.DEBUG)
if display_mode != radio.MODE_SLEEP:
if display_mode == radio.MODE_OPTIONS:
radio.changeOption(UP)
elif display_mode == radio.MODE_SEARCH and input_source == radio.PLAYER:
statusLed.set(StatusLed.BUSY)
radio.findNextArtist(UP)
else:
# Set the volume by the number of rotary encoder events
log.message("events=" + str(radio.getEvents()), log.DEBUG)
statusLed.set(StatusLed.BUSY)
if events > 1:
volAdjust = events/2
else:
volAdjust = events
if radio.muted():
radio.unmute()
volume = radio.getVolume()
range = radio.getVolumeRange()
while volAdjust > 0:
volume += 1
if volume > range:
volume = range
radio.setVolume(volume)
volAdjust -= 1
statusLed.set(StatusLed.NORMAL)
elif switch == mute_switch:
log.message("MUTE switch" ,log.DEBUG)
if display_mode != radio.MODE_SLEEP:
# If mute button held in for 2 seconds then mute otherwise speak information
if radio.speechEnabled() and not radio.muted():
log.message("Speech enabled" ,log.DEBUG)
MuteSwitch = volumeknob.getSwitchState(mute_switch)
log.message("Mute switch state=" + str(MuteSwitch), log.DEBUG)
count = 5
while MuteSwitch == 0:
time.sleep(0.2)
MuteSwitch = volumeknob.getSwitchState(mute_switch)
count = count - 1
if count < 0:
log.message("Mute", log.DEBUG)
MuteSwitch = 1
radio.mute()
if not radio.muted():
radio.speakInformation()
else:
if radio.muted():
radio.unmute()
time.sleep(1)
else:
radio.mute()
time.sleep(2)
# Reset all rotary encoder events to zero
radio.resetEvents()
radio.setSwitch(0)
return
# Update music library
def update_library(radio):
log.message("Updating library", log.INFO)
radio.updateLibrary()
return
# Reload if new source selected (RADIO or PLAYER)
def reload(radio):
radio.unmountAll()
source = radio.getSource()
if source == radio.RADIO:
dirList=os.listdir(PlaylistsDirectory)
for fname in dirList:
if os.path.isfile(fname):
continue
log.message("Loading " + fname, log.DEBUG)
time.sleep(0.1)
radio.loadStations()
elif source == radio.PLAYER:
radio.loadMedia()
current = radio.execMpcCommand("current")
if len(current) < 1:
update_library(radio)
return
# Get currently playing station or track number from MPC
def get_current_id():
current_id = 1
status = radio.execMpcCommand("status | grep \"\[\" ")
if len(status) > 1:
x = status.index('#')+1
y = status.index('/')
current_id = int(status[x:y])
exec_cmd ("echo " + str(current_id) + " > " + CurrentFile)
return current_id
# Get the last ID stored in /var/lib/radiod
def get_stored_id(current_file):
current_id = 5
if os.path.isfile(current_file):
current_id = int(exec_cmd("cat " + current_file) )
return current_id
# Execute system command
def exec_cmd(cmd):
p = os.popen(cmd)
result = p.readline().rstrip('\n')
return result
# Get list of tracks or stations
def get_mpc_list(cmd):
list = []
line = ""
p = os.popen("/usr/bin/mpc " + cmd)
while True:
line = p.readline().strip('\n')
if line.__len__() < 1:
break
list.append(line)
return list
def unmuteRadio(radio):
radio.unmute()
return
# Set the display state from the menu switch
def setMenu(radio,value):
global log
global menu_switch_value
log.message("setMenu " + str(value), log.DEBUG)
if radio.getDisplayMode != radio.MODE_TIME:
radio.setDisplayMode(radio.MODE_TIME)
source = radio.getSource()
if statusLed.get() is StatusLed.SELECT:
statusLed.set(StatusLed.NORMAL)
if value <= 1:
check4new(radio)
if source is radio.PLAYER:
statusLed.set(StatusLed.BUSY)
radio.setSource(radio.RADIO)
radio.setReload(True)
elif value == 2:
statusLed.set(StatusLed.BUSY)
radio.speakInformation()
elif value == 3:
statusLed.set(StatusLed.SELECT)
radio.setDisplayMode(radio.MODE_SEARCH)
elif value == 4:
check4new(radio)
if source is radio.RADIO:
radio.setSource(radio.PLAYER)
radio.setReload(True)
statusLed.set(StatusLed.BUSY)
check4new(radio)
elif value == 5:
radio.setSource(radio.PLAYER)
statusLed.set(StatusLed.BUSY)
update_library(radio)
statusLed.set(StatusLed.NORMAL)
menu_switch_value = 0
return
# Check if load new required
def check4new(radio):
if radio.loadNew():
log.message("Load new search=" + str(radio.getSearchIndex()), log.DEBUG)
radio.playNew(radio.getSearchIndex())
return
# Check state (play or pause)
# Returns paused True if paused
def checkState(radio):
paused = False
display_mode = radio.getDisplayMode()
state = radio.getState()
radio.getVolume()
if state == 'pause':
paused = True
if not radio.muted():
if radio.alarmActive() and not radio.getTimer():
radio.setDisplayMode(radio.MODE_SLEEP)
radio.mute()
elif state == 'play':
if radio.muted():
unmuteRadio(radio)
radio.setDisplayMode(radio.MODE_TIME)
return paused
### Main routine ###
if __name__ == "__main__":
daemon = MyDaemon('/var/run/radiod.pid')
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
daemon.start()
elif 'stop' == sys.argv[1]:
os.system("service mpd stop")
daemon.stop()
elif 'restart' == sys.argv[1]:
daemon.restart()
elif 'status' == sys.argv[1]:
daemon.status()
elif 'nodaemon' == sys.argv[1]:
daemon.nodaemon()
elif 'version' == sys.argv[1]:
print "Version " + radio.getVersion()
else:
print "Unknown command: " + sys.argv[1]
sys.exit(2)
sys.exit(0)
else:
print "usage: %s start|stop|restart|status|version" % sys.argv[0]
sys.exit(2)
# End of script