"""8BitDo Ultimate 2 Wireless (2.4 GHz dongle, USB 2DC8:310B) config access without the vendor app. Protocol from 8BitDoAdvance.dll (8BitDo Ultimate Software V2 1.30), disassembled. Vendor HID interface (usage page 0xFF7A): output report 0x81, input report 0x02, 64 bytes. request [0x81, 0x04, u16 cmd, u16 arg, u32 len, u32 total, u32 offset, data...] reply [0x02, 0x04, u16 0x0004, u16 cmd, u32 len, u32 total, u32 offset, data...] cmd 1 = write config bytes (max 45 per packet), cmd 2 = read config bytes, cmd 6 arg 0x123 = sent after a write (commit), cmd 0x40 = select lighting theme. The config record is 0x638 (1592) bytes. """ import struct, sys, time import hid VID, PID = 0x2DC8, 0x310B RECORD_LEN = 0x638 CHUNK = 45 class U2: def __init__(self): paths = [d["path"] for d in hid.enumerate(VID, PID) if d["usage_page"] == 0xFF7A] if not paths: raise SystemExit("8BitDo Ultimate 2 dongle not found (controller on and connected?)") self.h = hid.device() self.h.open_path(paths[0]) self.drain() def drain(self): self.h.set_nonblocking(1) while self.h.read(64): pass self.h.set_nonblocking(0) def send(self, cmd, arg=0, ln=0, total=0, off=0, data=b""): pkt = bytes([0x81, 0x04]) + struct.pack("= 18 and r[0] == 2 and r[1] == 4: w0, c, ln, total, off = struct.unpack_from("> 1) ^ 0xA001 if c & 1 else c >> 1 return c SINGLE_OFFSET = 0x50C # + slot * 100: u32 flag, then 24 x u32 0x00RRGGBB THEME_CUSTOM = 4 def write_bytes(u, off, data): pos = 0 while pos < len(data): ch = bytes(data[pos:pos + CHUNK]) u.send(1, 0, len(ch) | (crc16(ch) << 16), RECORD_LEN, off + pos, ch) r = u.reply(1) if not r or r[1] != off + pos: raise IOError(f"write not acknowledged at 0x{off + pos:x}") pos += len(ch) def write_colors(u, colors, slot=0, commit=False): """colors: 24 (r, g, b) tuples.""" data = b"".join(struct.pack("