MPD: joystick and interrupt handling

This commit is contained in:
raspbeguy 2017-03-12 22:38:17 +01:00
parent f08a1fc8cb
commit aaeafaae54
1 changed files with 80 additions and 31 deletions

View File

@ -1,8 +1,16 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# Author: raspbeguy <raspbeguy@hashtagueule.fr>
# 2017-03-12
# License: MIT
import queue import queue
import threading import threading
import signal
import sys
import dot3k.lcd as lcd import dot3k.lcd as lcd
import dot3k.backlight as backlight
import dot3k.joystick as nav
from mpd import MPDClient from mpd import MPDClient
rotate_pause = 10 # delay in rotation steps rotate_pause = 10 # delay in rotation steps
@ -57,7 +65,7 @@ def display():
album = '' album = ''
song_file = '' # Useful to determine if current song actually changed song_file = '' # Useful to determine if current song actually changed
step_title = step_artist = step_album = 0 step_title = step_artist = step_album = 0
while True: while run_event.is_set():
try: try:
arg = q.get(timeout=0.2) arg = q.get(timeout=0.2)
status = arg['status']['state'] status = arg['status']['state']
@ -69,18 +77,17 @@ def display():
else: else:
lcd.create_char(0, char_stop) lcd.create_char(0, char_stop)
if song_file != arg['song']['file']: if status not in {'play','pause'} or song_file != arg['song']['file']:
song_file = arg['song']['file'] song_file = arg['song']['file'] if 'file' in arg['song'].keys() else ''
title = arg['song']['title'] title = arg['song']['title'] if 'title' in arg['song'].keys() else ''
artist = arg['song']['artist'] artist = arg['song']['artist'] if 'artist' in arg['song'].keys() else ''
album = arg['song']['album'] album = arg['song']['album'] if 'album' in arg['song'].keys() else ''
lcd.clear()
lcd.set_cursor_position(0, 0)
lcd.write(chr(0))
step_title = step_artist = step_album = 0 step_title = step_artist = step_album = 0
delay_title = delay_artist = delay_album = rotate_pause delay_title = delay_artist = delay_album = rotate_pause
lcd.clear()
lcd.set_cursor_position(0, 0)
lcd.write(chr(0))
if len(title) <= 15: if len(title) <= 15:
lcd.set_cursor_position(1, 0) lcd.set_cursor_position(1, 0)
@ -93,24 +100,27 @@ def display():
lcd.write(album) lcd.write(album)
except queue.Empty: except queue.Empty:
if step_title == 0 and delay_title > 0: if len(title) > 15:
delay_title -= 1 if step_title == 0 and delay_title > 0:
else: delay_title -= 1
step_title = (step_title + 1) % (len(title) + 3) else:
if delay_title == 0: step_title = (step_title + 1) % (len(title) + 3)
delay_title = rotate_pause if delay_title == 0:
if step_artist == 0 and delay_artist > 0: delay_title = rotate_pause
delay_artist -= 1 if len(artist) > 16:
else: if step_artist == 0 and delay_artist > 0:
step_artist = (step_artist + 1) % (len(artist) + 3) delay_artist -= 1
if delay_title == 0: else:
delay_artist = rotate_pause step_artist = (step_artist + 1) % (len(artist) + 3)
if step_album == 0 and delay_album > 0: if delay_artist == 0:
delay_album -= 1 delay_artist = rotate_pause
else: if len(album) > 16:
step_album = (step_album + 1) % (len(album) + 3) if step_album == 0 and delay_album > 0:
if delay_title == 0: delay_album -= 1
delay_album = rotate_pause else:
step_album = (step_album + 1) % (len(album) + 3)
if delay_album == 0:
delay_album = rotate_pause
if len(title) > 15: if len(title) > 15:
lcd.set_cursor_position(1, 0) lcd.set_cursor_position(1, 0)
@ -121,14 +131,53 @@ def display():
if len(album) > 16: if len(album) > 16:
lcd.set_cursor_position(0, 2) lcd.set_cursor_position(0, 2)
lcd.write(stringrotate(album, 16, step_album)) lcd.write(stringrotate(album, 16, step_album))
lcd.clear()
@nav.on(nav.BUTTON)
def button_center(pin):
client = MPDClient() # Please close your eyes
client.connect("localhost", 6600) # Dirty dirty dirty...
if client.status()['state'] in {'play','pause'}:
client.pause()
else:
client.play()
client.close() # Okay, you can open your eyes
@nav.on(nav.LEFT)
def button_left(pin):
client = MPDClient() # Please close your eyes
client.connect("localhost", 6600) # Dirty dirty dirty...
client.previous()
client.close() # Okay, you can open your eyes
@nav.on(nav.RIGHT)
def button_right(pin):
client = MPDClient() # Please close your eyes
client.connect("localhost", 6600) # Dirty dirty dirty...
client.next()
client.close() # Okay, you can open your eyes
def on_exit(sig, func=None):
run_event.clear()
t_display.join()
backlight.rgb(0, 0, 0)
backlight.rgb(0, 0, 0)
print("Screen sound be blank and light off now.")
sys.exit(0)
backlight.rgb(55, 55, 55)
run_event = threading.Event()
run_event.set()
client = MPDClient() client = MPDClient()
client.connect("localhost", 6600) client.connect("localhost", 6600)
t_display = threading.Thread(name='display',target=display) t_display = threading.Thread(name='display',target=display)
t_display.start() t_display.start()
while True: signal.signal(signal.SIGTERM, on_exit)
q.put({'song': client.currentsong(), 'status' : client.status()}) try:
client.idle('player') while True:
q.put({'song': client.currentsong(), 'status' : client.status()})
client.idle('player')
except KeyboardInterrupt:
on_exit(None)