#include "haptics.h" #include "bonds.h" #include "config.h" #include "rf_link.h" #include // USBDevice.suspended() -> autonomous controller power-off on host sleep #include #include uint8_t g_relayOp = 0xE3; // E3 poll // relay sub-TLV TYPE byte. CONFIRMED from a real puck<->controller sniff: the puck wraps every host command as // E3 + [tlvlen][01][rid][innerlen][data] -- type 0x01 (same type as the GET poll's [01][45]; the rid selects // the action). The old 0x05 was wrong and, with the dropped innerlen byte, every relayed command was discarded. uint8_t g_relaySub = 0x01; volatile uint8_t g_testHaptic = 0; volatile uint8_t g_hapticStop = 0; unsigned long g_buzzFloodUntil = 0; // 10Hz re-init flood active while millis() < this; armed boot/connect/panel void hapticArmBuzzFlood(){ g_buzzFloodUntil = millis() + HAPTIC_FLOOD_MS; } // Controller power-off. CONFIRMED from a real Windows USB capture of the Valve puck (shutoff.pcapng): Steam's // "turn off controller" is the single feature-0x01 command 0x9F with payload ASCII "off!" (6F 66 66 21). The // PROTEUS dongle forwards host feature reports to the controller verbatim as SET_FR (esb.json host_usb_bridge), // so the controller acts on report 0x9F directly -- we relay it the same way (E3 SET sub-TLV). The wire relay is // NO-ACK, so send a small burst: a single lost frame must not leave the controller on. void hapticSendShutdown(){ static const uint8_t OFF[4] = {0x6f,0x66,0x66,0x21}; // "off!" for (uint8_t i=0; i arm a clear once it goes idle (catches a // latch that engaged during/after use, even seconds after connect) // ---- relay ring: multi-producer (USB ISR + loop-context console/xinput), single consumer (poll flush) ---- // Producers serialize through a brief PRIMASK critical section (copy is <=62 bytes); the consumer only ever // touches the tail entry, which no producer writes while the ring isn't full -- so a flush can't be torn. struct RelayMsg { uint8_t rid, len; uint8_t data[RELAY_MAXP]; }; #define RELAY_QLEN 32 // deep enough to hold a full Steam settings/LED transaction burst without loss static RelayMsg g_rq[RELAY_QLEN]; static volatile uint8_t g_rqHead = 0, g_rqTail = 0; // head=next write, tail=next read; empty when equal static inline uint8_t rqNext(uint8_t i){ return (uint8_t)((i+1)%RELAY_QLEN); } bool relayPending(){ return g_rqHead != g_rqTail; } bool relayEnqueue(uint8_t rid, const uint8_t* payload, uint8_t plen){ if (plen > RELAY_MAXP) plen = RELAY_MAXP; // 60B is the RF frame ceiling; longer can't be relayed uint32_t pm = __get_PRIMASK(); __disable_irq(); uint8_t h = g_rqHead, nx = rqNext(h); // Full -> evict the OLDEST entry, never the newest. Steam sends commands as bursts where the meaningful // frame comes LAST (a settings transaction ends with its commit; a haptic stream ends with its stop). The // old drop-newest policy threw away exactly those, so LED commits and haptic stops were the casualties -- // brightness never applied and a glide's final stop never reached the controller (stuck buzz). Dropping the // oldest instead guarantees the most recent command always lands. if (nx == g_rqTail) g_rqTail = rqNext(g_rqTail); g_rq[h].rid = rid; g_rq[h].len = plen; if (plen) memcpy(g_rq[h].data, payload, plen); g_rqHead = nx; // Any haptic relay (Steam OR Xbox rumble OR test) arms the idle-clear and refreshes its timer -- so the // during-use buzz gets cleared in EVERY USB mode, not just Steam. The re-init's own 0x81/0x87 don't match. if (rid==0x82){ g_haptic82Ms = millis(); g_hapClearArmed = true; } __set_PRIMASK(pm); return true; } #if OPK_LOG // diagnostic capture: a ring of the last OUTPUT reports Steam sends (rid/slot/bytes/ms), dumped with 'H'. // Reproduce the whine, press Steam to stop it, then 'H' to see the ON stream + the exact OFF frame Steam sends. struct HapLog { uint32_t ms; uint8_t slot, rid, n, b[16]; }; // 16 payload bytes: capture full 0x87 settings frames // Big always-on ring: log EVERYTHING from boot (Steam writes, our TX-to-controller, link edges) so a rare // reconnect-buzz can be caught after the fact -- the trigger happens moments after the puck boots, while this // RAM is fresh, and we dump it once the panel reconnects. 4096 * 20B ~= 80KB (we have plenty of free RAM). #define HAPLOG_N 4096 static HapLog g_hapLog[HAPLOG_N]; static uint16_t g_hapHead = 0; static uint16_t g_hapTail = 0; // live/dump drain cursor (loop-context reader; chases g_hapHead) void hapLogAdd(uint8_t slot, uint8_t rid, const uint8_t* b, uint16_t n){ // Written from the USB SET ISR (handleSet) AND loop context (relay flush / link edges) -> guard g_hapHead. // Special slot markers for the diagnostic capture: 0xFE = a frame WE transmitted to the controller (TX // relay); 0xFD = a link state edge (b[0]=1 up, 0 down). Real Steam writes use the interface index (0..3). uint32_t pm = __get_PRIMASK(); __disable_irq(); HapLog &e = g_hapLog[g_hapHead]; e.ms = millis(); e.slot = slot; e.rid = rid; e.n = (uint8_t)(n>255?255:n); for(int i=0;i<16;i++) e.b[i] = (i<(int)n) ? b[i] : 0; g_hapHead = (uint8_t)((g_hapHead+1) % (sizeof g_hapLog / sizeof g_hapLog[0])); __set_PRIMASK(pm); } void hapticDumpLog(){ // dump the captured history (oldest->newest) over CDC for the haptic-whine hunt const uint16_t N=HAPLOG_N; uint32_t now=millis(); Serial.printf("# --- capture history (now=%lu, connSlot=%d) ---\n",(unsigned long)now,g_connSlot); for(uint16_t i=0;i16)?16:e.n; memcpy(bytes16,e.b,16); return true; } return false; } #endif // OPK_LOG bool hapticLinkUp(){ return g_connSlot>=0 && (millis()-g_connReplyMs) < 300; } bool haptic82Blocked(){ return !hapticLinkUp() || (g_hapticBlockUntil && (int32_t)(millis()-g_hapticBlockUntil) < 0); } bool hapticRelaySlotOk(int slot){ return g_connSlot>=0 && slot==g_connSlot; } static bool haptic82PayloadOn(const uint8_t* p, uint16_t n){ if(n<3) return false; for(uint16_t i=2;i skip, take the next uint8_t rl = m.len; if(rl > RELAY_MAXP) rl = RELAY_MAXP; // On-air sub-TLV format -- CONFIRMED from real puck<->controller sniffs (puck_sniffer). The TYPE byte and // framing differ by report CLASS: // * actuators / haptics 0x80-0x86 -> E3 [1+rl][05][rid][data] (type 05, NO inner-len) // e.g. haptic on = e3 04 05 82 01 01 f7 // * config / settings / LED 0x87+ -> E3 [2+rl][01][rid][innerlen][data] (type 01, KEEPS the len byte) // e.g. brightness = e3 05 01 87 03 2d 63 00 , LED = e3 12 01 c1 10 ... // (Earlier OpenPuck sent type 05 + no inner-len for EVERYTHING: haptics worked but every 0x87+ command was // discarded -- the controller read data[0] as the length. A later all-01 attempt fixed those but broke the // 0x82 haptics. Splitting on 0x87 is what the real puck actually does.) uint8_t p[5+RELAY_MAXP], plen; if (m.rid >= 0x87){ p[0]=g_relayOp; p[1]=(uint8_t)(2+rl); p[2]=0x01; p[3]=m.rid; p[4]=rl; memcpy(p+5, m.data, rl); plen=(uint8_t)(5+rl); } else { p[0]=g_relayOp; p[1]=(uint8_t)(1+rl); p[2]=0x05; p[3]=m.rid; memcpy(p+4, m.data, rl); plen=(uint8_t)(4+rl); } hapLogAdd(0xFE, m.rid, m.data, rl); // log what we actually TX to the controller (slot 0xFE) for the buzz hunt g_rqTail = rqNext(g_rqTail); // copied out -> release the slot before the TX // s1 carries a PID distinct from the GET poll that follows (caller cycles it), so the controller's ESB // dedup never mistakes the GET for a retransmit of this relay. 80us RX window: the relay is NO-ACK, the // controller sends nothing back, so don't burn the full ~1.2ms reply window (that was halving the poll // rate during haptics). rfConnTx(ch,s1,p,plen,80); return; // ONE relay per poll cycle (matches the real puck's pacing) } g_rqTail = rqNext(g_rqTail); } } // Haptic-subsystem RE-INIT: the exact sequence Steam sends (captured on hardware) when it (re)takes control, // and which clears a stuck haptic script on the controller -- a 0x81 reset action plus 0x87 writes to the // haptic registers (30/07/08/31/52, 18/2e/34/35). We replay it to recover from the latched-buzz the // controller falls into across a reconnect. Brightness (0x87 reg 2d) is deliberately OMITTED so we don't // stomp the LED. Enqueued onto the normal relay (drains in the poll cadence). // Buzz-clear, replayed by the 30s FLOOD at 10Hz. 0x81 resets ONLY -- NO 0x87. Flooding the 0x34/0x35=0xffff // haptic amplitude writes at 10Hz is what spiked the connect buzz, so the flood must stay 0x87-free. The // gyro-enabling 0x87 config is sent separately by hapticConnectInit (a few connect shots, not the flood). void hapticReinit(){ static const uint8_t T81A[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00}; static const uint8_t T81B[]={0x01,0x00,0x00,0x00,0x00,0x00,0x00}; relayEnqueue(0x81, nullptr, 0); // reset action (FUN_0001f554) -- Steam sends this first relayEnqueue(0x81, T81A, sizeof T81A); relayEnqueue(0x81, T81B, sizeof T81B); } // Connect-time GYRO-ENABLE + haptic config. The full 0x87 config block (07/08/31/52, 18/2e, 34/35) is what makes // the controller STREAM gyro -- A/B confirmed: with the full block the gyro works; without it the IMU freezes, // and dropping just 34/35 made it INTERMITTENT (so 34/35 is part of the enable). The freeze is tied to connect // (when streaming starts it stays on), so we send this as a few CONNECT SHOTS (HAPTIC_REINIT_SHOTS, ~350ms apart) // -- NOT in the 10Hz/30s flood, because flooding 0x34/0x35=0xffff is the buzz source. A handful of shots at // connect = reliable gyro enable + only a few amplitude writes (minimal buzz). NO 0x30 (any 0x30 freezes gyro). void hapticConnectInit(){ static const uint8_t H30[]={0x07,0x07,0x00,0x08,0x07,0x00,0x31,0x02,0x00,0x52,0x03,0x00}; static const uint8_t H18[]={0x18,0x00,0x00,0x2e,0x00,0x00,0x34,0xff,0xff,0x35,0xff,0xff,0x34,0xff,0xff}; static const uint8_t H35[]={0x35,0xff,0xff,0x2e,0x00,0x00}; static const uint8_t T81A[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00}; static const uint8_t T81B[]={0x01,0x00,0x00,0x00,0x00,0x00,0x00}; relayEnqueue(0x81, nullptr, 0); relayEnqueue(0x87, H30, sizeof H30); // 07/08/31/52 relayEnqueue(0x87, H18, sizeof H18); // 18/2e/34/35 -- includes the writes that enable gyro streaming relayEnqueue(0x87, H35, sizeof H35); relayEnqueue(0x81, T81A, sizeof T81A); relayEnqueue(0x81, T81B, sizeof T81B); } void hapticInit(){ g_rqHead = g_rqTail = 0; g_haptic82On=false; if(g_buzzFlood) hapticArmBuzzFlood(); // flood the buzz-clear for the first HAPTIC_FLOOD_MS (g_buzzFlood: default ON) g_hapticBlockUntil = millis() + HAPTIC_RECONNECT_BLOCK_MS; // boot: block stale Steam 0x82 until link stable // NO fabricated stop burst. USB capture proves Steam only ever sends 0x82 [01 01 f7] pulses -- never a // zero-gain [01 01 00] "stop". Injecting our invented stop frame (which the real puck never sends) at // boot/connect is unfaithful and, given the controller's "haptic script already active -> ignore" behavior, // a likelier cause of the connect-time buzz than a cure (the buzz happened WITH this burst in place). Be a // pure pass-through of Steam's haptics, like the real puck. g_hapticStop = 0; } // Arm the post-(re)connect haptic block + schedule the clearing re-init. Called from rf_link the moment a // controller reply arrives after a gap (the reliable reconnect signal), and as a backup on hapticTask's // link-up edge. Idempotent -- safe to call repeatedly. void hapticOnReconnect(){ g_hapticBlockUntil = millis() + HAPTIC_RECONNECT_BLOCK_MS; // no haptics relayed for the next 3s g_haptic82On = false; hapticCancelPendingOn(); // drop any haptic ON queued before the link came up // Proactive haptic re-init across the connect window: first shot ~200ms after the link is up, then every // HAPTIC_REINIT_GAP_MS for HAPTIC_REINIT_SHOTS. NOTE: NO 0x30 write anywhere (hapticReinit omits it) -- any // 0x30 write freezes the gyro, so the re-init must stay 0x30-free to keep the IMU streaming. g_reinitAt = millis() + 200u; g_reinitLeft = HAPTIC_REINIT_SHOTS; // PRIMARY buzz mitigation (g_buzzFlood, default ON): also hammer the buzz-clear re-init at 10Hz for 30s. if(g_buzzFlood) hapticArmBuzzFlood(); uint8_t mk=2; hapLogAdd(0xFD, 0xEE, &mk, 1); // capture marker: RECONNECT detected (block+init armed) } void hapticTask(){ static bool wasHapticLinkUp=false; bool up=hapticLinkUp(); // Link-edge markers are diagnostic only now -- the block/re-init is armed reliably from rf_link // (hapticOnReconnect) on the first reply after a gap, which fires even when this 300ms edge doesn't. if(up && !wasHapticLinkUp){ uint8_t mk=1; hapLogAdd(0xFD,0xEE,&mk,1); hapticOnReconnect(); } if(!up && wasHapticLinkUp){ uint8_t mk=0; hapLogAdd(0xFD,0xEE,&mk,1); } wasHapticLinkUp=up; // HAPTIC-ENGINE INJECTION (connect config + buzz flood + idle re-init) runs ONLY in puck/Steam mode (!g_xbox). // In the EMULATED controller modes (Xbox/Switch/HORI/PS5/DS4) there is no Steam driving haptics, and writing // the controller's 0x81/0x87 config registers there is what was FREEZING the gyro -- the RF comms for emulated // modes must stay a pure poll+transform, leaving the controller's own (default-on) IMU streaming untouched. // Real 0x82 rumble (e.g. Xbox) is relayed separately via relayEnqueue and is NOT gated here. if(!g_xbox){ if(g_reinitAt && up && (int32_t)(millis()-g_reinitAt) >= 0){ // connect SHOTS: full gyro-enable config (not the flood) hapticConnectInit(); g_reinitAt = (g_reinitLeft && --g_reinitLeft) ? (millis()+HAPTIC_REINIT_GAP_MS) : 0; } // Buzz-clear FLOOD (g_buzzFlood, default ON): replay the 0x81 re-init at 10Hz across the armed window. static unsigned long floodMs=0; if (g_buzzFlood && (int32_t)(g_buzzFloodUntil - millis()) > 0 && up && millis()-floodMs >= HAPTIC_FLOOD_GAP_MS){ floodMs=millis(); hapticReinit(); } } else { g_reinitAt = 0; // emulated modes: never inject -- just poll + transform } // Controller power-off on host SLEEP: send the power-off command (0x9F "off!") the instant the USB bus // suspends, like the real puck. BUT only when USB power (VBUS) is still present -- i.e. a genuine host sleep, // NOT a cable unplug. Pulling the dongle ALSO trips the suspend edge (in the brief window it runs on residual // power), and we must NOT kill the controller then; the controller should only power off on a shutdown // chord/command or real host sleep. VBUSDETECT is 1 while the cable still delivers 5V, 0 once unplugged. // wasSusp starts true so a boot-into-suspended state never false-fires. static bool wasSusp=true; bool susp=USBDevice.suspended(); bool vbus = (NRF_POWER->USBREGSTATUS & POWER_USBREGSTATUS_VBUSDETECT_Msk) != 0; if (susp && !wasSusp && vbus) hapticSendShutdown(); wasSusp=susp; // Steam-mode: host went quiet -> mark the 0x82 stream inactive. Do NOT synthesize a stop: trackpad haptics // are one-shot pulses, so firing a 0x82-zero ~HAPTIC_QUIET_MS after a swipe ends is the extra end-of-movement // click the real puck doesn't make. Steam forwards its own stop for any sustained haptic. if (!g_xbox && g_haptic82On && millis()-g_haptic82Ms > HAPTIC_QUIET_MS) g_haptic82On=false; // OPT-IN (g_buzzFlood): after haptic activity goes idle, fire one re-init to clear any latch it left behind. // Off by default along with the flood; the connect config is the primary defense. Always disarm so it doesn't // accumulate while disabled. if (g_hapClearArmed && (millis()-g_haptic82Ms) > HAPTIC_CLEAR_IDLE_MS){ g_hapClearArmed=false; if(!g_xbox && g_buzzFlood) hapticReinit(); } }