triton.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // triton.h -- the controller's native input as it comes off the RF link, and the decoders for it.
  2. //
  3. // "Triton" is the Steam Controller 2 controller. Its HID input report 0x45 is what the puck relays; rf_link.cpp
  4. // decodes one into the shared g_in struct each fresh frame, and every USB personality reads g_in to build its
  5. // own host report. report 0x45 layout:
  6. // [0]=0x45 [1]=seq [2..5]=buttons u32; analog offsets below are from the buttons low byte (rep[2]).
  7. #pragma once
  8. #include <stdint.h>
  9. // ---- Triton button masks (the 32-bit field at rep[2..5]) ----
  10. #define TB_A 0x1u
  11. #define TB_B 0x2u
  12. #define TB_X 0x4u
  13. #define TB_Y 0x8u
  14. #define TB_QAM 0x10u
  15. #define TB_R3 0x20u
  16. #define TB_VIEW 0x40u
  17. #define TB_R4 0x80u
  18. #define TB_R5 0x100u
  19. #define TB_RB 0x200u
  20. #define TB_DDN 0x400u
  21. #define TB_DRT 0x800u
  22. #define TB_DLF 0x1000u
  23. #define TB_DUP 0x2000u
  24. #define TB_MENU 0x4000u
  25. #define TB_L3 0x8000u
  26. #define TB_STEAM 0x10000u
  27. #define TB_L4 0x20000u
  28. #define TB_L5 0x40000u
  29. #define TB_LB 0x80000u
  30. #define TB_RPADT 0x200000u
  31. #define TB_RPADC 0x400000u
  32. #define TB_LPADT 0x2000000u
  33. #define TB_LPADC 0x4000000u
  34. // Virtual PS targets, settable ONLY via a back-paddle/QAM remap (psOrBackCode / tritonFromCode), never by the
  35. // controller itself. They live on bits 30/31 -- the only bits the SC2 0x45 report does NOT use (see PROTOCOL.md
  36. // §8.1; bits 0..29 are all real). They originally aliased 0x00100000 (right stick touch) and 0x01000000 (left
  37. // stick touch), so merely RESTING a thumb on a capacitive stick fired the DualSense trackpad-click / mute.
  38. #define TB_TOUCH 0x40000000u // PS touchpad click
  39. #define TB_MUTE 0x80000000u // PS5 mute
  40. // all four back paddles held -> mode-switch chord guard
  41. #define CHORD_BACK4 (TB_R4 | TB_L4 | TB_R5 | TB_L5)
  42. // analog-trigger fraction (of 0xFF) at which digital ZL/ZR (Switch) etc. trip
  43. #define SW_TRIG_ON 40
  44. // ---- report 0x45 field decoders (offsets relative to rep[2], the buttons low byte) ----
  45. static inline int s16off(const uint8_t *r, int off)
  46. {
  47. int v = r[2 + off] | (r[2 + off + 1] << 8);
  48. return (v & 0x8000) ? v - 0x10000 : v;
  49. }
  50. static inline int u16off(const uint8_t *r, int off)
  51. {
  52. return r[2 + off] | (r[2 + off + 1] << 8);
  53. }
  54. // Controller trigger u16 tops out near half-scale (~0x8000) at a full pull, so a straight >>8 reads only ~0x80
  55. // (host sees a half-pressed trigger). Scale x2 (>>7) and saturate so a full pull maps to 0xFF.
  56. static inline uint8_t trigU8(int u16v)
  57. {
  58. int v = u16v >> 7;
  59. return (uint8_t)(v > 255 ? 255 : v);
  60. }
  61. static inline uint32_t btnsOf(const uint8_t *r)
  62. {
  63. return (uint32_t)r[2] | ((uint32_t)r[3] << 8) | ((uint32_t)r[4] << 16) |
  64. ((uint32_t)r[5] << 24);
  65. }
  66. // report 0x45 IMU offsets (PROTOCOL.md §8): accel @0x22, gyro @0x28 from report start.
  67. void imuFrom45(const uint8_t *r, int16_t *ax, int16_t *ay, int16_t *az,
  68. int16_t *gx, int16_t *gy, int16_t *gz);
  69. // ---- shared decoded input (filled by rf_link.cpp once per fresh report 0x45, read by every mode) ----
  70. // One PuckInput per bond slot: each controller in a multi-slot puck has its own decoded input. The stream
  71. // modes (Switch, PS5, DS4) are single-controller and read g_in[0] from task(); the puck/lizard mode reads
  72. // g_in[slot] inside onReport45/onAuxReport to forward to hid[slot].
  73. struct PuckInput {
  74. // raw Triton buttons (TB_*); per-mode builders apply their own chord masking
  75. uint32_t buttons;
  76. int16_t lx, ly, rx, ry; // sticks (int16, center 0)
  77. uint8_t lt, rt; // triggers scaled 0..255 (trigU8)
  78. int16_t lpx, lpy, rpx, rpy; // left / right trackpad coords (int16)
  79. int16_t ax, ay, az; // accelerometer
  80. int16_t gx, gy, gz; // gyroscope
  81. };
  82. #include "bonds.h" // NSLOT
  83. extern PuckInput g_in[NSLOT];