From dcbdfebd004fca234ae5cb0680bcf4036769116f Mon Sep 17 00:00:00 2001 From: Karel Blavka Date: Tue, 17 May 2022 15:11:16 +0200 Subject: [PATCH] Add latency option for app console --- src/hardwario/chester/cli/app.py | 5 +++-- src/hardwario/chester/console.py | 13 +++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/hardwario/chester/cli/app.py b/src/hardwario/chester/cli/app.py index 66ee48c..d74caf5 100644 --- a/src/hardwario/chester/cli/app.py +++ b/src/hardwario/chester/cli/app.py @@ -63,10 +63,11 @@ def command_reset(ctx, halt): @cli.command('console') @click.option('--reset', is_flag=True, help='Reset application firmware.') +@click.option('--latency', type=int, help='Latency for RTT readout in ms.', show_default=True, default=50) @click.option('--history-file', type=click.Path(writable=True), show_default=True, default=default_history_file) @click.option('--console-file', type=click.File('a', 'utf-8'), show_default=True, default=default_console_file) @click.pass_context -def command_console(ctx, reset, history_file, console_file): +def command_console(ctx, reset, latency, history_file, console_file): '''Start interactive console for shell and logging.''' logger.remove(2) # Remove stderr logger @@ -75,7 +76,7 @@ def command_console(ctx, reset, history_file, console_file): if reset: prog.reset() prog.go() - console.run(prog, console_file) + console.run(prog, console_file, latency=latency) def validate_pib_param(ctx, param, value): diff --git a/src/hardwario/chester/console.py b/src/hardwario/chester/console.py index 3664f9e..3219a16 100644 --- a/src/hardwario/chester/console.py +++ b/src/hardwario/chester/console.py @@ -136,7 +136,7 @@ def _(event): clipboard=PyperclipClipboard() ) - def run(self, prog: NRFJProg, console_file): + def run(self, prog: NRFJProg, console_file, latency=50): channels = prog.rtt_start() if 'Logger' not in channels: @@ -145,6 +145,8 @@ def run(self, prog: NRFJProg, console_file): if 'Terminal' not in channels: raise Exception('Not found RTT Terminal channel') + rtt_read_delay = latency / 1000.0 + async def task_rtt_read(channel, buffer): while prog.rtt_is_running: with logger.catch(message='task_rtt_read', reraise=True): @@ -162,7 +164,7 @@ async def task_rtt_read(channel, buffer): line = line.replace('\r', '') buffer.set_document(Document(buffer.text + line, None), True) - await asyncio.sleep(0.05) + await asyncio.sleep(rtt_read_delay) console_file.write(f'{ "*" * 80 }\n') @@ -176,6 +178,13 @@ async def task_rtt_read_terminal(): loop.create_task(task_rtt_read_logger()) loop.create_task(task_rtt_read_terminal()) + # t1 = threading.Thread(target=task_rtt_read, args=('Logger', self.logger_buffer)) + # t2 = threading.Thread(target=task_rtt_read, args=('Terminal', self.shell_buffer)) + # t1.daemon = True + # t2.daemon = True + # t1.start() + # t2.start() + def accept(buff): line = f'{buff.text}\n'.replace('\r', '') # self.shell_buffer.insert_text(line)