Skip to content

Commit

Permalink
pybricksdev.usb: add Pybricks USB device class/subclass/protocol
Browse files Browse the repository at this point in the history
This adds constants for the Pybricks USB device class, subclass and
protocol. These are used to identify hubs running the Pybricks firmware
when connected via USB.

The `usb` command of the CLI is modified to only accept Pybricks hubs
now. If we still want to support running programs on hubs running
official LEGO firmware, we can add that back with a different command
name.

Co-authored-by: Nate Karstens <[email protected]>
  • Loading branch information
dlech and nkarstens committed Jan 9, 2024
1 parent baae6d5 commit e6b6b9a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
22 changes: 19 additions & 3 deletions pybricksdev/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@

from .. import __name__ as MODULE_NAME
from .. import __version__ as MODULE_VERSION
from ..usb import (
LEGO_USB_VID,
PYBRICKS_USB_DEVICE_CLASS,
PYBRICKS_USB_DEVICE_PROTOCOL,
PYBRICKS_USB_DEVICE_SUBCLASS,
)

PROG_NAME = (
f"{path.basename(sys.executable)} -m {MODULE_NAME}"
Expand Down Expand Up @@ -171,9 +177,10 @@ def add_parser(self, subparsers: argparse._SubParsersAction):
)

async def run(self, args: argparse.Namespace):
from usb.core import find as find_usb

from ..ble import find_device as find_ble
from ..connections.ev3dev import EV3Connection
from ..connections.lego import REPLHub
from ..connections.pybricks import PybricksHub

# Pick the right connection
Expand All @@ -192,8 +199,17 @@ async def run(self, args: argparse.Namespace):
device_or_address = await find_ble(args.name)

elif args.conntype == "usb":
hub = REPLHub()
device_or_address = None
hub = PybricksHub()
device_or_address = find_usb(
idVendor=LEGO_USB_VID,
bDeviceClass=PYBRICKS_USB_DEVICE_CLASS,
bDeviceSubClass=PYBRICKS_USB_DEVICE_SUBCLASS,
bDeviceProtocol=PYBRICKS_USB_DEVICE_PROTOCOL,
)
if device_or_address is None:
print("No Pybricks USB device found.", file=sys.stderr)
exit(1)

else:
raise ValueError(f"Unknown connection type: {args.conntype}")

Expand Down
5 changes: 5 additions & 0 deletions pybricksdev/usb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ class LegoUsbPid(_enum.IntEnum):
SPIKE_ESSENTIAL = 0x000D
ROBOT_INVENTOR = 0x0010
ROBOT_INVENTOR_DFU = 0x0011


PYBRICKS_USB_DEVICE_CLASS = 0xFF
PYBRICKS_USB_DEVICE_SUBCLASS = 0xC5
PYBRICKS_USB_DEVICE_PROTOCOL = 0xF5

0 comments on commit e6b6b9a

Please sign in to comment.