| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- """Status decoding for the two Prod2U products matching the single frame protocol.
- GET_DATA:
- - 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
- - 6400 V1: 83 bytes (PTC0..23, Front0..3, Rear0..3, BMC0..23, CT0..26)
- - MRDIMM (v3.3.0): 65 bytes (0..62 unchanged + 63 hood_switch + 64 buzzer_state); backward-compat with 63 bytes
- DIAGNOSTIC (0x52):
- - 6400 V2: 20 bytes (format 1, board_id 0x64)
- - MRDIMM: 8 bytes (format 2, board_id 0x80)
- """
- from __future__ import annotations
- from typing import Any
- TEMPERATURE_INVALID = 0xFF
- def decode_temperature(byte: int) -> int | None:
- value = int(byte) & 0xFF
- return None if value == TEMPERATURE_INVALID else value
- def state_6400(payload: bytes | bytearray, layout: dict[str, Any] | None = None) -> dict[str, Any]:
- p = bytes(payload)
- if len(p) == 94:
- ptc = list(p[0:35])
- front = list(p[35:39])
- rear = list(p[39:43])
- bmc_raw = list(p[43:67])
- ct_raw = list(p[67:94])
- elif len(p) in (83, 86, 88, 90):
- ptc = list(p[0:24])
- front = list(p[24:28])
- rear = list(p[28:32])
- bmc_raw = list(p[32:56])
- ct_raw = list(p[56:83])
- else:
- raise ValueError(f"6400 GET_DATA payload length must be 83, 86, 88, 90 or 94, got {len(p)}")
- bmc_temps = [decode_temperature(b) for b in bmc_raw]
- ct_temps = [decode_temperature(b) for b in ct_raw]
- is_v2 = len(p) in (86, 88, 90)
- ssr = list(p[83:86]) if is_v2 else []
- if len(p) == 90:
- estop_btn = bool(p[86] & 1)
- hood_open_btn = bool(p[87] & 1)
- hood_close_btn = bool(p[88] & 1)
- buzzer = bool(p[89] & 1)
- hood_switch = bool(hood_open_btn or hood_close_btn)
- elif len(p) == 88:
- estop_btn = False
- hood_open_btn = False
- hood_close_btn = False
- hood_switch = bool(p[86] & 1)
- buzzer = bool(p[87] & 1)
- else:
- estop_btn = False
- hood_open_btn = False
- hood_close_btn = False
- hood_switch = False
- buzzer = False
- return {
- "product": "6400-v2" if is_v2 else "6400-front-rear",
- "ptc_applied": ptc,
- "front_applied": front,
- "rear_applied": rear,
- "ptc": ptc,
- "front": front,
- "rear": rear,
- "ssr": ssr,
- "ssr_applied": ssr,
- "estop_btn": estop_btn,
- "hood_open_btn": hood_open_btn,
- "hood_close_btn": hood_close_btn,
- "hood_switch": hood_switch,
- "buzzer": buzzer,
- "buzzer_state": buzzer,
- "buzzer_applied": buzzer,
- "bmc": {
- "raw": bmc_raw,
- "temperature_c": bmc_temps,
- "valid": [t is not None for t in bmc_temps],
- },
- "ct": {
- "raw": ct_raw,
- "temperature_c": ct_temps,
- "valid": [t is not None for t in ct_temps],
- },
- }
- def mrdimm_state(payload: bytes | bytearray, layout: dict[str, Any] | None = None) -> dict[str, Any]:
- p = bytes(payload)
- if len(p) not in (63, 65):
- raise ValueError(f"MRDIMM GET_DATA payload length must be 63 or 65, got {len(p)}")
- fan = list(p[0:4])
- ptc = list(p[4:8])
- bmc_raw = list(p[8:32])
- bmc_temps = [decode_temperature(b) for b in bmc_raw]
- ct_raw = list(p[32:59])
- ct_temps = [decode_temperature(b) for b in ct_raw]
- lower_mask = int.from_bytes(p[59:61], "little")
- upper_mask = int.from_bytes(p[61:63], "little")
- hood_switch = bool(p[63] & 1) if len(p) == 65 else False
- buzzer = bool(p[64] & 1) if len(p) == 65 else False
- axes = {}
- for i in range(12):
- axes[f"axis{i}"] = {
- "index": i,
- "lower_limit": bool(lower_mask & (1 << i)),
- "upper_limit": bool(upper_mask & (1 << i)),
- }
- return {
- "product": "mrdimm-chamber",
- "fan_applied": fan,
- "ptc_applied": ptc,
- "fan": fan,
- "ptc": ptc,
- "lower_limit_mask": lower_mask,
- "upper_limit_mask": upper_mask,
- "hood_switch": hood_switch,
- "buzzer": buzzer,
- "buzzer_state": buzzer,
- "buzzer_applied": buzzer,
- "axes": axes,
- "bmc": {
- "raw": bmc_raw,
- "temperature_c": bmc_temps,
- "valid": [t is not None for t in bmc_temps],
- },
- "ct": {
- "raw": ct_raw,
- "temperature_c": ct_temps,
- "valid": [t is not None for t in ct_temps],
- },
- }
- def diagnostic_6400_v2(payload: bytes | bytearray) -> dict[str, Any]:
- """Decode diagnostic format 1 (6400 V2.0, 20 bytes); retain unknown formats as raw evidence."""
- p = bytes(payload)
- if len(p) != 20:
- raise ValueError(f"6400 V2 DIAGNOSTIC payload length must be 20, got {len(p)}")
- result = {"format": p[0], "board_id": p[1], "raw_hex": p.hex(" ").upper(),
- "supported": p[0] == 1 and p[1] == 0x64}
- if not result["supported"]:
- return result
- error_names = {0: "SUCCESS", 1: "WRITE_ADDRESS_NACK", 2: "REGISTER_NACK",
- 3: "READ_ADDRESS_NACK", 4: "TIMEOUT", 5: "STUCK_LOW", 6: "ARBITRATION"}
- result.update(
- firmware_version=".".join(str(v) for v in p[2:5]),
- scl_high=bool(p[5] & 1), sda_high=bool(p[5] & 2),
- scl_drive_low=bool(p[5] & 4), sda_drive_low=bool(p[5] & 8),
- i2c_busy=bool(p[5] & 16), sample_seen=bool(p[5] & 32),
- scan_state=p[6] >> 6, current_channel=p[6] & 31,
- last_channel=p[7], device_address=p[8], register=p[9],
- error=p[10], error_name=error_names.get(p[10], "UNKNOWN"),
- is_temperature=p[7] <= 8 and p[9] <= 8,
- raw_register=int.from_bytes(p[11:13], "little"),
- raw_temperature=int.from_bytes(p[11:13], "little"),
- temperature_c=decode_temperature(p[13]),
- valid_temperature_mask=int.from_bytes(p[14:16], "little") & 0x1FF,
- scan_round=p[16], observed_result_mask=p[17], bmc_valid_count=p[18],
- bmc_failed=bool(p[19] & 1), bmc_framing_error_seen=bool(p[19] & 2),
- temperature_queue_overflow=bool(p[19] & 4),
- watchdog_expired=bool(p[19] & 8), duty_bank_valid=bool(p[19] & 16),
- buzzer_on=bool(p[19] & 32),
- )
- return result
- def diagnostic_mrdimm(payload: bytes | bytearray) -> dict[str, Any]:
- """Decode lean MRDIMM diagnostic format 2 (8 bytes); retain unknown formats as raw evidence."""
- p = bytes(payload)
- if len(p) != 8:
- raise ValueError(f"MRDIMM DIAGNOSTIC payload length must be 8, got {len(p)}")
- result = {"format": p[0], "board_id": p[1], "raw_hex": p.hex(" ").upper(),
- "supported": p[0] == 2 and p[1] == 0x80}
- if not result["supported"]:
- return result
- active_mask = int.from_bytes(p[6:8], "little") & 0x0FFF
- result.update(
- firmware_version=".".join(str(v) for v in p[2:5]),
- motor_any_active=bool(p[5] & 1),
- duty_bank_valid=bool(p[5] & 2),
- watchdog_expired=bool(p[5] & 4),
- buzzer_on=bool(p[5] & 8),
- motor_active_mask=active_mask,
- active_axis_mask=active_mask,
- active_axes=[i for i in range(12) if active_mask & (1 << i)],
- )
- return result
|