"""Decode compact binary controller envelopes — v2 native binary, v1 legacy JSON blobs.""" import json import struct BINARY_ENVELOPE_VERSION_1 = 1 BINARY_ENVELOPE_VERSION_2 = 2 HEADER_LEN = 5 def _brightness_0_255_from_wire(wire): w = max(0, min(127, int(wire))) return min(255, (w * 255) // 127) def _decode_preset_record(buf, off): nl = buf[off] off += 1 name = buf[off : off + nl].decode("utf-8") off += nl pl = buf[off] off += 1 pattern = buf[off : off + pl].decode("utf-8") off += pl nc = buf[off] off += 1 colors = [] for _ in range(nc): r, g, b = buf[off], buf[off + 1], buf[off + 2] off += 3 colors.append("#%02x%02x%02x" % (r, g, b)) if off + 16 > len(buf): raise ValueError("truncated") delay, br, auto, n1, n2, n3, n4, n5, n6 = struct.unpack_from( "