from signal import pause import time import board import adafruit_ssd1306 from mopidy_json_client import MopidyClient from datetime import datetime def print_oled(data): oled.fill(0) oled.text("Floppy Player", 26, 0, 1) oled.text(data["line1"]["text"], data["line1"]["line"], data["line1"]["row"], 1) oled.text(data["line2"]["text"], data["line2"]["line"], data["line2"]["row"], 1) oled.text(data["line3"]["text"], data["line3"]["line"], data["line3"]["row"], 1) oled.text(data["line4"]["text"], data["line4"]["line"], data["line4"]["row"], 1) oled.show() def clear_old(): oled.fill(0) oled.text("Floppy Player", 26, 0, 1) oled.text("", 0, 0, 1) oled.text("", 0, 0, 1) oled.text("", 0, 0, 1) oled.text("", 0, 0, 1) oled.show() def print_track_info(tl_track, title=False): track = tl_track.get("track") if tl_track else None if not track: print("No Track") return if title != False: print_oled( { "line1": {"text": track.get("album").get("name"), "line": 0, "row": 16}, "line2": { "text": track.get("name"), "line": 0, "row": 26, }, "line3": {"text": title[1], "line": 0, "row": 36}, "line4": {"text": title[0], "line": 0, "row": 46}, } ) else: print_oled( { "line1": {"text": track.get("name"), "line": 0, "row": 16}, "line2": { "text": track.get("artists")[0].get("name"), "line": 0, "row": 26, }, "line3": {"text": track.get("album").get("name"), "line": 0, "row": 36}, "line4": {"text": "", "line": 0, "row": 46}, } ) def stream_title_changed(title): data = title.split(" - ") print_track_info(mopidy.playback.get_current_tl_track(), data) def playback_state_changed(old_state, new_state): state = new_state if state == "paused": print_oled( { "line1": {"text": "", "line": 0, "row": 16}, "line2": {"text": "", "line": 0, "row": 26}, "line3": {"text": "Paused", "line": 32, "row": 36}, "line4": {"text": "", "line": 0, "row": 46}, } ) elif state == "stopped": print_oled( { "line1": {"text": "", "line": 0, "row": 16}, "line2": {"text": "Nothing", "line": 40, "row": 26}, "line3": {"text": "Playing", "line": 40, "row": 36}, "line4": {"text": "Insert a Disk", "line": 25, "row": 46}, } ) elif state == "playing": print_track_info(mopidy.playback.get_current_tl_track()) def volume_changed(volume): global old_vol if old_vol != volume: old_vol = volume print_oled( { "line1": {"text": "", "line": 0, "row": 16}, "line2": {"text": "Current Volume", "line": 32, "row": 26}, "line3": {"text": str(volume), "line": 32, "row": 36}, "line4": {"text": "", "line": 0, "row": 46}, } ) time.sleep(1) get_state() def mute_changed(mute): if mute == True: print_oled( { "line1": {"text": "", "line": 0, "row": 16}, "line2": {"text": "Muted", "line": 32, "row": 26}, "line3": {"text": "", "line": 32, "row": 36}, "line4": {"text": "", "line": 0, "row": 46}, } ) else: get_state() # def options_changed(): # options = { # "random": mopidy.tracklist.get_random(timeout=10), # "single": mopidy.tracklist.get_single(timeout=10), # "consume": mopidy.tracklist.get_consume(timeout=10), # "repeat": mopidy.tracklist.get_repeat(timeout=10), # } # print("Tracklist Options:", options, format="expand") def seeked(time_position): print(f" Current Position is {time_position}") mopidy = MopidyClient() mopidy.bind_event("track_playback_started", print_track_info) mopidy.bind_event("playback_state_changed", playback_state_changed) mopidy.bind_event("stream_title_changed", stream_title_changed) mopidy.bind_event("volume_changed", volume_changed) mopidy.bind_event("mute_changed", mute_changed) mopidy.bind_event("seeked", seeked) WIDTH = 128 HEIGHT = 64 # Use for I2C. i2c = board.I2C() oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C) def get_state(): state = mopidy.playback.get_state() if state == "playing": print_track_info(mopidy.playback.get_current_tl_track()) elif state == "paused": playback_state_changed("running", "paused") elif state == "stopped": playback_state_changed("running", "stopped") old_vol = mopidy.mixer.get_volume() import intro clear_old() get_state() pause()