Skip to main content

Another async libusb wrapper.

Project description

asyncusb

The goal of this library is to be an efficient and pythonic asynchronous usb library written in pure python.

Here's an example that sends some data to a predetermined device endpoint.

import asyncio
from asyncusb.core import *

async def main():
    with Context() as ctx:
        devices = ctx.get_device_list()

        # Inspect device attributes and choose a device. We'll just take the
        # first device for the purposes of this example.
        dev = devices[0]

        async with dev.open() as handle:
            # Handle kernel drivers automatically.
            handle.set_auto_detach_kernel_driver(True)

            # Claim the interface for the duration of this with block. This
            # example assumes we're using interface 1.
            with handle.bind_interface(1):
                # Create and submit the transfer. This example assumes
                # we're just sending some data to endpoint 0x02.
                transfer = handle.fill_bulk_transfer(0x02, b'hello world!')
                transfer.submit()

                # Wait for transfer completion. If we don't, the dev.open()
                # context manager might cancel our transfer before it
                # finishes!
                await transfer.wait()

asyncio.run(main())

That's it! No manual resouce cleanup is required, as all cleanup is handled by the context managers.

Built-in Termux Compatibility

There are two built-in methods for accessing devices on Termux. The simplest method is using the termux_compatibility_hook method from the compat module, like so:

dev_path = "/dev/bus/usb/001/002"

# This function gets the device from Termux:API and sets the TERMUX_USB_FD
# environment variable for the Termux patch of libusb.
termux_compatibility_hook(dev_path)

# Then use the context as normal.
with Context() as ctx:
    devices = ctx.get_device_list()

    # ...

This only allows using one device per Context, however. For more advanced cases, termux_get_device and Context.wrap_sys_device can be used, in conjunction with the no_discovery context option, like so:

dev_path = "/dev/bus/usb/001/002"

# Get the system device handle (a file descriptor) from Termux:API.
sys_dev = termux_get_device(dev_path)

# Create a context with the 'no_discovery' option. This creates the context
# without searching for devices. This also means that ctx.get_device_list()
# will return an empty list.
with Context(no_discovery=True) as ctx:

    # Open the device.
    async with ctx.wrap_sys_device(sys_dev) as handle:

        # Get a Device instance, if needed.
        dev = handle.get_device()

        # ...

These methods aren't just limited to this library. termux_compatibility_hook will work for any library that uses libusb, and termux_get_device will work for any library that implements a way to call libusb_wrap_sys_device.

The compat module also provides other functions, such as is_termux(), has_termux_api(), and termux_list_devices().


Current Features:

  • async event handling
  • thread-safe transfer creation and submission
  • automated resource handling
  • Termux compatibility

Planned Features:

  • async endpoint read/write streams

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asyncusb-0.0.2.tar.gz (38.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asyncusb-0.0.2-py3-none-any.whl (39.1 kB view details)

Uploaded Python 3

File details

Details for the file asyncusb-0.0.2.tar.gz.

File metadata

  • Download URL: asyncusb-0.0.2.tar.gz
  • Upload date:
  • Size: 38.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for asyncusb-0.0.2.tar.gz
Algorithm Hash digest
SHA256 a8eb226e78bfde6bf1a7e2afaad6062bd892931d9c2ed86b3c92356aeb7da0d1
MD5 e1addd78ff9c5224fd9d7434cb4a7be0
BLAKE2b-256 d4d397d52b9e8eee0c1d04ef1ecf5b6eae0ae700c891133a5b6260005e3633e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncusb-0.0.2.tar.gz:

Publisher: publish-to-pypi.yml on JacobTDC/asyncusb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file asyncusb-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: asyncusb-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 39.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for asyncusb-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1f4a65b0a1645421b13f97b32ffd9664bfe89306c34e2abc4731fac024421b8c
MD5 e228f9277123bf26d08499e490039a3a
BLAKE2b-256 6862c1121845fa9cac8acf941297c9cce7c24c1c2e19e60b40a95cac3b303892

See more details on using hashes here.

Provenance

The following attestation bundles were made for asyncusb-0.0.2-py3-none-any.whl:

Publisher: publish-to-pypi.yml on JacobTDC/asyncusb

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page