Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a6d01fe

Browse files
committed
tools/mpr: Improve device auto connection.
Signed-off-by: Damien George <[email protected]>
1 parent 656e38a commit a6d01fe

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

tools/mpr

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,9 @@ class SerialIntercept:
583583

584584

585585
class PyboardExtended(pyboard.Pyboard):
586-
def __init__(self, *args, **kwargs):
587-
super().__init__(*args, **kwargs)
586+
def __init__(self, dev, *args, **kwargs):
587+
super().__init__(dev, *args, **kwargs)
588+
self.device_name = dev
588589
self.mounted = False
589590

590591
def enter_raw_repl_without_soft_reset(self):
@@ -652,22 +653,6 @@ class PyboardExtended(pyboard.Pyboard):
652653
self.mounted = False
653654

654655

655-
def find_serial_device(dev):
656-
ports = serial.tools.list_ports.comports()
657-
if dev is None:
658-
for dev in device_shortcuts.values():
659-
if any(p.device == dev for p in ports):
660-
return dev
661-
print("no device found")
662-
sys.exit(1)
663-
else:
664-
dev = device_shortcuts.get(dev, dev)
665-
if any(p.device == dev for p in ports):
666-
return dev
667-
print(f"{dev} could not be accessed")
668-
sys.exit(1)
669-
670-
671656
class ConsolePosix:
672657
def __init__(self):
673658
self.infd = sys.stdin.fileno()
@@ -814,6 +799,33 @@ else:
814799
VT_ENABLED = False
815800

816801

802+
def do_connect(args):
803+
try:
804+
if args and (args[0] == "connect" or args[0] in device_shortcuts):
805+
if args[0] == "connect":
806+
args.pop(0)
807+
dev = args.pop(0)
808+
dev = device_shortcuts.get(dev, dev)
809+
return PyboardExtended(dev, baudrate=115200)
810+
else:
811+
# auto-detect and auto-connect
812+
ports = serial.tools.list_ports.comports()
813+
for dev in device_shortcuts.values():
814+
if any(p.device == dev for p in ports):
815+
try:
816+
return PyboardExtended(dev, baudrate=115200)
817+
except pyboard.PyboardError as er:
818+
if not er.args[0].startswith("failed to access"):
819+
raise er
820+
raise pyboard.PyboardError("no device found")
821+
except pyboard.PyboardError as er:
822+
msg = er.args[0]
823+
if msg.startswith("failed to access"):
824+
msg += " (it may be in use by another program)"
825+
print(msg)
826+
sys.exit(1)
827+
828+
817829
def do_filesystem(pyb, args):
818830
def _list_recursive(files, path):
819831
if os.path.isdir(path):
@@ -892,7 +904,7 @@ def do_repl_main_loop(pyb, console_in, console_out_write, file_to_inject):
892904
console_out_write(b"[%02x]" % ord(c))
893905

894906

895-
def do_repl(pyb, dev, args):
907+
def do_repl(pyb, args):
896908
if len(args) and args[0] == "--capture":
897909
args.pop(0)
898910
capture_file = args.pop(0)
@@ -901,7 +913,7 @@ def do_repl(pyb, dev, args):
901913

902914
file_to_inject = args.pop(0) if len(args) else None
903915

904-
print("Connected to MicroPython at %s" % dev)
916+
print("Connected to MicroPython at %s" % pyb.device_name)
905917
print("Use Ctrl-] to exit this shell")
906918
if capture_file is not None:
907919
print('Capturing session to file "%s"' % capture_file)
@@ -946,24 +958,7 @@ def execbuffer(pyb, buf, follow):
946958
def main():
947959
args = sys.argv[1:]
948960

949-
if args and args[0] in device_shortcuts:
950-
dev = find_serial_device(args.pop(0))
951-
elif args and args[0] == "connect":
952-
args.pop(0)
953-
dev = find_serial_device(args.pop(0))
954-
else:
955-
# auto-detect and auto-connect
956-
dev = find_serial_device(None)
957-
958-
try:
959-
pyb = PyboardExtended(dev, baudrate=115200)
960-
except pyboard.PyboardError as er:
961-
msg = er.args[0]
962-
if msg.startswith("failed to access"):
963-
msg += " (it may be in use by another program)"
964-
print(msg)
965-
sys.exit(1)
966-
961+
pyb = do_connect(args)
967962
in_raw_repl = False
968963
did_action = False
969964

@@ -1018,7 +1013,7 @@ def main():
10181013
elif cmd == "fs":
10191014
do_filesystem(pyb, args)
10201015
elif cmd == "repl":
1021-
do_repl(pyb, dev, args)
1016+
do_repl(pyb, args)
10221017
elif cmd == "run":
10231018
follow = True
10241019
if args[0] == "--no-follow":
@@ -1039,7 +1034,7 @@ def main():
10391034
if in_raw_repl:
10401035
pyb.exit_raw_repl()
10411036
in_raw_repl = False
1042-
do_repl(pyb, dev, args)
1037+
do_repl(pyb, args)
10431038
finally:
10441039
if pyb is not None:
10451040
if pyb.mounted:

0 commit comments

Comments
 (0)