Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/mpflash/usb'
Browse files Browse the repository at this point in the history
Signed-off-by: Jos Verlinde <Jos.Verlinde@microsoft.com>
  • Loading branch information
Josverl committed Jul 8, 2024
2 parents 5674fb7 + f83635a commit b76d592
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
28 changes: 28 additions & 0 deletions src/mpflash/mpflash/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fnmatch
import glob
import os
import sys
from dataclasses import dataclass, field
Expand Down Expand Up @@ -175,3 +176,30 @@ def filtered_comports(
)
# sort by device name
return sorted(comports, key=lambda x: x.device)


def find_serial_by_path(target_port: str):
"""Find the symbolic link path of a serial port by its device path."""
# sourcery skip: use-next

if os.name == "nt":
return None
# List all available serial ports
available_ports = list_ports.comports()
# Filter to get the device path of the target port
target_device_path = None
for port in available_ports:
if port.device == target_port:
target_device_path = port.device
break

if not target_device_path:
return None # Target port not found among available ports

# Search for all symbolic links in /dev/serial/by-path/
for symlink in glob.glob("/dev/serial/by-path/*"):
# Resolve the symbolic link to its target
if os.path.realpath(symlink) == target_device_path:
return symlink # Return the matching symlink path

return None # Return None if no match is found
16 changes: 11 additions & 5 deletions src/mpflash/mpflash/connected.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from rich.progress import BarColumn, Progress, SpinnerColumn, TextColumn, TimeElapsedColumn
from rich.table import Column

from mpflash.common import filtered_comports
from mpflash.common import filtered_comports, find_serial_by_path
from mpflash.mpremoteboard import MPRemoteBoard


Expand Down Expand Up @@ -47,7 +47,13 @@ def list_mcus(*, ignore: List[str], include: List[str], bluetooth: bool = False)
include=include,
bluetooth=bluetooth,
)
conn_mcus = [MPRemoteBoard(c.device, location=c.location or "?") for c in comports]
connected_mcus = [
MPRemoteBoard(
c.device,
location=find_serial_by_path(c.device) or c.location or "?",
)
for c in comports
]

# a lot of boilerplate to show a progress bar with the comport currently scanned
# low update rate to facilitate screen readers/narration
Expand All @@ -63,8 +69,8 @@ def list_mcus(*, ignore: List[str], include: List[str], bluetooth: bool = False)
progress.tasks[tsk_scan].visible = True
progress.start_task(tsk_scan)
try:
for mcu in conn_mcus:
progress.update(tsk_scan, device=mcu.serialport.replace("/dev/", ""))
for mcu in connected_mcus:
progress.update(tsk_scan, device=mcu.serialport.replace("/dev/tty", "tty"))
try:
mcu.get_mcu_info()
except ConnectionError as e:
Expand All @@ -74,4 +80,4 @@ def list_mcus(*, ignore: List[str], include: List[str], bluetooth: bool = False)
# transient
progress.stop_task(tsk_scan)
progress.tasks[tsk_scan].visible = False
return conn_mcus
return connected_mcus
4 changes: 2 additions & 2 deletions src/mpflash/mpflash/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ def mcu_table(
if needs_build:
table.add_column("Build" if is_wide else "Bld", justify="right")
if config.usb:
table.add_column("Location", overflow="fold", max_width=40)
table.add_column("Location", overflow="fold", max_width=60)
# fill the table with the data
for mcu in conn_mcus:
description = f"[italic bright_cyan]{mcu.description}" if mcu.description else ""
if "description" in mcu.toml:
description += f"\n[italic bright_green]{mcu.toml['description']}"
row = [
mcu.serialport.replace("/dev/", ""),
mcu.serialport if is_wide else mcu.serialport.replace("/dev/tty", "tty"),
abbrv_family(mcu.family, is_wide),
]
if is_wide:
Expand Down
2 changes: 1 addition & 1 deletion src/mpflash/mpflash/mpremoteboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def __init__(self, serialport: str = "", update: bool = False, *, location: str
self.arch = ""
self.mpy = ""
self.build = ""
self.location = location
self.location = location # USB location
self.toml = {}
if update:
self.get_mcu_info()
Expand Down

0 comments on commit b76d592

Please sign in to comment.