hth6400.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from dataclasses import dataclass
  2. SLAVE_ADDRESS = 0x2A
  3. REG_COMMAND = 0x0010
  4. REG_STAGING_BASE = 0x0100
  5. REG_SNAPSHOT_BASE = 0x0200
  6. REG_DEVICE_STATUS = 0x0000
  7. REG_RESPONSE_OK = 0x0001
  8. REG_RESPONSE_COMMAND = 0x0002
  9. REG_RESPONSE_STATUS = 0x0003
  10. REG_RESPONSE_DETAIL = 0x0004
  11. REG_WATCHDOG_REMAINING_SECONDS = 0x0005
  12. REG_SET_ALL_BYTES_WRITTEN = 0x0006
  13. REG_SNAPSHOT_LENGTH_L = 0x0007
  14. REG_SNAPSHOT_LENGTH_H = 0x0008
  15. ACK_STATUS = {
  16. 0: 'OK', 1: 'UNSUPPORTED', 2: 'BAD_LENGTH',
  17. 3: 'BAD_VALUE', 4: 'SAFETY_LOCKED', 5: 'BUSY',
  18. }
  19. def crc16(data: bytes) -> int:
  20. value = 0xFFFF
  21. for byte in data:
  22. value ^= byte << 8
  23. for _ in range(8):
  24. value = ((value << 1) ^ 0x1021) & 0xFFFF if value & 0x8000 else (value << 1) & 0xFFFF
  25. return value
  26. def uart_frame(frame_type: int, payload: bytes = b'') -> bytes:
  27. body = bytes((frame_type, len(payload))) + payload
  28. return b'\xA5\x5A' + body + crc16(body).to_bytes(2, 'little')
  29. def decode_frame(raw: bytes) -> tuple[int, bytes]:
  30. if len(raw) < 6 or raw[:2] != b'\xA5\x5A':
  31. raise ValueError('bad A5 5A header or short frame')
  32. if len(raw) != raw[3] + 6:
  33. raise ValueError('UART frame length mismatch')
  34. if int.from_bytes(raw[-2:], 'little') != crc16(raw[2:-2]):
  35. raise ValueError('CRC-16/CCITT-FALSE mismatch')
  36. return raw[2], raw[4:-2]
  37. def decode_ack_nack(raw: bytes) -> dict[str, int | bool | str]:
  38. kind, payload = decode_frame(raw)
  39. if kind != 0x06 or len(payload) != 4:
  40. raise ValueError('ACK/NACK requires type 0x06 and four payload bytes')
  41. ok, request, status, detail = payload
  42. return {'ok': bool(ok), 'request': request, 'status': status,
  43. 'status_name': ACK_STATUS.get(status, 'UNKNOWN'), 'detail': detail}
  44. def decode_snapshot(raw: bytes, expected_length: int | None = None) -> bytes:
  45. kind, payload = decode_frame(raw)
  46. if kind != 0x82:
  47. raise ValueError('SNAPSHOT_DATA requires type 0x82')
  48. if expected_length is not None and len(payload) != expected_length:
  49. raise ValueError('SNAPSHOT_DATA length mismatch')
  50. return payload
  51. @dataclass(frozen=True)
  52. class SetAll:
  53. ptc: tuple[int, ...]
  54. front: tuple[int, ...]
  55. rear: tuple[int, ...]
  56. def payload(self) -> bytes:
  57. values = (*self.ptc, *self.front, *self.rear)
  58. if len(values) != 32 or any(not 0 <= x <= 100 for x in values):
  59. raise ValueError('SET_ALL requires 24 PTC + 4 Front + 4 Rear values in 0..100')
  60. return bytes(values)
  61. def set_all_uart(value: SetAll) -> bytes:
  62. return uart_frame(0x01, value.payload())
  63. def set_all_i2c(value: SetAll) -> list[tuple[int, bytes, int]]:
  64. payload = value.payload()
  65. staging = bytes((REG_STAGING_BASE >> 8, REG_STAGING_BASE & 0xFF)) + payload
  66. command = bytes((REG_COMMAND >> 8, REG_COMMAND & 0xFF, 0x01))
  67. return [(SLAVE_ADDRESS, staging, 0), (SLAVE_ADDRESS, command, 0)]
  68. def get_snapshot_i2c() -> tuple[int, bytes, int]:
  69. return SLAVE_ADDRESS, bytes((REG_COMMAND >> 8, REG_COMMAND & 0xFF, 0x02)), 0
  70. def snapshot_reads(length: int, chunk: int = 64) -> list[tuple[int, bytes, int]]:
  71. if length < 0 or chunk <= 0: raise ValueError('invalid length/chunk')
  72. 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)]