Last active
August 16, 2020 17:05
-
-
Save vsefer/81f812db43429f82720ec43e952d40df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import sys | |
import os | |
import subprocess | |
import digitalio | |
import board | |
import threading | |
from PIL import Image, ImageDraw, ImageFont | |
from socket import error as socket_error | |
from datetime import datetime | |
import adafruit_rgb_display.ili9341 as ili9341 | |
import adafruit_rgb_display.st7789 as st7789 # pylint: disable=unused-import | |
import adafruit_rgb_display.hx8357 as hx8357 # pylint: disable=unused-import | |
import adafruit_rgb_display.st7735 as st7735 # pylint: disable=unused-import | |
import adafruit_rgb_display.ssd1351 as ssd1351 # pylint: disable=unused-import | |
import adafruit_rgb_display.ssd1331 as ssd1331 # pylint: disable=unused-import | |
# MPD Clint | |
from mpd import MPDClient, MPDError, CommandError, ConnectionError | |
# Configuration for CS and DC pins (these are PiTFT defaults): | |
cs_pin = digitalio.DigitalInOut(board.CE0) | |
dc_pin = digitalio.DigitalInOut(board.D25) | |
reset_pin = digitalio.DigitalInOut(board.D24) | |
# Config for display baudrate (default max is 24mhz): | |
BAUDRATE = 24000000 | |
# Setup SPI bus using hardware SPI: | |
spi = board.SPI() | |
# pylint: disable=line-too-long | |
# Create the display: | |
# disp = st7789.ST7789(spi, rotation=90, # 2.0" ST7789 | |
# disp = st7789.ST7789(spi, height=240, y_offset=80, rotation=180, # 1.3", 1.54" ST7789 | |
# disp = st7789.ST7789(spi, rotation=90, width=135, height=240, x_offset=53, y_offset=40, # 1.14" ST7789 | |
# disp = hx8357.HX8357(spi, rotation=180, # 3.5" HX8357 | |
# disp = st7735.ST7735R(spi, rotation=90, # 1.8" ST7735R | |
# disp = st7735.ST7735R(spi, rotation=270, height=128, x_offset=2, y_offset=3, # 1.44" ST7735R | |
# disp = st7735.ST7735R(spi, rotation=90, bgr=True, # 0.96" MiniTFT ST7735R | |
# disp = ssd1351.SSD1351(spi, rotation=180, # 1.5" SSD1351 | |
# disp = ssd1351.SSD1351(spi, height=96, y_offset=32, rotation=180, # 1.27" SSD1351 | |
# disp = ssd1331.SSD1331(spi, rotation=180, # 0.96" SSD1331 | |
disp = ili9341.ILI9341( | |
spi, | |
rotation=180, # 2.2", 2.4", 2.8", 3.2" ILI9341 | |
cs=cs_pin, | |
dc=dc_pin, | |
rst=reset_pin, | |
baudrate=BAUDRATE, | |
) | |
# pylint: enable=line-too-long | |
# MPD Client | |
class MPDConnect(object): | |
def __init__(self, host='localhost', port=6600): | |
self._mpd_client = MPDClient() | |
self._mpd_client.timeout = 10 | |
self._mpd_connected = False | |
self._host = host | |
self._port = port | |
def connect(self): | |
if not self._mpd_connected: | |
try: | |
self._mpd_client.ping() | |
except(socket_error, ConnectionError): | |
try: | |
self._mpd_client.connect(self._host, self._port) | |
self._mpd_connected = True | |
except(socket_error, ConnectionError, CommandError): | |
self._mpd_connected = False | |
def disconnect(self): | |
self._mpd_client.close() | |
self._mpd_client.disconnect() | |
def _play_pause(self): | |
self._mpd_client.pause() | |
#return False | |
def _next_track(self): | |
self._mpd_client.next() | |
#return False | |
def _prev_track(self): | |
self._mpd_client.previous() | |
#return False | |
def fetch(self): | |
# MPD current song | |
song_info = self._mpd_client.currentsong() | |
# Artist Name | |
if 'artist' in song_info: | |
artist = song_info['artist'] | |
else: | |
artist = 'Unknown Artist' | |
# Song Name | |
if 'title' in song_info: | |
title = song_info['title'] | |
else: | |
title = 'Unknown Title' | |
# Album Name | |
if 'album' in song_info: | |
album = song_info['album'] | |
else: | |
album = 'Unknown Album' | |
# MPD Status | |
song_stats = self._mpd_client.status() | |
# State | |
state = song_stats['state'] | |
# Song time | |
if 'elapsed' in song_stats: | |
elapsed = song_stats['elapsed'] | |
m,s = divmod(float(elapsed), 60) | |
h,m = divmod(m, 60) | |
eltime = "%d:%02d:%02d" % (h, m, s) | |
else: | |
eltime ="0:00:00" | |
# Audio | |
if 'audio' in song_stats: | |
bit = song_stats['audio'].split(':')[1] | |
frequency = song_stats['audio'].split(':')[0] | |
z, f = divmod( int(frequency), 1000 ) | |
if ( f == 0 ): | |
frequency = str(z) | |
else: | |
frequency = str( float(frequency) / 1000 ) | |
bitrate = song_stats['bitrate'] | |
audio_info = bit + "bit " + frequency + "kHz " + bitrate + "kbps" | |
else: | |
audio_info = "" | |
# Bitrate | |
if 'bitrate' in song_stats: | |
bitrate = song_stats['bitrate'] | |
else: | |
bitrate = 'N/A' | |
# Volume | |
vol = song_stats['volume'] | |
return({'state':state, 'artist':artist, 'title':title, 'album':album, 'eltime':eltime, 'volume':int(vol), 'audio_info':audio_info, 'bitrate':bitrate}) | |
display_mode = 'cover' | |
timer = 0 | |
run = True | |
#def main(): | |
# Create blank image for drawing. | |
# Make sure to create image with mode 'RGB' for full color. | |
if disp.rotation % 180 == 90: | |
height = disp.width # we swap height/width to rotate it to landscape! | |
width = disp.height | |
else: | |
width = disp.width # we swap height/width to rotate it to landscape! | |
height = disp.height | |
image = Image.new("RGB", (width, height)) | |
# Get drawing object to draw on image. | |
draw = ImageDraw.Draw(image) | |
# Draw a black filled box to clear the image. | |
def loop(): | |
global display_mode | |
global timer | |
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0)) | |
disp.image(image) | |
# Load a TTF font. Make sure the .ttf font file is in the | |
# same directory as the python script! | |
# Some other nice fonts to try: http://www.dafont.com/bitmap.php | |
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 12) | |
font_artist = ImageFont.truetype('/home/pi/roboto-mono/Roboto-Bold.ttf', 36) | |
font_title = ImageFont.truetype('/home/pi/roboto-mono/Roboto-Medium.ttf', 24) | |
font_album = ImageFont.truetype('/home/pi/roboto-mono/Roboto-Light.ttf', 18) | |
font_info = ImageFont.truetype('/home/pi/roboto-mono/Roboto-Light.ttf', 30) | |
font_time = ImageFont.truetype('/home/pi/roboto-mono/Roboto-Medium.ttf', 30) | |
font_time_label = ImageFont.truetype('/home/pi/roboto-mono/Roboto-Light.ttf', 12) | |
font_timenow = ImageFont.truetype('/home/pi/roboto-mono/Roboto-Bold.ttf', 90) | |
font_temp_standby = ImageFont.truetype('/home/pi/roboto-mono/Roboto-Bold.ttf', 80) | |
font_volume_standby = ImageFont.truetype('/home/pi/roboto-mono/Roboto-Bold.ttf', 180) | |
# MPD Connect | |
client = MPDConnect() | |
client.connect() | |
padding = 2 | |
shape_width = 20 | |
top = padding | |
bottom = height-padding | |
artoffset = 2 | |
titoffset = 2 | |
alboffset = 2 | |
animate = 30 | |
cmd = "amixer get Master | grep '%' | awk '{print $5}' | sort -u | tr -d '[]' | tr -d '%'" # pylint: disable=line-too-long | |
initial_volume = subprocess.check_output(cmd, shell=True).decode("utf-8") | |
while run: | |
# Draw a black filled box to clear the image. | |
draw.rectangle((0, 0, width, height), outline=0, fill=0) | |
# Fetch data | |
info = client.fetch() | |
state = info['state'] | |
eltime = info['eltime'] | |
vol = info['volume'] | |
artist = info['artist'] | |
title = info['title'] | |
album = info['album'] | |
audio = info['audio_info'] | |
bitrate = info['bitrate'] | |
# Position text of Artist | |
artwd,artz = draw.textsize(str(artist), font=font_artist) | |
# Artist animate | |
if artwd < width: | |
artx = (width - artwd) / 2 | |
artoffset = padding | |
else: | |
artx = artoffset | |
artoffset -= animate | |
if (artwd - (width + abs(artx))) < -232: | |
artoffset = 231 | |
# Position text of Title | |
titwd,titz = draw.textsize(str(title), font=font_title) | |
# Title animate | |
if titwd < width: | |
titx = (width - titwd) / 2 | |
titoffset = padding | |
else: | |
titx = titoffset | |
titoffset -= animate | |
if (titwd - (width + abs(titx))) < -232: | |
titoffset = 231 | |
# Position text of Album | |
albwd,albz = draw.textsize(str(album), font=font_album) | |
# Album animate | |
if albwd < width: | |
albx = (width - albwd) / 2 | |
alboffset = padding | |
else: | |
albx = alboffset | |
alboffset -= animate | |
if (albwd - (width + abs(albx))) < -232: | |
alboffset = 231 | |
if state == 'stop': | |
draw.rectangle((0, 0, width, height), outline=0, fill=0) | |
now = datetime.now() | |
current_time = now.strftime("%H:%M") | |
draw.rectangle((0, 160, width, height), outline=0, fill="#ff8000") | |
draw.text((5,-10), current_time, font=font_timenow, fill="#ff8000") | |
cmd = "cat /home/pi/zagreb-temp.log" # pylint: disable=line-too-long | |
zg_temp = subprocess.check_output(cmd, shell=True).decode("utf-8") | |
draw.text((10,75), zg_temp, font=font_temp_standby, fill="white") | |
cmd = "amixer get Master | grep '%' | awk '{print $5}' | sort -u | tr -d '[]' | tr -d '%'" # pylint: disable=line-too-long | |
volume = subprocess.check_output(cmd, shell=True).decode("utf-8") | |
draw.text((20,138), volume, font=font_volume_standby, fill="black") | |
else: | |
# Draw text. | |
cmd = "amixer get Master | grep '%' | awk '{print $5}' | sort -u | tr -d '[]' | tr -d '%'" # pylint: disable=line-too-long | |
volume = subprocess.check_output(cmd, shell=True).decode("utf-8") | |
if volume != initial_volume: | |
display_mode = 'volume' | |
timer = 2 | |
if display_mode == 'volume': | |
draw.rectangle((0, 0, width, height), outline=0, fill="#FF8000") | |
initial_volume = volume | |
draw.text((20,138), volume, font=font_volume_standby, fill="black") | |
timer -= 1 | |
if timer == 0: | |
display_mode = 'cover' | |
elif display_mode == 'cover': | |
draw.rectangle((0, 0, width, height), outline=0, fill=0) | |
draw.text((artx,180), str(artist).replace(";", ", "), font=font_artist, fill="#00FFFF") | |
draw.text((titx,220), str(title), font=font_title, fill="#FFFFFF") | |
draw.text((albx,250), str(album), font=font_album, fill="#FF8000") | |
# draw.text((63,275), "kbit/s", font=font_time_label, fill="#FF8000") | |
# draw.text((63,295), bitrate , font=font_info, fill="#00FFFF") | |
draw.text((72,285), eltime, font=font_time, fill="#FFFFFF") | |
draw.text((padding,285), str(vol), font=font_info, fill="#00FFFF") | |
draw.text((padding,275), "input", font=font_time_label, fill="#FF8000") | |
#cmd = "amixer --card 1 get Headphone | grep dB | awk '{print $5}' | sort -u | tr -d '[]'" # pylint: disable=line-too-long | |
cmd = "amixer get Master | grep '%' | awk '{print $5}' | sort -u | tr -d '[]' | tr -d '%'" # pylint: disable=line-too-long | |
volume = subprocess.check_output(cmd, shell=True).decode("utf-8") | |
cmd = "cat /proc/asound/card1/pcm0p/sub0/hw_params | grep rate | awk '{print $2 / 1000}'" | |
freq = subprocess.check_output(cmd, shell=True).decode("utf-8") | |
draw.text((205,285), str(volume) , font=font_info, fill="#00FFFF") | |
draw.text((204,275), "output", font=font_time_label, fill="#FF8000") | |
# draw.text((148,295), str(freq) , font=font_info, fill="#00FFFF") | |
# draw.text((145,275), "kHz", font=font_time_label, fill="#FF8000") | |
cover = Image.open("/home/pi/pidi/current.jpg").convert('RGB') | |
cover = cover.resize((170,170), Image.BICUBIC) | |
Image.Image.paste(image, cover, (35,5)) | |
# Draw the image buffer. | |
disp.image(image) | |
# Pause briefly before drawing next frame. | |
time.sleep(0.5) | |
if __name__ == "__main__": | |
y = threading.Thread(target=loop, args=()) | |
y.start() | |
for line in sys.stdin: | |
line = line[:-1] | |
print("INPUT: " + line) | |
if line == "v": | |
display_mode = 'volume' | |
timer = 5 | |
elif line == "q": | |
print("BYE") | |
run = False | |
y.join() | |
sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment