forked from audiophonics/MoodAudio-RaspDAC-Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaspDacDisplay.py
More file actions
468 lines (373 loc) · 12.9 KB
/
RaspDacDisplay.py
File metadata and controls
468 lines (373 loc) · 12.9 KB
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
import Winstar_GraphicOLED
import moment
import time
import json
import logging
import commands
from mpd import MPDClient
import telnetlib
import RPi.GPIO as GPIO
import Queue
from threading import Thread
import signal
import sys
STARTUP_MSG = "Raspdac\nStarting"
ARTIST_TIME = 8.0 # Amount of time to display Artist name (in seconds)
TITLE_TIME = 10.0 # Amount of time to display the Title (in seconds)
HESITATION_TIME = 2.5 # Amount of time to hesistate before scrolling (in seconds)
NOTPLAYING_TIMEDISPLAY = 8.0 # Amount of time to display Time
NOTPLAYING_IPDISPLAY = 1.5 # Amount of time to display IP address
# The Winstar display shipped with the RaspDac is capable of two lines of display
# when the 5x8 font is used. This code assumes that is what you will be using.
# The display logic would need significant rework to support a different number
# of display lines!
DISPLAY_WIDTH = 16 # the character width of the display
DISPLAY_HEIGHT = 2 # the number of lines on the display
# This is where the log file will be written
LOGFILE='/var/log/RaspDacDisplay.log'
# Adjust this setting to localize the time display to your region
TIMEZONE="Europe/Paris"
TIME24HOUR=False
#TIMEZONE="Europe/Paris"
# Logging level
#LOGLEVEL=logging.DEBUG
LOGLEVEL=logging.INFO
#LOGLEVEL=logging.WARNING
#LOGLEVEL=logging.CRITICAL
class RaspDac_Display:
def __init__(self):
logging.info("RaspDac_Display Initializing")
# Initilize the connections to the music Daemons. Currently supporting
# MPD and SPOP (for Spotify)
ATTEMPTS=3
# Will try to connect multiple times
for i in range (1,ATTEMPTS):
self.client = MPDClient(use_unicode=True)
try:
# Connect to the MPD daemon
self.client.connect("localhost", 6600)
break
except:
# logging.warning("Connection to MPD service attempt " + str(i) + " failed")
time.sleep(2)
else:
# After the alloted number of attempts did not succeed in connecting
logging.debug("Unable to connect to MPD service on startup")
# Now attempting to connect to the Spotify daemon
# This may fail if Spotify is not configured. That's ok!
for i in range (1,ATTEMPTS):
try:
self.spotclient = telnetlib.Telnet("localhost",6602)
self.spotclient.read_until("\n")
break
except:
# logging.warning("Connection to Spotify service attempt " + str(i) + " failed")
time.sleep(2)
else:
# After the alloted number of attempts did not succeed in connecting
logging.debug("Unable to connect to Spotify service on startup")
def status_mpd(self):
# Try to get status from MPD daemon
try:
m_status = self.client.status()
m_currentsong = self.client.currentsong()
except:
# Attempt to reestablish connection to daemon
try:
self.client.connect("localhost", 6600)
m_status=self.client.status()
m_currentsong = self.client.currentsong()
except:
logging.debug("Could not get status from MPD daemon")
return { 'state':u"notrunning", 'artist':u"", 'title':u"", 'current':0, 'duration':0 }
state = m_status.get('state')
if state == "play":
artist = m_currentsong.get('artist')
name = m_currentsong.get('name')
# Trying to have something to display. If artist is empty, try the
# name field instead.
if artist is None:
artist = name
title = m_currentsong.get('title')
(current, duration) = (m_status.get('time').split(":"))
# since we are returning the info as a JSON formatted return, convert
# any None's into reasonable values
if artist is None: artist = u""
if title is None: title = u""
if current is None: current = 0
if duration is None: duration = 0
return { 'state':state, 'artist':artist, 'title':title, 'current':current, 'duration': duration }
else:
return { 'state':u"stop", 'artist':u"", 'title':u"", 'current':0, 'duration':0 }
def status_spop(self):
# Try to get status from SPOP daemon
try:
self.spotclient.write("status\n")
spot_status_string = self.spotclient.read_until("\n").strip()
except:
# Try to reestablish connection to daemon
try:
self.spotclient = telnetlib.Telnet("localhost",6602)
self.spotclient.read_until("\n")
self.spotclient.write("status\n")
spot_status_string = self.spotclient.read_until("\n").strip()
except:
logging.debug("Could not get status from SPOP daemon")
return { 'state':u"notrunning", 'artist':u"", 'title':u"", 'current':0, 'duration':0 }
spot_status = json.loads(spot_status_string)
if spot_status.get('status') == "playing":
artist = spot_status.get('artist')
title = spot_status.get('title')
current = spot_status.get('position')
duration = spot_status.get('duration')
# since we are returning the info as a JSON formatted return, convert
# any None's into reasonable values
if artist is None: artist = u""
if title is None: title = u""
if current is None: current = 0
if duration is None:
duration = 0
else:
# The spotify client returns time in 1000's of a second
# Need to adjust to seconds to be consistent with MPD
duration = duration / 1000
return { 'state':u"play", 'artist':artist, 'title':title, 'current':current, 'duration': duration }
else:
return { 'state':u"stop", 'artist':u"", 'title':u"", 'current':0, 'duration':0 }
def status(self):
# Try MPD daemon first
status = self.status_mpd()
# If MPD is stopped
if status.get('state') != "play":
# Try SPOP
status = self.status_spop()
return status
def Display(q, l, c):
# q - Queue to receive updates from
# l - number of lines in display
# c - number of columns in display
lines = []
columns = []
lcd = Winstar_GraphicOLED.Winstar_GraphicOLED()
lcd.oledReset()
lcd.home()
lcd.clear()
lcd.message(STARTUP_MSG)
time.sleep(2)
for i in range (0, l):
lines.append("")
columns.append(0)
# Get first display update off of the queue
item = q.get()
q.task_done()
lcd.home()
lcd.clear()
for i in range(len(item)):
# Convert from Unicode to UTF-8
#item[i] = item[i].encode("utf-8")
lines[i] = item[i]
lcd.setCursor(0,i)
lcd.message( lines[i][0:c] )
prev_time = time.time()
while True:
short_lines=True
# Smooth animation
if time.time() - prev_time < .08:
time.sleep(.08-(time.time()-prev_time))
try:
# Determine if any lines have been udpated and if yes display them
for i in range(len(item)):
# Convert from Unicode into UTF-8
#item[i] = item[i].encode("utf-8")
# Check if line is longer than display
if len(item[i])>c:
short_lines = False
# Check if line has been updated
if lines[i] != item[i]:
# Create a line to print that is at least as long as the existing line
# This is to erase any extraneous characters on the display
buf = item[i].ljust(len(lines[i]))
# Reset cursor to beginning of changed line and then display the change
lcd.setCursor(0,i)
lcd.message(buf[0:c])
# Update the local line data and reset the column position for the line
lines[i] = item[i]
columns[i] = 0
# If lines all fit on display then we can wait for new input
if short_lines:
item=q.get()
q.task_done()
else:
# Update all long lines
for i in range(len(lines)):
if len(lines[i])>c:
buf = "%s %s" % (lines[i], lines[i][0:DISPLAY_WIDTH-1])
#buf = "{} {}".format(lines[i],lines[i][0:DISPLAY_WIDTH-1])
#buf = lines[i]+" "+lines[i][0:c]
columns[i] = columns[i]+1
if columns[i] > len(buf)-c:
columns[i]=0
lcd.setCursor(0,i)
# Print the portion of the string that is currently visible
lcd.message(buf[columns[i]:columns[i]+c])
# Since we have to continue updating the display, check for a new update but don't block
item=q.get_nowait()
q.task_done()
prev_time = time.time()
except Queue.Empty:
prev_time = time.time()
pass
def sigterm_handler(_signo, _stack_frame):
sys.exit(0)
if __name__ == '__main__':
signal.signal(signal.SIGTERM, sigterm_handler)
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', filename=LOGFILE, level=LOGLEVEL)
logging.info("RaspDac Display Startup")
try:
dq = Queue.Queue() # Create display Queue
dm = Thread(target=Display, args=(dq,DISPLAY_HEIGHT,DISPLAY_WIDTH))
dm.setDaemon(True)
dm.start()
rd = RaspDac_Display()
except:
#e = sys.exc_info()[0]
#logging.critical("Received exception: %s" % e)
logging.critical("Unable to initialize RaspDac Display. Exiting...")
sys.exit(0)
try:
display_mode = "ARTIST"
beenplaying = True
currentArtist = ""
currentTitle = ""
cpos = ""
ctime = ""
hesitate = False
hesitation_etime = 0
display_etime = 0
notplaying_state = "TIME"
ctime = ""
hesitation_etime = time.time() + HESITATION_TIME
while True:
if TIME24HOUR == True:
current_time = moment.utcnow().timezone(TIMEZONE).format("HH:mm").strip()
else:
current_time = moment.utcnow().timezone(TIMEZONE).format("h:m a").strip()
current_ip = commands.getoutput("ip -4 route get 1 | head -1 | cut -d' ' -f8 | tr -d '\n'").strip()
cstatus = rd.status()
state = cstatus.get('state')
if state != "play":
if beenplaying:
beenplaying = False
currentArtist = ""
currentTitle = ""
notplaying_state = "TIME"
hesitation_etime = time.time() + NOTPLAYING_TIMEDISPLAY
cip = "" # This will guarantee that the IP gets displayed
ctime = "" # This will guarantee time gets displayed
if notplaying_state == "TIME":
# check to see if the time to switch the not playing display has been reached
if (hesitation_etime < time.time()):
notplaying_state = "IP"
hesitation_etime = time.time() + NOTPLAYING_IPDISPLAY
cip = ""
else:
# Only update display time if the time has changed
if current_time != ctime:
logging.info("Ready " + current_time)
dq.put(["Ready".center(DISPLAY_WIDTH), current_time.center(DISPLAY_WIDTH)])
ctime = current_time
if notplaying_state == "IP":
# check to see if the time to switch the not playing display has been reached
if (hesitation_etime < time.time()):
notplaying_state = "TIME"
hesitation_etime = time.time() + NOTPLAYING_TIMEDISPLAY
ctime = ""
else:
if current_ip != cip:
logging.info("IP " + current_ip)
dq.put([current_ip.center(DISPLAY_WIDTH), current_time.center(DISPLAY_WIDTH)])
cip = current_ip
time.sleep(1)
else:
title = cstatus.get('title')
artist = cstatus.get('artist')
playing_song = artist + ": " + title
current = cstatus.get("current")
duration = cstatus.get("duration")
# if duration is not available, then suppress its display
if int(duration) > 0:
timepos = time.strftime("%M:%S", time.gmtime(int(current))) + "/" + time.strftime("%M:%S", time.gmtime(int(duration)))
else:
timepos = time.strftime("%M:%S", time.gmtime(int(current)))
update_needed = False
if (beenplaying == False) or (currentTitle != title) or (currentArtist != artist):
beenplaying = True
currentTitle = title
currentArtist = artist
logging.info(current_time + " (local): " + playing_song)
display_mode = "ARTIST"
display_etime = time.time()+ARTIST_TIME
if (len(artist)>DISPLAY_WIDTH):
hesitation_etime = time.time()+HESITATION_TIME
hesitate=True
display = artist[0:DISPLAY_WIDTH]
elif (len(artist) > 0):
display = artist
else:
display_etime = 0 # force artist display to be skipped if the field is empty
update_needed = True
else:
# Only update display if the time position has changed
if timepos != cpos:
update_needed = True
cpos = timepos
if (hesitation_etime < time.time() and hesitate):
update_needed = True
hesitate = False
if display_mode == "ARTIST":
display = artist
else:
display = title
if display_etime < time.time():
update_needed = True
if display_mode == "ARTIST":
display_etime = time.time() + TITLE_TIME
if len(title) > 0:
display_mode = "TITLE"
display = title
else:
display_etime = time.time() + ARTIST_TIME
if len(artist) > 0:
display_mode = "ARTIST"
display = artist
if len(artist) == 0 and len(title) == 0:
# if neither artist and title contain values
display = "No song info"
if (len(display)>DISPLAY_WIDTH):
hesitate = True
hesitation_etime = time.time() + HESITATION_TIME
display = display[0:DISPLAY_WIDTH]
# Only update if one of the display items has changed
if update_needed:
# add new display items to display queue
dq.put([display, timepos])
update_needed = False
time.sleep(.25)
except KeyboardInterrupt:
pass
finally:
dq.put(["Goodbye!",""])
logging.info("Goodbye!")
try:
rd.client.disconnect()
except:
pass
try:
rd.spotclient.write("bye\n")
rd.spotclient.close()
except:
pass
time.sleep(2)
dq.put(["",""])
time.sleep(1)
GPIO.cleanup()