mode_switch_hori.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "mode_switch_hori.h"
  2. #include "triton.h"
  3. #include "gamepad_util.h"
  4. #include "config.h"
  5. #include <Adafruit_TinyUSB.h>
  6. #include <Arduino.h>
  7. SwitchHoriController g_switchHori;
  8. // 8-byte input report: [btn_lo][btn_hi][hat][LX][LY][RX][RY][vendor], sticks uint8 center 0x80.
  9. static const uint8_t SWITCH_HID_DESC[] = {
  10. // Usage Page (Generic Desktop), Usage (Game Pad), Collection (App)
  11. 0x05, 0x01, 0x09, 0x05, 0xA1, 0x01, 0x15, 0x00, 0x25, 0x01, 0x35, 0x00,
  12. 0x45, 0x01, 0x75, 0x01, 0x95, 0x10, // 16 x 1-bit buttons
  13. 0x05, 0x09, 0x19, 0x01, 0x29, 0x10, 0x81, 0x02, 0x05, 0x01, 0x25, 0x07,
  14. 0x46, 0x3B, 0x01, 0x75, 0x04, 0x95, 0x01, 0x65, 0x14, 0x09, 0x39, 0x81,
  15. 0x42, // hat switch (4 bits)
  16. 0x65, 0x00, 0x95, 0x01, 0x81, 0x01, // 4-bit padding to a byte
  17. 0x26, 0xFF, 0x00, 0x46, 0xFF, 0x00, 0x09, 0x30, 0x09, 0x31, 0x09, 0x32,
  18. 0x09, 0x35, 0x75, 0x08, 0x95, 0x04, 0x81, 0x02, // X,Y,Z,Rz
  19. // vendor byte (input)
  20. 0x06, 0x00, 0xFF, 0x09, 0x20, 0x95, 0x01, 0x81, 0x02,
  21. // vendor (output, 8 bytes; unused)
  22. 0x0A, 0x21, 0x26, 0x95, 0x08, 0x91, 0x02, 0xC0
  23. };
  24. static Adafruit_USBD_HID g_switch; // HORIPAD gamepad HID interface
  25. static unsigned long g_swLastMs = 0;
  26. // back-paddle code (g_back[]) -> 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)
  27. static uint16_t codeToSwitch(uint8_t c, uint16_t fA, uint16_t fB, uint16_t fX,
  28. uint16_t fY)
  29. {
  30. switch (c) {
  31. case 1:
  32. return fA;
  33. case 2:
  34. return fB;
  35. case 3:
  36. return fX;
  37. case 4:
  38. return fY;
  39. case 5:
  40. return 0x10;
  41. case 6:
  42. return 0x20;
  43. case 7:
  44. return 0x400;
  45. case 8:
  46. return 0x800;
  47. case 9:
  48. return 0x100;
  49. case 10:
  50. return 0x200;
  51. case 11:
  52. return 0x1000;
  53. default:
  54. return 0;
  55. }
  56. }
  57. // Back-paddle code 12..15 map to D-pad Up/Down/Left/Right; fold them into the hat direction flags.
  58. static inline void backCodeToHatDirs(uint8_t c, bool &u, bool &d, bool &l,
  59. bool &r)
  60. {
  61. if (c == 12)
  62. u = true;
  63. else if (c == 13)
  64. d = true;
  65. else if (c == 14)
  66. l = true;
  67. else if (c == 15)
  68. r = true;
  69. }
  70. // 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
  71. static void switchBuildHoripad(uint8_t out[8])
  72. {
  73. uint32_t b = g_in.buttons;
  74. uint16_t btn = 0;
  75. if (g_qamMap && (b & TB_QAM)) {
  76. b &= ~(uint32_t)TB_QAM;
  77. b |= tritonFromCode(g_qamMap);
  78. }
  79. // Mode-switch chord (all 4 back + A/X/Y): don't pass the face press to the console while the back-4 are held.
  80. if ((b & CHORD_BACK4) == CHORD_BACK4)
  81. b &= ~(uint32_t)(TB_A | TB_X | TB_Y);
  82. // face buttons with optional A/B + X/Y swap (Nintendo physical-vs-label layout)
  83. uint16_t fY = g_abSwap ? 0x08 : 0x01, fB = g_abSwap ? 0x04 : 0x02,
  84. fA = g_abSwap ? 0x02 : 0x04, fX = g_abSwap ? 0x01 : 0x08;
  85. if (b & TB_Y)
  86. btn |= fY;
  87. if (b & TB_B)
  88. btn |= fB;
  89. if (b & TB_A)
  90. btn |= fA;
  91. if (b & TB_X)
  92. btn |= fX;
  93. if (b & TB_LB)
  94. btn |= 0x10;
  95. if (b & TB_RB)
  96. btn |= 0x20; // L, R
  97. // ZL/ZR digital: trip on the analog threshold (activates early) OR the full-press click bit
  98. if ((g_in.lt >= SW_TRIG_ON) || (b & 0x8000000u))
  99. btn |= 0x40;
  100. if ((g_in.rt >= SW_TRIG_ON) || (b & 0x800000u))
  101. btn |= 0x80;
  102. if (b & TB_MENU)
  103. btn |= 0x100;
  104. if (b & TB_VIEW)
  105. btn |= 0x200; // Minus, Plus
  106. if (b & TB_L3)
  107. btn |= 0x400;
  108. if (b & TB_R3)
  109. btn |= 0x800; // LClick, RClick
  110. if (b & TB_STEAM)
  111. btn |= 0x1000; // Home
  112. // back paddles -> configurable mapping (same g_back[] as Xbox: default L4->LB R4->RB L5->L3 R5->R3)
  113. if (b & TB_L4)
  114. btn |= codeToSwitch(g_back[0], fA, fB, fX, fY);
  115. if (b & TB_R4)
  116. btn |= codeToSwitch(g_back[1], fA, fB, fX, fY);
  117. if (b & TB_L5)
  118. btn |= codeToSwitch(g_back[2], fA, fB, fX, fY);
  119. if (b & TB_R5)
  120. btn |= codeToSwitch(g_back[3], fA, fB, fX, fY);
  121. bool u = b & TB_DUP, d = b & TB_DDN, l = b & TB_DLF,
  122. r = b & TB_DRT; // hat: 0=N..7=NW, 8=neutral
  123. if (b & TB_L4)
  124. backCodeToHatDirs(g_back[0], u, d, l, r);
  125. if (b & TB_R4)
  126. backCodeToHatDirs(g_back[1], u, d, l, r);
  127. if (b & TB_L5)
  128. backCodeToHatDirs(g_back[2], u, d, l, r);
  129. if (b & TB_R5)
  130. backCodeToHatDirs(g_back[3], u, d, l, r);
  131. uint8_t hat = 8;
  132. if (u && r)
  133. hat = 1;
  134. else if (r && d)
  135. hat = 3;
  136. else if (d && l)
  137. hat = 5;
  138. else if (l && u)
  139. hat = 7;
  140. else if (u)
  141. hat = 0;
  142. else if (r)
  143. hat = 2;
  144. else if (d)
  145. hat = 4;
  146. else if (l)
  147. hat = 6;
  148. out[0] = btn & 0xFF;
  149. out[1] = btn >> 8;
  150. out[2] = hat;
  151. out[3] = swStick(g_in.lx, false);
  152. out[4] = swStick(g_in.ly, true); // HID Y is down-positive -> invert
  153. out[5] = swStick(g_in.rx, false);
  154. out[6] = swStick(g_in.ry, true);
  155. out[7] = 0;
  156. }
  157. void SwitchHoriController::begin()
  158. {
  159. USBDevice.setID(0x0F0D, 0x0092);
  160. // Windows caches config by VID:PID:bcdDevice, so any interface change MUST bump this
  161. USBDevice.setDeviceVersion(0x0202);
  162. USBDevice.setManufacturerDescriptor("HORI CO.,LTD.");
  163. USBDevice.setProductDescriptor("POKKEN CONTROLLER");
  164. g_switch.enableOutEndpoint(true);
  165. g_switch.setReportDescriptor(SWITCH_HID_DESC, sizeof SWITCH_HID_DESC);
  166. // 1ms bInterval so the RF rate is the only latency limit (matches Xbox)
  167. g_switch.setPollInterval(1);
  168. g_switch.begin();
  169. }
  170. void SwitchHoriController::task()
  171. { // stream the 8-byte HORIPAD report at ~250Hz (no handshake needed)
  172. if (!g_switch.ready())
  173. return;
  174. if (millis() - g_swLastMs < USB_STREAM_MS)
  175. return;
  176. g_swLastMs = millis();
  177. uint8_t p[8];
  178. switchBuildHoripad(p);
  179. g_switch.sendReport(0, p, sizeof p); // report-id-less descriptor
  180. }