def packet_read(self): valid = False picommon.spi_select_device(self.device_port) data = self.read_bytes(1) if (data[0] == self.STX): data.extend(self.read_bytes(2)) size = data[1] type = data[2] data.extend(self.read_bytes(size - 3)) checksum = sum(data) & 0xFF if (checksum == 0): data = data[3:(size - 1)] valid = True if not valid: data = [] return valid, type, data
def packet_write(self, type, data): msg = [2, len(data) + 4, type] + data checksum = (-(sum(msg) & 0xFF)) & 0xFF msg.append(checksum) picommon.spi_select_device(self.device_port) picommon.spi.xfer2(msg)