From aaeafaae54a850c4c119bff9fd6468996e8cc847 Mon Sep 17 00:00:00 2001 From: raspbeguy Date: Sun, 12 Mar 2017 22:38:17 +0100 Subject: [PATCH] MPD: joystick and interrupt handling --- mpd_dot3k.py | 111 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 80 insertions(+), 31 deletions(-) diff --git a/mpd_dot3k.py b/mpd_dot3k.py index 93d5cc7..61d2266 100644 --- a/mpd_dot3k.py +++ b/mpd_dot3k.py @@ -1,8 +1,16 @@ #!/usr/bin/env python3 +# Author: raspbeguy +# 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,24 +100,27 @@ def display(): lcd.write(album) except queue.Empty: - 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 step_artist == 0 and delay_artist > 0: - delay_artist -= 1 - else: - step_artist = (step_artist + 1) % (len(artist) + 3) - if delay_title == 0: - delay_artist = rotate_pause - if step_album == 0 and delay_album > 0: - delay_album -= 1 - else: - step_album = (step_album + 1) % (len(album) + 3) - if delay_title == 0: - delay_album = rotate_pause + 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_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_album == 0: + delay_album = rotate_pause if len(title) > 15: lcd.set_cursor_position(1, 0) @@ -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() -while True: - q.put({'song': client.currentsong(), 'status' : client.status()}) - client.idle('player') +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)