diff --git a/protocol.py b/protocol.py index 7cff6d7..187e648 100644 --- a/protocol.py +++ b/protocol.py @@ -34,6 +34,13 @@ class I2CPCClient: def write_read_i2c(self, addr: int, write_data: bytes, read_len: int): payload = bytes([2, addr, len(write_data)]) + write_data + bytes([read_len]) return self._send_packet(payload) + + def write_eeprom(self, addr: int, data: bytes): + payload = bytes([7, (addr >> 8) & 0xff, addr & 0xff]) + data + return self._send_packet(payload) + def read_eeprom(self, addr: int, len: int): + payload = bytes([8, (addr >> 8) & 0xff, addr & 0xff, len]) + return self._send_packet(payload) def version(self): return self._send_packet(bytes([4])) diff --git a/rtc.py b/rtc.py index 1347e31..e7764ef 100644 --- a/rtc.py +++ b/rtc.py @@ -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 \ No newline at end of file + p.write_i2c(ADDRESS, b"\x1f\x00") # start the clock + +p = I2CPCClient("COM17") +sync_rtc(p) +p.reboot() +p.close() \ No newline at end of file