Files
pirate_audio/cover.py
2022-01-29 09:48:05 +00:00

62 lines
1.4 KiB
Python
Executable File

from PIL import Image
import ST7789 as ST7789
import RPi.GPIO as GPIO
import urllib2 as urllib
import requests
import io
import time
# My music server URL
url = "http://192.168.0.114:6680/mopidy/rpc"
# Define the display
disp = ST7789.ST7789(
port=0,
cs=ST7789.BG_SPI_CS_FRONT,
dc=9,
backlight=13,
spi_speed_hz=80 * 1000 * 1000,
)
# First request fetches the current track info
js1 = { "method": "core.playback.get_current_track", "jsonrpc": "2.0", "params": {}, "id": 1 }
js2 = requests.post(url, json=js1)
uri = js2.json()['result']['uri']
# Second request gets the url for the cover image
js3 = { "method": "core.library.get_images", "jsonrpc": "2.0", "params": { "uris": [uri] }, "id": 1}
js4 = requests.post(url, json=js3)
cover = js4.json()['result'].values()[0][0]['uri']
fd = urllib.urlopen(cover)
image_file = io.BytesIO(fd.read())
WIDTH = disp.width
HEIGHT = disp.height
# Initialize display.
disp.begin()
# Setup to be able to change backlight
GPIO.setmode(GPIO.BCM)
GPIO.setup(13, GPIO.OUT)
backlight = GPIO.PWM(13, 500)
# Set max backlight
backlight.start(100)
# Load an image.
print('Loading image: {}...'.format(image_file))
image = Image.open(image_file)
# Resize the image
image = image.resize((WIDTH, HEIGHT))
# Draw the image on the display hardware.
print('Drawing image')
disp.display(image)
time.sleep(120)
backlight.ChangeDutyCycle(1)