mode_switch_hori.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "mode_switch_hori.h"
  2. #include "triton.h"
  3. #include "gamepad_util.h"
  4. #include "config.h"
  5. #include "bonds.h"
  6. #include "usb_mount.h"
  7. #include "usb_tx.h"
  8. #include <Adafruit_TinyUSB.h>
  9. #include <Arduino.h>
  10. SwitchHoriController g_switchHori;
  11. // 8-byte input report: [btn_lo][btn_hi][hat][LX][LY][RX][RY][vendor], sticks uint8 center 0x80.
  12. static const uint8_t SWITCH_HID_DESC[] = {
  13. // Usage Page (Generic Desktop), Usage (Game Pad), Collection (App)
  14. 0x05, 0x01, 0x09, 0x05, 0xA1, 0x01, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00,
  15. 0x45, 0x01, 0x75, 0x01, 0x95, 0x10, // 16 x 1-bit buttons
  16. 0x05, 0x09, 0x19, 0x01, 0x29, 0x10, 0x81, 0x02, 0x05, 0x01, 0x25, 0x07,
  17. 0x46, 0x3B, 0x01, 0x75, 0x04, 0x95, 0x01, 0x65, 0x14, 0x09, 0x39, 0x81,
  18. 0x42, // hat switch (4 bits)
  19. 0x65, 0x00, 0x95, 0x01, 0x81, 0x01, // 4-bit padding to a byte
  20. 0x26, 0xFF, 0x00, 0x46, 0xFF, 0x00, 0x09, 0x30, 0x09, 0x31, 0x09, 0x32,
  21. 0x09, 0x35, 0x75, 0x08, 0x95, 0x04, 0x81, 0x02, // X,Y,Z,Rz
  22. // vendor byte (input)
  23. 0x06, 0x00, 0xFF, 0x09, 0x20, 0x95, 0x01, 0x81, 0x02,
  24. // vendor (output, 8 bytes; unused)
  25. 0x0A, 0x21, 0x26, 0x95, 0x08, 0x91, 0x02, 0xC0
  26. };
  27. // NSLOT HORIPAD HID instances -- one per bond slot. The Switch console binds each as a separate gamepad.
  28. static Adafruit_USBD_HID g_switch[NSLOT];
  29. static unsigned long g_swLastMs[NSLOT] = { 0 };
  30. // back-paddle / QAM code (g_back[], g_qamMap) -> Switch button bit. 0=none 1=A 2=B 3=X 4=Y 5=LB 6=RB 7=L3 8=R3 9=Back(Minus) 10=Start(Plus) 11=Guide(Home) 18=Capture
  31. static uint16_t codeToSwitch(uint8_t c, uint16_t fA, uint16_t fB, uint16_t fX,
  32. uint16_t fY)
  33. {
  34. switch (c) {
  35. case 1:
  36. return fA;
  37. case 2:
  38. return fB;
  39. case 3:
  40. return fX;
  41. case 4:
  42. return fY;
  43. case 5:
  44. return 0x10;
  45. case 6:
  46. return 0x20;
  47. case 7:
  48. return 0x400;
  49. case 8:
  50. return 0x800;
  51. case 9:
  52. return 0x100;
  53. case 10:
  54. return 0x200;
  55. case 11:
  56. return 0x1000;
  57. case 18:
  58. return 0x2000; // Capture / Screenshot (Switch-only target)
  59. case 19:
  60. return 0x40; // ZL (left trigger)
  61. case 20:
  62. return 0x80; // ZR (right trigger)
  63. default:
  64. return 0;
  65. }
  66. }
  67. // Back-paddle code 12..15 map to D-pad Up/Down/Left/Right; fold them into the hat direction flags.
  68. static inline void backCodeToHatDirs(uint8_t c, bool &u, bool &d, bool &l,
  69. bool &r)
  70. {
  71. if (c == 12)
  72. u = true;
  73. else if (c == 13)
  74. d = true;
  75. else if (c == 14)
  76. l = true;
  77. else if (c == 15)
  78. r = true;
  79. }
  80. // HORIPAD/Switch button bits: Y=1 B=2 A=4 X=8 L=10 R=20 ZL=40 ZR=80 Minus=100 Plus=200 LClick=400 RClick=800 Home=1000 Capture=2000
  81. // Per-slot: each controller's decoded input is in g_in[slot].
  82. static void switchBuildHoripad(uint8_t slot, uint8_t out[8])
  83. {
  84. uint32_t b = g_in[slot].buttons;
  85. uint16_t btn = 0;
  86. // QAM (3 dots) remap: the configured code is applied to the Switch output just like a back paddle below
  87. // (so Capture(18) and every Switch target work). 0 = unmapped -> QAM does nothing on Switch.
  88. bool qam = g_qamMap && (b & TB_QAM);
  89. // Mode-switch chord (all 4 back + face/dpad): don't pass the press to the console while the back-4 are held.
  90. if ((b & CHORD_BACK4) == CHORD_BACK4)
  91. b &= ~(uint32_t)(TB_A | TB_X | TB_Y | TB_DUP | TB_DDN | TB_DLF |
  92. TB_DRT);
  93. // face buttons with optional A/B + X/Y swap (Nintendo physical-vs-label layout)
  94. uint16_t fY = g_abSwap ? 0x08 : 0x01, fB = g_abSwap ? 0x04 : 0x02,
  95. fA = g_abSwap ? 0x02 : 0x04, fX = g_abSwap ? 0x01 : 0x08;
  96. if (b & TB_Y)
  97. btn |= fY;
  98. if (b & TB_B)
  99. btn |= fB;
  100. if (b & TB_A)
  101. btn |= fA;
  102. if (b & TB_X)
  103. btn |= fX;
  104. if (b & TB_LB)
  105. btn |= 0x10;
  106. if (b & TB_RB)
  107. btn |= 0x20; // L, R
  108. // ZL/ZR digital: trip on the analog threshold (activates early) OR the full-press click bit
  109. if ((g_in[slot].lt >= SW_TRIG_ON) || (b & 0x8000000u))
  110. btn |= 0x40;
  111. if ((g_in[slot].rt >= SW_TRIG_ON) || (b & 0x800000u))
  112. btn |= 0x80;
  113. if (b & TB_MENU)
  114. btn |= 0x100;
  115. if (b & TB_VIEW)
  116. btn |= 0x200; // Minus, Plus
  117. if (b & TB_L3)
  118. btn |= 0x400;
  119. if (b & TB_R3)
  120. btn |= 0x800; // LClick, RClick
  121. if (b & TB_STEAM)
  122. btn |= 0x1000; // Home
  123. // back paddles -> configurable mapping (same g_back[] as Xbox: default L4->LB R4->RB L5->L3 R5->R3)
  124. if (b & TB_L4)
  125. btn |= codeToSwitch(g_back[0], fA, fB, fX, fY);
  126. if (b & TB_R4)
  127. btn |= codeToSwitch(g_back[1], fA, fB, fX, fY);
  128. if (b & TB_L5)
  129. btn |= codeToSwitch(g_back[2], fA, fB, fX, fY);
  130. if (b & TB_R5)
  131. btn |= codeToSwitch(g_back[3], fA, fB, fX, fY);
  132. if (qam)
  133. btn |= codeToSwitch(g_qamMap, fA, fB, fX, fY);
  134. bool u = b & TB_DUP, d = b & TB_DDN, l = b & TB_DLF,
  135. r = b & TB_DRT; // hat: 0=N..7=NW, 8=neutral
  136. if (b & TB_L4)
  137. backCodeToHatDirs(g_back[0], u, d, l, r);
  138. if (b & TB_R4)
  139. backCodeToHatDirs(g_back[1], u, d, l, r);
  140. if (b & TB_L5)
  141. backCodeToHatDirs(g_back[2], u, d, l, r);
  142. if (b & TB_R5)
  143. backCodeToHatDirs(g_back[3], u, d, l, r);
  144. if (qam)
  145. backCodeToHatDirs(g_qamMap, u, d, l, r);
  146. uint8_t hat = 8;
  147. if (u && r)
  148. hat = 1;
  149. else if (r && d)
  150. hat = 3;
  151. else if (d && l)
  152. hat = 5;
  153. else if (l && u)
  154. hat = 7;
  155. else if (u)
  156. hat = 0;
  157. else if (r)
  158. hat = 2;
  159. else if (d)
  160. hat = 4;
  161. else if (l)
  162. hat = 6;
  163. out[0] = btn & 0xFF;
  164. out[1] = btn >> 8;
  165. out[2] = hat;
  166. int16_t lx, ly, rx, ry;
  167. slotSticks(slot, &lx, &ly, &rx, &ry);
  168. out[3] = swStick(lx, false);
  169. out[4] = swStick(ly, true); // HID Y is down-positive -> invert
  170. out[5] = swStick(rx, false);
  171. out[6] = swStick(ry, true);
  172. out[7] = 0;
  173. }
  174. // Dynamic-mount mode: begin() is unused (setup() calls beginPool()+usbReenumerate instead).
  175. void SwitchHoriController::begin()
  176. {
  177. }
  178. // Wake mouse (1 HID) is present in Switch mode, leaving CFG_TUD_HID-1 for the HORIPAD pool.
  179. uint8_t SwitchHoriController::maxSlots() const
  180. {
  181. uint8_t cap = (uint8_t)(CFG_TUD_HID - 1);
  182. return cap < NSLOT ? cap : (uint8_t)NSLOT;
  183. }
  184. void SwitchHoriController::usbIdentity()
  185. {
  186. USBDevice.setID(0x0F0D, 0x0092);
  187. USBDevice.setVersion(0x0200);
  188. USBDevice.setDeviceVersion(0x0210);
  189. USBDevice.setManufacturerDescriptor("HORI CO.,LTD.");
  190. USBDevice.setProductDescriptor("POKKEN CONTROLLER");
  191. }
  192. void SwitchHoriController::beginPool()
  193. {
  194. uint8_t pool = maxSlots();
  195. for (uint8_t s = 0; s < pool; s++) {
  196. g_switch[s].enableOutEndpoint(true);
  197. g_switch[s].setReportDescriptor(SWITCH_HID_DESC,
  198. sizeof SWITCH_HID_DESC);
  199. g_switch[s].setPollInterval(1);
  200. g_switch[s].begin();
  201. }
  202. }
  203. void SwitchHoriController::mountSlots(uint8_t k)
  204. {
  205. for (uint8_t u = 0; u < k; u++)
  206. USBDevice.addInterface(g_switch[u]);
  207. }
  208. void SwitchHoriController::task()
  209. {
  210. // stream the 8-byte HORIPAD report at ~250Hz per CONNECTED controller. usbSlot u -> bond slot for input;
  211. // switchBuildHoripad reads g_in[bond] (no per-USB-slot state in the HORIPAD report).
  212. for (uint8_t u = 0; u < g_usbMountCount; u++) {
  213. if (!g_switch[u].ready())
  214. continue;
  215. if (millis() - g_swLastMs[u] < USB_STREAM_MS)
  216. continue;
  217. int bond = g_usbToBond[u];
  218. if (bond < 0)
  219. continue;
  220. g_swLastMs[u] = millis();
  221. uint8_t p[8];
  222. switchBuildHoripad((uint8_t)bond, p);
  223. usbTxHid(&g_switch[u], 0, p,
  224. sizeof p); // report-id-less descriptor
  225. }
  226. }