1
0

store rtc time offset

This commit is contained in:
2026-02-18 16:22:25 +01:00
parent bfb1e71d7d
commit eae1252493
2 changed files with 20 additions and 2 deletions

15
rtc.py
View File

@@ -2,10 +2,16 @@ import datetime
from protocol import I2CPCClient
def sync_rtc(p: I2CPCClient):
TIMEOFFSET_ADDR = 2275
dtime = datetime.datetime.now(datetime.UTC)
utc_off = d.total_seconds() / 3600 if (d := datetime.datetime.now().astimezone().utcoffset()) else 0
if(p.version()[-1] >= 2): p.write_eeprom(TIMEOFFSET_ADDR, int(utc_off).to_bytes(1, signed=True))
else: dtime = datetime.datetime.now()
ADDRESS = 0x32
p.write_i2c(ADDRESS, b"\x1f\x40") # halt the clock
dtime = datetime.datetime.now()
def toBCD(val: int) -> int:
return ((val // 10) << 4) | (val % 10)
data = b"\x10"
@@ -18,4 +24,9 @@ def sync_rtc(p: I2CPCClient):
data += toBCD((1900 + dtime.year - 26) % 100).to_bytes(1)
p.write_i2c(ADDRESS, data)
p.write_i2c(ADDRESS, b"\x1f\x00") # start the clock
p.write_i2c(ADDRESS, b"\x1f\x00") # start the clock
p = I2CPCClient("COM17")
sync_rtc(p)
p.reboot()
p.close()