#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Decoder for the HaptiData fabric tactile stream (glove / insole / torso). Wire format, 921,600 bps, 8-bit cells: header AA 55 03 99 (4 bytes, fixed) seq 01 / 02 (1 byte: first or second packet) type 01 LH 02 RH 03 LF 04 RF 05 WB (1 byte, sensor) payload packet 1: 128 bytes -> cells 1..128 packet 2: 128 bytes -> cells 129..256, then 16 bytes IMU (four signed 16-bit quaternion components q0..q3) Nothing here is gated or obfuscated: this file is the whole protocol. Cell indices match the mapping tables published next to this decoder (assets/tactile-map.json). MIT licence. The hardware and its specification belong to HaptiData, Weihai; this decoder is published by HaptiData for anyone integrating the product. Usage: python3 protocol.py stream.bin # decode whole binary capture python3 protocol.py --hex frames.txt # decode a text hex dump """ import json import os import struct import sys HEADER = b"\xAA\x55\x03\x99" SENSOR_TYPES = {0x01: "LH", 0x02: "RH", 0x03: "LF", 0x04: "RF", 0x05: "WB"} PACKET1_LEN = 1 + 1 + 128 # seq + type + 128 cells PACKET2_LEN = 1 + 1 + 128 + 16 # seq + type + 128 cells + quaternion def _frame_len(seq): return PACKET1_LEN if seq == 0x01 else PACKET2_LEN def decode_frames(buf): """Yield one dict per complete frame: {sensor, seq, cells(1-based dict), quat}.""" i = 0 n = len(buf) while True: i = buf.find(HEADER, i) if i < 0 or i + 5 >= n: return seq = buf[i + 4] need = _frame_len(seq) + 4 if seq not in (0x01, 0x02) or i + need > n: i += 1 continue body = buf[i + 4:i + need] stype = body[1] sensor = SENSOR_TYPES.get(stype, "0x%02X" % stype) cells = body[2:2 + 128] offset = 0 if seq == 0x01 else 128 out = { "sensor": sensor, "seq": seq, "cells": {offset + k + 1: cells[k] for k in range(128)}, "quat": None, } if seq == 0x02: raw = body[130:146] out["quat"] = struct.unpack("<4h", raw) yield out i += need def assemble(frames): """Merge packet 1 and packet 2 of one sensor into a 256-cell frame.""" grid = {} quat = None sensor = None for f in frames: sensor = f["sensor"] if sensor is None else sensor grid.update(f["cells"]) if f["quat"] is not None: quat = f["quat"] if not grid: return None full = [grid.get(k, 0) for k in range(1, 257)] return {"sensor": sensor, "cells": full, "quat": quat} def named(cells, mapping, hand): """Map a 256-cell list to the named regions of one hand.""" m = mapping[hand] out = {"finger_pressure": {}, "finger_bend": {}} for finger, idx in m["finger_pressure"].items(): out["finger_pressure"][finger] = [cells[i - 1] for i in idx] for finger, idx in m["finger_bend"].items(): out["finger_bend"][finger] = cells[idx - 1] out["palm"] = [cells[i - 1] for i in m["palm"]] return out def load_hexdump(path): text = open(path, encoding="utf-8", errors="ignore").read() tokens = text.replace("\n", " ").replace(",", " ").split() return bytes(int(t, 16) for t in tokens if len(t) in (2, 4, 8) or t.lower().startswith("0x")) def main(argv): if len(argv) < 2: print(__doc__) return 1 if argv[1] == "--hex": buf = load_hexdump(argv[2]) else: buf = open(argv[1], "rb").read() frames = list(decode_frames(buf)) if not frames: print("no complete frame found (%d bytes scanned)" % len(buf)) return 1 frame = assemble(frames) print("sensor %s, %d bytes, %d frames" % (frame["sensor"], len(buf), len(frames))) cells = frame["cells"] grid = [cells[i:i + 16] for i in range(0, 256, 16)] for row in grid: print(" ".join("%3d" % v for v in row)) if frame["quat"]: print("quaternion (raw int16):", frame["quat"]) here = os.path.dirname(os.path.abspath(__file__)) mapping_path = os.path.join(here, "tactile-map.json") if os.path.exists(mapping_path) and frame["sensor"] in ("LH", "RH"): mapping = json.load(open(mapping_path, encoding="utf-8")) hand = "left" if frame["sensor"] == "LH" else "right" print(json.dumps(named(cells, mapping, hand), ensure_ascii=False, indent=2)) return 0 if __name__ == "__main__": sys.exit(main(sys.argv))