Merge pull request #102 from mogenson/libusb_package

Load libusb-1.0 shared library from libusb_package wheel
This commit is contained in:
Michael Mogenson
2022-12-15 21:53:56 -05:00
committed by GitHub
5 changed files with 31 additions and 5 deletions

View File

@@ -17,6 +17,7 @@
# -----------------------------------------------------------------------------
import asyncio
import logging
import libusb_package
import usb.core
import usb.util
import threading
@@ -203,13 +204,13 @@ async def open_pyusb_transport(spec):
# Find the device according to the spec moniker
if ':' in spec:
vendor_id, product_id = spec.split(':')
device = usb.core.find(
device = libusb_package.find(
idVendor=int(vendor_id, 16), idProduct=int(product_id, 16)
)
else:
device_index = int(spec)
devices = list(
usb.core.find(
libusb_package.find(
find_all=1,
bDeviceClass=USB_DEVICE_CLASS_WIRELESS_CONTROLLER,
bDeviceSubClass=USB_DEVICE_SUBCLASS_RF_CONTROLLER,

View File

@@ -17,9 +17,12 @@
# -----------------------------------------------------------------------------
import asyncio
import logging
import libusb_package
import usb1
import threading
import collections
import ctypes
import platform
from colors import color
from .common import Transport, ParserSource
@@ -33,6 +36,19 @@ logger = logging.getLogger(__name__)
# -----------------------------------------------------------------------------
def load_libusb():
'''
Attempt to load the libusb-1.0 C library from libusb_package in site-packages.
If library exists, we create a DLL object and initialize the usb1 backend.
This only needs to be done once, but bufore a usb1.USBContext is created.
If library does not exists, do nothing and usb1 will search default system paths
when usb1.USBContext is created.
'''
if libusb_path := libusb_package.get_library_path():
dll_loader = ctypes.WinDLL if platform.system() == 'Windows' else ctypes.CDLL
libusb_dll = dll_loader(libusb_path, use_errno=True, use_last_error=True)
usb1.loadLibrary(libusb_dll)
async def open_usb_transport(spec):
'''
Open a USB transport.
@@ -305,6 +321,7 @@ async def open_usb_transport(spec):
self.context.close()
# Find the device according to the spec moniker
load_libusb()
context = usb1.USBContext()
context.open()
try: