-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio.py
69 lines (56 loc) · 1.48 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
import subprocess
import os
import time
import RPi.GPIO as GPIO
RADIO_PIN1 = 11
RADIO_PIN2 = 12
RADIO_PIN3 = 15
TOGGLE_PIN = 16
PLAYLISTS = [
'http://www.bbc.co.uk/radio/listen/live/r4_aaclca.pls',
'http://www.bbc.co.uk/radio/listen/live/r6_aaclca.pls',
'http://media.kcrw.com/live/kcrwmusic.pls'
]
def main():
GPIO.setup(RADIO_PIN1, GPIO.IN)
GPIO.setup(RADIO_PIN2, GPIO.IN)
GPIO.setup(RADIO_PIN3, GPIO.IN)
GPIO.setup(TOGGLE_PIN, GPIO.IN)
mplayerBaseArgs = ['mplayer', '-softvol', '-cache', '256', '-afm', 'mp3lib', '-quiet', '-playlist']
shairportArgs = ['perl', '/usr/local/bin/shairport.pl']
mode = 0
pid = None
print "Started. Waiting for GPIO input."
while 1:
oldMode = mode
if (not GPIO.input(TOGGLE_PIN)):
mode = 4 #airplay
elif (not GPIO.input(RADIO_PIN1)):
mode = 1 #mplayer
elif (not GPIO.input(RADIO_PIN2)):
mode = 2 #mplayer
elif (not GPIO.input(RADIO_PIN3)):
mode = 3 #mplayer
else:
mode = 0 #stopped
#mode has changed
if (oldMode != mode):
print 'Mode changed'
#kill old process, if any
if (pid):
os.kill(pid, 15)
print "Killing process " + str(pid)
if (mode == 4):
args = shairportArgs
print "Starting ShairPort"
elif (mode == 0):
print "Stopped"
continue
elif (0 < mode < 4):
args = mplayerBaseArgs + [PLAYLISTS[mode-1]]
print "Playing " + PLAYLISTS[mode-1]
pid = subprocess.Popen(args).pid
#sleep for a short time
time.sleep(0.5)
if __name__ == '__main__':
main()