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
# Author: raspbeguy <raspbeguy@hashtagueule.fr>
# 2017-03-12
# License: MIT
import queue
import threading
import signal
import sys
import dot3k.lcd as lcd
import dot3k.backlight as backlight
import dot3k.joystick as nav
from mpd import MPDClient
rotate_pause = 10 # delay in rotation steps
@ -57,7 +65,7 @@ def display():
album = ''
song_file = '' # Useful to determine if current song actually changed
step_title = step_artist = step_album = 0
while True:
while run_event.is_set():
try:
arg = q.get(timeout=0.2)
status = arg['status']['state']
@ -69,18 +77,17 @@ def display():
else:
lcd.create_char(0, char_stop)
if song_file != arg['song']['file']:
song_file = arg['song']['file']
title = arg['song']['title']
artist = arg['song']['artist']
album = arg['song']['album']
if status not in {'play','pause'} or song_file != arg['song']['file']:
song_file = arg['song']['file'] if 'file' in arg['song'].keys() else ''
title = arg['song']['title'] if 'title' in arg['song'].keys() else ''
artist = arg['song']['artist'] if 'artist' in arg['song'].keys() else ''
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
delay_title = delay_artist = delay_album = rotate_pause
lcd.clear()
lcd.set_cursor_position(0, 0)
lcd.write(chr(0))
if len(title) <= 15:
lcd.set_cursor_position(1, 0)
@ -93,23 +100,26 @@ def display():
lcd.write(album)
except queue.Empty:
if len(title) > 15:
if step_title == 0 and delay_title > 0:
delay_title -= 1
else:
step_title = (step_title + 1) % (len(title) + 3)
if delay_title == 0:
delay_title = rotate_pause
if len(artist) > 16:
if step_artist == 0 and delay_artist > 0:
delay_artist -= 1
else:
step_artist = (step_artist + 1) % (len(artist) + 3)
if delay_title == 0:
if delay_artist == 0:
delay_artist = rotate_pause
if len(album) > 16:
if step_album == 0 and delay_album > 0:
delay_album -= 1
else:
step_album = (step_album + 1) % (len(album) + 3)
if delay_title == 0:
if delay_album == 0:
delay_album = rotate_pause
if len(title) > 15:
@ -121,14 +131,53 @@ def display():
if len(album) > 16:
lcd.set_cursor_position(0, 2)
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.connect("localhost", 6600)
t_display = threading.Thread(name='display',target=display)
t_display.start()
signal.signal(signal.SIGTERM, on_exit)
try:
while True:
q.put({'song': client.currentsong(), 'status' : client.status()})
client.idle('player')
except KeyboardInterrupt:
on_exit(None)