| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- from dataclasses import dataclass
- SLAVE_ADDRESS = 0x2A
- REG_COMMAND = 0x0010
- REG_STAGING_BASE = 0x0100
- REG_SNAPSHOT_BASE = 0x0200
- REG_DEVICE_STATUS = 0x0000
- REG_RESPONSE_OK = 0x0001
- REG_RESPONSE_COMMAND = 0x0002
- REG_RESPONSE_STATUS = 0x0003
- REG_RESPONSE_DETAIL = 0x0004
- REG_WATCHDOG_REMAINING_SECONDS = 0x0005
- REG_SET_ALL_BYTES_WRITTEN = 0x0006
- REG_SNAPSHOT_LENGTH_L = 0x0007
- REG_SNAPSHOT_LENGTH_H = 0x0008
- ACK_STATUS = {
- 0: 'OK', 1: 'UNSUPPORTED', 2: 'BAD_LENGTH',
- 3: 'BAD_VALUE', 4: 'SAFETY_LOCKED', 5: 'BUSY',
- }
- def crc16(data: bytes) -> int:
- value = 0xFFFF
- for byte in data:
- value ^= byte << 8
- for _ in range(8):
- value = ((value << 1) ^ 0x1021) & 0xFFFF if value & 0x8000 else (value << 1) & 0xFFFF
- return value
- def uart_frame(frame_type: int, payload: bytes = b'') -> bytes:
- body = bytes((frame_type, len(payload))) + payload
- return b'\xA5\x5A' + body + crc16(body).to_bytes(2, 'little')
- def decode_frame(raw: bytes) -> tuple[int, bytes]:
- if len(raw) < 6 or raw[:2] != b'\xA5\x5A':
- raise ValueError('bad A5 5A header or short frame')
- if len(raw) != raw[3] + 6:
- raise ValueError('UART frame length mismatch')
- if int.from_bytes(raw[-2:], 'little') != crc16(raw[2:-2]):
- raise ValueError('CRC-16/CCITT-FALSE mismatch')
- return raw[2], raw[4:-2]
- def decode_ack_nack(raw: bytes) -> dict[str, int | bool | str]:
- kind, payload = decode_frame(raw)
- if kind != 0x06 or len(payload) != 4:
- raise ValueError('ACK/NACK requires type 0x06 and four payload bytes')
- ok, request, status, detail = payload
- return {'ok': bool(ok), 'request': request, 'status': status,
- 'status_name': ACK_STATUS.get(status, 'UNKNOWN'), 'detail': detail}
- def decode_snapshot(raw: bytes, expected_length: int | None = None) -> bytes:
- kind, payload = decode_frame(raw)
- if kind != 0x82:
- raise ValueError('SNAPSHOT_DATA requires type 0x82')
- if expected_length is not None and len(payload) != expected_length:
- raise ValueError('SNAPSHOT_DATA length mismatch')
- return payload
- @dataclass(frozen=True)
- class SetAll:
- ptc: tuple[int, ...]
- front: tuple[int, ...]
- rear: tuple[int, ...]
- def payload(self) -> bytes:
- values = (*self.ptc, *self.front, *self.rear)
- if len(values) != 32 or any(not 0 <= x <= 100 for x in values):
- raise ValueError('SET_ALL requires 24 PTC + 4 Front + 4 Rear values in 0..100')
- return bytes(values)
- def set_all_uart(value: SetAll) -> bytes:
- return uart_frame(0x01, value.payload())
- def set_all_i2c(value: SetAll) -> list[tuple[int, bytes, int]]:
- payload = value.payload()
- staging = bytes((REG_STAGING_BASE >> 8, REG_STAGING_BASE & 0xFF)) + payload
- command = bytes((REG_COMMAND >> 8, REG_COMMAND & 0xFF, 0x01))
- return [(SLAVE_ADDRESS, staging, 0), (SLAVE_ADDRESS, command, 0)]
- def get_snapshot_i2c() -> tuple[int, bytes, int]:
- return SLAVE_ADDRESS, bytes((REG_COMMAND >> 8, REG_COMMAND & 0xFF, 0x02)), 0
- def snapshot_reads(length: int, chunk: int = 64) -> list[tuple[int, bytes, int]]:
- if length < 0 or chunk <= 0: raise ValueError('invalid length/chunk')
- return [(SLAVE_ADDRESS, bytes(((REG_SNAPSHOT_BASE + off) >> 8, (REG_SNAPSHOT_BASE + off) & 0xFF)), min(chunk, length - off)) for off in range(0, length, chunk)]
|