Skip to content

Commit d2dcd05

Browse files
committed
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 <[email protected]>
1 parent 77c529e commit d2dcd05

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

tools/mpremote/mpremote/console.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import sys
1+
import sys, time
22

33
try:
44
import select, termios
55
except ImportError:
66
termios = None
77
select = None
8-
import msvcrt
8+
import msvcrt, signal
99

1010

1111
class ConsolePosix:
@@ -31,9 +31,9 @@ def enter(self):
3131
def exit(self):
3232
termios.tcsetattr(self.infd, termios.TCSANOW, self.orig_attr)
3333

34-
def waitchar(self):
35-
# TODO pyb.serial might not have fd
36-
select.select([console_in.infd, pyb.serial.fd], [], [])
34+
def waitchar(self, pyb_serial):
35+
# TODO pyb_serial might not have fd
36+
select.select([self.infd, pyb_serial.fd], [], [])
3737

3838
def readchar(self):
3939
res = select.select([self.infd], [], [], 0)
@@ -75,20 +75,29 @@ class ConsoleWindows:
7575
b"\x94": b"Z", # Ctrl-Tab = BACKTAB,
7676
}
7777

78+
def __init__(self):
79+
self.ctrl_c = 0
80+
81+
def _sigint_handler(self, signo, frame):
82+
self.ctrl_c += 1
83+
7884
def enter(self):
79-
pass
85+
signal.signal(signal.SIGINT, self._sigint_handler)
8086

8187
def exit(self):
82-
pass
88+
signal.signal(signal.SIGINT, signal.SIG_DFL)
8389

8490
def inWaiting(self):
85-
return 1 if msvcrt.kbhit() else 0
91+
return 1 if self.ctrl_c or msvcrt.kbhit() else 0
8692

87-
def waitchar(self):
88-
while not (self.inWaiting() or pyb.serial.inWaiting()):
93+
def waitchar(self, pyb_serial):
94+
while not (self.inWaiting() or pyb_serial.inWaiting()):
8995
time.sleep(0.01)
9096

9197
def readchar(self):
98+
if self.ctrl_c:
99+
self.ctrl_c -= 1
100+
return b"\x03"
92101
if msvcrt.kbhit():
93102
ch = msvcrt.getch()
94103
while ch in b"\x00\xe0": # arrow or function key prefix?
@@ -120,7 +129,7 @@ def write(self, buf):
120129

121130
# Windows VT mode ( >= win10 only)
122131
# https://bugs.python.org/msg291732
123-
import ctypes
132+
import ctypes, os
124133
from ctypes import wintypes
125134

126135
kernel32 = ctypes.WinDLL("kernel32", use_last_error=True)

tools/mpremote/mpremote/main.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
mpremote repl -- enter REPL
1818
"""
1919

20-
import os, select, sys, time
20+
import os, sys
2121
import serial.tools.list_ports
2222

2323
from . import pyboardextended as pyboard
@@ -249,12 +249,7 @@ def _list_recursive(files, path):
249249

250250
def do_repl_main_loop(pyb, console_in, console_out_write, *, code_to_inject, file_to_inject):
251251
while True:
252-
if isinstance(console_in, ConsolePosix):
253-
# TODO pyb.serial might not have fd
254-
select.select([console_in.infd, pyb.serial.fd], [], [])
255-
else:
256-
while not (console_in.inWaiting() or pyb.serial.inWaiting()):
257-
time.sleep(0.01)
252+
console_in.waitchar(pyb.serial)
258253
c = console_in.readchar()
259254
if c:
260255
if c == b"\x1d": # ctrl-], quit

tools/mpremote/setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = mpremote
3-
version = 0.0.4
3+
version = 0.0.5
44
author = Damien George
55
author_email = [email protected]
66
description = Tool for interacting remotely with MicroPython

0 commit comments

Comments
 (0)