Skip to content

micropython/usb/usb-device/usb/device/core.py: Add alternate setting support plus a few fixes and improvements.#1031

Open
HLammers wants to merge 3 commits into
micropython:masterfrom
HLammers:master
Open

micropython/usb/usb-device/usb/device/core.py: Add alternate setting support plus a few fixes and improvements.#1031
HLammers wants to merge 3 commits into
micropython:masterfrom
HLammers:master

Conversation

@HLammers

@HLammers HLammers commented Jul 5, 2025

Copy link
Copy Markdown

Summary

  • Added bAlternateSetting as argument to Descriptor.interface, because it is used for USB MIDI 2.0
  • Fixed the loop reserving other string indexes used by built-in drivers while len(strs) < builtin_driver.str_max: to while len(strs) < builtin\_driver.str\_max - 1: (row 169) because str_max equals to one more than the highest string descriptor index value used by any built-in descriptor according to the docs and added a comment that this loop is probably unnecessary or even wrong
  • Removed import time (not used)
  • Removed unused/duplicate _Device.config argument config_str
  • Some code optimisations (in particular to the Buffer class)

Testing

  • Tested with micropython/usb/examples/device/midi_example.py
  • Tested the Buffer class with micropython/usb/usb-device/usb/device/tests/test_core_buffer.py
  • Tested with a soon to be made public multi-port MIDI module draft which adds port names to strs

Trade-offs and Alternatives

None

HLammers added 3 commits July 5, 2025 11:49
⦁	Removed `import time` (not used)
⦁	Removed unused/duplicate `_Device.config` argument `config_str`
⦁	Fixed the loop reserving other string indexes used by built-in drivers `while len(strs) < builtin_driver.str_max:` to `while len(strs) < builtin\_driver.str\_max - 1:` (row 169)because str_max equals to one more than the highest string descriptor index value used by any built-in descriptor according to the [docs](https://docs.micropython.org/en/latest/library/machine.USBDevice.html) and added a comment that this loop is probably unnecessary or even wrong
⦁	Added `bAlternateSetting` as argument to `Descriptor.interface`, because it is used for USB MIDI 2.0
⦁	Some code optimisations
Update fromatting
@HLammers HLammers changed the title micropython/usb/usb-device/usb/device/core.[: Add alternate setting support plus a few fixes and improvements. micropython/usb/usb-device/usb/device/core.py: Add alternate setting support plus a few fixes and improvements. Jul 5, 2025
@HLammers

HLammers commented Jul 8, 2025

Copy link
Copy Markdown
Author

The updated version of core.py is used in this project, which also shows that it is working: https://github.com/orgs/micropython/discussions/17640

@dpgeorge dpgeorge requested a review from projectgus July 1, 2026 05:50

@projectgus projectgus left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey @HLammers,

Sorry I never saw this when you originally submitted it. Appreciate you submitting these improvements, thank you!

I have a couple of questions but generally this looks good to me.

from micropython import const
import machine
import struct
import time

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Confused by this as PR description says:

Removed import time (not used)

... and it is indeed not used, from what I can see?

Comment on lines +171 to +173
): # This is possibly unnecessary or wrong because
# https://docs.micropython.org/en/latest/library/machine.USBDevice.html
# states all string values except index 0 should be plain ASCII

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What's happening here is that we're expanding the strs list to cover the built-in driver indexes, because these str index values are actually handled by C code and never read by Python code. By putting something here we make sure that the indexes elsewhere in the driver match what the host sees.

This is why the documentation says the strings arguments should be strings - the values provided by the Python caller should be strings, these values are internal placeholders to pad the index out.

I assume the off-by-one is a real bug, though - I'm guessing you found that strings were reported wrong without this?

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