--- a/src/eric7/MicroPython/Devices/CircuitPythonDevices.py Thu Mar 09 16:59:39 2023 +0100 +++ b/src/eric7/MicroPython/Devices/CircuitPythonDevices.py Fri Mar 10 18:04:06 2023 +0100 @@ -1368,6 +1368,95 @@ return scanResults, "" + ################################################################## + ## Methods below implement NTP related methods + ################################################################## + + def hasNetworkTime(self): + """ + Public method to check the availability of network time functions. + + @return flag indicating the availability of network time functions + @rtype bool + """ + command = """ +def has_ntp(): + try: + import adafruit_ntp + if hasattr(adafruit_ntp, 'NTP'): + return True + except ImportError: + pass + + return False + +print(has_ntp()) +del has_ntp +""" + out, err = self._interface.execute(command, mode=self._submitMode) + if err: + raise OSError(self._shortError(err)) + return out.strip() == b"True" + + def setNetworkTime(self, server="0.pool.ntp.org", tzOffset=0, timeout=10): + """ + Public method to set the time to the network time retrieved from an + NTP server. + + @param server name of the NTP server to get the network time from + (defaults to "0.pool.ntp.org") + @type str (optional) + @param tzOffset offset with respect to UTC (defaults to 0) + @type int (optional) + @param timeout maximum time to wait for a server response in seconds + (defaults to 10) + @type int + @return tuple containing a flag indicating success and an error string + @rtype tuple of (bool, str) + """ + command = """ +def set_ntp_time(server, tz_offset, timeout): + import rtc + import socketpool + import wifi + + import adafruit_ntp + + + r = wifi.radio + if r.ipv4_address is None: + return False + + pool = socketpool.SocketPool(r) + ntp = adafruit_ntp.NTP( + pool, server=server, tz_offset=tz_offset, socket_timeout=timeout + ) + rtc.RTC().datetime = ntp.datetime + return True + +try: + print({{ + 'result': set_ntp_time({0}, {1}, {2}), + 'error': '', + }}) +except Exception as err: + print({{ + 'result': False, + 'error': str(err), + }}) +del set_ntp_time +""".format( + repr(server), tzOffset, timeout + ) + out, err = self._interface.execute( + command, mode=self._submitMode, timeout=(timeout + 2) * 1000 + ) + if err: + return False, err + else: + res = ast.literal_eval(out.decode("utf-8")) + return res["result"], res["error"] + def createDevice(microPythonWidget, deviceType, vid, pid, boardName, serialNumber): """