eric6/MicroPython/MicroPythonCommandsInterface.py

changeset 8117
aaa5e0eacd4e
parent 8115
cef34b4ee310
child 8143
2c730d5fd177
--- a/eric6/MicroPython/MicroPythonCommandsInterface.py	Fri Feb 19 17:34:33 2021 +0100
+++ b/eric6/MicroPython/MicroPythonCommandsInterface.py	Sat Feb 20 11:52:49 2021 +0100
@@ -747,11 +747,13 @@
             raise OSError(self.__shortError(err))
         return ast.literal_eval(out.decode("utf-8"))
     
-    def syncTime(self):
+    def syncTime(self, deviceType):
         """
         Public method to set the time of the connected device to the local
         computer's time.
         
+        @param deviceType type of board to sync time to
+        @type str
         @exception OSError raised to indicate an issue with the device
         """
         # rtc_time[0] - year    4 digit
@@ -763,83 +765,88 @@
         # rtc_time[6] - second  0..59
         # rtc_time[7] - yearday 1..366
         # rtc_time[8] - isdst   0, 1, or -1
+        if deviceType == "pyboard":
+            # Pyboard (pyboard doesn't have machine.RTC()).
+            # The pyb.RTC.datetime function takes the arguments in the
+            # order: (year, month, day, weekday, hour, minute, second,
+            # subseconds)
+            # http://docs.micropython.org/en/latest/library/pyb.RTC.html
+            # #pyb.RTC.datetime
+            set_time = "\n".join([
+                "def set_time(rtc_time):",
+                "    import pyb",
+                "    rtc = pyb.RTC()",
+                "    rtc.datetime(rtc_time[:7] + (0,))",
+            ])
+        elif deviceType == "esp":
+            # The machine.RTC documentation was incorrect and doesn't agree
+            # with the code, so no link is presented here. The order of the
+            # arguments is the same as the pyboard except for LoBo MPy.
+            set_time = "\n".join([
+                "def set_time(rtc_time):",
+                "    import machine",
+                "    rtc = machine.RTC()",
+                "    try:",                 # ESP8266 may use rtc.datetime()
+                "        rtc.datetime(rtc_time[:7] + (0,))",
+                "    except Exception:",    # ESP32 uses rtc.init()
+                "        import os",
+                "        if 'LoBo' in os.uname()[0]:",      # LoBo MPy
+                "            clock_time = rtc_time[:3] +"
+                " rtc_time[4:7] + (rtc_time[3], rtc_time[7])",
+                "        else:",
+                "            clock_time = rtc_time[:7] + (0,)",
+                "        rtc.init(clock_time)",
+            ])
+        elif deviceType == "circuitpython":
+            set_time = "\n".join([
+                "def set_time(rtc_time):",
+                "    import rtc",
+                "    import time",
+                "    clock = rtc.RTC()",
+                "    clock_time = rtc_time[:3] + rtc_time[4:7] + (rtc_time[3],"
+                " rtc_time[7], rtc_time[8])",
+                "    clock.datetime = time.struct_time(clock_time)",
+            ])
+        elif deviceType in ("bbc_microbit", "calliope"):
+            # BBC micro:bit and Calliope mini don't support time commands
+            return
+        elif deviceType == "rp2040":
+            # Raspberry Pi Pico (RP2040) - machine.RTC doesn't exist
+            set_time = "\n".join([
+                "def set_time(rtc_time):",
+                "    setup_0 = rtc_time[0] << 12 | rtc_time[1] << 8 |"
+                " rtc_time[2]",
+                "    setup_1 = (rtc_time[3] % 7) << 24 | rtc_time[4] << 16 |"
+                " rtc_time[5] << 8 | rtc_time[6]",
+                "    machine.mem32[0x4005c004] = setup_0",
+                "    machine.mem32[0x4005c008] = setup_1",
+                "    machine.mem32[0x4005c00c] |= 0x10",
+            ])
+        elif deviceType == "pycom":
+            # PyCom's machine.RTC takes its arguments in a slightly
+            # different order than the official machine.RTC.
+            # (year, month, day, hour, minute, second[, microsecond[,
+            # tzinfo]])
+            # https://docs.pycom.io/firmwareapi/pycom/machine/rtc/
+            # #rtc-init-datetime-none-source-rtc-internal-rc
+            set_time = "\n".join([
+                "def set_time(rtc_time):",
+                "    import pycom",
+                "    rtc_time2 = rtc_time[:3] + rtc_time[4:7]",
+                "    import machine",
+                "    rtc = machine.RTC()",
+                "    rtc.init(rtc_time2)",
+            ])
+        else:
+            # no set_time() support for generic boards
+            return
+        
         now = time.localtime(time.time())
         commands = [
-            "\n".join([
-                "def set_time(rtc_time):",
-                "    rtc = None",
-                # Pyboard (pyboard doesn't have machine.RTC()).
-                # The pyb.RTC.datetime function takes the arguments in the
-                # order:
-                # (year, month, day, weekday, hour, minute, second, subseconds)
-                # http://docs.micropython.org/en/latest/library/pyb.RTC.html
-                # #pyb.RTC.datetime
-                "    try:",
-                "        import pyb",
-                "        rtc = pyb.RTC()",
-                "        rtc.datetime(rtc_time)",
-                "    except Exception:",
-                "        try:",
-                # PyCom's machine.RTC takes its arguments in a slightly
-                # different order than the official machine.RTC.
-                # (year, month, day, hour, minute, second[, microsecond[,
-                # tzinfo]])
-                # https://docs.pycom.io/firmwareapi/pycom/machine/rtc/
-                # #rtc-init-datetime-none-source-rtc-internal-rc
-                "            import pycom",
-                "            rtc_time2 = (rtc_time[0], rtc_time[1],"
-                " rtc_time[2], rtc_time[4], rtc_time[5], rtc_time[6])",
-                "            import machine",
-                "            rtc = machine.RTC()",
-                "            rtc.init(rtc_time2)",
-                "        except:",
-                "            try:",
-                # The machine.RTC documentation was incorrect and doesn't agree
-                # with the code, so no link is presented here. The order of the
-                # arguments is the same as the pyboard except for LoBo MPy.
-                "                import machine",
-                "                rtc = machine.RTC()",
-                "                try:",      # ESP8266 may use rtc.datetime()
-                "                    rtc.datetime(rtc_time)",
-                "                except Exception:",  # ESP32 uses rtc.init()
-                "                    import os",
-                "                    if 'LoBo' in os.uname()[0]:",  # LoBo MPy
-                "                        clock_time = rtc_time[:3] +"
-                " rtc_time[4:7] + (rtc_time[3], 0)",
-                "                    else:",
-                "                        clock_time = rtc_time",
-                "                    rtc.init(clock_time)",
-                "            except:",
-                "                try:",
-                # Check for CircuitPython devices
-                "                    import rtc",
-                "                    import time",
-                "                    clock = rtc.RTC()",
-                "                    clock_time = rtc_time[:3] + rtc_time[4:7]"
-                " + (rtc_time[3], -1, -1)",
-                "                    clock.datetime = time.struct_time("
-                "clock_time)",
-                "                except:",
-                # Check for the Raspberry Pi Pico - machine.RTC doesn't exist
-                "                    try:",
-                "                        import os",
-                "                        if os.uname().sysname == 'rp2':",
-                "                            setup_0 = rtc_time[0] << 12 |"
-                " rtc_time[1] << 8 | rtc_time[2]",
-                "                            setup_1 = (rtc_time[3] % 7) << 24"
-                " | rtc_time[4] << 16 | rtc_time[5] << 8 | rtc_time[6]",
-                "                            machine.mem32[0x4005c004] ="
-                " setup_0",
-                "                            machine.mem32[0x4005c008] ="
-                " setup_1",
-                "                            machine.mem32[0x4005c00c] |="
-                " 0x10",
-                "                    except:",
-                "                        pass",
-            ]),
+            set_time,
             "set_time({0})".format((
-                now.tm_year, now.tm_mon, now.tm_mday, now.tm_wday + 1, 
-                now.tm_hour, now.tm_min, now.tm_sec, 0
+                now.tm_year, now.tm_mon, now.tm_mday, now.tm_wday + 1,
+                now.tm_hour, now.tm_min, now.tm_sec, now.tm_yday, now.tm_isdst
             )),
             "del set_time",
         ]

eric ide

mercurial