colmi_r02_client.packet

 1def make_packet(command: int, sub_data: bytearray | None = None) -> bytearray:
 2    """
 3    Make a well formed packet from a command key and optional sub data.
 4
 5    That means ensuring it's 16 bytes long and the last byte is a valid CRC.
 6
 7    command must be between 0 and 255 (inclusive)
 8    sub_data must have a length between 0 and 14
 9    """
10    assert 0 <= command <= 255, "Invalid command, must be between 0 and 255"
11    packet = bytearray(16)
12    packet[0] = command
13
14    if sub_data:
15        assert len(sub_data) <= 14, "Sub data must be less than 14 bytes"
16        for i in range(len(sub_data)):
17            packet[i + 1] = sub_data[i]
18
19    packet[-1] = checksum(packet)
20
21    return packet
22
23
24def checksum(packet: bytearray) -> int:
25    """
26    Packet checksum
27
28    Add all the bytes together modulus 255
29    """
30
31    return sum(packet) & 255
def make_packet(command: int, sub_data: bytearray | None = None) -> bytearray:
 2def make_packet(command: int, sub_data: bytearray | None = None) -> bytearray:
 3    """
 4    Make a well formed packet from a command key and optional sub data.
 5
 6    That means ensuring it's 16 bytes long and the last byte is a valid CRC.
 7
 8    command must be between 0 and 255 (inclusive)
 9    sub_data must have a length between 0 and 14
10    """
11    assert 0 <= command <= 255, "Invalid command, must be between 0 and 255"
12    packet = bytearray(16)
13    packet[0] = command
14
15    if sub_data:
16        assert len(sub_data) <= 14, "Sub data must be less than 14 bytes"
17        for i in range(len(sub_data)):
18            packet[i + 1] = sub_data[i]
19
20    packet[-1] = checksum(packet)
21
22    return packet

Make a well formed packet from a command key and optional sub data.

That means ensuring it's 16 bytes long and the last byte is a valid CRC.

command must be between 0 and 255 (inclusive) sub_data must have a length between 0 and 14

def checksum(packet: bytearray) -> int:
25def checksum(packet: bytearray) -> int:
26    """
27    Packet checksum
28
29    Add all the bytes together modulus 255
30    """
31
32    return sum(packet) & 255

Packet checksum

Add all the bytes together modulus 255