Skip to content

Commit 1f1c2f2

Browse files
authored
Merge pull request #1 from tannewt/lint
Linted
2 parents ec8f5d6 + 04db86f commit 1f1c2f2

File tree

5 files changed

+119
-22
lines changed

5 files changed

+119
-22
lines changed

README.rst

Lines changed: 54 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,32 +23,20 @@ Introduction
2323

2424
Helpers for getting USB descriptors
2525

26-
2726
Dependencies
2827
=============
2928
This driver depends on:
3029

31-
* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
30+
* `Adafruit CircuitPython 9+ <https://github.com/adafruit/circuitpython>`_
3231

3332
Please ensure all dependencies are available on the CircuitPython filesystem.
3433
This is easily achieved by downloading
3534
`the Adafruit library and driver bundle <https://circuitpython.org/libraries>`_
3635
or individual libraries can be installed using
3736
`circup <https://github.com/adafruit/circup>`_.
3837

39-
40-
41-
.. todo:: Describe the Adafruit product this library works with. For PCBs, you can also add the
42-
image from the assets folder in the PCB's GitHub repo.
43-
44-
`Purchase one from the Adafruit shop <http://www.adafruit.com/products/>`_
45-
4638
Installing from PyPI
4739
=====================
48-
.. note:: This library is not available on PyPI yet. Install documentation is included
49-
as a standard element. Stay tuned for PyPI availability!
50-
51-
.. todo:: Remove the above note if PyPI version is/will be available at time of release.
5240

5341
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
5442
PyPI <https://pypi.org/project/adafruit-circuitpython-usb-host-descriptors/>`_.
@@ -99,8 +87,59 @@ Or the following command to update an existing version:
9987
Usage Example
10088
=============
10189

102-
.. todo:: Add a quick, simple example. It and other examples should live in the
103-
examples folder and be included in docs/examples.rst.
90+
Print basic information about a device and its first (and usually only) configuration.
91+
92+
.. code-block:: python
93+
94+
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
95+
# SPDX-FileCopyrightText: Copyright (c) 2023 Scott Shawcroft for Adafruit Industries
96+
#
97+
# SPDX-License-Identifier: Unlicense
98+
99+
import time
100+
import usb.core
101+
102+
import adafruit_usb_host_descriptors
103+
104+
DIR_IN = 0x80
105+
106+
while True:
107+
print("searching for devices")
108+
for device in usb.core.find(find_all=True):
109+
print("pid", hex(device.idProduct))
110+
print("vid", hex(device.idVendor))
111+
print("man", device.manufacturer)
112+
print("product", device.product)
113+
print("serial", device.serial_number)
114+
print("config[0]:")
115+
config_descriptor = adafruit_usb_host_descriptors.get_configuration_descriptor(
116+
device, 0
117+
)
118+
119+
i = 0
120+
while i < len(config_descriptor):
121+
descriptor_len = config_descriptor[i]
122+
descriptor_type = config_descriptor[i + 1]
123+
if descriptor_type == adafruit_usb_host_descriptors.DESC_CONFIGURATION:
124+
config_value = config_descriptor[i + 5]
125+
print(f" value {config_value:d}")
126+
elif descriptor_type == adafruit_usb_host_descriptors.DESC_INTERFACE:
127+
interface_number = config_descriptor[i + 2]
128+
interface_class = config_descriptor[i + 5]
129+
interface_subclass = config_descriptor[i + 6]
130+
print(f" interface[{interface_number:d}]")
131+
print(
132+
f" class {interface_class:02x} subclass {interface_subclass:02x}"
133+
)
134+
elif descriptor_type == adafruit_usb_host_descriptors.DESC_ENDPOINT:
135+
endpoint_address = config_descriptor[i + 2]
136+
if endpoint_address & DIR_IN:
137+
print(f" IN {endpoint_address:02x}")
138+
else:
139+
print(f" OUT {endpoint_address:02x}")
140+
i += descriptor_len
141+
print()
142+
time.sleep(5)
104143
105144
Documentation
106145
=============

adafruit_usb_host_descriptors.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,35 @@
3535
DESC_INTERFACE = 0x04
3636
DESC_ENDPOINT = 0x05
3737

38+
3839
def get_descriptor(device, desc_type, index, buf, language_id=0):
40+
"""Fetch the descriptor from the device into buf."""
41+
# Allow capitalization that matches the USB spec.
42+
# pylint: disable=invalid-name
3943
wValue = desc_type << 8 | index
4044
wIndex = language_id
41-
device.ctrl_transfer(_REQ_RCPT_DEVICE | _REQ_TYPE_STANDARD | _DIR_IN, _REQ_GET_DESCRIPTOR, wValue, wIndex, buf)
45+
device.ctrl_transfer(
46+
_REQ_RCPT_DEVICE | _REQ_TYPE_STANDARD | _DIR_IN,
47+
_REQ_GET_DESCRIPTOR,
48+
wValue,
49+
wIndex,
50+
buf,
51+
)
52+
4253

4354
def get_device_descriptor(device):
55+
"""Fetch the device descriptor and return it."""
4456
buf = bytearray(1)
4557
get_descriptor(device, DESC_DEVICE, 0, buf)
4658
full_buf = bytearray(buf[0])
4759
get_descriptor(device, DESC_DEVICE, 0, full_buf)
4860
return full_buf
4961

62+
5063
def get_configuration_descriptor(device, index):
64+
"""Fetch the configuration descriptor, its associated descriptors and return it."""
65+
# Allow capitalization that matches the USB spec.
66+
# pylint: disable=invalid-name
5167
buf = bytearray(4)
5268
get_descriptor(device, DESC_CONFIGURATION, index, buf)
5369
wTotalLength = struct.unpack("<xxH", buf)[0]

docs/examples.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Simple test
22
------------
33

4-
Ensure your device works with this simple test.
4+
Print basic information about all connected USB devices, including basic info
5+
about the first available configuration.
56

67
.. literalinclude:: ../examples/usb_host_descriptors_simpletest.py
78
:caption: examples/usb_host_descriptors_simpletest.py

docs/index.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,10 @@ Table of Contents
2424
.. toctree::
2525
:caption: Tutorials
2626

27-
.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave
28-
the toctree above for use later.
29-
3027
.. toctree::
3128
:caption: Related Products
3229

33-
.. todo:: Add any product links here. If there are none, then simply delete this todo and leave
34-
the toctree above for use later.
30+
Adafruit Feather RP2040 USB Host <https://www.adafruit.com/product/5723>
3531

3632
.. toctree::
3733
:caption: Other Links

examples/usb_host_descriptors_simpletest.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,48 @@
22
# SPDX-FileCopyrightText: Copyright (c) 2023 Scott Shawcroft for Adafruit Industries
33
#
44
# SPDX-License-Identifier: Unlicense
5+
6+
import time
7+
import usb.core
8+
9+
import adafruit_usb_host_descriptors
10+
11+
DIR_IN = 0x80
12+
13+
while True:
14+
print("searching for devices")
15+
for device in usb.core.find(find_all=True):
16+
print("pid", hex(device.idProduct))
17+
print("vid", hex(device.idVendor))
18+
print("man", device.manufacturer)
19+
print("product", device.product)
20+
print("serial", device.serial_number)
21+
print("config[0]:")
22+
config_descriptor = adafruit_usb_host_descriptors.get_configuration_descriptor(
23+
device, 0
24+
)
25+
26+
i = 0
27+
while i < len(config_descriptor):
28+
descriptor_len = config_descriptor[i]
29+
descriptor_type = config_descriptor[i + 1]
30+
if descriptor_type == adafruit_usb_host_descriptors.DESC_CONFIGURATION:
31+
config_value = config_descriptor[i + 5]
32+
print(f" value {config_value:d}")
33+
elif descriptor_type == adafruit_usb_host_descriptors.DESC_INTERFACE:
34+
interface_number = config_descriptor[i + 2]
35+
interface_class = config_descriptor[i + 5]
36+
interface_subclass = config_descriptor[i + 6]
37+
print(f" interface[{interface_number:d}]")
38+
print(
39+
f" class {interface_class:02x} subclass {interface_subclass:02x}"
40+
)
41+
elif descriptor_type == adafruit_usb_host_descriptors.DESC_ENDPOINT:
42+
endpoint_address = config_descriptor[i + 2]
43+
if endpoint_address & DIR_IN:
44+
print(f" IN {endpoint_address:02x}")
45+
else:
46+
print(f" OUT {endpoint_address:02x}")
47+
i += descriptor_len
48+
print()
49+
time.sleep(5)

0 commit comments

Comments
 (0)