Skip to content

Commit 2930231

Browse files
committed
pybricks.connections.pybricks: Clear halt on endpoints instead of resetting device.
Change how we ensure the USB endpoints are in a good state when connecting to a device. Instead of resetting the device, which is not supported on all platforms, we now clear the halt condition on both endpoints. We were doing the reset to ensure that the even/odd data toggle state was correct. The reset, if supported, would completely reset the connection and re-enumerate the device, which is more that we need. On Windows, resetting is not supported and libusb would poke all endpoints like this as a best-effort approximation of a reset. So now, the code reflects what is actually important and will be helpful if we switch to something other than pyusb in the future.
1 parent 65210a2 commit 2930231

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

pybricksdev/connections/pybricks.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,6 @@ def __init__(self, device: USBDevice):
946946
self._response_queue = asyncio.Queue[bytes]()
947947

948948
async def _client_connect(self) -> bool:
949-
# Reset is essential to ensure endpoints are in a good state.
950-
self._device.reset()
951949
self._device.set_configuration()
952950

953951
# Save input and output endpoints
@@ -964,6 +962,12 @@ async def _client_connect(self) -> bool:
964962
== ENDPOINT_OUT,
965963
)
966964

965+
# Clearing halt (even if not stalled) resets the even/odd state on the
966+
# endpoints in case it was left in an invalid state by a previous
967+
# connection.
968+
self._ep_in.clear_halt()
969+
self._ep_out.clear_halt()
970+
967971
# There is 1 byte overhead for PybricksUsbMessageType
968972
self._max_write_size = self._ep_out.wMaxPacketSize - 1
969973

0 commit comments

Comments
 (0)