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

usb swaping now working on mac, also playing from usb

parent 2c8e241e
import os
from os.path import isfile, join
import pygame
import keyboard
# import keyboard
from storage_media import USBDeviceHandler
# import time
# import signal
# import keyboard
pygame.init()
winW = pygame.display.Info().current_w * 0.9
winH = pygame.display.Info().current_h * 0.9
window = pygame.display.set_mode((winW,winH))
#Fullscreen = True
#window.fill((205,100,100))
#pygame.display.update()
winW = pygame.display.Info().current_w * 0.2
winH = pygame.display.Info().current_h * 0.2
window = pygame.display.set_mode((winW, winH))
# Fullscreen = True
# window.fill((205,100,100))
# pygame.display.update()
#clock = pygame.time.Clock()
# clock = pygame.time.Clock()
dir_path = os.getcwd()
sound_dir = os.getcwd() + "/sounds"
contentlist = os.listdir(sound_dir)
sound_array = []
cut_current_sound = False
channel = False
current_sound_index = -1
def reset_sounds():
global sound_array
contentlist = sorted(contentlist)
usb_handler = USBDeviceHandler()
sound_array = []
setup_sounds()
def setup_sounds():
global sound_array
global contentlist
global sound_dir
print("inside setup")
print(contentlist)
for content in contentlist:
print(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_array.append(sound)
print(content)
print("sound array")
print(sound_array)
......@@ -45,6 +51,7 @@ def play_sound(index):
global cut_current_sound
global current_sound_index
global channel
global sound_array
if not cut_current_sound:
current_sound_index = 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
while run:
#clock.tick(30)
# clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
# keyboard module
'''
"""
if keyboard.is_pressed("q"):
print("You Pressed A Key!")
play_sound(0)
......@@ -96,19 +135,19 @@ while run:
elif keyboard.is_pressed("r"):
print("You Pressed A Key!")
play_sound(3)
'''
"""
if event.type == pygame.KEYDOWN:
print("you pressed " + pygame.key.name(event.key))
if event.key == pygame.K_q:
play_sound(0)
elif event.key == pygame.K_w:
play_sound(1)
play_sound(1)
elif event.key == pygame.K_e:
play_sound(2)
elif event.key == pygame.K_r:
play_sound(3)
'''
"""
elif event.key == pygame.K_f:
if Fullscreen:
window = pygame.display.set_mode((winW,winH))
......@@ -116,10 +155,10 @@ while run:
else:
window = pygame.display.set_mode((0,0), pygame.FULLSCREEN)
Fullscreen = True
'''
"""
pygame.quit()
exit()
# time.sleep(5.5)
# signal.pause()
\ No newline at end of file
# signal.pause()
from glob import glob
from subprocess import check_output, CalledProcessError
# from subprocess import check_output, CalledProcessError
import platform
import os
# import os
import psutil
import time
from usbmonitor import USBMonitor
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):
print("init USB handler")
self.monitor = USBMonitor()
self.startUSBmonitor()
......@@ -24,18 +49,24 @@ class USBDeviceHandler:
def on_connect(self, device_id, device_info):
print("Connected: " + self.device_info_str(device_info=device_info))
print(self.get_usb_devices())
self.trigger('example_event')
def on_disconnect(self, device_id, device_info):
print("Disconnected: " + self.device_info_str(device_info=device_info))
print(self.get_usb_devices())
self.trigger('example_event')
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()
print(current_os)
disks = psutil.disk_partitions()
# MacOS
if current_os == "Darwin":
os.chdir("/Volumes")
os_drive_path = "/Volumes"
"""os.chdir("/Volumes")
# then do some listing
List = os.listdir()
i = 0
......@@ -47,12 +78,12 @@ class USBDeviceHandler:
List[i] = "/Volumes/" + List[i]
i += 1
return List
return List"""
# Linux
elif current_os == "Linux":
def get_linux_usb_devices():
os_drive_path = ""
"""def get_linux_usb_devices():
sdb_devices = map(os.path.realpath, glob("/sys/block/sd*"))
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)
......@@ -62,3 +93,11 @@ class USBDeviceHandler:
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]))
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