This commit is contained in:
AndBondStyle
2023-10-30 02:03:02 +03:00
parent f4d66533b5
commit 3d48cb4a7b
14 changed files with 572 additions and 285 deletions
+30
View File
@@ -0,0 +1,30 @@
class NiimbotPacket:
def __init__(self, type_, data):
self.type = type_
self.data = data
@classmethod
def from_bytes(cls, pkt):
assert pkt[:2] == b"\x55\x55"
assert pkt[-2:] == b"\xaa\xaa"
type_ = pkt[2]
len_ = pkt[3]
data = pkt[4 : 4 + len_]
checksum = type_ ^ len_
for i in data:
checksum ^= i
assert checksum == pkt[-3]
return cls(type_, data)
def to_bytes(self):
checksum = self.type ^ len(self.data)
for i in self.data:
checksum ^= i
return bytes(
(0x55, 0x55, self.type, len(self.data), *self.data, checksum, 0xAA, 0xAA)
)
def __repr__(self):
return f"<NiimbotPacket type={self.type} data={self.data}>"