forked from jotak/jopi-mpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jopi-mpd.py
executable file
·265 lines (226 loc) · 5.88 KB
/
jopi-mpd.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
#!/usr/bin/python
from time import sleep, localtime, strftime
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from cleaner import cleanString
import sys
import subprocess
import threading
import os
import re
import logging
welcomeText = "Welcome on jopi-mpd!"
musicAbsPath = "/var/lib/mpd/music/"
playlistRelPath = "WEBRADIO/playlists/"
class TextScroller:
text = ''
position = 0
textLength = 0
step = 0
playPos = -1
playAnimPos = 0
def scroll(self):
nextPos = self.position + self.step
if nextPos < 0 or nextPos >= self.textLength - 16:
self.step *= -1
else:
self.position = nextPos
if self.playAnimPos + 1 >= self.playPos:
self.playAnimPos = 0
else:
self.playAnimPos = self.playAnimPos + 1
self.display()
def setText(self, newText):
self.text = newText
self.textLength = len(newText)
self.position = 0
if self.textLength <= 16:
# Text is small enough => deactivate scrolling
self.step = 0
else:
# 1 means left to right
self.step = 1
def setPlayPos(self, newPos):
self.playPos = newPos
if self.playAnimPos >= newPos:
self.playAnimPos = newPos - 1
if self.playAnimPos < 0:
self.playAnimPos = 0
def display(self):
global lcd
topStart = self.position
topEnd = topStart + 16
top = cleanString(self.text[topStart:topEnd])
fullArt = "~~~~~~~~~~~~~~~~"
bottom = " "
if self.playPos < 0:
bottom = fullArt
else:
bottom = bottom[:self.playAnimPos] + fullArt[self.playAnimPos] + bottom[self.playAnimPos+1:]
bottom = bottom[:self.playPos] + ">" + bottom[self.playPos+1:]
fmtText = top + "\n" + bottom
lcd.clear()
lcd.message(fmtText)
scroller = TextScroller()
# Initialize the LCD plate. Should auto-detect correct I2C bus. If not,
# pass '0' for early 256 MB Model B boards or '1' for all later versions
lcd = Adafruit_CharLCDPlate()
def display(text):
fmtText = cleanString(text[:16] + "\n" + text[16:])
lcd.clear()
lcd.message(fmtText)
# Clear display and show greeting, pause 1 sec
display(welcomeText)
sleep(3)
state = "play"
# Use subprocess.check_output to get list of playlists (mpc lsplaylists)
lists = ["no-list"]
currentList = 0
fileListIdx = -1
previousSong = ""
# Buttons
buttons = (lcd.SELECT, lcd.LEFT, lcd.UP, lcd.DOWN, lcd.RIGHT)
curPressed = -1
# Colors
colors = (lcd.RED , lcd.YELLOW, lcd.GREEN, lcd.TEAL, lcd.BLUE, lcd.VIOLET)
curColor = lcd.RED
showingTime = False
def fetchLists():
global lists, fileListIdx, musicAbsPath, playlistRelPath
lists = subprocess.check_output(["mpc", "lsplaylists"]).splitlines()
fileListIdx = len(lists)
lists.extend(os.listdir(musicAbsPath + playlistRelPath))
fetchLists()
def buttonPressed(i):
global state
if state == "menu":
buttonPressedMenu(i)
else:
buttonPressedPlayback(i)
def changeColor():
global curColor
curColor += 1
curColor %= len(colors)
lcd.backlight(colors[curColor])
def buttonPressedMenu(i):
global state, currentList, lists, showingTime
if i == 0:
state = "play"
refreshModePlaying(True)
elif i == 1:
# prev list
if currentList == 0:
currentList = len(lists) - 1
else:
currentList -= 1
refreshModeList()
elif i == 2:
showingTime = not showingTime
if not showingTime:
refreshModeList()
elif i == 3:
# play list
subprocess.call(["mpc", "clear"])
if currentList < fileListIdx:
subprocess.call(["mpc", "load", lists[currentList]])
else:
subprocess.call(["mpc", "load", playlistRelPath + lists[currentList]])
subprocess.call(["mpc", "play"])
state = "play"
refreshModePlaying(True)
elif i == 4:
# next list
currentList += 1
if currentList == len(lists):
currentList = 0
refreshModeList()
def buttonPressedPlayback(i):
global state, showingTime
if i == 0:
fetchLists()
refreshModeList()
state = "menu"
elif i == 1:
subprocess.call(["mpc", "prev"])
elif i == 2:
showingTime = not showingTime
refreshModePlaying(True)
elif i == 3:
subprocess.call(["mpc", "toggle"])
elif i == 4:
subprocess.call(["mpc", "next"])
def refreshModeList():
global lists, currentList, scroller
scroller.setText(lists[currentList])
def refreshModePlaying(forceSetText=False):
global previousSong, scroller
statusLines = subprocess.check_output(["mpc"]).split('\n')
curSong = statusLines[0]
pct = -1
if len(statusLines) > 2:
search = re.search('\\((\\d+)%', statusLines[1])
if search:
pct = int(0.16 * int(search.group(1)))
scroller.setPlayPos(pct)
if previousSong != curSong:
previousSong = curSong
changeColor()
if curSong == "":
scroller.setText("No song selected")
else:
scroller.setText(curSong)
elif forceSetText:
scroller.setText(curSong)
def refreshModeTime():
scroller.setText(strftime("%a, %d %b %Y %H:%M:%S", localtime()))
def checkRun():
try:
with open('/var/lock/stop-jopi-mpd'):
return False
except IOError:
return True
class DisplayThread(threading.Thread):
def run (self):
dispError = False
while True:
try:
global state, showingTime, scroller
if checkRun() == False:
break
if showingTime:
refreshModeTime()
elif state == "play":
refreshModePlaying()
scroller.scroll()
sleep(.9)
dispError = False
except Exception, err:
if dispError == False:
logging.exception('')
dispError = True
else:
sleep(1)
DisplayThread().start()
globError = False
while True:
try:
if checkRun() == False:
break
sleep(.1)
nothingPressed = True
for i in range(5):
if lcd.buttonPressed(buttons[i]):
nothingPressed = False
if i != curPressed:
buttonPressed(i)
curPressed = i
break
if nothingPressed:
curPressed = -1
globError = False
except Exception, err:
if globError == False:
logging.exception('')
globError = True
else:
sleep(1)
display("Goodbye!")