Skip to content

Commit

Permalink
pybricksdev.usb: add new usb subpackage
Browse files Browse the repository at this point in the history
We were starting to get repeated defines of USB stuff, so let's put it
all in one place.
  • Loading branch information
dlech authored and nkarstens committed Feb 20, 2024
1 parent 5ed0886 commit c06d239
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
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
19 changes: 8 additions & 11 deletions pybricksdev/dfu.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,17 @@
from . import resources
from ._vendored import dfu_create, dfu_upload
from .ble.lwp3.bytecodes import HubKind
from .usb import LEGO_USB_VID, LegoUsbPid

FIRMWARE_ADDRESS = 0x08008000
FIRMWARE_SIZE = 1 * 1024 * 1024 - 32 * 1024 # 1MiB - 32KiB
LEGO_VID = 0x0694
SPIKE_PRIME_PID = 0x0008
SPIKE_ESSENTIAL_PID = 0x000C
MINDSTORMS_INVENTOR_PID = 0x0011

ALL_PIDS = {
MINDSTORMS_INVENTOR_PID: HubKind.TECHNIC_LARGE,
SPIKE_ESSENTIAL_PID: HubKind.TECHNIC_SMALL,
SPIKE_PRIME_PID: HubKind.TECHNIC_LARGE,
LegoUsbPid.SPIKE_PRIME_DFU: HubKind.TECHNIC_LARGE,
LegoUsbPid.SPIKE_ESSENTIAL_DFU: HubKind.TECHNIC_SMALL,
LegoUsbPid.ROBOT_INVENTOR_DFU: HubKind.TECHNIC_LARGE,
}
ALL_DEVICES = [f"{LEGO_VID:04x}:{pid:04x}" for pid in ALL_PIDS.keys()]
ALL_DEVICES = [f"{LEGO_USB_VID:04x}:{pid:04x}" for pid in ALL_PIDS.keys()]


def _get_dfu_util() -> ContextManager[os.PathLike]:
Expand Down Expand Up @@ -165,7 +162,7 @@ def flash_dfu(firmware_bin: bytes, metadata: dict) -> None:
try:
# Determine correct product ID

devices = dfu_upload.get_dfu_devices(idVendor=LEGO_VID)
devices = dfu_upload.get_dfu_devices(idVendor=LEGO_USB_VID)
if not devices:
print(
"No DFU devices found.",
Expand All @@ -184,11 +181,11 @@ def flash_dfu(firmware_bin: bytes, metadata: dict) -> None:
exit(1)

# Create dfu file
device = "0x{0:04x}:0x{1:04x}".format(LEGO_VID, product_id)
device = "0x{0:04x}:0x{1:04x}".format(LEGO_USB_VID, product_id)
dfu_create.build(outfile, [[target]], device)

# Init dfu tool
dfu_upload.__VID = LEGO_VID
dfu_upload.__VID = LEGO_USB_VID
dfu_upload.__PID = product_id
dfu_upload.init()
elements = dfu_upload.read_dfu_file(outfile)
Expand Down
23 changes: 23 additions & 0 deletions pybricksdev/usb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""
Common USB definitions.
"""

import enum as _enum

LEGO_USB_VID = 0x0694
NXT_SAMBA_VID = 0x03EB
NXT_SAMBA_PID = 0x6124


class LegoUsbPid(_enum.IntEnum):
RCX_IR_TOWER = 0x0001
NXT = 0x0002
WEDO = 0x0003
EV3 = 0x0005
EV3_FW_UPDATE = 0x0006
SPIKE_PRIME_DFU = 0x0008
SPIKE_PRIME = 0x0009
SPIKE_ESSENTIAL_DFU = 0x000C
SPIKE_ESSENTIAL = 0x000D
ROBOT_INVENTOR = 0x0010
ROBOT_INVENTOR_DFU = 0x0011

0 comments on commit c06d239

Please sign in to comment.