mode_switch_hori.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 + A/X/Y): don't pass the face 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);
  92. // face buttons with optional A/B + X/Y swap (Nintendo physical-vs-label layout)
  93. uint16_t fY = g_abSwap ? 0x08 : 0x01, fB = g_abSwap ? 0x04 : 0x02,
  94. fA = g_abSwap ? 0x02 : 0x04, fX = g_abSwap ? 0x01 : 0x08;
  95. if (b & TB_Y)
  96. btn |= fY;
  97. if (b & TB_B)
  98. btn |= fB;
  99. if (b & TB_A)
  100. btn |= fA;
  101. if (b & TB_X)
  102. btn |= fX;
  103. if (b & TB_LB)
  104. btn |= 0x10;
  105. if (b & TB_RB)
  106. btn |= 0x20; // L, R
  107. // ZL/ZR digital: trip on the analog threshold (activates early) OR the full-press click bit
  108. if ((g_in[slot].lt >= SW_TRIG_ON) || (b & 0x8000000u))
  109. btn |= 0x40;
  110. if ((g_in[slot].rt >= SW_TRIG_ON) || (b & 0x800000u))
  111. btn |= 0x80;
  112. if (b & TB_MENU)
  113. btn |= 0x100;
  114. if (b & TB_VIEW)
  115. btn |= 0x200; // Minus, Plus
  116. if (b & TB_L3)
  117. btn |= 0x400;
  118. if (b & TB_R3)
  119. btn |= 0x800; // LClick, RClick
  120. if (b & TB_STEAM)
  121. btn |= 0x1000; // Home
  122. // back paddles -> configurable mapping (same g_back[] as Xbox: default L4->LB R4->RB L5->L3 R5->R3)
  123. if (b & TB_L4)
  124. btn |= codeToSwitch(g_back[0], fA, fB, fX, fY);
  125. if (b & TB_R4)
  126. btn |= codeToSwitch(g_back[1], fA, fB, fX, fY);
  127. if (b & TB_L5)
  128. btn |= codeToSwitch(g_back[2], fA, fB, fX, fY);
  129. if (b & TB_R5)
  130. btn |= codeToSwitch(g_back[3], fA, fB, fX, fY);
  131. if (qam)
  132. btn |= codeToSwitch(g_qamMap, fA, fB, fX, fY);
  133. bool u = b & TB_DUP, d = b & TB_DDN, l = b & TB_DLF,
  134. r = b & TB_DRT; // hat: 0=N..7=NW, 8=neutral
  135. if (b & TB_L4)
  136. backCodeToHatDirs(g_back[0], u, d, l, r);
  137. if (b & TB_R4)
  138. backCodeToHatDirs(g_back[1], u, d, l, r);
  139. if (b & TB_L5)
  140. backCodeToHatDirs(g_back[2], u, d, l, r);
  141. if (b & TB_R5)
  142. backCodeToHatDirs(g_back[3], u, d, l, r);
  143. if (qam)
  144. backCodeToHatDirs(g_qamMap, u, d, l, r);
  145. uint8_t hat = 8;
  146. if (u && r)
  147. hat = 1;
  148. else if (r && d)
  149. hat = 3;
  150. else if (d && l)
  151. hat = 5;
  152. else if (l && u)
  153. hat = 7;
  154. else if (u)
  155. hat = 0;
  156. else if (r)
  157. hat = 2;
  158. else if (d)
  159. hat = 4;
  160. else if (l)
  161. hat = 6;
  162. out[0] = btn & 0xFF;
  163. out[1] = btn >> 8;
  164. out[2] = hat;
  165. out[3] = swStick(g_in[slot].lx, false);
  166. out[4] = swStick(g_in[slot].ly,
  167. true); // HID Y is down-positive -> invert
  168. out[5] = swStick(g_in[slot].rx, false);
  169. out[6] = swStick(g_in[slot].ry, true);
  170. out[7] = 0;
  171. }
  172. // Dynamic-mount mode: begin() is unused (setup() calls beginPool()+usbReenumerate instead).
  173. void SwitchHoriController::begin()
  174. {
  175. }
  176. // Wake mouse (1 HID) is present in Switch mode, leaving CFG_TUD_HID-1 for the HORIPAD pool.
  177. uint8_t SwitchHoriController::maxSlots() const
  178. {
  179. uint8_t cap = (uint8_t)(CFG_TUD_HID - 1);
  180. return cap < NSLOT ? cap : (uint8_t)NSLOT;
  181. }
  182. void SwitchHoriController::usbIdentity()
  183. {
  184. USBDevice.setID(0x0F0D, 0x0092);
  185. USBDevice.setDeviceVersion(0x0210);
  186. USBDevice.setManufacturerDescriptor("HORI CO.,LTD.");
  187. USBDevice.setProductDescriptor("POKKEN CONTROLLER");
  188. }
  189. void SwitchHoriController::beginPool()
  190. {
  191. uint8_t pool = maxSlots();
  192. for (uint8_t s = 0; s < pool; s++) {
  193. g_switch[s].enableOutEndpoint(true);
  194. g_switch[s].setReportDescriptor(SWITCH_HID_DESC,
  195. sizeof SWITCH_HID_DESC);
  196. g_switch[s].setPollInterval(1);
  197. g_switch[s].begin();
  198. }
  199. }
  200. void SwitchHoriController::mountSlots(uint8_t k)
  201. {
  202. for (uint8_t u = 0; u < k; u++)
  203. USBDevice.addInterface(g_switch[u]);
  204. }
  205. void SwitchHoriController::task()
  206. {
  207. // stream the 8-byte HORIPAD report at ~250Hz per CONNECTED controller. usbSlot u -> bond slot for input;
  208. // switchBuildHoripad reads g_in[bond] (no per-USB-slot state in the HORIPAD report).
  209. for (uint8_t u = 0; u < g_usbMountCount; u++) {
  210. if (!g_switch[u].ready())
  211. continue;
  212. if (millis() - g_swLastMs[u] < USB_STREAM_MS)
  213. continue;
  214. int bond = g_usbToBond[u];
  215. if (bond < 0)
  216. continue;
  217. g_swLastMs[u] = millis();
  218. uint8_t p[8];
  219. switchBuildHoripad((uint8_t)bond, p);
  220. usbTxHid(&g_switch[u], 0, p,
  221. sizeof p); // report-id-less descriptor
  222. }
  223. }