python-DiskPlayer/screen_output.py

63 lines
1.7 KiB
Python
Raw Normal View History

2021-11-04 15:15:59 +00:00
import asyncio
import time
import board
import adafruit_ssd1306
from mopidy_async_client import MopidyClient
from datetime import datetime
async def current_track():
mopidy = await MopidyClient().connect()
2021-11-04 16:45:48 +00:00
current_track_data = await mopidy.playback.get_current_track()
time_position = datetime.fromtimestamp(
2021-11-04 15:15:59 +00:00
await mopidy.playback.get_time_position() / 1000.0
).strftime("%M:%S")
2021-11-04 16:45:48 +00:00
if "ytmusic" in current_track_data["uri"]:
totalTime = current_track_data["length"]
track_total_length = datetime.fromtimestamp(totalTime / 1000.0).strftime(
"%M:%S"
)
2021-11-04 15:15:59 +00:00
current_data = {
2021-11-04 16:45:48 +00:00
"Artist": current_track_data["artists"][0]["name"],
"Album": current_track_data["album"]["name"],
"Song": current_track_data["name"],
"TrackTime": time_position + "/" + track_total_length,
2021-11-04 15:15:59 +00:00
}
else:
2021-11-04 16:45:48 +00:00
stream_title = await mopidy.playback.get_stream_title()
artist_and_song = stream_title.split(" - ")
2021-11-04 15:15:59 +00:00
current_data = {
2021-11-04 16:45:48 +00:00
"Artist": current_track_data["name"],
"Album": artist_and_song[0],
"Song": artist_and_song[1],
2021-11-04 15:15:59 +00:00
"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
WIDTH = 128
2021-11-04 16:45:48 +00:00
HEIGHT = 64
2021-11-04 15:15:59 +00:00
# Use for I2C.
i2c = board.I2C()
2021-11-04 16:45:48 +00:00
oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C)
2021-11-04 15:15:59 +00:00
async def main():
while True:
oled.fill(0)
oled.text("Floppy Player", 26, 0, 1)
await current_track()
oled.show()
2021-11-04 16:45:48 +00:00
time.sleep(10)
2021-11-04 15:15:59 +00:00
asyncio.run(main())