Ingen beskrivning
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

osci-pi.py 7.4KB

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