Commit d2dcd05a authored by Damien George's avatar Damien George

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: default avatarDamien George <damien@micropython.org>
parent 77c529e8
import sys import sys, time
try: try:
import select, termios import select, termios
except ImportError: except ImportError:
termios = None termios = None
select = None select = None
import msvcrt import msvcrt, signal
class ConsolePosix: class ConsolePosix:
...@@ -31,9 +31,9 @@ class ConsolePosix: ...@@ -31,9 +31,9 @@ class ConsolePosix:
def exit(self): def exit(self):
termios.tcsetattr(self.infd, termios.TCSANOW, self.orig_attr) termios.tcsetattr(self.infd, termios.TCSANOW, self.orig_attr)
def waitchar(self): def waitchar(self, pyb_serial):
# TODO pyb.serial might not have fd # TODO pyb_serial might not have fd
select.select([console_in.infd, pyb.serial.fd], [], []) select.select([self.infd, pyb_serial.fd], [], [])
def readchar(self): def readchar(self):
res = select.select([self.infd], [], [], 0) res = select.select([self.infd], [], [], 0)
...@@ -75,20 +75,29 @@ class ConsoleWindows: ...@@ -75,20 +75,29 @@ class ConsoleWindows:
b"\x94": b"Z", # Ctrl-Tab = BACKTAB, 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): def enter(self):
pass signal.signal(signal.SIGINT, self._sigint_handler)
def exit(self): def exit(self):
pass signal.signal(signal.SIGINT, signal.SIG_DFL)
def inWaiting(self): def inWaiting(self):
return 1 if msvcrt.kbhit() else 0 return 1 if self.ctrl_c or msvcrt.kbhit() else 0
def waitchar(self): def waitchar(self, pyb_serial):
while not (self.inWaiting() or pyb.serial.inWaiting()): while not (self.inWaiting() or pyb_serial.inWaiting()):
time.sleep(0.01) time.sleep(0.01)
def readchar(self): def readchar(self):
if self.ctrl_c:
self.ctrl_c -= 1
return b"\x03"
if msvcrt.kbhit(): if msvcrt.kbhit():
ch = msvcrt.getch() ch = msvcrt.getch()
while ch in b"\x00\xe0": # arrow or function key prefix? while ch in b"\x00\xe0": # arrow or function key prefix?
...@@ -120,7 +129,7 @@ else: ...@@ -120,7 +129,7 @@ else:
# Windows VT mode ( >= win10 only) # Windows VT mode ( >= win10 only)
# https://bugs.python.org/msg291732 # https://bugs.python.org/msg291732
import ctypes import ctypes, os
from ctypes import wintypes from ctypes import wintypes
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)
......
...@@ -17,7 +17,7 @@ MicroPython device over a serial connection. Commands supported are: ...@@ -17,7 +17,7 @@ MicroPython device over a serial connection. Commands supported are:
mpremote repl -- enter REPL mpremote repl -- enter REPL
""" """
import os, select, sys, time import os, sys
import serial.tools.list_ports import serial.tools.list_ports
from . import pyboardextended as pyboard from . import pyboardextended as pyboard
...@@ -249,12 +249,7 @@ def do_filesystem(pyb, args): ...@@ -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): def do_repl_main_loop(pyb, console_in, console_out_write, *, code_to_inject, file_to_inject):
while True: while True:
if isinstance(console_in, ConsolePosix): console_in.waitchar(pyb.serial)
# 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)
c = console_in.readchar() c = console_in.readchar()
if c: if c:
if c == b"\x1d": # ctrl-], quit if c == b"\x1d": # ctrl-], quit
......
[metadata] [metadata]
name = mpremote name = mpremote
version = 0.0.4 version = 0.0.5
author = Damien George author = Damien George
author_email = damien@micropython.org author_email = damien@micropython.org
description = Tool for interacting remotely with MicroPython description = Tool for interacting remotely with MicroPython
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment