更新大部分mpy-lib固件(蓝牙HID,RTCTIME)
This commit is contained in:
45
boards/default_src/micropython/origin/build/lib/rtctime.py
Normal file
45
boards/default_src/micropython/origin/build/lib/rtctime.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""RTC Time"""
|
||||
import gc
|
||||
from time import *
|
||||
from machine import RTC
|
||||
import usocket as socket
|
||||
import ustruct as struct
|
||||
|
||||
# NTP_DELTA (date(2000, 1, 1) - date(1900, 1, 1)).days * 24*60*60
|
||||
NTP_DELTA = 3155673600
|
||||
|
||||
def ntptime(host="pool.ntp.org", utc=28800):
|
||||
NTP_QUERY = bytearray(48)
|
||||
NTP_QUERY[0] = 0x1B
|
||||
addr = socket.getaddrinfo(host, 123)[0][-1]
|
||||
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
try:
|
||||
s.settimeout(1)
|
||||
res = s.sendto(NTP_QUERY, addr)
|
||||
msg = s.recv(48)
|
||||
finally:
|
||||
del addr
|
||||
s.close()
|
||||
gc.collect()
|
||||
val = struct.unpack("!I", msg[40:44])[0]
|
||||
return gmtime(val - NTP_DELTA + utc)
|
||||
|
||||
# There's currently no timezone support in MicroPython, and the RTC is set in UTC time.
|
||||
def settime(times):
|
||||
if isinstance(times, str):
|
||||
try:
|
||||
val = eval(times)
|
||||
if len(val) >= 6:
|
||||
times=(val[0], val[1], val[2], 0, val[3], val[4], val[5], 0)
|
||||
else:
|
||||
raise ValueError("Clock information format error")
|
||||
except:
|
||||
raise ValueError("Clock information format error, use ',' to separate at least 6 numerical values")
|
||||
if isinstance(times, tuple):
|
||||
if 6 <= len(times) <= 8:
|
||||
RTC().datetime((times[0], times[1], times[2], 0, times[3], times[4], times[5], 0))
|
||||
else:
|
||||
raise ValueError("Settime needs a tuple of length 6~8")
|
||||
|
||||
def strtime():
|
||||
return '{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}'.format(*localtime())
|
||||
Reference in New Issue
Block a user