prod2u_status.py 7.1 KB

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