-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
mpd_control_vwcdpic.py
executable file
·218 lines (186 loc) · 4.67 KB
/
mpd_control_vwcdpic.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
#!/usr/bin/python
#
# Created: 23.06.2013 20:00:51
# Author: Dennis Schuett, dev.shyd.de
# port for arduino by kovo
#
import os
import serial
import string
import time
# bytes sent when radio keys are pressed
CDC_END_CMD = chr(0x14)
CDC_PLAY = "MENABLE\n"#chr(0xE4)
CDC_STOP = "MDISABLE\n"#chr(0x10)
CDC_NEXT = "NEXT\n"#chr(0xF8)
CDC_PREV = "PREVIOUS\n"#chr(0x78)
CDC_SEEK_FWD = chr(0xD8)
CDC_SEEK_RWD = chr(0x58)
CDC_CD1 = "LIST1\n"#chr(0x0C)
CDC_CD2 = "LIST2\n"#chr(0x8C)
CDC_CD3 = "LIST3\n"#chr(0x4C)
CDC_CD4 = "LIST4\n"#chr(0xCC)
CDC_CD5 = "LIST5\n"#chr(0x2C)
CDC_CD6 = "LIST6\n"#chr(0xAC)
CDC_CDSET = "NXT_LIST\n"#chr(0x38)
CDC_SCAN = "SCAN\n"#chr(0xA0)
CDC_SFL = "RANDOM\n"#chr(0x60)
CDC_PLAY_NORMAL = "PLAY\n"#chr(0x08)
CDC_PREV_CD = "PRV_LIST\n"#chr(0x18)
# control bytes to set the display
CMD_SCAN = chr(0xCA)
CMD_SHFFL = chr(0xCB)
CMD_PLAY = chr(0xCC)
CD_MASK = 0xC0
cd_filename = "./last_cd"
global ser
ser = None
timeout_seek = 0.5
timeout_normal = 2
# send current cd no. to radio and save it for next reboot
def set_cd_no(val):
ser.write(val)
fo = open(cd_filename, "wb")
fo.write(val)
fo.close()
# load new cd-dir
def play_cd(cd_no):
r = os.popen("mpc lsplaylists| grep cd" + str(cd_no)).read()
if r != "":
set_cd_no(chr(CD_MASK + cd_no))
os.popen("mpc clear")
os.popen("mpc load cd" + str(cd_no))
os.popen("mpc play")
os.popen("mpc random off")
# init serial and last cd no. for correct radio display
def connect():
while(1):
try:
global ser
ser = serial.Serial('/dev/ttyUSB0', 9600)
ser.timeout = timeout_normal #we need a timeout to update the track no.
ser.writeTimeout = timeout_normal
try:
fo = open(cd_filename, "rb")
cd = fo.read(1)
fo.close()
ser.write(cd)
except:
print "no last cd no. [not sending cd#]"
return
except:
time.sleep(2) #wait before retrying
try:
#os.popen("/etc/init.d/mpd restart") #restart mpd due to alsa-equalizer
connect()
# read cmds from the radio and act like a cd changer
while(1):
try:
c = ser.readline()
print c
if c == CDC_PLAY:
#os.popen("/etc/init.d/mpd start")
os.popen("mpc play")
os.popen("mpc repeat on")
elif c == CDC_STOP:
os.popen("mpc pause")
# stop service to sync current status
#os.popen("/etc/init.d/mpd stop")
elif c == CDC_NEXT:
os.popen("mpc next")
elif c == CDC_PREV:
os.popen("mpc prev")
elif c == CDC_SEEK_FWD:
#ser.timeout = timeout_seek
#while(1):
os.popen("mpc seek +00:00:01")
# if ser.read() == CDC_PLAY and ser.read() == CDC_END_CMD:
# break
#ser.timeout = timeout_normal
elif c == CDC_SEEK_RWD:
#ser.timeout = timeout_seek
#while(1):
os.popen("mpc seek -00:00:01")
# if ser.read() == CDC_PLAY and ser.read() == CDC_END_CMD:
# break
#ser.timeout = timeout_normal
elif c == CDC_CD1:
play_cd(1)
elif c == CDC_CD2:
play_cd(2)
elif c == CDC_CD3:
play_cd(3)
elif c == CDC_CD4:
play_cd(4)
elif c == CDC_CD5:
play_cd(5)
elif c == CDC_CD6:
play_cd(6)
elif c == CDC_CDSET:
fo = open(cd_filename, "rb")
cd = fo.read(1)
fo.close()
if cd == chr(0xC1):
play_cd(2)
elif cd == chr(0xC2):
play_cd(3)
elif cd == chr(0xC3):
play_cd(4)
elif cd == chr(0xC4):
play_cd(5)
elif cd == chr(0xC5):
play_cd(6)
elif cd == chr(0xC6):
play_cd(1)
elif c == CDC_PREV_CD:
fo = open(cd_filename, "rb")
cd = fo.read(1)
fo.close()
if cd == chr(0xC1):
play_cd(6)
elif cd == chr(0xC2):
play_cd(1)
elif cd == chr(0xC3):
play_cd(2)
elif cd == chr(0xC4):
play_cd(3)
elif cd == chr(0xC5):
play_cd(4)
elif cd == chr(0xC6):
play_cd(5)
elif c == CDC_SCAN:
os.popen("mpc update")
elif c == CDC_SFL:
os.popen("mpc random")#just togle
elif c == CDC_PLAY_NORMAL:
os.popen("mpc play")
#get current track no. (radio display will be delayed up to {timeout_normal})
mpc = os.popen("mpc |grep ] #").read()
try:
mpc = mpc.split("/", 1)
mpc = mpc[0].split("#", 1)
tr = chr(string.atoi(mpc[1], 16))
ser.write(tr)
except:
print "not playing"
#get current playmode
mpc = os.popen("mpc | grep random:").read()
try:
mpc = mpc.split("random: ", 1)
if mpc[1].startswith("on"):
ser.write(CMD_SHFFL)
elif mpc[1].startswith("off"):
ser.write(CMD_PLAY)
except:
print "not playing"
except (serial.SerialException, serial.SerialTimeoutException):
print "serial port unavailable, reconnecting..."
ser.close()
connect()
except:
raise
except (KeyboardInterrupt):
if ser is not None:
ser.close()
print "port closed!"
print "KeyboardInterrupt detected, exiting..."