puck_hid.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #include "puck_hid.h"
  2. #include "bonds.h"
  3. #include "config.h"
  4. #include "identity.h"
  5. #include "haptics.h"
  6. #include "rf_link.h"
  7. #include "triton.h"
  8. #include "mode_lizard.h"
  9. #include <Adafruit_TinyUSB.h>
  10. #include <Arduino.h>
  11. #include <string.h>
  12. uint8_t g_fwdNewOnly = 1;
  13. SteamPuckController g_steamPuck;
  14. // ---- cloned puck HID report descriptor (verbatim): mouse(0x40)+keyboard(0x41)+vendor(FF00) with the 63-byte
  15. // FEATURE command reports on report id 1/2. Each of the 4 interfaces uses this. ----
  16. static const uint8_t PUCK_HID_DESC[] = {
  17. 0x05,0x01,0x09,0x02,0xA1,0x01,0x85,0x40,0x09,0x01,0xA1,0x00,0x05,0x09,0x19,0x01,
  18. 0x29,0x02,0x15,0x00,0x25,0x01,0x75,0x01,0x95,0x02,0x81,0x02,0x75,0x06,0x95,0x01,
  19. 0x81,0x01,0x05,0x01,0x09,0x30,0x09,0x31,0x15,0x81,0x25,0x7F,0x75,0x08,0x95,0x02,
  20. 0x81,0x06,0x95,0x01,0x09,0x38,0x81,0x06,0x05,0x0C,0x0A,0x38,0x02,0x95,0x01,0x81,
  21. 0x06,0xC0,0xC0,0x05,0x01,0x09,0x06,0xA1,0x01,0x85,0x41,0x05,0x07,0x19,0xE0,0x29,
  22. 0xE7,0x15,0x00,0x25,0x01,0x75,0x01,0x95,0x08,0x81,0x02,0x81,0x01,0x19,0x00,0x29,
  23. 0x65,0x15,0x00,0x25,0x65,0x75,0x08,0x95,0x06,0x81,0x00,0xC0,0x06,0x00,0xFF,0x09,
  24. 0x01,0xA1,0x01,0x85,0x42,0x15,0x00,0x26,0xFF,0x00,0x75,0x08,0x95,0x35,0x09,0x42,
  25. 0x81,0x02,0x85,0x44,0x15,0x00,0x26,0xFF,0x00,0x75,0x08,0x95,0x05,0x09,0x44,0x81,
  26. 0x02,0x85,0x79,0x15,0x00,0x26,0xFF,0x00,0x75,0x08,0x95,0x01,0x09,0x79,0x81,0x02,
  27. 0x85,0x43,0x15,0x00,0x26,0xFF,0x00,0x75,0x08,0x95,0x0E,0x09,0x43,0x81,0x02,0x85,
  28. 0x7B,0x15,0x00,0x26,0xFF,0x00,0x75,0x08,0x95,0x0C,0x09,0x7B,0x81,0x02,0x85,0x45,
  29. 0x15,0x00,0x26,0xFF,0x00,0x75,0x08,0x95,0x2D,0x09,0x45,0x81,0x02,0x85,0x80,0x15,
  30. 0x00,0x26,0xFF,0x00,0x75,0x08,0x95,0x09,0x09,0x80,0x91,0x02,0x85,0x81,0x15,0x00,
  31. 0x26,0xFF,0x00,0x75,0x08,0x95,0x07,0x09,0x81,0x91,0x02,0x85,0x82,0x15,0x00,0x26,
  32. 0xFF,0x00,0x75,0x08,0x95,0x03,0x09,0x82,0x91,0x02,0x85,0x83,0x15,0x00,0x26,0xFF,
  33. 0x00,0x75,0x08,0x95,0x09,0x09,0x83,0x91,0x02,0x85,0x84,0x15,0x00,0x26,0xFF,0x00,
  34. 0x75,0x08,0x95,0x08,0x09,0x84,0x91,0x02,0x85,0x85,0x15,0x00,0x26,0xFF,0x00,0x75,
  35. 0x08,0x95,0x03,0x09,0x85,0x91,0x02,0x85,0x86,0x15,0x00,0x26,0xFF,0x00,0x75,0x08,
  36. 0x95,0x03,0x09,0x86,0x91,0x02,0x85,0x87,0x15,0x00,0x26,0xFF,0x00,0x75,0x08,0x95,
  37. 0x3F,0x09,0x87,0x91,0x02,0x85,0x89,0x15,0x00,0x26,0xFF,0x00,0x75,0x08,0x95,0x3F,
  38. 0x09,0x89,0x91,0x02,0x85,0x88,0x15,0x00,0x26,0xFF,0x00,0x75,0x08,0x95,0x3F,0x09,
  39. 0x88,0x91,0x02,0x85,0x01,0x95,0x3F,0x09,0x01,0xB1,0x02,0x85,0x02,0x95,0x3F,0x09,
  40. 0x01,0xB1,0x02,0xC0
  41. };
  42. static Adafruit_USBD_HID hid[NSLOT];
  43. // ===================== seamless LIZARD decision =====================
  44. // Steam, while running, re-sends settings report 0x87 (lizard-off) every ~3s as a heartbeat (captured on HW),
  45. // and ANY OUTPUT report likewise stamps g_steamAliveMs. When the heartbeat stops we fall back to lizard, so the
  46. // controller drives desktop keyboard+mouse whenever Steam isn't running. MODE_LIZARD forces lizard always.
  47. static unsigned long g_steamAliveMs = 0; // millis of last Steam OUTPUT/settings write; 0 at boot => lizard until Steam appears
  48. // Fall back to lizard this long after Steam's ~3s settings heartbeat stops. Keep >2x the cadence (7s): the
  49. // haptic relay is gated by !lizardActive(), so a shorter window lets a single jittered/delayed heartbeat flip
  50. // lizard mid-session while Steam is still running -> a haptic that arrives in that window gets gated out
  51. // (dropped/missing haptic). 7s tolerates one missed beat. (This matches main; do not shorten without a lizard
  52. // trigger that doesn't depend on the heartbeat timeout.)
  53. #define LIZARD_WD_MS 7000u
  54. static bool g_autoLizard = true; // master switch; false => Steam mode always forwards 0x45
  55. // Single source of truth, shared by the USB input path AND the haptic relay gate: if we ever relay a 0x82 to
  56. // the controller while presenting lizard (Steam isn't reading 0x45 back), Steam loops the same haptic -> buzz.
  57. static inline bool steamDrivingGamepad(){ return g_steamAliveMs && (millis()-g_steamAliveMs < LIZARD_WD_MS); }
  58. static inline bool lizardActive(){ return modeIsPuck(g_usbMode) && (g_usbMode==MODE_LIZARD || (g_autoLizard && !steamDrivingGamepad())); }
  59. // Right after the host resumes from suspend, MUTE input forwarding briefly. Otherwise the controller's input
  60. // in that instant (a trackpad click/trigger, or residual button state from the wake gesture) gets forwarded
  61. // as a real click/keypress into the just-woken desktop -- which was activating the highlighted Start tile
  62. // (Edge) and closing the Start menu on every wake. Set when task() sees the suspended->active transition.
  63. static unsigned long g_resumeMs = 0;
  64. #define POST_RESUME_MUTE_MS 1500u
  65. // ===================== puck feature command channel =====================
  66. // `slot` is the interface index (interface N == bond slot N).
  67. static void handleSet(int slot, uint8_t rid, hid_report_type_t type, uint8_t const *b, uint16_t n) {
  68. if (type == HID_REPORT_TYPE_OUTPUT) { // Steam OUTPUT reports 0x80-0x89. The haptic/actuator reports (0x80-0x86)
  69. // are relayed to the controller, and ONLY when they arrive on the CONNECTED slot's interface. We have one
  70. // controller but expose 4 puck slots, and a report aimed at a DIFFERENT slot made the controller buzz at
  71. // random -> that is what the slot gate below fixes. (This used to also clamp to 0x82-ONLY, which silently
  72. // dropped the ping / grip / test haptics: those ride other report IDs such as 0x85/0x86, so they never
  73. // reached the controller.) The 63-byte settings/config reports 0x87/0x88/0x89 are NOT haptics and are not
  74. // pushed here -- 0x87 (lizard-off/settings) reaches the controller through the feature-0x01 passthrough path.
  75. if (rid >= 0x80 && rid <= 0x89) {
  76. hapLogAdd((uint8_t)slot, rid, b, n); // capture ALL OUTPUT reports (even un-relayed) for the 'H' dump
  77. g_steamAliveMs = millis(); // ANY Steam OUTPUT report (not just the 0x87 heartbeat) means Steam is present and
  78. // driving -> leave lizard for gamepad NOW, so a haptic that arrives before the
  79. // first 0x87 doesn't get relayed while we're still presenting lizard (-> buzz loop).
  80. }
  81. // Post-resume mute also gates haptics: while onReport45 is muted Steam reads NO 0x45 back, which is the
  82. // exact condition under which Steam loops the same haptic command (-> connect/wake buzz loop). Same logic
  83. // as the lizard gate; the mute is short (1.5s), so a dropped wake chime is the cheap end of the trade.
  84. bool muted = g_resumeMs && millis()-g_resumeMs < POST_RESUME_MUTE_MS;
  85. if (rid >= 0x80 && rid <= 0x86 && n >= 1 && hapticRelaySlotOk(slot) && !lizardActive() && !muted) { // wrap as a SET sub-TLV like the report-01 path
  86. if (!haptic82Blocked()) {
  87. relayEnqueue(rid, b, (uint8_t)(n > RELAY_MAXP ? RELAY_MAXP : n));
  88. // Track on/off from what was actually RELAYED (= the controller's believed state), not from every host
  89. // report: a BLOCKED stop must leave "on" set (the controller may be latched -> reconnect stop-burst),
  90. // and a blocked ON must not set it (nothing reached the controller -> no spurious burst clicks).
  91. if (rid == 0x82) haptic82HostReport(b, n);
  92. }
  93. }
  94. if (Serial.availableForWrite() > 80) { // log so we can see what Steam actually sends (e.g. glide haptics)
  95. Serial.printf("# OUT if%d rid=%02X n=%u:", slot, rid, n);
  96. for (uint16_t i = 0; i < n && i < 14; i++) Serial.printf(" %02X", b[i]);
  97. Serial.println();
  98. }
  99. return;
  100. }
  101. if (type != HID_REPORT_TYPE_FEATURE || n < 1) return;
  102. Slot &S = g_slot[slot];
  103. uint8_t cmd = b[0], len = (n > 1) ? b[1] : 0;
  104. const uint8_t *pl = b + 2; uint16_t pln = (n >= 2) ? n - 2 : 0;
  105. if (cmd >= 0x80 && cmd <= 0x89) g_steamAliveMs = millis(); // any Steam settings/haptic/LED report (incl. the 0x87 lizard-off heartbeat) -> Steam present, forward gamepad, suppress auto-lizard
  106. if (rid == 1 && n >= 2) { // report 0x01 = raw passthrough -> queue for RF relay to the controller
  107. hapLogAdd((uint8_t)slot, cmd, b, n); // capture EVERY relayed feature-1 command for the WebUSB capture view
  108. // (haptics, LED SET_LED_COLOR, 0x87 settings, 0x9F power-off). The log
  109. // shows cmd as "rid"; bytes start [cmd][len]... Was gated to 0x80-0x89,
  110. // which hid exactly the LED/power-off frames we now need to see.
  111. bool haptic82 = (cmd == 0x82 && len <= pln);
  112. bool muted = g_resumeMs && millis()-g_resumeMs < POST_RESUME_MUTE_MS; // see the OUTPUT path: no haptics while Steam can't read 0x45 back
  113. bool relayOk = hapticRelaySlotOk(slot) && !(haptic82 && (lizardActive() || muted)); // never push haptics to the controller while presenting lizard (Steam isn't reading 0x45 -> would buzz-loop)
  114. if (relayOk && (!haptic82 || !haptic82Blocked())) {
  115. // Relay the DECLARED length (up to the 60B RF frame ceiling), not a truncation: Steam's multi-register
  116. // 0x87 settings blocks (LED brightness) and calibration writes are longer than the old 18B cap, and the
  117. // chopped frames were why those settings never landed on the controller.
  118. uint8_t rl = (len <= pln) ? len : (uint8_t)pln;
  119. if (len > RELAY_MAXP && Serial.availableForWrite() > 60)
  120. Serial.printf("# RELAY TRUNC cmd=%02X len=%u>%u\n", cmd, len, (unsigned)RELAY_MAXP);
  121. relayEnqueue(cmd, pl, rl);
  122. if (haptic82) haptic82HostReport(pl, len); // track from RELAYED frames only (see the OUTPUT path)
  123. }
  124. }
  125. if (Serial.availableForWrite() > 80) { // log host feature writes (non-blocking)
  126. Serial.printf("# SET if%d rid=%02X cmd=%02X len=%u:", slot, rid, cmd, len);
  127. for (uint16_t i = 0; i < n && i < 14; i++) Serial.printf(" %02X", b[i]);
  128. Serial.println();
  129. }
  130. memset(S.resp, 0, sizeof S.resp); S.resp_len = 0;
  131. switch (cmd) {
  132. case 0x83:
  133. S.resp[0] = 0x83; S.resp[1] = sizeof ATTR83; memcpy(S.resp + 2, ATTR83, sizeof ATTR83); S.resp_len = 63; break;
  134. case 0xAE: {
  135. uint8_t idx = pln > 0 ? pl[0] : 1; const char *s = (idx == 0) ? g_board : (idx == 1) ? g_unit : "NA";
  136. S.resp[0] = 0xAE; S.resp[1] = 0x14; S.resp[2] = idx; memset(S.resp + 3, 0, 60); memcpy(S.resp + 3, s, strlen(s)); S.resp_len = 63; break; }
  137. case 0xB4: // connection/version state per slot: value 0x02 = controller connected, 0x01 = not
  138. S.resp[0] = 0xB4; S.resp[1] = 0x01;
  139. S.resp[2] = (slot == g_connSlot && !g_xbox && (millis() - g_connReplyMs < 500)) ? 0x02 : 0x01;
  140. S.resp_len = 63; break;
  141. case 0xAD:
  142. g_pairing = (pln > 0 && pl[0] != 0); Serial.printf("# pairing %s\n", g_pairing ? "ON" : "off");
  143. S.resp[0] = 0xAD; S.resp[1] = 0; S.resp_len = 63; break;
  144. case 0xA2: // write/clear THIS interface's slot
  145. if (len >= 24 && pln >= 24) {
  146. if (recEmpty(pl)) { S.used = false; memset(S.rec, 0, 24); }
  147. else { memcpy(S.rec, pl, 24); S.used = true; }
  148. g_dirty = true; Serial.printf("# slot %d %s\n", slot, recEmpty(pl) ? "cleared" : "bonded");
  149. }
  150. S.resp[0] = 0xA2; S.resp[1] = 0; S.resp_len = 63; break;
  151. case 0xA3: // read THIS interface's slot
  152. S.resp[0] = 0xA3; S.resp[1] = 0x18; memset(S.resp + 2, 0, 24);
  153. if (S.used) memcpy(S.resp + 2, S.rec, 24); S.resp_len = 63; break;
  154. default:
  155. S.resp[0] = cmd; S.resp[1] = len; if (pln) memcpy(S.resp + 2, pl, pln > 60 ? 60 : pln); S.resp_len = 63; break;
  156. }
  157. }
  158. static uint16_t handleGet(int slot, uint8_t rid, hid_report_type_t type, uint8_t *buf, uint16_t reqlen) {
  159. (void)rid;
  160. if (type != HID_REPORT_TYPE_FEATURE) return 0;
  161. Slot &S = g_slot[slot];
  162. uint16_t n = S.resp_len ? S.resp_len : 63; if (n > reqlen) n = reqlen;
  163. memcpy(buf, S.resp, n); return n;
  164. }
  165. // one callback pair per interface (the Adafruit core routes by interface to the matching object)
  166. #define SLOTCB(N) \
  167. static void setcb##N(uint8_t r, hid_report_type_t t, uint8_t const *b, uint16_t n) { handleSet(N, r, t, b, n); } \
  168. static uint16_t getcb##N(uint8_t r, hid_report_type_t t, uint8_t *bf, uint16_t rl) { return handleGet(N, r, t, bf, rl); }
  169. SLOTCB(0) SLOTCB(1) SLOTCB(2) SLOTCB(3)
  170. typedef uint16_t (*getcb_t)(uint8_t, hid_report_type_t, uint8_t *, uint16_t);
  171. typedef void (*setcb_t)(uint8_t, hid_report_type_t, uint8_t const *, uint16_t);
  172. static getcb_t GETCB[NSLOT] = { getcb0, getcb1, getcb2, getcb3 };
  173. static setcb_t SETCB[NSLOT] = { setcb0, setcb1, setcb2, setcb3 };
  174. // ===================== IController =====================
  175. void SteamPuckController::begin(){
  176. USBDevice.setID(0x28DE, 0x1304);
  177. // Distinct bcdDevice so Windows keys a FRESH usbflags entry (cache is VID:PID:bcdDevice) and actually runs
  178. // MS OS 2.0 / WinUSB binding for the WebUSB vendor interface -- instead of reusing a stale "no WinUSB" entry
  179. // tied to the real Steam Controller (28DE:1304), which has no WebUSB interface. The normal (wake-mouse) and
  180. // one-shot debug (CDC) boots present DIFFERENT interface sets, so they use DIFFERENT bcdDevice values --
  181. // otherwise Windows would serve one's cached descriptor for the other when you reboot between them.
  182. USBDevice.setDeviceVersion(g_debugCdcThisBoot ? 0x0212 : 0x0211);
  183. USBDevice.setManufacturerDescriptor("Valve Software");
  184. USBDevice.setProductDescriptor("Steam Controller Puck");
  185. for (int i = 0; i < NSLOT; i++) {
  186. hid[i].setReportDescriptor(PUCK_HID_DESC, sizeof PUCK_HID_DESC);
  187. hid[i].setReportCallback(GETCB[i], SETCB[i]);
  188. hid[i].setPollInterval(1); // 1ms USB poll (was default 10ms = 100/s cap -> choppy)
  189. hid[i].begin();
  190. }
  191. }
  192. // Forward the controller's report 0x45 to Steam, or drive lizard kb/mouse when Steam is closed. This is a
  193. // PURELY USB-SIDE decision -- it changes nothing about the RF poll or the host->controller relay.
  194. void SteamPuckController::onReport45(const uint8_t* rep, bool fresh, uint8_t bodyTlen){
  195. // Host asleep -> forward NOTHING. While suspended, every sendReport attempt (a lizard-mouse report from a
  196. // trackpad graze, a 0x45 forward) can translate into a host wake -- which made the PC wake on any controller
  197. // movement and nearly impossible to keep asleep. Waking is an explicit gesture only (Steam-button short press
  198. // / controller connect), handled in rf_link.cpp via the device-level USB resume signal.
  199. if (USBDevice.suspended()) return;
  200. // Just woke? Hold off forwarding for a beat so the wake gesture's residual controller input doesn't click /
  201. // type into the freshly-woken desktop (it was launching Edge from the Start menu on every wake).
  202. if (g_resumeMs && millis()-g_resumeMs < POST_RESUME_MUTE_MS) return;
  203. if (g_connSlot < 0 || g_connSlot >= NSLOT) return;
  204. if (lizardActive()){
  205. rfLizard(rep, &hid[g_connSlot], &hid[g_connSlot], 0x40, 0x41);
  206. } else {
  207. uint8_t blen = bodyTlen - 1; if(blen>45)blen=45; // body after the 0x45 id byte
  208. // forward the puck's raw pad coords untouched -- Steam does its own interpolation/smoothing. Forward only
  209. // FRESH reports (the real puck dedupes -> Steam gets a clean unique stream; sending stale repeats makes
  210. // Steam's velocity/smoothing stair-step). g_fwdNewOnly toggles for A/B.
  211. if((fresh || !g_fwdNewOnly) && g_slot[g_connSlot].used && hid[g_connSlot].ready())
  212. hid[g_connSlot].sendReport(0x45, rep+1, blen); // Steam/SDL: input report -> "connected"
  213. }
  214. }
  215. // ---- wake nudge: a bare USB resume signal is NOT enough to wake some hosts (Windows in particular) -- they
  216. // only wake when actual mouse/keyboard input follows. So on a deliberate wake gesture we play a HARMLESS mouse
  217. // JIGGLE (move a few px right, then back -- NET ZERO cursor, NO button): real mouse activity wakes the host,
  218. // but it clicks nothing and doesn't close/activate anything (an open Start menu stays open). Earlier versions
  219. // also sent a click / space / Ctrl; those either activated the focused item or weren't needed. Queued by
  220. // wakeEvent() (rf_link, on a Steam short press / controller connect while suspended); reports can't cross a
  221. // suspended bus, so it's delivered once the bus has resumed.
  222. static uint8_t g_nudgeStep = 0; // 0=idle; 1=jiggle+, 2=jiggle-
  223. static unsigned long g_nudgeMs = 0;
  224. #define NUDGE_JIGGLE_PX 10
  225. void SteamPuckController::wakeEvent(){
  226. g_nudgeStep = 1; g_nudgeMs = millis();
  227. }
  228. static void wakeNudgeTask(){
  229. if(!g_nudgeStep) return;
  230. if(millis()-g_nudgeMs > 5000){ g_nudgeStep=0; return; } // bus never resumed -> drop the nudge
  231. if(USBDevice.suspended()) return; // wait for resume; reports can't cross a suspended bus
  232. if(g_connSlot<0 || g_connSlot>=NSLOT || !hid[g_connSlot].ready()) return;
  233. static unsigned long stepMs=0;
  234. if(millis()-stepMs < 15) return; // pace the edges
  235. stepMs=millis();
  236. hid_mouse_report_t m; m.buttons=0; m.x=(g_nudgeStep==1)?NUDGE_JIGGLE_PX:-NUDGE_JIGGLE_PX; m.y=0; m.wheel=0; m.pan=0;
  237. hid[g_connSlot].sendReport(0x40,&m,sizeof m); // jiggle right, then back
  238. g_nudgeStep = (g_nudgeStep>=2) ? 0 : (uint8_t)(g_nudgeStep+1);
  239. }
  240. // USB connection presentation (like the real dongle): report 0x79 = connection state (01=disc, 02=conn),
  241. // edge-triggered, + periodic 0x7B status. Live-captured: this is what Steam reads to mark the controller
  242. // connected. Without it Steam shows disconnected even though 0x45 input is streaming.
  243. void SteamPuckController::task(){
  244. wakeNudgeTask();
  245. { static bool wasSusp=false; bool susp=USBDevice.suspended(); // stamp the suspended->active edge for the post-resume mute
  246. if(wasSusp && !susp) g_resumeMs=millis();
  247. wasSusp=susp; }
  248. if (USBDevice.suspended()) return; // no periodic 0x79/0x7B while the host sleeps -- those sends can wake it too
  249. static bool usbConn=false; static unsigned long last79=0, last7B=0, connEdgeMs=0;
  250. bool conn = (g_connSlot>=0 && millis()-g_connReplyMs < 300);
  251. if (g_connSlot>=0 && g_connSlot<NSLOT && hid[g_connSlot].ready()){
  252. // 0x79 connection state: on edge, then repeated every 750ms ONLY until Steam reacts (its first OUTPUT/
  253. // settings write after the edge -- g_steamAliveMs). The real puck sends 0x79 ONCE, edge-triggered; the old
  254. // unconditional forever-resend could re-trigger Steam's connect handling (connect chime) every 750ms
  255. // before Steam started consuming 0x45 -> the connect-time loop of haptic buzzes that only stopped on the
  256. // first real input (the Steam button). Resending until acked still covers "Steam missed the edge".
  257. bool steamAcked = g_steamAliveMs && (int32_t)(g_steamAliveMs - connEdgeMs) >= 0;
  258. if (conn!=usbConn || (conn && !steamAcked && millis()-last79>=750)){
  259. if (conn && !usbConn) connEdgeMs = millis();
  260. uint8_t st=conn?0x02:0x01; hid[g_connSlot].sendReport(0x79,&st,1); usbConn=conn; last79=millis();
  261. } else if (conn && millis()-last7B>=2000){
  262. // 0x7B status, live-captured template. Byte 8 is the controller->puck signal strength as signed dBm
  263. // (capture showed 0xDD = -35; replaying it verbatim is why Steam pinned -35dBm forever) -- patch in the
  264. // smoothed RSSI the radio now samples on each controller reply (rf_link). 0 = no sample yet -> keep the
  265. // capture value rather than report garbage.
  266. //
  267. // CALIBRATION: our raw RSSISAMPLE reads ~RSSI_DBM_OFFSET dB lower than the real Valve puck at the same
  268. // distance (the Pro Micro's PCB-trace antenna vs Valve's tuned front-end). The 2Mbit ESB link has ~55dB
  269. // of margin, so a -75dBm reading still works across a house -- but Steam's bar maps raw dBm to "weak"
  270. // long before that. Adding the offset lines our close-range value up with the puck's captured -35 so the
  271. // bar tracks usable range instead of antenna gain. Clamp keeps it in a sane window (no wrap/garbage).
  272. uint8_t s7b[12]={0xF7,0x01,0x89,0x00,0x00,0x00,0x03,0x00,0xDD,0x00,0x3A,0x02};
  273. if (g_linkRssi){
  274. int mag = (int)g_linkRssi - RSSI_DBM_OFFSET;
  275. if (mag < 25) mag = 25; else if (mag > 95) mag = 95;
  276. s7b[8]=(uint8_t)(0u-(uint8_t)mag);
  277. }
  278. hid[g_connSlot].sendReport(0x7B,s7b,12); last7B=millis();
  279. }
  280. } else if(!conn) usbConn=false;
  281. }