prod2u_status.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. """Status decoding for the two Prod2U products matching the single frame protocol.
  2. GET_DATA:
  3. - 6400 V2 (v3.1.0): 88 bytes (0..85 unchanged + 86 hood_switch + 87 buzzer_state); backward-compat with 86 bytes
  4. - 6400 V1: 83 bytes (PTC0..23, Front0..3, Rear0..3, BMC0..23, CT0..26)
  5. - MRDIMM (v3.1.0): 65 bytes (0..62 unchanged + 63 hood_switch + 64 buzzer_state); backward-compat with 63 bytes
  6. DIAGNOSTIC (0x52):
  7. - 6400 V2: 20 bytes (format 1, board_id 0x64)
  8. - MRDIMM: 8 bytes (format 2, board_id 0x80)
  9. """
  10. from __future__ import annotations
  11. from typing import Any
  12. TEMPERATURE_INVALID = 0xFF
  13. def decode_temperature(byte: int) -> int | None:
  14. value = int(byte) & 0xFF
  15. return None if value == TEMPERATURE_INVALID else value
  16. def state_6400(payload: bytes | bytearray, layout: dict[str, Any] | None = None) -> dict[str, Any]:
  17. p = bytes(payload)
  18. if len(p) == 94:
  19. ptc = list(p[0:35])
  20. front = list(p[35:39])
  21. rear = list(p[39:43])
  22. bmc_raw = list(p[43:67])
  23. ct_raw = list(p[67:94])
  24. elif len(p) in (83, 86, 88):
  25. ptc = list(p[0:24])
  26. front = list(p[24:28])
  27. rear = list(p[28:32])
  28. bmc_raw = list(p[32:56])
  29. ct_raw = list(p[56:83])
  30. else:
  31. raise ValueError(f"6400 GET_DATA payload length must be 83, 86, 88 or 94, got {len(p)}")
  32. bmc_temps = [decode_temperature(b) for b in bmc_raw]
  33. ct_temps = [decode_temperature(b) for b in ct_raw]
  34. is_v2 = len(p) in (86, 88)
  35. ssr = list(p[83:86]) if is_v2 else []
  36. hood_switch = bool(p[86] & 1) if len(p) == 88 else False
  37. buzzer = bool(p[87] & 1) if len(p) == 88 else False
  38. return {
  39. "product": "6400-v2" if is_v2 else "6400-front-rear",
  40. "ptc_applied": ptc,
  41. "front_applied": front,
  42. "rear_applied": rear,
  43. "ptc": ptc,
  44. "front": front,
  45. "rear": rear,
  46. "ssr": ssr,
  47. "ssr_applied": ssr,
  48. "hood_switch": hood_switch,
  49. "buzzer": buzzer,
  50. "buzzer_state": buzzer,
  51. "buzzer_applied": buzzer,
  52. "bmc": {
  53. "raw": bmc_raw,
  54. "temperature_c": bmc_temps,
  55. "valid": [t is not None for t in bmc_temps],
  56. },
  57. "ct": {
  58. "raw": ct_raw,
  59. "temperature_c": ct_temps,
  60. "valid": [t is not None for t in ct_temps],
  61. },
  62. }
  63. def mrdimm_state(payload: bytes | bytearray, layout: dict[str, Any] | None = None) -> dict[str, Any]:
  64. p = bytes(payload)
  65. if len(p) not in (63, 65):
  66. raise ValueError(f"MRDIMM GET_DATA payload length must be 63 or 65, got {len(p)}")
  67. fan = list(p[0:4])
  68. ptc = list(p[4:8])
  69. bmc_raw = list(p[8:32])
  70. bmc_temps = [decode_temperature(b) for b in bmc_raw]
  71. ct_raw = list(p[32:59])
  72. ct_temps = [decode_temperature(b) for b in ct_raw]
  73. lower_mask = int.from_bytes(p[59:61], "little")
  74. upper_mask = int.from_bytes(p[61:63], "little")
  75. hood_switch = bool(p[63] & 1) if len(p) == 65 else False
  76. buzzer = bool(p[64] & 1) if len(p) == 65 else False
  77. axes = {}
  78. for i in range(12):
  79. axes[f"axis{i}"] = {
  80. "index": i,
  81. "lower_limit": bool(lower_mask & (1 << i)),
  82. "upper_limit": bool(upper_mask & (1 << i)),
  83. }
  84. return {
  85. "product": "mrdimm-chamber",
  86. "fan_applied": fan,
  87. "ptc_applied": ptc,
  88. "fan": fan,
  89. "ptc": ptc,
  90. "lower_limit_mask": lower_mask,
  91. "upper_limit_mask": upper_mask,
  92. "hood_switch": hood_switch,
  93. "buzzer": buzzer,
  94. "buzzer_state": buzzer,
  95. "buzzer_applied": buzzer,
  96. "axes": axes,
  97. "bmc": {
  98. "raw": bmc_raw,
  99. "temperature_c": bmc_temps,
  100. "valid": [t is not None for t in bmc_temps],
  101. },
  102. "ct": {
  103. "raw": ct_raw,
  104. "temperature_c": ct_temps,
  105. "valid": [t is not None for t in ct_temps],
  106. },
  107. }
  108. def diagnostic_6400_v2(payload: bytes | bytearray) -> dict[str, Any]:
  109. """Decode diagnostic format 1 (6400 V2.0, 20 bytes); retain unknown formats as raw evidence."""
  110. p = bytes(payload)
  111. if len(p) != 20:
  112. raise ValueError(f"6400 V2 DIAGNOSTIC payload length must be 20, got {len(p)}")
  113. result = {"format": p[0], "board_id": p[1], "raw_hex": p.hex(" ").upper(),
  114. "supported": p[0] == 1 and p[1] == 0x64}
  115. if not result["supported"]:
  116. return result
  117. error_names = {0: "SUCCESS", 1: "WRITE_ADDRESS_NACK", 2: "REGISTER_NACK",
  118. 3: "READ_ADDRESS_NACK", 4: "TIMEOUT", 5: "STUCK_LOW", 6: "ARBITRATION"}
  119. result.update(
  120. firmware_version=".".join(str(v) for v in p[2:5]),
  121. scl_high=bool(p[5] & 1), sda_high=bool(p[5] & 2),
  122. scl_drive_low=bool(p[5] & 4), sda_drive_low=bool(p[5] & 8),
  123. i2c_busy=bool(p[5] & 16), sample_seen=bool(p[5] & 32),
  124. scan_state=p[6] >> 6, current_channel=p[6] & 31,
  125. last_channel=p[7], device_address=p[8], register=p[9],
  126. error=p[10], error_name=error_names.get(p[10], "UNKNOWN"),
  127. is_temperature=p[7] <= 8 and p[9] <= 8,
  128. raw_register=int.from_bytes(p[11:13], "little"),
  129. raw_temperature=int.from_bytes(p[11:13], "little"),
  130. temperature_c=decode_temperature(p[13]),
  131. valid_temperature_mask=int.from_bytes(p[14:16], "little") & 0x1FF,
  132. scan_round=p[16], observed_result_mask=p[17], bmc_valid_count=p[18],
  133. bmc_failed=bool(p[19] & 1), bmc_framing_error_seen=bool(p[19] & 2),
  134. temperature_queue_overflow=bool(p[19] & 4),
  135. watchdog_expired=bool(p[19] & 8), duty_bank_valid=bool(p[19] & 16),
  136. buzzer_on=bool(p[19] & 32),
  137. )
  138. return result
  139. def diagnostic_mrdimm(payload: bytes | bytearray) -> dict[str, Any]:
  140. """Decode lean MRDIMM diagnostic format 2 (8 bytes); retain unknown formats as raw evidence."""
  141. p = bytes(payload)
  142. if len(p) != 8:
  143. raise ValueError(f"MRDIMM DIAGNOSTIC payload length must be 8, got {len(p)}")
  144. result = {"format": p[0], "board_id": p[1], "raw_hex": p.hex(" ").upper(),
  145. "supported": p[0] == 2 and p[1] == 0x80}
  146. if not result["supported"]:
  147. return result
  148. active_mask = int.from_bytes(p[6:8], "little") & 0x0FFF
  149. result.update(
  150. firmware_version=".".join(str(v) for v in p[2:5]),
  151. motor_any_active=bool(p[5] & 1),
  152. duty_bank_valid=bool(p[5] & 2),
  153. watchdog_expired=bool(p[5] & 4),
  154. buzzer_on=bool(p[5] & 8),
  155. motor_active_mask=active_mask,
  156. active_axis_mask=active_mask,
  157. active_axes=[i for i in range(12) if active_mask & (1 << i)],
  158. )
  159. return result