report45.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include "report45.h"
  2. #include "triton.h"
  3. #include <string.h>
  4. // Write a signed 16-bit value little-endian at rep[2+off] (the same indexing triton.h's s16off reads).
  5. static inline void put_s16(uint8_t *rep, int off, int16_t v)
  6. {
  7. rep[2 + off] = (uint8_t)(v & 0xFF);
  8. rep[2 + off + 1] = (uint8_t)((v >> 8) & 0xFF);
  9. }
  10. // Write an unsigned 16-bit value little-endian at rep[2+off] (matches u16off).
  11. static inline void put_u16(uint8_t *rep, int off, uint16_t v)
  12. {
  13. rep[2 + off] = (uint8_t)(v & 0xFF);
  14. rep[2 + off + 1] = (uint8_t)((v >> 8) & 0xFF);
  15. }
  16. void buildReport45(uint8_t *out, uint8_t seq)
  17. {
  18. memset(out, 0, REPORT45_LEN);
  19. out[0] = 0x45;
  20. out[1] = seq;
  21. // buttons u32 at rep[2..5] (inverse of btnsOf)
  22. out[2] = (uint8_t)(g_in.buttons & 0xFF);
  23. out[3] = (uint8_t)((g_in.buttons >> 8) & 0xFF);
  24. out[4] = (uint8_t)((g_in.buttons >> 16) & 0xFF);
  25. out[5] = (uint8_t)((g_in.buttons >> 24) & 0xFF);
  26. // triggers: the controller's analog trigger is a u16 that trigU8 maps via >>7 (saturating). Inverse:
  27. // u16 = lt << 7 (so a full 0xFF pull -> 0x7F80 -> trigU8 -> 0xFF). offset 4 = LT, offset 6 = RT.
  28. put_u16(out, 4, (uint16_t)(g_in.lt << 7));
  29. put_u16(out, 6, (uint16_t)(g_in.rt << 7));
  30. // sticks (int16, center 0): lx@8 ly@10 rx@12 ry@14
  31. put_s16(out, 8, g_in.lx);
  32. put_s16(out, 10, g_in.ly);
  33. put_s16(out, 12, g_in.rx);
  34. put_s16(out, 14, g_in.ry);
  35. // trackpads: LPad x@16 y@18 pressure@20, RPad x@22 y@24 pressure@26 (per PROTOCOL.md report 0x45)
  36. put_s16(out, 16, g_in.lpx);
  37. put_s16(out, 18, g_in.lpy);
  38. put_u16(out, 20, g_in.lpp);
  39. put_s16(out, 22, g_in.rpx);
  40. put_s16(out, 24, g_in.rpy);
  41. put_u16(out, 26, g_in.rpp);
  42. // IMU: accel @0x22 (offset 32), gyro @0x28 (offset 38) -- inverse of imuFrom45
  43. put_s16(out, 32, g_in.ax);
  44. put_s16(out, 34, g_in.ay);
  45. put_s16(out, 36, g_in.az);
  46. put_s16(out, 38, g_in.gx);
  47. put_s16(out, 40, g_in.gy);
  48. put_s16(out, 42, g_in.gz);
  49. }