Commit 6ebecb8b authored by santiago duque's avatar santiago duque

usb swaping now working on mac, also playing from usb

parent 2c8e241e
import os import os
from os.path import isfile, join from os.path import isfile, join
import pygame import pygame
import keyboard
# import keyboard
from storage_media import USBDeviceHandler from storage_media import USBDeviceHandler
# import time # import time
# import signal # import signal
# import keyboard # import keyboard
pygame.init() pygame.init()
winW = pygame.display.Info().current_w * 0.9 winW = pygame.display.Info().current_w * 0.2
winH = pygame.display.Info().current_h * 0.9 winH = pygame.display.Info().current_h * 0.2
window = pygame.display.set_mode((winW,winH)) window = pygame.display.set_mode((winW, winH))
#Fullscreen = True # Fullscreen = True
#window.fill((205,100,100)) # window.fill((205,100,100))
#pygame.display.update() # pygame.display.update()
#clock = pygame.time.Clock() # clock = pygame.time.Clock()
dir_path = os.getcwd() def reset_sounds():
sound_dir = os.getcwd() + "/sounds" global sound_array
contentlist = os.listdir(sound_dir)
sound_array = []
cut_current_sound = False
channel = False
current_sound_index = -1
contentlist = sorted(contentlist) sound_array = []
usb_handler = USBDeviceHandler() setup_sounds()
def setup_sounds(): def setup_sounds():
global sound_array
global contentlist
global sound_dir
print("inside setup")
print(contentlist)
for content in contentlist: for content in contentlist:
print(join(sound_dir, content))
if isfile(join(sound_dir, content)): if isfile(join(sound_dir, content)):
if content.endswith(".mp3") or content.endswith(".wav"): if (content.endswith(".mp3") or content.endswith(".wav")) and (
not content.startswith("._")
):
sound = pygame.mixer.Sound(join(sound_dir, content)) sound = pygame.mixer.Sound(join(sound_dir, content))
sound_array.append(sound) sound_array.append(sound)
print(content) print(content)
print("sound array")
print(sound_array) print(sound_array)
...@@ -45,6 +51,7 @@ def play_sound(index): ...@@ -45,6 +51,7 @@ def play_sound(index):
global cut_current_sound global cut_current_sound
global current_sound_index global current_sound_index
global channel global channel
global sound_array
if not cut_current_sound: if not cut_current_sound:
current_sound_index = index current_sound_index = index
...@@ -72,18 +79,50 @@ def play_sound(index): ...@@ -72,18 +79,50 @@ def play_sound(index):
""" """
setup_sounds() def setup_paths(val):
global contentlist
global sound_dir
global sound_dir_local
global usb_handler
sound_dir = sound_dir_local
usbdisks = usb_handler.get_usb_devices()
if len(usbdisks) > 0:
sound_dir = usbdisks[0].mountpoint
contentlist = os.listdir(sound_dir)
contentlist = sorted(contentlist)
reset_sounds()
# prepare local paths in case no USB
sound_dir_local = os.getcwd() + "/sounds"
sound_dir = ""
# start USB handler with listener
usb_handler = USBDeviceHandler()
usb_handler.on("example_event", setup_paths)
# prepare sound variables
sound_array = []
cut_current_sound = False
channel = False
current_sound_index = -1
# run setup
setup_paths(False)
run = True run = True
while run: while run:
#clock.tick(30) # clock.tick(30)
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT: if event.type == pygame.QUIT:
run = False run = False
# keyboard module # keyboard module
''' """
if keyboard.is_pressed("q"): if keyboard.is_pressed("q"):
print("You Pressed A Key!") print("You Pressed A Key!")
play_sound(0) play_sound(0)
...@@ -96,19 +135,19 @@ while run: ...@@ -96,19 +135,19 @@ while run:
elif keyboard.is_pressed("r"): elif keyboard.is_pressed("r"):
print("You Pressed A Key!") print("You Pressed A Key!")
play_sound(3) play_sound(3)
''' """
if event.type == pygame.KEYDOWN: if event.type == pygame.KEYDOWN:
print("you pressed " + pygame.key.name(event.key)) print("you pressed " + pygame.key.name(event.key))
if event.key == pygame.K_q: if event.key == pygame.K_q:
play_sound(0) play_sound(0)
elif event.key == pygame.K_w: elif event.key == pygame.K_w:
play_sound(1) play_sound(1)
elif event.key == pygame.K_e: elif event.key == pygame.K_e:
play_sound(2) play_sound(2)
elif event.key == pygame.K_r: elif event.key == pygame.K_r:
play_sound(3) play_sound(3)
''' """
elif event.key == pygame.K_f: elif event.key == pygame.K_f:
if Fullscreen: if Fullscreen:
window = pygame.display.set_mode((winW,winH)) window = pygame.display.set_mode((winW,winH))
...@@ -116,10 +155,10 @@ while run: ...@@ -116,10 +155,10 @@ while run:
else: else:
window = pygame.display.set_mode((0,0), pygame.FULLSCREEN) window = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
Fullscreen = True Fullscreen = True
''' """
pygame.quit() pygame.quit()
exit() exit()
# time.sleep(5.5) # time.sleep(5.5)
# signal.pause() # signal.pause()
\ No newline at end of file
from glob import glob from glob import glob
from subprocess import check_output, CalledProcessError
# from subprocess import check_output, CalledProcessError
import platform import platform
import os
# import os
import psutil
import time
from usbmonitor import USBMonitor from usbmonitor import USBMonitor
from usbmonitor.attributes import ID_MODEL, ID_MODEL_ID, ID_VENDOR_ID, DEVNAME from usbmonitor.attributes import ID_MODEL, ID_MODEL_ID, ID_VENDOR_ID, DEVNAME
class USBDeviceHandler: class ObjectWithEvents(object):
callbacks = None
def on(self, event_name, callback):
print('calling ON')
if self.callbacks is None:
self.callbacks = {}
if event_name not in self.callbacks:
self.callbacks[event_name] = [callback]
else:
self.callbacks[event_name].append(callback)
def trigger(self, event_name):
print('calling TRIGGER')
if self.callbacks is not None and event_name in self.callbacks:
for callback in self.callbacks[event_name]:
callback(self)
class USBDeviceHandler(ObjectWithEvents):
def __init__(self): def __init__(self):
print("init USB handler")
self.monitor = USBMonitor() self.monitor = USBMonitor()
self.startUSBmonitor() self.startUSBmonitor()
...@@ -24,18 +49,24 @@ class USBDeviceHandler: ...@@ -24,18 +49,24 @@ class USBDeviceHandler:
def on_connect(self, device_id, device_info): def on_connect(self, device_id, device_info):
print("Connected: " + self.device_info_str(device_info=device_info)) print("Connected: " + self.device_info_str(device_info=device_info))
print(self.get_usb_devices()) print(self.get_usb_devices())
self.trigger('example_event')
def on_disconnect(self, device_id, device_info): def on_disconnect(self, device_id, device_info):
print("Disconnected: " + self.device_info_str(device_info=device_info)) print("Disconnected: " + self.device_info_str(device_info=device_info))
print(self.get_usb_devices()) print(self.get_usb_devices())
self.trigger('example_event')
def get_usb_devices(self): def get_usb_devices(self):
# delay the reading for 1 second, i guess until it is mounted as partition?
time.sleep(2)
current_os = platform.system() current_os = platform.system()
print(current_os) print(current_os)
disks = psutil.disk_partitions()
# MacOS # MacOS
if current_os == "Darwin": if current_os == "Darwin":
os.chdir("/Volumes") os_drive_path = "/Volumes"
"""os.chdir("/Volumes")
# then do some listing # then do some listing
List = os.listdir() List = os.listdir()
i = 0 i = 0
...@@ -47,12 +78,12 @@ class USBDeviceHandler: ...@@ -47,12 +78,12 @@ class USBDeviceHandler:
List[i] = "/Volumes/" + List[i] List[i] = "/Volumes/" + List[i]
i += 1 i += 1
return List return List"""
# Linux # Linux
elif current_os == "Linux": elif current_os == "Linux":
os_drive_path = ""
def get_linux_usb_devices(): """def get_linux_usb_devices():
sdb_devices = map(os.path.realpath, glob("/sys/block/sd*")) sdb_devices = map(os.path.realpath, glob("/sys/block/sd*"))
usb_devices = (dev for dev in sdb_devices if "usb" in dev.split("/")[5]) usb_devices = (dev for dev in sdb_devices if "usb" in dev.split("/")[5])
return dict((os.path.basename(dev), dev) for dev in usb_devices) return dict((os.path.basename(dev), dev) for dev in usb_devices)
...@@ -62,3 +93,11 @@ class USBDeviceHandler: ...@@ -62,3 +93,11 @@ class USBDeviceHandler:
is_usb = lambda path: any(dev in path for dev in devices) is_usb = lambda path: any(dev in path for dev in devices)
usb_info = (line for line in output if is_usb(line.split()[0])) usb_info = (line for line in output if is_usb(line.split()[0]))
return [(info.split()[0], info.split()[2]) for info in usb_info] return [(info.split()[0], info.split()[2]) for info in usb_info]
"""
usb_disks = []
for disk in disks:
if (os_drive_path in disk.mountpoint) and (disk.fstype == "exfat"):
usb_disks.append(disk)
return usb_disks
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment