No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

osci-pi.py 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/usr/bin/env python3
  2. # Oscilloscope Music Player for Raspberry Pi
  3. # https://oscilloscopemusic.com
  4. #
  5. # Tested with a Pi Zero 2 W:
  6. # https://www.raspberrypi.com/products/raspberry-pi-zero-2-w/
  7. #
  8. # Connected to a HiFiBerry DAC+ Zero:
  9. # https://www.hifiberry.com/shop/boards/hifiberry-dac-zero/
  10. #
  11. # Tested with Raspberry Pi OS (Legacy, 32bit) Lite:
  12. # https://downloads.raspberrypi.com/raspios_oldstable_lite_armhf/images/raspios_oldstable_lite_armhf-2023-12-06/2023-12-05-raspios-bullseye-armhf-lite.img.xz
  13. #
  14. # Add "dtoverlay=hifiberry-dac" to "/boot/config.txt"
  15. #
  16. # Install ffmpeg for the ffplay dependency
  17. #
  18. # Powered by PiSugar 2:
  19. # https://github.com/PiSugar/pisugar-power-manager-rs
  20. # https://github.com/PiSugar/pisugar-server-py
  21. #
  22. # sudo sh -c 'echo "dtoverlay=hifiberry-dac" >> /boot/config.txt'
  23. # sudo apt-get update
  24. # sudo apt-get install python3 python3-pip python3-pil libjpeg-dev zlib1g-dev libfreetype6-dev liblcms2-dev libopenjp2-7 libtiff5 ffmpeg
  25. # curl http://cdn.pisugar.com/release/pisugar-power-manager.sh | sudo bash
  26. # pip install pisugar
  27. # pip install luma.oled
  28. # pip install psutil
  29. # sudo usermod -a -G spi,gpio,i2c $USER
  30. #
  31. # IP address code taken from:
  32. # https://github.com/rm-hull/luma.examples/blob/master/examples/sys_info_extended.py
  33. #
  34. # ----------------------------------------------------------------------------
  35. # Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
  36. #
  37. # This program is free software: you can redistribute it and/or modify
  38. # it under the terms of the GNU General Public License as published by
  39. # the Free Software Foundation, either version 3 of the License, or
  40. # (at your option) any later version.
  41. #
  42. # This program is distributed in the hope that it will be useful,
  43. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  45. # GNU General Public License for more details.
  46. #
  47. # See <http://www.gnu.org/licenses/>.
  48. # ----------------------------------------------------------------------------
  49. import sys
  50. import os
  51. import random
  52. import subprocess
  53. import signal
  54. import glob
  55. import socket
  56. from collections import OrderedDict
  57. import time
  58. import pisugar
  59. from luma.core.interface.serial import i2c
  60. from luma.core.render import canvas
  61. from luma.oled.device import ssd1306
  62. from luma.core.error import DeviceNotFoundError
  63. import psutil
  64. import RPi.GPIO as GPIO
  65. basevol = "70"
  66. LCD_REFRESH = 5.0
  67. BTN_ARTIST = 16
  68. BTN_NEXT = 26
  69. currentplaying = None
  70. lcd = None
  71. bat = None
  72. songlist = None
  73. currentfile = None
  74. lasttime = None
  75. basedir = sys.argv[1]
  76. if basedir.endswith("/"):
  77. basedir = basedir.removesuffix("/")
  78. def get_artist(fn):
  79. parts = fn.replace(basedir + "/", "").split(os.sep)
  80. artist = parts[0].replace("_", " ")
  81. return artist
  82. #originalsongs = os.listdir(basedir)
  83. originalsongs = []
  84. artists = []
  85. for fn in glob.iglob(os.path.join(basedir, '**', '*.wav'), recursive=True):
  86. originalsongs.append(fn)
  87. artist = get_artist(fn)
  88. if artist not in artists:
  89. artists.append(artist)
  90. random.shuffle(artists)
  91. currentartist = artists[0]
  92. def find_single_ipv4_address(addrs):
  93. for addr in addrs:
  94. if addr.family == socket.AddressFamily.AF_INET: # IPv4
  95. return addr.address
  96. def get_ipv4_address(interface_name=None):
  97. if_addrs = psutil.net_if_addrs()
  98. if isinstance(interface_name, str) and interface_name in if_addrs:
  99. addrs = if_addrs.get(interface_name)
  100. address = find_single_ipv4_address(addrs)
  101. return address if isinstance(address, str) else ""
  102. else:
  103. if_stats = psutil.net_if_stats()
  104. if_stats_filtered = {key: if_stats[key] for key, stat in if_stats.items() if "loopback" not in stat.flags}
  105. if_names_sorted = [stat[0] for stat in sorted(if_stats_filtered.items(), key=lambda x: (x[1].isup, x[1].duplex), reverse=True)]
  106. if_addrs_sorted = OrderedDict((key, if_addrs[key]) for key in if_names_sorted if key in if_addrs)
  107. for _, addrs in if_addrs_sorted.items():
  108. address = find_single_ipv4_address(addrs)
  109. if isinstance(address, str):
  110. return address
  111. return ""
  112. def status(filename):
  113. try:
  114. with canvas(lcd) as draw:
  115. f = filename.replace(".wav", "")
  116. f = f.replace(basedir + "/", "")
  117. f = f.replace("/", "\n")
  118. f = f.replace("_", " ")
  119. f += "\n"
  120. f += "Batt: {:.0f}% {:.2f}V {:.2f}A".format(bat.get_battery_level(), bat.get_battery_voltage(), bat.get_battery_current())
  121. f += "\n"
  122. f += "IP: %s" % (get_ipv4_address())
  123. draw.multiline_text((0, 0), f, fill="white")
  124. except Exception as e:
  125. raise e
  126. def stop():
  127. global currentplaying
  128. if running():
  129. try:
  130. print("Stopping running player")
  131. os.kill(currentplaying.pid, signal.SIGINT)
  132. if not currentplaying.poll():
  133. currentplaying = None
  134. else:
  135. print("Error stopping player")
  136. except ProcessLookupError as e:
  137. currentplaying = None
  138. else:
  139. currentplaying = None
  140. def play(filename):
  141. global currentplaying
  142. global lcd
  143. global basedir
  144. global bat
  145. global currentfile
  146. stop()
  147. print('Now playing "' + filename + '"')
  148. currentfile = filename
  149. status(currentfile)
  150. currentplaying = subprocess.Popen(["ffplay", "-hide_banner", "-nostats", "-nodisp", "-autoexit", "-volume", basevol, filename])
  151. def running():
  152. global currentplaying
  153. if currentplaying != None:
  154. if currentplaying.poll() == None:
  155. return True
  156. return False
  157. def playlist():
  158. global songlist
  159. global lasttime
  160. if not running():
  161. while True:
  162. if (songlist == None) or (len(songlist) <= 0):
  163. songlist = originalsongs.copy()
  164. random.shuffle(songlist)
  165. song = songlist.pop()
  166. artist = get_artist(song)
  167. if artist == currentartist:
  168. play(song)
  169. lasttime = time.time()
  170. break
  171. else:
  172. if (time.time() - lasttime) >= LCD_REFRESH:
  173. status(currentfile)
  174. lasttime = time.time()
  175. def switch_artist():
  176. global artists
  177. global currentartist
  178. ca = currentartist
  179. while currentartist == ca:
  180. random.shuffle(artists)
  181. currentartist = artists[0]
  182. switch_track()
  183. def switch_track():
  184. stop()
  185. def button(ch):
  186. val = not GPIO.input(ch)
  187. #name = "Unknown"
  188. #if ch == BTN_ARTIST:
  189. # name = "BTN_ARTIST"
  190. #elif ch == BTN_NEXT:
  191. # name = "BTN_NEXT"
  192. #print(name + " is now " + str(val))
  193. if val:
  194. if ch == BTN_ARTIST:
  195. switch_artist()
  196. elif ch == BTN_NEXT:
  197. switch_track()
  198. def main():
  199. global lcd
  200. global bat
  201. global currentfile
  202. if len(sys.argv) <= 1:
  203. print("Usage:")
  204. print("\t" + sys.argv[0] + " PATH")
  205. sys.exit(1)
  206. GPIO.setmode(GPIO.BCM)
  207. for b in [ BTN_ARTIST, BTN_NEXT ]:
  208. GPIO.setup(b, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  209. GPIO.add_event_detect(b, GPIO.BOTH, callback=button, bouncetime=100)
  210. try:
  211. bus = i2c(port=1, address=0x3C)
  212. lcd = ssd1306(bus)
  213. except DeviceNotFoundError as E:
  214. print("No LCD connected")
  215. lcd = None
  216. conn, event_conn = pisugar.connect_tcp()
  217. bat = pisugar.PiSugarServer(conn, event_conn)
  218. print(bat.get_model() + " " + bat.get_version())
  219. print(str(bat.get_battery_level()) + "% " + str(bat.get_battery_voltage()) + "V " + str(bat.get_battery_current()) + "A")
  220. print("Plug=" + str(bat.get_battery_power_plugged()) + " Charge=" + str(bat.get_battery_charging()))
  221. try:
  222. while True:
  223. playlist()
  224. time.sleep(0.05)
  225. except KeyboardInterrupt:
  226. print("Bye")
  227. GPIO.cleanup()
  228. sys.exit(0)
  229. if __name__ == "__main__":
  230. main()