Revert "Clean up python (#212)"

This reverts commit 0a8bc01870.
This commit is contained in:
ITotalJustice
2025-08-31 04:27:53 +01:00
parent 876be3b7b6
commit b6b1af5959
3 changed files with 102 additions and 162 deletions

View File

@@ -1,96 +1,50 @@
import struct
from time import sleep
from usb.core
import usb.core
import usb.util
import time
SPLASH = """
:@@@@@@@@@@@@@@@@@@@@@@@@@@:
#@ @#
#@ @#
#@ @#
#@ @@@@@@ @@@@@@ @#
#@ @@@@@@ @@@@@@ @#
#@ @@@@@@ @@@@@@ @#
#@ @#
#@ @#
#@ @#
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ @% @@@@@@@@@@@@
#@ *@@@@* @% @@@@@@@@@@@@@
#@ @@@@@@@@ @% @@@@@@@@@@@@@
#@ @@@@@@@@ @% @@@@@@@@@@@@@
#@ *@@@@* @% @@@@@@@@@@@@@
#@ @% @@@@@@@@@@@@@
#@ @% @@@@@@@@@@@@@
#@ @% @@@@@@@@@@@@@
#@ @% @@@@@@@@@@@@@
#@ @% @@@@@@@@@@@@@
#@ @% @@@@@=--=@@@@
#@ @% @@@- -@@
#@ @% @@@ @@
#@ @% @@@- -@@
=@@ @% @@@@@=--=@@@@
=@@ @% @@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
"""
# magic number (SPH0) for the script and switch.
MAGIC = 0x53504830
class USB_ENUM:
# (SPH0) for the script and Switch
MAGIC = 0x53504830
# commands
CMD_QUIT = 0
CMD_OPEN = 1
CMD_EXPORT = 1
# Commands
CMD_QUIT = 0
CMD_OPEN = 1
CMD_EXPORT = 1
# results
RESULT_OK = 0
RESULT_ERROR = 1
# Result Codes
RESULT_OK = 0
RESULT_ERROR = 1
# flags
FLAG_NONE = 0
FLAG_STREAM = 1 << 0
# Flags
FLAG_NONE = 0
FLAG_STREAM = 1 << 0
# Switch Vendor / Product ID
VENDOR_ID = 0x057E
PRODUCT_ID = 0x3000
ENABLE_ZLT = 0
def find_switch() -> object | None:
return usb.core.find(
idVendor=USB_ENUM.VENDOR_ID,
idProduct=USB_ENUM.PRODUCT_ID
)
# disabled, see usbds.cpp usbDsEndpoint_SetZlt
ENABLE_ZLT = 0
class Usb:
def __init__(self):
self._out_ep = None
self._in_ep = None
self._packet_size = 0
self._packet_index = 0
self.__out_ep = None
self.__in_ep = None
self.__packet_size = 0
def wait_for_connect(self) -> None:
print(SPLASH)
print("Waiting for Switch...", end="")
print("waiting for switch")
dev = None
while dev is None:
if (dev := find_switch()):
break
print(".", end="")
sleep(0.5)
while (dev is None):
dev = usb.core.find(idVendor=0x057E, idProduct=0x3000)
if (dev is None):
time.sleep(0.5)
print("Found the Switch!\n")
print("found the switch!\n")
cfg = None
print("Getting configuration...")
try:
cfg = dev.get_active_configuration()
print("Found active config")
print("found active config")
except usb.core.USBError:
print("No currently active config")
print("no currently active config")
cfg = None
if cfg is None:
@@ -100,36 +54,26 @@ class Usb:
is_out_ep = lambda ep: usb.util.endpoint_direction(ep.bEndpointAddress) == usb.util.ENDPOINT_OUT
is_in_ep = lambda ep: usb.util.endpoint_direction(ep.bEndpointAddress) == usb.util.ENDPOINT_IN
self._out_ep = usb.util.find_descriptor(cfg[(0,0)], custom_match=is_out_ep)
self._in_ep = usb.util.find_descriptor(cfg[(0,0)], custom_match=is_in_ep)
self.__out_ep = usb.util.find_descriptor(cfg[(0,0)], custom_match=is_out_ep)
self.__in_ep = usb.util.find_descriptor(cfg[(0,0)], custom_match=is_in_ep)
assert self.__out_ep is not None
assert self.__in_ep is not None
if not self._out_ep:
raise ValueError("Failed to get USB OUT address")
if not self._in_ep:
raise ValueError("Failed to get USB IN address")
print(f"iManufacturer: {dev.manufacturer} \
iProduct: {dev.product} \
iSerialNumber: {dev.serial_number}")
print(f"bcdUSB: {hex(dev.bcdUSB)} \
bMaxPacketSize0: {dev.bMaxPacketSize0}")
self._packet_size = 1 << dev.bMaxPacketSize0
print("iManufacturer: {} iProduct: {} iSerialNumber: {}".format(dev.manufacturer, dev.product, dev.serial_number))
print("bcdUSB: {} bMaxPacketSize0: {}".format(hex(dev.bcdUSB), dev.bMaxPacketSize0))
self.__packet_size = 1 << dev.bMaxPacketSize0
def read(self, size: int, timeout: int = 0) -> bytes:
if (ENABLE_ZLT and size and not (size % self._packet_size)):
if (ENABLE_ZLT and size and (size % self.__packet_size) == 0):
size += 1
return self._in_ep.read(size, timeout)
return self.__in_ep.read(size, timeout)
def write(self, buf: bytes, timeout: int = 0) -> int:
packet = self._packet_index
self._packet_index += 1
# Todo, implement packet index long
return self._out_ep.write(data=buf, timeout=timeout)
return self.__out_ep.write(data=buf, timeout=timeout)
def get_send_header(self) -> tuple[int, int, int]:
header = self.read(16)
magic, arg2, arg3, arg4 = struct.unpack('<IIII', header)
[magic, arg2, arg3, arg4] = struct.unpack('<IIII', header)
if magic != MAGIC:
raise Exception("Unexpected magic {}".format(magic))
@@ -142,4 +86,4 @@ class Usb:
def send_result(self, result: int, arg3: int = 0, arg4: int = 0) -> None:
send_data = struct.pack('<IIII', MAGIC, result, arg3, arg4)
self.write(send_data)
self.write(send_data)