Skip to content

Fix compatibility with native PyUSB usb.core.Device.ctrl_transfer#11

Merged
tannewt merged 3 commits intoadafruit:mainfrom
relic-se:blinka
Feb 12, 2026
Merged

Fix compatibility with native PyUSB usb.core.Device.ctrl_transfer#11
tannewt merged 3 commits intoadafruit:mainfrom
relic-se:blinka

Conversation

@relic-se
Copy link
Copy Markdown
Contributor

This update accounts for the difference in the implementation of usb.core.Device.ctrl_transfer between native PyUSB and CircuitPython. Tested on MacOS with Python 3.14 and Adafruit Fruit Jam with CircuitPython 10.

Related to adafruit/Adafruit_CircuitPython_USB_Host_Mouse#18

@tannewt
Copy link
Copy Markdown
Member

tannewt commented Feb 11, 2026

What is the difference? Should we update CircuitPython so we can remove this in the future?

@relic-se
Copy link
Copy Markdown
Contributor Author

What is the difference?

Native PyUSB uses the data_or_wLength parameter just to determine data length, and doesn't update the data of the bytearray. Instead, it returns an array.array object with typecode "B" and the corresponding data. Without this update, the data within the bytearray buffer remains all zeroes. The documentation of PyUSB isn't completely clear on this functionality, but this is what I could ascertain after some analysis.

Should we update CircuitPython so we can remove this in the future?

After reviewing the PyUSB library code (https://github.com/pyusb/pyusb/blob/a63c4f01df62a20bf35ce973ac4ba623d6a7693d/usb/core.py#L1057-L1108), the problem might be that we're using a bytearray rather than an array.array object. I should be able to get around to testing that theory later, but I don't have a strong opinion on the matter.

@relic-se
Copy link
Copy Markdown
Contributor Author

@tannewt I've determined that using array.array does fix the issue in CPython and is still compatible with CircuitPython. I've updated this PR to use those objects instead of bytearrays, but I'm still casting back to bytearray in return statements. It's not exactly necessary to do this, but it keeps it consistent with the prior implementation.

@tannewt
Copy link
Copy Markdown
Member

tannewt commented Feb 12, 2026

This is context about bytearray support too: pyusb/pyusb#489

@tannewt tannewt self-requested a review February 12, 2026 17:38
Copy link
Copy Markdown
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks!

@tannewt tannewt merged commit 52cb81c into adafruit:main Feb 12, 2026
1 check passed
@relic-se
Copy link
Copy Markdown
Contributor Author

If proper bytearray support was added to PyUSB, it would likely make these changes unnecessary. I'll look into it at some point.

@relic-se relic-se deleted the blinka branch February 12, 2026 17:40
adafruit-adabot pushed a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Feb 24, 2026
Updating https://github.com/adafruit/Adafruit_CircuitPython_SEN6x to 1.0.4 from 1.0.3:
  > Merge pull request adafruit/Adafruit_CircuitPython_SEN6x#6 from shamlian/shamlian/fix_voc_nox_scale

Updating https://github.com/adafruit/Adafruit_CircuitPython_USB_Host_Descriptors to 0.5.1 from 0.5.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_USB_Host_Descriptors#11 from relic-se/blinka

Updating https://github.com/adafruit/Adafruit_CircuitPython_YotoPlayer to 1.0.1 from 1.0.0:
  > more demos!
  > more demos!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants