triton.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // virtual: PS Touch Click (back-paddle/QAM target, not a real Triton button)
  33. #define TB_TOUCH 0x100000u
  34. // virtual: PS5 Mute button (back-paddle/QAM target, not a real Triton button)
  35. #define TB_MUTE 0x1000000u
  36. #define TB_LPADT 0x2000000u
  37. #define TB_LPADC 0x4000000u
  38. // all four back paddles held -> mode-switch chord guard
  39. #define CHORD_BACK4 (TB_R4 | TB_L4 | TB_R5 | TB_L5)
  40. // analog-trigger fraction (of 0xFF) at which digital ZL/ZR (Switch) etc. trip
  41. #define SW_TRIG_ON 40
  42. // ---- report 0x45 field decoders (offsets relative to rep[2], the buttons low byte) ----
  43. static inline int s16off(const uint8_t *r, int off)
  44. {
  45. int v = r[2 + off] | (r[2 + off + 1] << 8);
  46. return (v & 0x8000) ? v - 0x10000 : v;
  47. }
  48. static inline int u16off(const uint8_t *r, int off)
  49. {
  50. return r[2 + off] | (r[2 + off + 1] << 8);
  51. }
  52. // Controller trigger u16 tops out near half-scale (~0x8000) at a full pull, so a straight >>8 reads only ~0x80
  53. // (host sees a half-pressed trigger). Scale x2 (>>7) and saturate so a full pull maps to 0xFF.
  54. static inline uint8_t trigU8(int u16v)
  55. {
  56. int v = u16v >> 7;
  57. return (uint8_t)(v > 255 ? 255 : v);
  58. }
  59. static inline uint32_t btnsOf(const uint8_t *r)
  60. {
  61. return (uint32_t)r[2] | ((uint32_t)r[3] << 8) | ((uint32_t)r[4] << 16) |
  62. ((uint32_t)r[5] << 24);
  63. }
  64. // report 0x45 IMU offsets (PROTOCOL.md §8): accel @0x22, gyro @0x28 from report start.
  65. void imuFrom45(const uint8_t *r, int16_t *ax, int16_t *ay, int16_t *az,
  66. int16_t *gx, int16_t *gy, int16_t *gz);
  67. // ---- shared decoded input (filled by rf_link.cpp once per fresh report 0x45, read by every mode) ----
  68. struct PuckInput {
  69. // raw Triton buttons (TB_*); per-mode builders apply their own chord masking
  70. uint32_t buttons;
  71. int16_t lx, ly, rx, ry; // sticks (int16, center 0)
  72. uint8_t lt, rt; // triggers scaled 0..255 (trigU8)
  73. int16_t lpx, lpy, rpx, rpy; // left / right trackpad coords (int16)
  74. int16_t ax, ay, az; // accelerometer
  75. int16_t gx, gy, gz; // gyroscope
  76. };
  77. extern PuckInput g_in;