288 lines
9.7 KiB
Python
288 lines
9.7 KiB
Python
#!/usr/bin/env python3
|
|
|
|
# Author: raspbeguy <raspbeguy@hashtagueule.fr>
|
|
# 2017-03-12
|
|
# License: MIT
|
|
|
|
import queue
|
|
import threading
|
|
import signal
|
|
import sys
|
|
import os
|
|
import select
|
|
import audioop
|
|
import dot3k.lcd as lcd
|
|
import dot3k.backlight as backlight
|
|
import dot3k.joystick as nav
|
|
from mpd import MPDClient
|
|
|
|
# When a line is too long to fit in a line of the LCD display,
|
|
# it scrolls with a cool effect to let you read the entire text.
|
|
# You may choose the speed of the scrolling and the pause between
|
|
# two rotations.
|
|
step_length = 0.2 # delay in seconds
|
|
rotate_pause = 10 # delay in steps count
|
|
|
|
|
|
# Symbols play, pause and stop are not defined in the dot3k library.
|
|
# We need to draw them manually.
|
|
# On the display, a character is a 8x5 pixels matrix. It is represented
|
|
# in python as a list of 8 integers, one per line. Each integer has
|
|
# 5 bits, one per raw.
|
|
# It is easier to draw if you represent your integers in binary notation.
|
|
|
|
char_play = [
|
|
0b10000,
|
|
0b11000,
|
|
0b11100,
|
|
0b11110,
|
|
0b11110,
|
|
0b11100,
|
|
0b11000,
|
|
0b10000,
|
|
]
|
|
|
|
char_pause = [
|
|
0b00000,
|
|
0b11011,
|
|
0b11011,
|
|
0b11011,
|
|
0b11011,
|
|
0b11011,
|
|
0b11011,
|
|
0b00000,
|
|
]
|
|
|
|
char_stop = [
|
|
0b00000,
|
|
0b11111,
|
|
0b11111,
|
|
0b11111,
|
|
0b11111,
|
|
0b11111,
|
|
0b11111,
|
|
0b00000,
|
|
]
|
|
|
|
# We use a queue to communicate the song info to the display thread.
|
|
# The main reason is that it has some amazing blocking properties that
|
|
# enable not to think too much about mutex/lock/whatever logic you might
|
|
# think about especially if you coded in C before.
|
|
# It's python magic...
|
|
q = queue.Queue()
|
|
|
|
# This variable is the flag that will be turned down by the main thread
|
|
# to notify the other threads that they need to stop in order for the
|
|
# program to quit gracefully.
|
|
run_event = threading.Event()
|
|
|
|
def vumeter():
|
|
'''
|
|
This function will me used as the thread in charge of the LEDs
|
|
behind the screen. We'll use them to watch the MPD output power
|
|
in real time. Cool, huh? In order to do that, you need to configure
|
|
an extra output in MPD by adding this snippet in /etc/mpd.conf:
|
|
|
|
audio_output {
|
|
type "fifo"
|
|
name "my_fifo"
|
|
path "/tmp/mpd.fifo"
|
|
format "44100:16:2"
|
|
}
|
|
'''
|
|
fifo = os.open('/tmp/mpd.fifo', os.O_RDONLY)
|
|
while run_event.is_set():
|
|
# When MPD is in pause or stopped, it doesn't write any EOF in the fifo
|
|
# so os.read is blocked. In order to avoid that, we need to put a timeout,
|
|
# and as os.open doesn't support any timeout, we need to put a select
|
|
# before, as it has a timeout parameter. We only need the r return variable,
|
|
# which is the list of readable file descriptors given in parameters,
|
|
# returned as soon as one get ready or after the timeout.
|
|
# We do all this in order to have a clean quit, else we wouldn't give a shit.
|
|
r, w, e = select.select([ fifo ], [], [], 1)
|
|
if fifo in r:
|
|
try:
|
|
# We read a chunk of the sound that MPD put in the FIFO.
|
|
# Each chunk is a sequence of samples that describes the signal.
|
|
rawStream = os.read(fifo, 4096)
|
|
except OSError as err:
|
|
if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK:
|
|
rawStream = None
|
|
else:
|
|
raise
|
|
if rawStream:
|
|
# The RMS function calculates the square root of the average of
|
|
# the squared samples: sqrt(sum(Si^2)/n)
|
|
# This is a good power indicator.
|
|
# Then, we just normalize it to reduce it between 0 and 1.
|
|
# Finally, we call the set_graph methos that will display the
|
|
# fraction by putting on the right number of LEDs.
|
|
backlight.set_graph(audioop.rms(rawStream,2)/32768)
|
|
# After exiting the almost-infinite loop, we turn the LEDs off.
|
|
backlight.set_graph(0)
|
|
|
|
|
|
def stringrotate(text, width, step):
|
|
'''
|
|
This function's purpose is to scroll the given text to the given step,
|
|
knowing that the screen has the given width.
|
|
'''
|
|
if len(text) > width:
|
|
fulltext = text + " - "
|
|
fulltext = fulltext[step:] + fulltext[:step]
|
|
return fulltext[:width]
|
|
else:
|
|
# No need to scroll the text if it can fit on the screen
|
|
return text
|
|
|
|
def display():
|
|
'''
|
|
This function will be run as the thread that will manage the LCD screan
|
|
itself. It uses the global queue defined at the top of the script.
|
|
'''
|
|
title = ''
|
|
artist = ''
|
|
album = ''
|
|
song_file = '' # Useful to determine if current song actually changed
|
|
step_title = step_artist = step_album = 0
|
|
while run_event.is_set():
|
|
try:
|
|
# If a new MPD even comes before the timeout, grab it, else
|
|
# throw an exception that will be caught.
|
|
arg = q.get(timeout=step_length)
|
|
# From here, we assume a new MPD event has occured (song or status change).
|
|
status = arg['status']['state']
|
|
|
|
if status == "play":
|
|
backlight.rgb(0, 0, 255) # green
|
|
lcd.create_char(0, char_play)
|
|
elif status == "pause":
|
|
backlight.rgb(0, 255, 0) # blue
|
|
lcd.create_char(0, char_pause)
|
|
else:
|
|
backlight.rgb(255, 0, 0)
|
|
lcd.create_char(0, char_stop)
|
|
|
|
# Tests if there is some new text to display, including nothing.
|
|
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
|
|
|
|
if len(title) <= 15:
|
|
lcd.set_cursor_position(1, 0)
|
|
lcd.write(title)
|
|
if len(artist) <= 16:
|
|
lcd.set_cursor_position(0, 1)
|
|
lcd.write(artist)
|
|
if len(album) <= 16:
|
|
lcd.set_cursor_position(0, 2)
|
|
lcd.write(album)
|
|
|
|
except queue.Empty:
|
|
# Is this code is executed, it means that no new MPD event
|
|
# has occured, so an exception were thrown after the timeout.
|
|
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
|
|
|
|
# Redraw everything.
|
|
if len(title) > 15:
|
|
lcd.set_cursor_position(1, 0)
|
|
lcd.write(stringrotate(title, 15, step_title))
|
|
if len(artist) > 16:
|
|
lcd.set_cursor_position(0, 1)
|
|
lcd.write(stringrotate(artist, 16, step_artist))
|
|
if len(album) > 16:
|
|
lcd.set_cursor_position(0, 2)
|
|
lcd.write(stringrotate(album, 16, step_album))
|
|
lcd.clear()
|
|
|
|
|
|
# These functions are event handlers related to the joystick.
|
|
# Each time the button is used, the matching function will be
|
|
# executed. That said, because the python-mpd2 library isn't
|
|
# thread-safe, we need to open a client for each event.
|
|
# This is ugly as fuck.
|
|
|
|
@ 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):
|
|
'''
|
|
Basically, close everything before quitting.
|
|
'''
|
|
run_event.clear()
|
|
# Join the threads
|
|
t_display.join()
|
|
t_vumeter.join()
|
|
backlight.rgb(0, 0, 0)
|
|
backlight.rgb(0, 0, 0)
|
|
print("Screen sound be blank and light off now.")
|
|
sys.exit(0)
|
|
|
|
lcd.set_contrast(50)
|
|
run_event.set()
|
|
client = MPDClient()
|
|
client.connect("localhost", 6600)
|
|
|
|
# Launch the threads
|
|
t_display = threading.Thread(name='display',target=display)
|
|
t_vumeter = threading.Thread(name='vumeter',target=vumeter)
|
|
t_display.start()
|
|
t_vumeter.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)
|