RTC support.
parent
fdf94da495
commit
db19d26fc1
3
main.py
3
main.py
|
@ -1,8 +1,9 @@
|
|||
import serial
|
||||
import datetime
|
||||
|
||||
from metawatch.watch import Watch
|
||||
|
||||
w = Watch(serial.Serial('/dev/rfcomm0'))
|
||||
print(w.get_device_type())
|
||||
w.set_rtc(datetime.datetime.now())
|
||||
print(w.get_rtc())
|
||||
|
||||
|
|
|
@ -63,6 +63,20 @@ class GetRTC(OutboundPacket):
|
|||
def __init__(self):
|
||||
self._generate(0x27, 0)
|
||||
|
||||
class SetRTC(OutboundPacket):
|
||||
def __init__(self, dt):
|
||||
payload = []
|
||||
payload.append(dt.year & 0xFF)
|
||||
payload.append(dt.year >> 8)
|
||||
payload.append(dt.month)
|
||||
payload.append(dt.day)
|
||||
payload.append(dt.weekday() + 1)
|
||||
payload.append(dt.hour)
|
||||
payload.append(dt.minute)
|
||||
payload.append(dt.second)
|
||||
self._generate(0x26, 0, *payload)
|
||||
|
||||
|
||||
class PacketDecodeError(Exception):
|
||||
pass
|
||||
|
||||
|
@ -94,10 +108,10 @@ class GetRTCResponse(TypedInboundPacket):
|
|||
def parse(self):
|
||||
self._assert_type(0x28)
|
||||
self._assert_payload_length(8)
|
||||
year = (self._payload[1] << 8) | self._payload[0]
|
||||
year = (self._payload[0] << 8) | self._payload[1]
|
||||
month = self._payload[2]
|
||||
day = self._payload[3]
|
||||
h, m, s = self._payload[4:7]
|
||||
h, m, s = self._payload[5:8]
|
||||
dt = datetime.datetime(year=year, month=month, day=day, hour=h, minute=m, second=s)
|
||||
self.datetime = dt
|
||||
|
||||
|
|
|
@ -26,3 +26,7 @@ class Watch:
|
|||
o.emit(self._fd)
|
||||
i = self._read_packet(protocol.GetRTCResponse)
|
||||
return i.datetime
|
||||
|
||||
def set_rtc(self, dt):
|
||||
o = protocol.SetRTC(dt)
|
||||
o.emit(self._fd)
|
||||
|
|
Loading…
Reference in New Issue