triton.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // triton.h -- the controller's native input model + report 0x45 field offsets.
  2. //
  3. // *** Copied from OpenPuck/triton.h (the shared input contract). ***
  4. // On the PUCK, rf_link.cpp DECODES an incoming report 0x45 into g_in. On the CONTROLLER emulator we go
  5. // the other way: deck_input.cpp fills g_in from the Steam Deck, and report45.cpp ENCODES g_in back into
  6. // a report 0x45 to transmit. The decoders below are kept so the offsets are documented in one place and
  7. // the round-trip is provably symmetric. report 0x45 layout:
  8. // [0]=0x45 [1]=seq [2..5]=buttons u32; analog offsets below are from the buttons low byte (rep[2]).
  9. #pragma once
  10. #include <stdint.h>
  11. // ---- Triton button masks (the 32-bit field at rep[2..5]) ----
  12. #define TB_A 0x1u
  13. #define TB_B 0x2u
  14. #define TB_X 0x4u
  15. #define TB_Y 0x8u
  16. #define TB_QAM 0x10u
  17. #define TB_R3 0x20u
  18. #define TB_VIEW 0x40u
  19. #define TB_R4 0x80u
  20. #define TB_R5 0x100u
  21. #define TB_RB 0x200u
  22. #define TB_DDN 0x400u
  23. #define TB_DRT 0x800u
  24. #define TB_DLF 0x1000u
  25. #define TB_DUP 0x2000u
  26. #define TB_MENU 0x4000u
  27. #define TB_L3 0x8000u
  28. #define TB_STEAM 0x10000u
  29. #define TB_L4 0x20000u
  30. #define TB_L5 0x40000u
  31. #define TB_LB 0x80000u
  32. #define TB_RPADT 0x200000u
  33. #define TB_RPADC 0x400000u
  34. #define TB_LPADT 0x2000000u
  35. #define TB_LPADC 0x4000000u
  36. // all four back paddles held -> mode-switch chord guard
  37. #define CHORD_BACK4 (TB_R4 | TB_L4 | TB_R5 | TB_L5)
  38. // analog-trigger fraction (of 0xFF) at which digital ZL/ZR (Switch) etc. trip
  39. #define SW_TRIG_ON 40
  40. // ---- report 0x45 field decoders (offsets relative to rep[2], the buttons low byte) ----
  41. static inline int s16off(const uint8_t *r, int off)
  42. {
  43. int v = r[2 + off] | (r[2 + off + 1] << 8);
  44. return (v & 0x8000) ? v - 0x10000 : v;
  45. }
  46. static inline int u16off(const uint8_t *r, int off)
  47. {
  48. return r[2 + off] | (r[2 + off + 1] << 8);
  49. }
  50. // Controller trigger u16 tops out near half-scale (~0x8000) at a full pull, so a straight >>8 reads only
  51. // ~0x80. Scale x2 (>>7) and saturate so a full pull maps to 0xFF.
  52. static inline uint8_t trigU8(int u16v)
  53. {
  54. int v = u16v >> 7;
  55. return (uint8_t)(v > 255 ? 255 : v);
  56. }
  57. static inline uint32_t btnsOf(const uint8_t *r)
  58. {
  59. return (uint32_t)r[2] | ((uint32_t)r[3] << 8) | ((uint32_t)r[4] << 16) |
  60. ((uint32_t)r[5] << 24);
  61. }
  62. // report 0x45 IMU offsets (PROTOCOL.md §8): accel @0x22, gyro @0x28 from report start.
  63. void imuFrom45(const uint8_t *r, int16_t *ax, int16_t *ay, int16_t *az,
  64. int16_t *gx, int16_t *gy, int16_t *gz);
  65. // ---- shared decoded input (filled by deck_input.cpp, read by report45.cpp) ----
  66. struct PuckInput {
  67. // raw Triton buttons (TB_*)
  68. uint32_t buttons;
  69. int16_t lx, ly, rx, ry; // sticks (int16, center 0)
  70. uint8_t lt, rt; // triggers scaled 0..255 (trigU8)
  71. int16_t lpx, lpy, rpx, rpy; // left / right trackpad coords (int16)
  72. int16_t ax, ay, az; // accelerometer
  73. int16_t gx, gy, gz; // gyroscope
  74. };
  75. extern PuckInput g_in;