Skip to content

Commit

Permalink
tools/mpremote: Add rtc commands to get and set the RTC.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jimmo authored and dpgeorge committed Jun 2, 2023
1 parent d736a2f commit 46715e3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
20 changes: 20 additions & 0 deletions tools/mpremote/mpremote/commands.py
Expand Up @@ -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 changes: 12 additions & 4 deletions tools/mpremote/mpremote/main.py
Expand Up @@ -34,6 +34,7 @@
do_eval,
do_run,
do_resume,
do_rtc,
do_soft_reset,
)
from .mip import do_mip
Expand Down Expand Up @@ -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)")
Expand Down Expand Up @@ -266,6 +273,10 @@ def argparse_none(description):
do_run,
argparse_run,
),
"rtc": (
do_rtc,
argparse_rtc,
),
"fs": (
do_filesystem,
argparse_filesystem,
Expand Down Expand Up @@ -324,10 +335,7 @@ def argparse_none(description):
],
"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",
}
Expand Down

0 comments on commit 46715e3

Please sign in to comment.