haptics.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. // Block ALL haptic relays for this long after a (re)connect: a just-powered-on controller's haptic engine
  23. // isn't ready, and feeding it haptics in that window leaves it in a degraded/latched state (slow/stuck/missing
  24. // haptics until a re-init). 3s gives it time to initialize.
  25. #define HAPTIC_RECONNECT_BLOCK_MS 3000u
  26. // 0x82-zero relays per stop event (sent at poll cadence -- not loop rate)
  27. #define HAPTIC_STOP_BURST 4u
  28. // max relayed payload bytes per entry: RF frame = [E3][len][05][rid][payload] and MAXLEN=64 -> 60
  29. #define RELAY_MAXP 60u
  30. // Proactive post-(re)connect haptic re-init: this many shots, this far apart, starting ~200ms after the link
  31. // comes up -- covers ~0.2s..3s so the brief controller-side connect buzz gets reset before it can sustain.
  32. #define HAPTIC_REINIT_SHOTS 8u
  33. #define HAPTIC_REINIT_GAP_MS 350u
  34. // After haptic activity, if it's been idle this long, fire one clear-re-init -- kills a latch that engaged
  35. // during use (a buzz that starts seconds after connect and won't self-clear). Long enough not to fire between
  36. // rapid in-game haptics; short enough to clear a stuck buzz soon after the user pauses.
  37. #define HAPTIC_CLEAR_IDLE_MS 1200u
  38. // Controller power-off: hapticSendShutdown() relays Steam's confirmed "turn off controller" command (feature-0x01
  39. // cmd 0x9F, payload "off!" -- captured from the real puck). Sent as a small burst because the RF relay is NO-ACK.
  40. #define HAPTIC_SHUTDOWN_SHOTS 3u
  41. // ---- relay queue (written by puck_hid.cpp, mode_*.cpp, serial_console.cpp; drained by rf_link.cpp) ----
  42. // Enqueue one host->controller report. `slot` = bond slot (0..NSLOT-1) or 0xFF to broadcast to every
  43. // connected controller (used by hapticSendShutdown / hapticReinit / test haptics). ISR-safe (PRIMASK).
  44. bool relayEnqueue(uint8_t rid, const uint8_t *payload, uint8_t plen,
  45. uint8_t slot = 0xFF);
  46. // anything still queued (xinput uses it to pace rumble re-queues)
  47. bool relayPending();
  48. extern uint8_t g_relayOp; // relay frame opcode (E3 poll)
  49. extern uint8_t g_relaySub; // relay sub-TLV type byte = SET
  50. extern volatile uint8_t g_testHaptic; // 't<n>' injects n test haptics
  51. // pending haptic-STOP frames to relay (kill a latched whine)
  52. extern volatile uint8_t g_hapticStop;
  53. // Per-slot block: arm after a (re)connect, drop haptics aimed at the slot for HAPTIC_RECONNECT_BLOCK_MS.
  54. extern unsigned long g_hapticBlockUntil[NSLOT];
  55. // relay the controller power-off (0x9F "off!"), burst x3 (Steam 0x9F / host-suspend / test button)
  56. void hapticSendShutdown();
  57. // ---- diagnostic capture (compiled in only when OPK_LOG): a ring of recent host->controller commands +
  58. // link/TX markers, dumped over WebUSB. No-ops in a production build so call sites vanish. ----
  59. #if OPK_LOG
  60. void hapLogAdd(uint8_t slot, uint8_t rid, const uint8_t *b, uint16_t n);
  61. void hapticDumpLog(); // 'H' console dump of the recent OUTPUT-report history
  62. // WebUSB capture drain: resetDrain(true) rewinds to the OLDEST entry (dump the whole ring from boot);
  63. // resetDrain(false) starts at "now" (live only). pull yields each entry once, oldest->newest, skipping empties.
  64. void hapLogResetDrain(bool fromBoot);
  65. bool hapLogPull(uint32_t *logMs, uint8_t *slot, uint8_t *rid, uint8_t *n,
  66. uint8_t bytes16[16]);
  67. #else
  68. static inline void hapLogAdd(uint8_t, uint8_t, const uint8_t *, uint16_t)
  69. {
  70. }
  71. static inline void hapticDumpLog()
  72. {
  73. }
  74. static inline void hapLogResetDrain(bool)
  75. {
  76. }
  77. static inline bool hapLogPull(uint32_t *, uint8_t *, uint8_t *, uint8_t *,
  78. uint8_t *)
  79. {
  80. return false;
  81. }
  82. #endif
  83. bool hapticLinkUp(int slot = -1);
  84. bool haptic82Blocked(int slot = -1);
  85. bool hapticRelaySlotOk(int slot);
  86. void haptic82HostReport(const uint8_t *p, uint16_t n);
  87. // queue a Steam/Triton 0x80 rumble frame. `slot` = bond slot of the originating controller (0..NSLOT-1);
  88. // defaults to 0 for the legacy single-controller callers. Per-slot so each connected controller can have its
  89. // own active rumble stream when the host presents multiple gamepads (e.g. 4 XInput devices).
  90. bool hapticSteamRumble(uint16_t lowFreq, uint16_t highFreq, uint8_t slot = 0);
  91. // queue + flush the pending host/test/stop relay inside the poll cadence (called from rf_link).
  92. // rfConnFlushRelay's s1 must carry a PID distinct from the GET poll that follows it (rf_link cycles the shared
  93. // PID counter), so the controller doesn't dedup the GET as a retransmit of the relay.
  94. void rfConnQueueHapticRelay();
  95. void rfConnFlushRelay(uint8_t ch, uint8_t s1);
  96. // boot reset: clear relay/active flags, arm the reconnect block
  97. void hapticInit();
  98. // per-loop upkeep: link-edge markers + steam 0x82 quiet timeout + fires the scheduled re-init
  99. void hapticTask();
  100. // replay Steam's haptic-subsystem re-init to the controller -> clears a latched/stuck buzz.
  101. // `slot` defaults to 0xFF (broadcast to all connected) -- the re-init is a settings-only reset and is
  102. // harmless on healthy controllers, so it's worth re-initializing every slot the firmware knows about.
  103. void hapticReinit(uint8_t slot = 0xFF);
  104. // Called from rf_link the instant a controller (re)connect is detected (an F-reply after a gap): blocks haptic
  105. // relays for HAPTIC_RECONNECT_BLOCK_MS and schedules a re-init just after, to keep the freshly-booted
  106. // controller out of the degraded/latched haptic state. Reliable -- independent of hapticTask's link heuristic.
  107. // Per-slot: only the slot that just reconnected is blocked, the others keep relaying.
  108. void hapticOnReconnect(int slot);