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>
pull/7395/head
Damien George 2021-06-13 23:59:03 +10:00
rodzic 77c529e8be
commit d2dcd05adc
3 zmienionych plików z 23 dodań i 19 usunięć

Wyświetl plik

@ -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)

Wyświetl plik

@ -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

Wyświetl plik

@ -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