Browse Source

tools/mpremote: Add `rtc` commands to get and set the RTC.

Replaces the existing functionality provided by the `setrtc` alias to use
the current time, rather than a hard-coded date/time.

Use `rtc` to query the current time.  Use `rtc --set` to set it.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
pull/11659/head
Jim Mussared 1 year ago
committed by Damien George
parent
commit
46715e370d
  1. 20
      tools/mpremote/mpremote/commands.py
  2. 16
      tools/mpremote/mpremote/main.py

20
tools/mpremote/mpremote/commands.py

@ -236,3 +236,23 @@ def do_resume(state, _args=None):
def do_soft_reset(state, _args=None):
state.ensure_raw_repl(soft_reset=True)
def do_rtc(state, args):
if args.set:
import datetime
now = datetime.datetime.now()
timetuple = "({}, {}, {}, {}, {}, {}, {}, {})".format(
now.year,
now.month,
now.day,
now.weekday(),
now.hour,
now.minute,
now.second,
now.microsecond,
)
_do_execbuffer(state, "import machine; machine.RTC().datetime({})".format(timetuple), True)
else:
_do_execbuffer(state, "import machine; print(machine.RTC().datetime())", True)

16
tools/mpremote/mpremote/main.py

@ -34,6 +34,7 @@ from .commands import (
do_eval,
do_run,
do_resume,
do_rtc,
do_soft_reset,
)
from .mip import do_mip
@ -172,6 +173,12 @@ def argparse_run():
return cmd_parser
def argparse_rtc():
cmd_parser = argparse.ArgumentParser(description="get (default) or set the device RTC")
_bool_flag(cmd_parser, "set", "s", False, "set the RTC to the current local time")
return cmd_parser
def argparse_filesystem():
cmd_parser = argparse.ArgumentParser(description="execute filesystem commands on the device")
_bool_flag(cmd_parser, "recursive", "r", False, "recursive copy (for cp command only)")
@ -266,6 +273,10 @@ _COMMANDS = {
do_run,
argparse_run,
),
"rtc": (
do_rtc,
argparse_rtc,
),
"fs": (
do_filesystem,
argparse_filesystem,
@ -324,10 +335,7 @@ _BUILTIN_COMMAND_EXPANSIONS = {
],
"help": "make the device enter its bootloader",
},
"setrtc": [
"exec",
"import machine; machine.RTC().datetime((2020, 1, 1, 0, 10, 0, 0, 0))",
],
# Simple aliases.
"--help": "help",
"--version": "version",
}

Loading…
Cancel
Save