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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #!/usr/bin/env python3
  2. # IP address code taken from:
  3. # https://github.com/rm-hull/luma.examples/blob/master/examples/sys_info_extended.py
  4. #
  5. # ----------------------------------------------------------------------------
  6. # Copyright (c) 2024 Thomas Buck (thomas@xythobuz.de)
  7. # Copyright (c) 2024 Philipp Schönberger (mail@phschoen.de)
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # See <http://www.gnu.org/licenses/>.
  20. # ----------------------------------------------------------------------------
  21. import sys
  22. import os
  23. import random
  24. import subprocess
  25. import signal
  26. import glob
  27. import socket
  28. from collections import OrderedDict
  29. import time
  30. import pisugar
  31. from luma.core.interface.serial import i2c
  32. from luma.core.render import canvas
  33. from luma.oled.device import ssd1306
  34. from luma.core.error import DeviceNotFoundError
  35. import psutil
  36. import RPi.GPIO as GPIO
  37. from PIL import ImageFont
  38. basevol = "70"
  39. debouncems = 100
  40. #fontfile = "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf"
  41. fontfile = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
  42. fontsize = 11
  43. LCD_REFRESH = 5.0
  44. BTN_ARTIST = 16
  45. BTN_NEXT = 26
  46. currentplaying = None
  47. lcd = None
  48. font = None
  49. bat = None
  50. songlist = None
  51. currentfile = None
  52. lasttime = None
  53. currvol = basevol
  54. vol_list = [
  55. ("Harley_Matthews", "100"),
  56. ]
  57. basedir = sys.argv[1]
  58. if basedir.endswith("/"):
  59. basedir = basedir.removesuffix("/")
  60. def get_artist(fn):
  61. global currvol
  62. parts = fn.replace(basedir + "/", "").split(os.sep)
  63. artist = parts[0].replace("_", " ")
  64. currvol = basevol
  65. for i in vol_list:
  66. if i[0] in fn:
  67. currvol = i[1]
  68. break
  69. return artist
  70. originalsongs = []
  71. originalartists = []
  72. for fn in glob.iglob(os.path.join(basedir, '**', '*.wav'), recursive=True):
  73. originalsongs.append(fn)
  74. artist = get_artist(fn)
  75. if artist not in originalartists:
  76. originalartists.append(artist)
  77. artists = originalartists.copy()
  78. random.shuffle(artists)
  79. currentartist = artists[0]
  80. def find_single_ipv4_address(addrs):
  81. for addr in addrs:
  82. if addr.family == socket.AddressFamily.AF_INET: # IPv4
  83. return addr.address
  84. def get_ipv4_address(interface_name=None):
  85. if_addrs = psutil.net_if_addrs()
  86. if isinstance(interface_name, str) and interface_name in if_addrs:
  87. addrs = if_addrs.get(interface_name)
  88. address = find_single_ipv4_address(addrs)
  89. return address if isinstance(address, str) else ""
  90. else:
  91. if_stats = psutil.net_if_stats()
  92. if_stats_filtered = {key: if_stats[key] for key, stat in if_stats.items() if "loopback" not in stat.flags}
  93. if_names_sorted = [stat[0] for stat in sorted(if_stats_filtered.items(), key=lambda x: (x[1].isup, x[1].duplex), reverse=True)]
  94. if_addrs_sorted = OrderedDict((key, if_addrs[key]) for key in if_names_sorted if key in if_addrs)
  95. for _, addrs in if_addrs_sorted.items():
  96. address = find_single_ipv4_address(addrs)
  97. if isinstance(address, str):
  98. return address
  99. return ""
  100. def status(filename):
  101. try:
  102. with canvas(lcd) as draw:
  103. f = filename.replace(".wav", "")
  104. f = f.replace(basedir + "/", "")
  105. f = f.replace("/", "\n")
  106. f = f.replace("_", " ")
  107. f += "\n\n"
  108. f += "Bat: {:.0f}% {:.2f}V {:.2f}A".format(bat.get_battery_level(), bat.get_battery_voltage(), bat.get_battery_current())
  109. ip = get_ipv4_address()
  110. if len(ip) > 0:
  111. f += "\n"
  112. f += "IP: %s" % (ip)
  113. with open("/proc/asound/card0/pcm0p/sub0/hw_params", "r") as rf:
  114. for line in rf:
  115. if line.startswith("rate:"):
  116. rate = int(line.split(" ")[1])
  117. f += "\n"
  118. f += "Rate: {:.0f}kHz".format(rate / 1000)
  119. draw.multiline_text((0, 0), f, font=font, fill="white", spacing=-1)
  120. except Exception as e:
  121. pass
  122. def stop():
  123. global currentplaying
  124. if running():
  125. try:
  126. print("Stopping running player")
  127. os.kill(currentplaying.pid, signal.SIGINT)
  128. if not currentplaying.poll():
  129. currentplaying = None
  130. else:
  131. print("Error stopping player")
  132. except ProcessLookupError as e:
  133. currentplaying = None
  134. else:
  135. currentplaying = None
  136. def play(filename):
  137. global currentplaying
  138. global lcd
  139. global basedir
  140. global bat
  141. global currentfile
  142. stop()
  143. print('Now playing "' + filename + '"')
  144. currentfile = filename
  145. status(currentfile)
  146. print("volume %s " % currvol)
  147. currentplaying = subprocess.Popen(["ffplay", "-hide_banner", "-nostats", "-nodisp", "-autoexit", "-volume", currvol, filename])
  148. def running():
  149. global currentplaying
  150. if currentplaying != None:
  151. if currentplaying.poll() == None:
  152. return True
  153. return False
  154. def playlist():
  155. global songlist
  156. global lasttime
  157. if not running():
  158. while True:
  159. if (songlist == None) or (len(songlist) <= 0):
  160. switch_artist()
  161. songlist = originalsongs.copy()
  162. random.shuffle(songlist)
  163. song = songlist.pop()
  164. artist = get_artist(song)
  165. if artist == currentartist:
  166. play(song)
  167. lasttime = time.time()
  168. break
  169. else:
  170. if (time.time() - lasttime) >= LCD_REFRESH:
  171. status(currentfile)
  172. lasttime = time.time()
  173. def switch_artist():
  174. global artists
  175. global currentartist
  176. if len(artists) <= 0:
  177. artists = originalartists.copy()
  178. random.shuffle(artists)
  179. currentartist = artists.pop()
  180. switch_track()
  181. def switch_track():
  182. stop()
  183. def button(ch):
  184. val = not GPIO.input(ch)
  185. #name = "Unknown"
  186. #if ch == BTN_ARTIST:
  187. # name = "BTN_ARTIST"
  188. #elif ch == BTN_NEXT:
  189. # name = "BTN_NEXT"
  190. #print(name + " is now " + str(val))
  191. if val:
  192. if ch == BTN_ARTIST:
  193. switch_artist()
  194. elif ch == BTN_NEXT:
  195. switch_track()
  196. def main():
  197. global lcd
  198. global font
  199. global bat
  200. global currentfile
  201. if len(sys.argv) <= 1:
  202. print("Usage:")
  203. print("\t" + sys.argv[0] + " PATH")
  204. sys.exit(1)
  205. os.system("killall ffplay")
  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=debouncems)
  210. try:
  211. bus = i2c(port=1, address=0x3C)
  212. lcd = ssd1306(bus)
  213. font = ImageFont.truetype(fontfile, fontsize)
  214. except DeviceNotFoundError as E:
  215. print("No LCD connected")
  216. lcd = None
  217. conn, event_conn = pisugar.connect_tcp()
  218. bat = pisugar.PiSugarServer(conn, event_conn)
  219. print(bat.get_model() + " " + bat.get_version())
  220. print(str(bat.get_battery_level()) + "% " + str(bat.get_battery_voltage()) + "V " + str(bat.get_battery_current()) + "A")
  221. print("Plug=" + str(bat.get_battery_power_plugged()) + " Charge=" + str(bat.get_battery_charging()))
  222. try:
  223. while True:
  224. playlist()
  225. time.sleep(0.05)
  226. except KeyboardInterrupt:
  227. print("Bye")
  228. GPIO.cleanup()
  229. sys.exit(0)
  230. if __name__ == "__main__":
  231. main()