haptics.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // haptics.h -- host -> controller relay queue (haptics + settings) + the watchdogs that stop a stuck buzz.
  2. //
  3. // Steam and translated host rumble send OUTPUT/feature reports the real dongle forwards to the controller as
  4. // a SET sub-TLV inside the E3 poll. We do the same: handleSet()/the console enqueue with relayEnqueue(), and
  5. // rfConnFlushRelay() emits ONE entry per poll cycle (rf_link.cpp), never at raw loop rate.
  6. //
  7. // One ring per bond slot: the consumer (rfConnFlushRelay) only drains the current slot's queue, so commands
  8. // addressed to other slots are never consumed out of turn. ISR producers write under PRIMASK; the consumer
  9. // runs in loop context and never races the same slot's head pointer.
  10. //
  11. // The controller's haptic LATCHES until told to stop. If the host's stop is lost over RF (or the link drops
  12. // mid-buzz) the actuator whines forever -- so we send 0x82-zero stop bursts on reconnect, but ONLY when a
  13. // haptic was actually active when the link dropped: every extra 0x82 frame is an audible click on the
  14. // controller (captured), so an unconditional burst on each link-up edge is itself a buzz source on a flapping
  15. // link. The g_hapLog ring captures recent OUTPUT reports for the 'H' dump.
  16. #pragma once
  17. #include <stdint.h>
  18. #include "config.h" // OPK_LOG
  19. #include "bonds.h" // NSLOT
  20. // after this much host silence, consider the current 0x82 haptic stream inactive
  21. #define HAPTIC_QUIET_MS 300u
  22. // Post-(re)connect haptic block default. While armed, ALL Steam haptic relays to that slot are dropped: a
  23. // just-powered-on controller's haptic engine isn't ready, and feeding it haptics in that window leaves it in a
  24. // degraded/latched state (slow/stuck/missing haptics until a re-init). Runtime-adjustable (g_hapticBlockMs) and
  25. // toggleable (g_hapticBlockOn) from the WebUSB panel; this is the boot default.
  26. #define HAPTIC_BLOCK_MS_DEFAULT 10000u
  27. // 0x82-zero relays per stop event (sent at poll cadence -- not loop rate)
  28. #define HAPTIC_STOP_BURST 4u
  29. // max relayed payload bytes per entry: RF frame = [E3][len][05][rid][payload] and MAXLEN=64 -> 60
  30. #define RELAY_MAXP 60u
  31. // Proactive post-(re)connect haptic re-init: this many shots, this far apart, starting ~200ms after the link
  32. // comes up -- covers ~0.2s..3s so the brief controller-side connect buzz gets reset before it can sustain.
  33. #define HAPTIC_REINIT_SHOTS 8u
  34. #define HAPTIC_REINIT_GAP_MS 350u
  35. // After haptic activity, if it's been idle this long, fire one clear-re-init -- kills a latch that engaged
  36. // during use (a buzz that starts seconds after connect and won't self-clear). Long enough not to fire between
  37. // rapid in-game haptics; short enough to clear a stuck buzz soon after the user pauses.
  38. #define HAPTIC_CLEAR_IDLE_MS 1200u
  39. // Controller power-off: hapticSendShutdown() relays Steam's confirmed "turn off controller" command (feature-0x01
  40. // cmd 0x9F, payload "off!" -- captured from the real puck). Sent as a small burst because the RF relay is NO-ACK.
  41. #define HAPTIC_SHUTDOWN_SHOTS 3u
  42. // ---- relay queue (written by puck_hid.cpp, mode_*.cpp, serial_console.cpp; drained by rf_link.cpp) ----
  43. // Enqueue one host->controller report. `slot` = bond slot (0..NSLOT-1) or 0xFF to broadcast to every
  44. // connected controller (used by hapticSendShutdown / hapticReinit / test haptics). ISR-safe (PRIMASK).
  45. bool relayEnqueue(uint8_t rid, const uint8_t *payload, uint8_t plen,
  46. uint8_t slot = 0xFF);
  47. // id9=0 hold (MODE_STEAM only): land the controller's SET_SETTINGS index 9 (digital-mappings/lizard-active)
  48. // at 0, once per LIZKEEP_MS per connected slot, like the real puck. This holds the controller's autonomous
  49. // mapping/haptic engine OFF so it can't latch into the deep-inside buzz seen after repeated reconnects
  50. // (capture-for-haptics.txt: the buzz is controller-internal; OpenPuck relays no haptics in that state). It
  51. // also disables the controller's autonomous touchpad ticks (id9 gates the whole pad layer) -- fine in Steam
  52. // mode (Steam owns haptics), so it is scoped to MODE_STEAM; pure MODE_LIZARD is left alone to keep its ticks.
  53. #define LIZKEEP_MS 2000u
  54. extern uint8_t
  55. g_lizKeep; // 1 = hold on (default, persisted); console 'u' toggles for A/B
  56. // Experiment: land ALL relayed 0x87 SET_SETTINGS verbatim (real-puck relay) instead of the discard-whitelist.
  57. // Default 0 (whitelist). Console "L87" toggles; persisted. See haptics.cpp for the buzz hypothesis it tests.
  58. extern uint8_t g_landAll87;
  59. // Land Steam's amp/haptic-config 0x87 (regs 0x18/0x2E/0x34/0x35, not gyro 0x30) so haptics play as clean
  60. // ticks not a default-amp buzz. On by default; console "AMP" toggles.
  61. extern uint8_t g_landAmp;
  62. // Master enable for the puck->controller haptic relay (Steam 0x80-0x86 rumble/pad-feedback). Console "HR"
  63. // toggles it to isolate the drag-smoothness cost of relaying Steam's trackpad haptics. See haptics.cpp.
  64. extern bool g_hapticRelay;
  65. // Post-connect haptic block (persisted, panel-controlled): when g_hapticBlockOn, Steam haptics are dropped for
  66. // g_hapticBlockMs after a (re)connect so the controller's haptic engine settles before the first real haptic.
  67. extern uint8_t
  68. g_hapticBlockOn; // 1 = block enabled, 0 = relay haptics immediately on connect (default)
  69. extern uint16_t
  70. g_hapticBlockMs; // block duration in ms (default HAPTIC_BLOCK_MS_DEFAULT)
  71. // anything still queued (xinput uses it to pace rumble re-queues)
  72. bool relayPending();
  73. extern uint8_t g_relayOp; // relay frame opcode (E3 poll)
  74. extern uint8_t g_relaySub; // relay sub-TLV type byte = SET
  75. extern volatile uint8_t g_testHaptic; // 't<n>' injects n test haptics
  76. // pending haptic-STOP frames to relay (kill a latched whine)
  77. extern volatile uint8_t g_hapticStop;
  78. // Per-slot block: arm after a (re)connect, drop haptics aimed at the slot for g_hapticBlockMs (when g_hapticBlockOn).
  79. extern unsigned long g_hapticBlockUntil[NSLOT];
  80. // relay the controller power-off (0x9F "off!"), burst x3. Steam's per-interface 0x9F passes that slot so
  81. // only that controller powers off; host-suspend / the panel test button keep the broadcast default (all off).
  82. void hapticSendShutdown(uint8_t slot = 0xFF);
  83. // ---- diagnostic capture (compiled in only when OPK_LOG): a ring of recent host->controller commands +
  84. // link/TX markers, dumped over WebUSB. No-ops in a production build so call sites vanish. ----
  85. #if OPK_LOG
  86. void hapLogAdd(uint8_t slot, uint8_t rid, const uint8_t *b, uint16_t n);
  87. void hapticDumpLog(); // 'H' console dump of the recent OUTPUT-report history
  88. // WebUSB capture drain: resetDrain(true) rewinds to the OLDEST entry (dump the whole ring from boot);
  89. // resetDrain(false) starts at "now" (live only). pull yields each entry once, oldest->newest, skipping empties.
  90. void hapLogResetDrain(bool fromBoot);
  91. bool hapLogPull(uint32_t *logMs, uint8_t *slot, uint8_t *rid, uint8_t *n,
  92. uint8_t bytes16[16]);
  93. #else
  94. static inline void hapLogAdd(uint8_t, uint8_t, const uint8_t *, uint16_t)
  95. {
  96. }
  97. static inline void hapticDumpLog()
  98. {
  99. }
  100. static inline void hapLogResetDrain(bool)
  101. {
  102. }
  103. static inline bool hapLogPull(uint32_t *, uint8_t *, uint8_t *, uint8_t *,
  104. uint8_t *)
  105. {
  106. return false;
  107. }
  108. #endif
  109. bool hapticLinkUp(int slot = -1);
  110. bool haptic82Blocked(int slot = -1);
  111. bool hapticRelaySlotOk(int slot);
  112. void haptic82HostReport(const uint8_t *p, uint16_t n);
  113. // queue a Steam/Triton 0x80 rumble frame. `slot` = bond slot of the originating controller (0..NSLOT-1);
  114. // defaults to 0 for the legacy single-controller callers. Per-slot so each connected controller can have its
  115. // own active rumble stream when the host presents multiple gamepads (e.g. 4 XInput devices).
  116. bool hapticSteamRumble(uint16_t lowFreq, uint16_t highFreq, uint8_t slot = 0);
  117. // queue + flush the pending host/test/stop relay inside the poll cadence (called from rf_link).
  118. // rfConnFlushRelay's s1 must carry a PID distinct from the GET poll that follows it. g_relayPid
  119. // is initialised 2 ahead of g_pollPid and both increment once per cycle, so the 2-bit PIDs stay
  120. // 2 apart (mod 4) forever and never collide — keeping the controller from deduplicating the GET.
  121. // Stability test: when g_stabTest (WebUSB cmd 0x0F), buzz all controllers every 10s to keep them awake for an
  122. // unattended uptime-until-hang measurement. hapticStabTask() is called from loop().
  123. extern bool g_stabTest;
  124. void hapticStabTask();
  125. void rfConnQueueHapticRelay();
  126. // returns true if a relay frame was actually transmitted this call (queue had an entry), so the poll loop can
  127. // count relay TXs separately from poll cycles.
  128. bool rfConnFlushRelay(uint8_t ch, uint8_t s1);
  129. // times a relay-ring drain hit its iteration cap (head/tail desync or corruption) -- non-zero means we caught
  130. // and recovered from what would otherwise be an IRQ-off watchdog hang. Surfaced on the WebUSB panel.
  131. extern volatile uint16_t g_ringFault;
  132. // boot reset: clear relay/active flags, arm the reconnect block
  133. void hapticInit();
  134. // per-loop upkeep: link-edge markers + steam 0x82 quiet timeout + fires the scheduled re-init
  135. void hapticTask();
  136. // replay Steam's haptic-subsystem re-init to the controller -> clears a latched/stuck buzz.
  137. // `slot` defaults to 0xFF (broadcast to all connected) -- the re-init is a settings-only reset and is
  138. // harmless on healthy controllers, so it's worth re-initializing every slot the firmware knows about.
  139. void hapticReinit(uint8_t slot = 0xFF);
  140. // Called from rf_link the instant a controller (re)connect is detected (an F-reply after a gap): blocks haptic
  141. // relays for g_hapticBlockMs and schedules a re-init just after, to keep the freshly-booted
  142. // controller out of the degraded/latched haptic state. Reliable -- independent of hapticTask's link heuristic.
  143. // Per-slot: only the slot that just reconnected is blocked, the others keep relaying.
  144. void hapticOnReconnect(int slot);
  145. // Write the controller's global trackpad-haptics enable setting (per active emulated type). slot 0xFF = broadcast.
  146. void hapticSetPadEnabled(uint8_t slot, bool on);