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() if await mopidy.playback.get_state() == "stopped": oled.text("Nothing", 32, 26, 1) oled.text("Playing", 32, 36, 1) return current_track_data = await mopidy.playback.get_current_track() time_position = datetime.fromtimestamp( await mopidy.playback.get_time_position() / 1000.0 ).strftime("%M:%S") if "ytmusic" in current_track_data["uri"]: if "album" in current_track_data: totalTime = current_track_data["length"] track_total_length = datetime.fromtimestamp(totalTime / 1000.0).strftime( "%M:%S" ) current_data = { "Artist": current_track_data["artists"][0]["name"], "Album": current_track_data["album"]["name"], "Song": current_track_data["name"], "TrackTime": time_position + "/" + track_total_length, } elif "ytmusic:track" in current_track_data["uri"]: totalTime = current_track_data["length"] track_total_length = datetime.fromtimestamp(totalTime / 1000.0).strftime( "%M:%S" ) current_data = { "Artist": "-", "Album": current_track_data["artists"][0]["name"], "Song": current_track_data["name"], "TrackTime": time_position + "/" + track_total_length, } else: try: stream_title = await mopidy.playback.get_stream_title() artist_and_song = stream_title.split(" - ") except: stream_title = "None" artist_and_song = ['None','None'] current_data = { "Artist": current_track_data["name"], "Album": artist_and_song[0], "Song": artist_and_song[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 WIDTH = 128 HEIGHT = 64 # Use for I2C. i2c = board.I2C() oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C) 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())