tools/mpremote: Add `sleep` command.

This allows the sequence to be paused (e.g. wait for device, etc).

Also removes the t_ms arg in reset/bootloader, because these arguments
don't really need to be changed, and keeping them would mean inconsistent
units used for time delays.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This commit is contained in:
Jim Mussared 2023-05-30 13:23:12 +10:00 committed by Damien George
parent e4886dda85
commit 7c2c9ea21c
1 changed files with 20 additions and 6 deletions

View File

@ -18,7 +18,7 @@ MicroPython device over a serial connection. Commands supported are:
"""
import argparse
import os, sys
import os, sys, time
from collections.abc import Mapping
from textwrap import dedent
@ -42,6 +42,10 @@ from .repl import do_repl
_PROG = "mpremote"
def do_sleep(state, args):
time.sleep(args.ms[0])
def do_help(state, _args=None):
def print_commands_help(cmds, help_key):
max_command_len = max(len(cmd) for cmd in cmds.keys())
@ -98,6 +102,12 @@ def argparse_connect():
return cmd_parser
def argparse_sleep():
cmd_parser = argparse.ArgumentParser(description="sleep before executing next command")
cmd_parser.add_argument("ms", nargs=1, type=float, help="milliseconds to sleep for")
return cmd_parser
def argparse_edit():
cmd_parser = argparse.ArgumentParser(description="edit files on the device")
cmd_parser.add_argument("files", nargs="+", help="list of remote paths")
@ -212,6 +222,10 @@ _COMMANDS = {
do_connect,
argparse_connect,
),
"sleep": (
do_sleep,
argparse_sleep,
),
"disconnect": (
do_disconnect,
argparse_none("disconnect current device"),
@ -294,19 +308,19 @@ _BUILTIN_COMMAND_EXPANSIONS = {
"import uos\nprint('mount \\tsize \\tused \\tavail \\tuse%')\nfor _m in [''] + uos.listdir('/'):\n _s = uos.stat('/' + _m)\n if not _s[0] & 1 << 14: continue\n _s = uos.statvfs(_m)\n if _s[0]:\n _size = _s[0] * _s[2]; _free = _s[0] * _s[3]; print(_m, _size, _size - _free, _free, int(100 * (_size - _free) / _size), sep='\\t')",
],
# Other shortcuts.
"reset t_ms=100": {
"reset": {
"command": [
"exec",
"--no-follow",
"import utime, machine; utime.sleep_ms(t_ms); machine.reset()",
"import time, machine; time.sleep_ms(100); machine.reset()",
],
"help": "reset the device after delay",
"help": "hard reset the device",
},
"bootloader t_ms=100": {
"bootloader": {
"command": [
"exec",
"--no-follow",
"import utime, machine; utime.sleep_ms(t_ms); machine.bootloader()",
"import time, machine; time.sleep_ms(100); machine.bootloader()",
],
"help": "make the device enter its bootloader",
},