mode_ps5.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "mode_ps5.h"
  2. #include "triton.h"
  3. #include "gamepad_util.h"
  4. #include "config.h"
  5. #include <Adafruit_TinyUSB.h>
  6. #include <Arduino.h>
  7. #include <string.h>
  8. Ps5Controller g_ps5Ctl;
  9. static const uint8_t PS5_HID_DESC[]={
  10. 0x05,0x01,0x09,0x05,0xA1,0x01,0x85,0x01,0x09,0x30,0x09,0x31,0x09,0x32,0x09,0x35,
  11. 0x09,0x33,0x09,0x34,0x15,0x00,0x26,0xFF,0x00,0x75,0x08,0x95,0x06,0x81,0x02,0x06,
  12. 0x00,0xFF,0x09,0x20,0x95,0x01,0x81,0x02,0x05,0x01,0x09,0x39,0x15,0x00,0x25,0x07,
  13. 0x35,0x00,0x46,0x3B,0x01,0x65,0x14,0x75,0x04,0x95,0x01,0x81,0x42,0x65,0x00,0x05,
  14. 0x09,0x19,0x01,0x29,0x0F,0x15,0x00,0x25,0x01,0x75,0x01,0x95,0x0F,0x81,0x02,0x06,
  15. 0x00,0xFF,0x09,0x21,0x95,0x0D,0x81,0x02,0x06,0x00,0xFF,0x09,0x22,0x15,0x00,0x26,
  16. 0xFF,0x00,0x75,0x08,0x95,0x34,0x81,0x02,0x85,0x02,0x09,0x23,0x95,0x2F,0x91,0x02,
  17. 0x85,0x05,0x09,0x33,0x95,0x28,0xB1,0x02,0x85,0x08,0x09,0x34,0x95,0x2F,0xB1,0x02,
  18. 0x85,0x09,0x09,0x24,0x95,0x13,0xB1,0x02,0x85,0x0A,0x09,0x25,0x95,0x1A,0xB1,0x02,
  19. 0x85,0x20,0x09,0x26,0x95,0x3F,0xB1,0x02,0x85,0x21,0x09,0x27,0x95,0x04,0xB1,0x02,
  20. 0x85,0x22,0x09,0x40,0x95,0x3F,0xB1,0x02,0x85,0x80,0x09,0x28,0x95,0x3F,0xB1,0x02,
  21. 0x85,0x81,0x09,0x29,0x95,0x3F,0xB1,0x02,0x85,0x82,0x09,0x2A,0x95,0x09,0xB1,0x02,
  22. 0x85,0x83,0x09,0x2B,0x95,0x3F,0xB1,0x02,0x85,0x84,0x09,0x2C,0x95,0x3F,0xB1,0x02,
  23. 0x85,0x85,0x09,0x2D,0x95,0x02,0xB1,0x02,0x85,0xA0,0x09,0x2E,0x95,0x01,0xB1,0x02,
  24. 0x85,0xE0,0x09,0x2F,0x95,0x3F,0xB1,0x02,0x85,0xF0,0x09,0x30,0x95,0x3F,0xB1,0x02,
  25. 0x85,0xF1,0x09,0x31,0x95,0x3F,0xB1,0x02,0x85,0xF2,0x09,0x32,0x95,0x0F,0xB1,0x02,
  26. 0x85,0xF4,0x09,0x35,0x95,0x3F,0xB1,0x02,0x85,0xF5,0x09,0x36,0x95,0x03,0xB1,0x02,0xC0
  27. };
  28. #define PS5_TOUCH_H 1080
  29. #define PS5_STATUS_USB 0x1A // charging + level 10 (~100%)
  30. static unsigned long g_ps5LastMs=0;
  31. static Adafruit_USBD_HID g_ps5;
  32. static void ps5Build(uint8_t out[63]){
  33. uint32_t b=psButtonsFromSteam(g_in.buttons);
  34. bool lTouch=(b&TB_LPADT)||(b&TB_LPADC), rTouch=(b&TB_RPADT)||(b&TB_RPADC);
  35. memset(out,0,63);
  36. out[0]=swStick(g_in.lx,false); out[1]=swStick(g_in.ly,true);
  37. out[2]=swStick(g_in.rx,false); out[3]=swStick(g_in.ry,true);
  38. out[4]=g_in.lt; out[5]=g_in.rt;
  39. static uint8_t seq=0; out[6]=seq++;
  40. out[7]=psHatNibble(b)|psFaceNibble(b);
  41. out[8]=psShouldersByte(b);
  42. out[9]=((b&TB_STEAM)?0x01:0)|((b&TB_LPADC||b&TB_RPADC)?0x02:0);
  43. out[15]=g_in.gx&0xFF; out[16]=g_in.gx>>8;
  44. out[17]=g_in.gz&0xFF; out[18]=g_in.gz>>8;
  45. out[19]=g_in.gy&0xFF; out[20]=g_in.gy>>8;
  46. out[21]=g_in.ax&0xFF; out[22]=g_in.ax>>8;
  47. out[23]=g_in.ay&0xFF; out[24]=g_in.ay>>8;
  48. out[25]=g_in.az&0xFF; out[26]=g_in.az>>8;
  49. uint16_t lx, ly, rx, ry;
  50. steamPadsToTouch(b, PS5_TOUCH_H, g_in.lpx, g_in.lpy, g_in.rpx, g_in.rpy, &lx, &ly, &rx, &ry);
  51. touchPackPads(out+32, lTouch, rTouch, lx, ly, rx, ry);
  52. out[52]=PS5_STATUS_USB;
  53. }
  54. void Ps5Controller::begin(){
  55. USBDevice.setID(0x054C, 0x0CE6);
  56. USBDevice.setDeviceVersion(0x0101); // bumped from 0x0100 for the added wake-mouse interface (Windows caches config by VID:PID:bcdDevice)
  57. USBDevice.setManufacturerDescriptor("Sony Interactive Entertainment");
  58. USBDevice.setProductDescriptor("DualSense Wireless Controller");
  59. g_ps5.enableOutEndpoint(true);
  60. g_ps5.setReportDescriptor(PS5_HID_DESC, sizeof PS5_HID_DESC);
  61. g_ps5.setPollInterval(4);
  62. g_ps5.begin();
  63. }
  64. void Ps5Controller::task(){
  65. if(!g_ps5.ready()) return;
  66. if(millis()-g_ps5LastMs<USB_STREAM_MS) return; g_ps5LastMs=millis();
  67. uint8_t p[63]; ps5Build(p);
  68. g_ps5.sendReport(0x01, p, sizeof p);
  69. }