Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add USB support #69

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pybricksdev/ble/pybricks.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ def _standard_uuid(short: int) -> str:
.. availability:: Since Pybricks protocol v1.0.0.
"""

DEVICE_NAME_UUID = _standard_uuid(0x2A00)
"""Standard Device Name UUID
.. availability:: Since Pybricks protocol v1.0.0.
nkarstens marked this conversation as resolved.
Show resolved Hide resolved
"""

FW_REV_UUID = _standard_uuid(0x2A26)
"""Standard Firmware Revision String characteristic UUID
Expand Down
26 changes: 21 additions & 5 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 ..ble import find_device
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 @@ -189,11 +196,20 @@ async def run(self, args: argparse.Namespace):
# It is a Pybricks Hub with BLE. Device name or address is given.
hub = PybricksHub()
print(f"Searching for {args.name or 'any hub with Pybricks service'}...")
device_or_address = await find_device(args.name)
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
9 changes: 2 additions & 7 deletions pybricksdev/connections/ev3.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import hid

from ..tools import chunk

LEGO_VENDOR_ID = 0x0694
EV3_PRODUCT_ID = 0x0005
EV3_BOOTLOADER_PRODUCT_ID = 0x0006
from ..usb import LEGO_USB_VID, LegoUsbPid


class MessageType(enum.IntEnum):
Expand Down Expand Up @@ -70,9 +67,7 @@ def open(self) -> None:
"""
Opens an HID connection to the EV3 bootloader.
"""
self._device.open(
vendor_id=LEGO_VENDOR_ID, product_id=EV3_BOOTLOADER_PRODUCT_ID
)
self._device.open(vendor_id=LEGO_USB_VID, product_id=LegoUsbPid.EV3_FW_UPDATE)

def close(self) -> None:
"""
Expand Down
6 changes: 5 additions & 1 deletion pybricksdev/connections/lego.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from serial.tools import list_ports

from ..tools import chunk
from ..usb import LEGO_USB_VID
from .pybricks import PybricksHub

FILE_PACKET_SIZE = 1024
Expand Down Expand Up @@ -77,7 +78,10 @@ async def connect(self, device=None):
port = None
devices = list_ports.comports()
for dev in devices:
if dev.product == "LEGO Technic Large Hub in FS Mode" or dev.vid == 0x0694:
if (
dev.product == "LEGO Technic Large Hub in FS Mode"
or dev.vid == LEGO_USB_VID
):
port = dev.device
break

Expand Down
Loading
Loading