python-DiskPlayer/screen_output.py
2021-11-04 15:15:59 +00:00

80 lines
2.1 KiB
Python

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
This demo will fill the screen with white, draw a black box on top
and then print Hello World! in the center of the display
This example is for use on (Linux) computers that are using CPython with
Adafruit Blinka to support CircuitPython libraries. CircuitPython does
not support PIL/pillow (python imaging library)!
"""
import asyncio
import time
import board
import digitalio
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
from mopidy_async_client import MopidyClient
from datetime import datetime
async def current_track():
mopidy = await MopidyClient().connect()
res = await mopidy.playback.get_current_track()
b = datetime.fromtimestamp(
await mopidy.playback.get_time_position() / 1000.0
).strftime("%M:%S")
if "ytmusic" in res["uri"]:
totalTime = res["length"]
a = datetime.fromtimestamp(totalTime / 1000.0).strftime("%M:%S")
current_data = {
"Artist": res["artists"][0]["name"],
"Album": res["album"]["name"],
"Song": res["name"],
"TrackTime": b + "/" + a,
}
else:
a = await mopidy.playback.get_stream_title()
data = a.split(" - ")
current_data = {
"Artist": res["name"],
"Album": data[0],
"Song": data[1],
"TrackTime": "",
}
await mopidy.disconnect()
oled.text(current_data["Artist"], 0, 16, 1)
oled.text(current_data["Album"], 0, 26, 1)
oled.text(current_data["Song"], 0, 36, 1)
oled.text(current_data["TrackTime"], 0, 46, 1)
return
# Define the Reset Pin
oled_reset = digitalio.DigitalInOut(board.D4)
# Change these
# to the right size for your display!
WIDTH = 128
HEIGHT = 64 # Change to 64 if needed
BORDER = 0
# Use for I2C.
i2c = board.I2C()
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C, reset=oled_reset)
async def main():
while True:
oled.fill(0)
oled.text("Floppy Player", 26, 0, 1)
await current_track()
oled.show()
time.sleep(5)
asyncio.run(main())