diff options
| author | Damien George <damien@micropython.org> | 2021-06-13 23:59:03 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2021-06-15 13:52:31 +1000 |
| commit | d2dcd05adc0228dcb46cd31d5cc4751d88460b80 (patch) | |
| tree | bd11bff6c50d9864126d2c0510d22204490e6c1e | |
| parent | 77c529e8be7302a8436152ead6dacf050c03d601 (diff) | |
tools/mpremote: Use signal to capture and handle ctrl-C on Windows.
Now a ctrl-C will not stop mpremote, rather this character will be passed
through to the attached device.
The mpremote version is also increased to 0.0.5.
Signed-off-by: Damien George <damien@micropython.org>
| -rw-r--r-- | tools/mpremote/mpremote/console.py | 31 | ||||
| -rw-r--r-- | tools/mpremote/mpremote/main.py | 9 | ||||
| -rw-r--r-- | tools/mpremote/setup.cfg | 2 |
3 files changed, 23 insertions, 19 deletions
diff --git a/tools/mpremote/mpremote/console.py b/tools/mpremote/mpremote/console.py index 646bbfa22..2652c7393 100644 --- a/tools/mpremote/mpremote/console.py +++ b/tools/mpremote/mpremote/console.py @@ -1,11 +1,11 @@ -import sys +import sys, time try: import select, termios except ImportError: termios = None select = None - import msvcrt + import msvcrt, signal class ConsolePosix: @@ -31,9 +31,9 @@ class ConsolePosix: def exit(self): termios.tcsetattr(self.infd, termios.TCSANOW, self.orig_attr) - def waitchar(self): - # TODO pyb.serial might not have fd - select.select([console_in.infd, pyb.serial.fd], [], []) + def waitchar(self, pyb_serial): + # TODO pyb_serial might not have fd + select.select([self.infd, pyb_serial.fd], [], []) def readchar(self): res = select.select([self.infd], [], [], 0) @@ -75,20 +75,29 @@ class ConsoleWindows: b"\x94": b"Z", # Ctrl-Tab = BACKTAB, } + def __init__(self): + self.ctrl_c = 0 + + def _sigint_handler(self, signo, frame): + self.ctrl_c += 1 + def enter(self): - pass + signal.signal(signal.SIGINT, self._sigint_handler) def exit(self): - pass + signal.signal(signal.SIGINT, signal.SIG_DFL) def inWaiting(self): - return 1 if msvcrt.kbhit() else 0 + return 1 if self.ctrl_c or msvcrt.kbhit() else 0 - def waitchar(self): - while not (self.inWaiting() or pyb.serial.inWaiting()): + def waitchar(self, pyb_serial): + while not (self.inWaiting() or pyb_serial.inWaiting()): time.sleep(0.01) def readchar(self): + if self.ctrl_c: + self.ctrl_c -= 1 + return b"\x03" if msvcrt.kbhit(): ch = msvcrt.getch() while ch in b"\x00\xe0": # arrow or function key prefix? @@ -120,7 +129,7 @@ else: # Windows VT mode ( >= win10 only) # https://bugs.python.org/msg291732 - import ctypes + import ctypes, os from ctypes import wintypes kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) diff --git a/tools/mpremote/mpremote/main.py b/tools/mpremote/mpremote/main.py index 121eec3bf..b7c46a1f1 100644 --- a/tools/mpremote/mpremote/main.py +++ b/tools/mpremote/mpremote/main.py @@ -17,7 +17,7 @@ MicroPython device over a serial connection. Commands supported are: mpremote repl -- enter REPL """ -import os, select, sys, time +import os, sys import serial.tools.list_ports from . import pyboardextended as pyboard @@ -249,12 +249,7 @@ def do_filesystem(pyb, args): def do_repl_main_loop(pyb, console_in, console_out_write, *, code_to_inject, file_to_inject): while True: - if isinstance(console_in, ConsolePosix): - # TODO pyb.serial might not have fd - select.select([console_in.infd, pyb.serial.fd], [], []) - else: - while not (console_in.inWaiting() or pyb.serial.inWaiting()): - time.sleep(0.01) + console_in.waitchar(pyb.serial) c = console_in.readchar() if c: if c == b"\x1d": # ctrl-], quit diff --git a/tools/mpremote/setup.cfg b/tools/mpremote/setup.cfg index 4a1bf27c1..4e78d05a9 100644 --- a/tools/mpremote/setup.cfg +++ b/tools/mpremote/setup.cfg @@ -1,6 +1,6 @@ [metadata] name = mpremote -version = 0.0.4 +version = 0.0.5 author = Damien George author_email = damien@micropython.org description = Tool for interacting remotely with MicroPython |
