From 1068b3dbbe7c0ddc2f5a0648a9d9e430efb112ac Mon Sep 17 00:00:00 2001 From: Wind-stormger Date: Fri, 13 Jan 2023 14:28:00 +0800 Subject: [PATCH] Implement list command Co-Authored-By: Wind-stormger <76462385+Wind-stormger@users.noreply.github.com> --- README.md | 5 +++++ mpbridge/bridge.py | 19 +++++++++++++++++++ mpbridge/shell.py | 9 +++++++++ pyproject.toml | 2 +- 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9e99001..708c357 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,11 @@ You can use `mpbridge` in several ways based on your needs: * Run `mpbridge clear ` * This command deletes all files and directories from `MicroPython` board and exits. +#### ⚜️ List all connected devices + +* Run `mpbridge list` +* This command lists all connected devices. + **Note** : `` can be the **full port path** or one of the **short forms** below : * `c[n]` for `COM[n]` (`c3` is equal to `COM3`) diff --git a/mpbridge/bridge.py b/mpbridge/bridge.py index 2b52d3c..f0d73df 100644 --- a/mpbridge/bridge.py +++ b/mpbridge/bridge.py @@ -2,6 +2,7 @@ import time from argparse import Namespace +import serial.tools.list_ports from colorama import Fore from mpremote.main import State, argparse_repl from watchdog.observers import Observer @@ -111,3 +112,21 @@ def push_deletes(pyb, path, old_ls: tuple): pyb.fs_verbose_rm(file) for ddir in sorted(set(new_ls[0]).difference(new_ls[0]), reverse=True): pyb.fs_verbose_rmdir(ddir) + + +def list_devices(): + ports = sorted(serial.tools.list_ports.comports()) + if ports: + for i, port in enumerate(ports): + print(Fore.LIGHTYELLOW_EX, + "{}. {} {} {:04x}:{:04x} {} {}".format( + i + 1, + port.device, + port.serial_number or "null", + port.vid if isinstance(port.vid, int) else 0, + port.pid if isinstance(port.pid, int) else 0, + port.manufacturer or "null", + port.product or "null")) + else: + print(Fore.LIGHTYELLOW_EX, "Couldn't find any connected devices") + utils.reset_term_color() diff --git a/mpbridge/shell.py b/mpbridge/shell.py index b3e7b6d..49a7af8 100644 --- a/mpbridge/shell.py +++ b/mpbridge/shell.py @@ -81,3 +81,12 @@ def clear(port): c[n] connect to serial port "COM[n]" """ bridge.clear(port=port) + + +@main.command("list", short_help='List available devices') +def list_devices(): + """List available devices + + [device] [serial_number] [vid]:[pid] [manufacturer] [product] + """ + bridge.list_devices() diff --git a/pyproject.toml b/pyproject.toml index 55a21af..b01f828 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -22,7 +22,7 @@ requires-python = ">=3.7" dependencies = [ "watchdog>=2.2.0", "colorama>=0.4.0", - "mpremote>=0.4.0", + "mpremote==0.4.0", "click" ] classifiers = [