From 110b71c5ce42d439a3384f4c42fcd8bfacd37de8 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Sun, 6 Oct 2019 19:50:01 +0200
Subject: [PATCH] feat(pycard10): Add flag to set card10 system time

Add a --set-time flag to allow setting card10 system time via the
USB-Serial connection.  Use like this:

    ./tools/pycard10.py --set-time

Signed-off-by: Rahix <rahix@rahix.de>
---
 tools/pycard10.py | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/tools/pycard10.py b/tools/pycard10.py
index d5b51e7e..820925d1 100755
--- a/tools/pycard10.py
+++ b/tools/pycard10.py
@@ -213,6 +213,11 @@ def main():
         "--device", default="/dev/ttyACM0", help="the serial device of the card10"
     )
     cmd_parser.add_argument("-c", "--command", help="program passed in as string")
+    cmd_parser.add_argument(
+        "--set-time",
+        action="store_true",
+        help="Set card10 system time to this host's time",
+    )
     cmd_parser.add_argument(
         "-w",
         "--wait",
@@ -241,7 +246,7 @@ def main():
     if args.reset:
         card10.soft_reset()
 
-    elif args.command is not None or len(args.files):
+    elif args.set_time or args.command is not None or len(args.files):
         # we must enter raw-REPL mode to execute commands
         # this will do a soft-reset of the board
         try:
@@ -268,6 +273,16 @@ def main():
                 stdout_write_bytes(ret_err)
                 sys.exit(1)
 
+        # Set card10 system time
+        if args.set_time:
+            now = round(time.time())
+            code = """\
+import utime
+utime.set_unix_time({time})
+print("Time was set to {time}!")
+"""
+            execbuffer(code.format(time=now))
+
         # run the command, if given
         if args.command is not None:
             execbuffer(args.command.encode("utf-8"))
-- 
GitLab