deck_input.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #include "deck_input.h"
  2. #include "triton.h"
  3. #include "ctrl_bonds.h"
  4. #include "ctrl_link.h"
  5. #include "radio.h"
  6. #include <Adafruit_TinyUSB.h>
  7. #include <Arduino.h>
  8. #include <string.h>
  9. static bool g_forwarding = false;
  10. static unsigned long g_lastInputMs = 0;
  11. static unsigned long g_lastStatusMs = 0;
  12. bool deckForwarding()
  13. {
  14. return g_forwarding;
  15. }
  16. unsigned long deckLastInputMs()
  17. {
  18. return g_lastInputMs;
  19. }
  20. // ---- little-endian payload readers ----
  21. static inline int16_t rd_s16(const uint8_t *p)
  22. {
  23. return (int16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8));
  24. }
  25. static inline uint32_t rd_u32(const uint8_t *p)
  26. {
  27. return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) |
  28. ((uint32_t)p[3] << 24);
  29. }
  30. static inline uint16_t rd_u16(const uint8_t *p)
  31. {
  32. return (uint16_t)p[0] | ((uint16_t)p[1] << 8);
  33. }
  34. // INPUT payload = buttons u32, lx ly rx ry s16, lt rt u8, lpx lpy rpx rpy s16, lpp rpp u16, IMU 6h.
  35. #define DECK_INPUT_LEN 38
  36. static void applyInput(const uint8_t *p, uint8_t n)
  37. {
  38. if (n < DECK_INPUT_LEN)
  39. return;
  40. g_in.buttons = rd_u32(p + 0);
  41. g_in.lx = rd_s16(p + 4);
  42. g_in.ly = rd_s16(p + 6);
  43. g_in.rx = rd_s16(p + 8);
  44. g_in.ry = rd_s16(p + 10);
  45. g_in.lt = p[12];
  46. g_in.rt = p[13];
  47. g_in.lpx = rd_s16(p + 14);
  48. g_in.lpy = rd_s16(p + 16);
  49. g_in.rpx = rd_s16(p + 18);
  50. g_in.rpy = rd_s16(p + 20);
  51. g_in.lpp = rd_u16(p + 22);
  52. g_in.rpp = rd_u16(p + 24);
  53. g_in.ax = rd_s16(p + 26);
  54. g_in.ay = rd_s16(p + 28);
  55. g_in.az = rd_s16(p + 30);
  56. g_in.gx = rd_s16(p + 32);
  57. g_in.gy = rd_s16(p + 34);
  58. g_in.gz = rd_s16(p + 36);
  59. g_lastInputMs = millis();
  60. }
  61. static void handleFrame(uint8_t type, const uint8_t *p, uint8_t n)
  62. {
  63. switch (type) {
  64. case DECK_T_INPUT:
  65. applyInput(p, n);
  66. break;
  67. case DECK_T_CONTROL:
  68. if (n >= 2 && p[0] == 0x01)
  69. g_forwarding = (p[1] != 0);
  70. else if (n >= 2 &&
  71. p[0] == 0x02) // clear-bond [slot] (GUI "remove")
  72. ctrlBondClear(p[1]);
  73. break;
  74. case DECK_T_REQ_STATUS:
  75. g_lastStatusMs = 0; // force an immediate status emit
  76. break;
  77. default:
  78. break;
  79. }
  80. }
  81. // ---- RX state machine: resync on the 2-byte sync word ----
  82. void deckInputPoll()
  83. {
  84. // state: 0 wait sync0, 1 wait sync1, 2 type, 3 len, 4 payload, 5 sum
  85. static uint8_t st = 0;
  86. static uint8_t type = 0, len = 0, idx = 0, sum = 0;
  87. static uint8_t buf[64];
  88. while (Serial.available()) {
  89. uint8_t c = (uint8_t)Serial.read();
  90. switch (st) {
  91. case 0:
  92. if (c == DECK_SYNC0)
  93. st = 1;
  94. break;
  95. case 1:
  96. st = (c == DECK_SYNC1) ? 2 : (c == DECK_SYNC0 ? 1 : 0);
  97. break;
  98. case 2:
  99. type = c;
  100. sum = c;
  101. st = 3;
  102. break;
  103. case 3:
  104. len = c;
  105. sum += c;
  106. idx = 0;
  107. if (len > sizeof buf) { // oversized -> drop, resync
  108. st = 0;
  109. break;
  110. }
  111. st = (len == 0) ? 5 : 4;
  112. break;
  113. case 4:
  114. buf[idx++] = c;
  115. sum += c;
  116. if (idx >= len)
  117. st = 5;
  118. break;
  119. case 5:
  120. if (c == sum)
  121. handleFrame(type, buf, len);
  122. st = 0;
  123. break;
  124. }
  125. }
  126. }
  127. // ---- frame TX ----
  128. static void sendFrame(uint8_t type, const uint8_t *p, uint8_t n)
  129. {
  130. // need room for the whole frame or skip (never block the RF loop on CDC backpressure)
  131. if ((int)Serial.availableForWrite() < n + 5)
  132. return;
  133. uint8_t hdr[4] = { DECK_SYNC0, DECK_SYNC1, type, n };
  134. uint8_t sum = (uint8_t)(type + n);
  135. for (uint8_t i = 0; i < n; i++)
  136. sum += p[i];
  137. Serial.write(hdr, 4);
  138. if (n)
  139. Serial.write(p, n);
  140. Serial.write(&sum, 1);
  141. }
  142. void deckText(const char *s)
  143. {
  144. size_t n = strlen(s);
  145. if (n > 60)
  146. n = 60;
  147. sendFrame(DECK_T_TEXT, (const uint8_t *)s, (uint8_t)n);
  148. }
  149. // Latest relayed haptic, STAGED by deckForwardHaptic (called from the RF poll path) and flushed over CDC
  150. // by deckHapticTask() at loop level. Staging-then-flushing (a) keeps the CDC write OUT of the RF receive
  151. // path, and (b) COALESCES a burst to the newest state -- Steam streams rumble at the poll rate, and
  152. // replaying every frame one-behind is what caused the lag; newest-wins removes the backlog.
  153. static uint8_t g_hapPayload[8];
  154. static volatile bool g_hapPending = false;
  155. static unsigned long g_lastHapMs = 0;
  156. // cap CDC haptic frames to ~60 Hz: enough for smooth rumble, but no flood of the CDC link / Deck USB.
  157. #define HAPTIC_MIN_MS 16u
  158. void deckForwardHaptic(uint8_t rid, const uint8_t *d, uint8_t n)
  159. {
  160. // Only while forwarding -- otherwise the Deck isn't grabbed and there's nothing to buzz.
  161. if (!g_forwarding)
  162. return;
  163. uint16_t intensity = 0, left = 0, right = 0;
  164. uint8_t lgain = 0, rgain = 0;
  165. if (rid == 0x80 && n >= 9) {
  166. // SDL Triton rumble report 0x80: [type][intensity u16][left u16][lgain][right u16][rgain].
  167. // type 0 = off/zero report (everything stays 0).
  168. if (d[0] != 0) {
  169. intensity = (uint16_t)(d[1] | (d[2] << 8));
  170. left = (uint16_t)(d[3] | (d[4] << 8));
  171. lgain = d[5];
  172. right = (uint16_t)(d[6] | (d[7] << 8));
  173. rgain = d[8];
  174. }
  175. } else if (rid == 0x81 && n >= 3) {
  176. // OUTPUT report 0x81 = HAPTIC_PULSE [side][on_us u16][off_us u16][repeat u16] -- Steam's trackpad
  177. // CLICK and trigger full-pull cues. (0x81 in the FEATURE command space is CLEAR_DIGITAL_MAPPINGS,
  178. // a different thing entirely; this path only ever sees relayed OUTPUT reports.) The Deck has no
  179. // per-pad clicker, so synthesize a short mid-strength buzz on both motors.
  180. if (d[1] || d[2]) { // on_us != 0
  181. intensity = 0x6000;
  182. left = right = 0x6000;
  183. }
  184. } else if (rid == 0x82 || rid == 0x8F) {
  185. // discrete haptic click/pulse (data ~ [01 01 F7]; trailing byte F7=on, 00=off). The Deck has no
  186. // per-pad clicker, so synthesize a short mid-strength buzz on both motors.
  187. bool on = (n >= 3) ? (d[2] != 0) : (n >= 1 && d[0] != 0);
  188. if (on) {
  189. intensity = 0x6000;
  190. left = right = 0x6000;
  191. }
  192. } else {
  193. return; // not a haptic the Deck can render
  194. }
  195. // stage newest (coalesce). The actual sendFrame happens in deckHapticTask, off the RF path.
  196. g_hapPayload[0] = (uint8_t)(intensity & 0xFF);
  197. g_hapPayload[1] = (uint8_t)(intensity >> 8);
  198. g_hapPayload[2] = (uint8_t)(left & 0xFF);
  199. g_hapPayload[3] = (uint8_t)(left >> 8);
  200. g_hapPayload[4] = (uint8_t)(right & 0xFF);
  201. g_hapPayload[5] = (uint8_t)(right >> 8);
  202. g_hapPayload[6] = lgain;
  203. g_hapPayload[7] = rgain;
  204. g_hapPending = true;
  205. }
  206. void deckHapticTask()
  207. {
  208. if (!g_hapPending)
  209. return;
  210. if (millis() - g_lastHapMs < HAPTIC_MIN_MS)
  211. return; // rate-limit the newest staged haptic to ~60 Hz
  212. g_hapPending = false;
  213. g_lastHapMs = millis();
  214. sendFrame(DECK_T_HAPTIC, g_hapPayload, sizeof g_hapPayload);
  215. }
  216. // STATUS payload: [flags][linkSlot][sessCh][nbondMax][count] + per USED bond
  217. // [slotIdx][alive][puuid4][iuuid4][serial16] (26 B). Only used bonds are sent, so the typical 1-2-puck
  218. // frame stays small; the slot index lets the GUI target a bond for removal.
  219. void deckStatusTask()
  220. {
  221. if (millis() - g_lastStatusMs < 250)
  222. return;
  223. g_lastStatusMs = millis();
  224. uint8_t p[5 + NBOND * 26];
  225. bool linkUp = ctrlLinkUp();
  226. p[0] = (uint8_t)((g_forwarding ? 1 : 0) | (linkUp ? 2 : 0));
  227. p[1] = (g_linkSlot >= 0) ? (uint8_t)g_linkSlot : 0xFF;
  228. p[2] = g_sessCh;
  229. p[3] = NBOND;
  230. uint8_t count = 0;
  231. uint8_t *q = p + 5;
  232. for (int i = 0; i < NBOND; i++) {
  233. CtrlBond &b = g_bond[i];
  234. if (!b.used)
  235. continue;
  236. q[0] = (uint8_t)i; // firmware slot index (for GUI removal)
  237. q[1] = (i == g_linkSlot && linkUp) ? 1 : 0;
  238. memcpy(q + 2, b.rec + 0, 4); // proteus_uuid
  239. memcpy(q + 6, b.rec + 4, 4); // ibex_uuid
  240. memcpy(q + 10, b.rec + 8, 16); // puck serial
  241. q += 26;
  242. count++;
  243. }
  244. p[4] = count;
  245. sendFrame(DECK_T_STATUS, p, (uint8_t)(5 + count * 26));
  246. }