screen.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. from signal import pause
  2. import time
  3. import board
  4. import adafruit_ssd1306
  5. from mopidy_json_client import MopidyClient
  6. from datetime import datetime
  7. def print_oled(data):
  8. oled.fill(0)
  9. oled.text("Floppy Player", 26, 0, 1)
  10. oled.text(data["line1"]["text"], data["line1"]["line"], data["line1"]["row"], 1)
  11. oled.text(data["line2"]["text"], data["line2"]["line"], data["line2"]["row"], 1)
  12. oled.text(data["line3"]["text"], data["line3"]["line"], data["line3"]["row"], 1)
  13. oled.text(data["line4"]["text"], data["line4"]["line"], data["line4"]["row"], 1)
  14. oled.show()
  15. def clear_old():
  16. oled.fill(0)
  17. oled.text("Floppy Player", 26, 0, 1)
  18. oled.text("", 0, 0, 1)
  19. oled.text("", 0, 0, 1)
  20. oled.text("", 0, 0, 1)
  21. oled.text("", 0, 0, 1)
  22. oled.show()
  23. def print_track_info(tl_track, title=False):
  24. track = tl_track.get("track") if tl_track else None
  25. if not track:
  26. print("No Track")
  27. return
  28. if title != False:
  29. print_oled(
  30. {
  31. "line1": {"text": track.get("album").get("name"), "line": 0, "row": 16},
  32. "line2": {
  33. "text": track.get("name"),
  34. "line": 0,
  35. "row": 26,
  36. },
  37. "line3": {"text": title[1], "line": 0, "row": 36},
  38. "line4": {"text": title[0], "line": 0, "row": 46},
  39. }
  40. )
  41. else:
  42. print_oled(
  43. {
  44. "line1": {"text": track.get("name"), "line": 0, "row": 16},
  45. "line2": {
  46. "text": track.get("artists")[0].get("name"),
  47. "line": 0,
  48. "row": 26,
  49. },
  50. "line3": {"text": track.get("album").get("name"), "line": 0, "row": 36},
  51. "line4": {"text": "", "line": 0, "row": 46},
  52. }
  53. )
  54. def stream_title_changed(title):
  55. data = title.split(" - ")
  56. print_track_info(mopidy.playback.get_current_tl_track(), data)
  57. def playback_state_changed(old_state, new_state):
  58. state = new_state
  59. if state == "paused":
  60. print_oled(
  61. {
  62. "line1": {"text": "", "line": 0, "row": 16},
  63. "line2": {"text": "", "line": 0, "row": 26},
  64. "line3": {"text": "Paused", "line": 32, "row": 36},
  65. "line4": {"text": "", "line": 0, "row": 46},
  66. }
  67. )
  68. elif state == "stopped":
  69. print_oled(
  70. {
  71. "line1": {"text": "", "line": 0, "row": 16},
  72. "line2": {"text": "Nothing", "line": 40, "row": 26},
  73. "line3": {"text": "Playing", "line": 40, "row": 36},
  74. "line4": {"text": "Insert a Disk", "line": 25, "row": 46},
  75. }
  76. )
  77. elif state == "playing":
  78. print_track_info(mopidy.playback.get_current_tl_track())
  79. def volume_changed(volume):
  80. global old_vol
  81. if old_vol != volume:
  82. old_vol = volume
  83. print_oled(
  84. {
  85. "line1": {"text": "", "line": 0, "row": 16},
  86. "line2": {"text": "Current Volume", "line": 32, "row": 26},
  87. "line3": {"text": str(volume), "line": 32, "row": 36},
  88. "line4": {"text": "", "line": 0, "row": 46},
  89. }
  90. )
  91. time.sleep(1)
  92. get_state()
  93. def mute_changed(mute):
  94. if mute == True:
  95. print_oled(
  96. {
  97. "line1": {"text": "", "line": 0, "row": 16},
  98. "line2": {"text": "Muted", "line": 32, "row": 26},
  99. "line3": {"text": "", "line": 32, "row": 36},
  100. "line4": {"text": "", "line": 0, "row": 46},
  101. }
  102. )
  103. else:
  104. get_state()
  105. # def options_changed():
  106. # options = {
  107. # "random": mopidy.tracklist.get_random(timeout=10),
  108. # "single": mopidy.tracklist.get_single(timeout=10),
  109. # "consume": mopidy.tracklist.get_consume(timeout=10),
  110. # "repeat": mopidy.tracklist.get_repeat(timeout=10),
  111. # }
  112. # print("Tracklist Options:", options, format="expand")
  113. def seeked(time_position):
  114. print(f" Current Position is {time_position}")
  115. mopidy = MopidyClient()
  116. mopidy.bind_event("track_playback_started", print_track_info)
  117. mopidy.bind_event("playback_state_changed", playback_state_changed)
  118. mopidy.bind_event("stream_title_changed", stream_title_changed)
  119. mopidy.bind_event("volume_changed", volume_changed)
  120. mopidy.bind_event("mute_changed", mute_changed)
  121. mopidy.bind_event("seeked", seeked)
  122. WIDTH = 128
  123. HEIGHT = 64
  124. # Use for I2C.
  125. i2c = board.I2C()
  126. oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C)
  127. def get_state():
  128. state = mopidy.playback.get_state()
  129. if state == "playing":
  130. print_track_info(mopidy.playback.get_current_tl_track())
  131. elif state == "paused":
  132. playback_state_changed("running", "paused")
  133. elif state == "stopped":
  134. playback_state_changed("running", "stopped")
  135. old_vol = mopidy.mixer.get_volume()
  136. import intro
  137. clear_old()
  138. get_state()
  139. pause()