Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions pyocd/subcommands/rtt_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def get_args(cls) -> List[argparse.ArgumentParser]:
help="Down channel ID.")
rtt_options.add_argument("-d", "--log-file", type=str, default=None,
help="Log file name. When specified, logging mode is enabled.")
rtt_options.add_argument("-l", "--line-mode", action="store_true",
help="Line-buffered input. Keystrokes are buffered on the host "
"and only sent to the target when Enter is pressed; "
"backspace and ctrl-U edit the pending line.")

return [cls.CommonOptions.COMMON, cls.CommonOptions.CONNECT, rtt_parser]

Expand Down Expand Up @@ -174,6 +178,8 @@ def logger_loop(self, up_chan, kb):
def viewer_loop(self, up_chan, down_chan, kb):
# byte array to send via RTT
cmd = bytes()
# characters typed but not yet sent (line mode only)
line = ""

while True:
# poll at most 1000 times per second to limit CPU use
Expand All @@ -191,11 +197,31 @@ def viewer_loop(self, up_chan, down_chan, kb):

if ord(c) == 27: # process ESC
break
elif c.isprintable() or c == '\n':
print(c, end="", flush=True)

# add char to buffer
cmd += c.encode("utf-8")
if self._args.line_mode:
# Hold the line on the host and only send it once the user
# presses Enter, to support targets expecting line-based commands.
if c in ("\r", "\n"):
print("", flush=True)
# submit the line, terminated by the character typed
cmd += (line + c).encode("utf-8")
line = ""
elif c in ("\b", "\x7f"): # backspace / delete
if line:
line = line[:-1]
print("\b \b", end="", flush=True)
elif c == "\x15": # ctrl-U, kill line
print("\b \b" * len(line), end="", flush=True)
line = ""
elif c.isprintable():
line += c
print(c, end="", flush=True)
else:
if c.isprintable() or c == '\n':
print(c, end="", flush=True)

# add char to buffer
cmd += c.encode("utf-8")

# write buffer to target
if not cmd:
Expand Down
Loading