aed3 / PS4-esp32

Use a ps4 controller with an esp32
328 stars 103 forks source link

Parse additional PS4 information from the 64 byte messages. #22

Open KurtE opened 2 years ago

KurtE commented 2 years ago

As I mentioned in another issue today. @mjs513 and myself have been working on the Teensy T3.6/T4.1 library USBHost_t36 that adds USB Host support including Bluetooth and Joysticks including the PS4... So when I find issues with some devices like clone PS4s that will not connect, I try other hardware and libraries to see if they work on the alternatives...

Also Trossen Robotics now has a setup of their PhantomX using a PS4 and ESP32 using this library to then talk to an OpenCM 9.04 board, which I would like to extend.

So while playing with an ESP32 and this library, I thought I would see if this library exposes some of the additional information that one of our USBHost Joystick sketch exposes, such as the touchpad as well as the Gyro and Accel sensors. Which I found was not currently implemented. Looked like there was a start of this, but code was commented out.

So I added some code to extract that as part of a test sketch, which I added to my fork/branch: https://github.com/KurtE/PS4-esp32/tree/build_fixes

Most of the interesting stuff is in the lines:

      // Trackpad touch 1: id, active, x, y
      uint16_t xc = ((PS4.data.latestPacket[37 + 12] & 0x0f) << 8) | PS4.data.latestPacket[36 + 12];
      uint16_t yc = PS4.data.latestPacket[38 + 12] << 4 | ((PS4.data.latestPacket[37 + 12] & 0xf0) >> 4);
      uint8_t  isTouched = (PS4.data.latestPacket[35 + 12] & 0x80) == 0;
      if (!isTouched) xc = yc = 0;
      Serial.printf(" - %4u %4u", xc, yc);

      // Accel info
      int accelx = (int16_t)(PS4.data.latestPacket[12 + 20] << 8) | PS4.data.latestPacket[12 + 19];
      int accelz = (int16_t)(PS4.data.latestPacket[12 + 22] << 8) | PS4.data.latestPacket[12 + 21];
      int accely = (int16_t)(PS4.data.latestPacket[12 + 24] << 8) | PS4.data.latestPacket[12 + 23];

      float ax = (float) accelx / 8192;
      float ay = (float) accely / 8192;
      float az = (float) accelz / 8192;
      Serial.printf(" - %4d(%3.2f) %4d(%3.2f) %4d(%3.2f)", accelx, ax, accely, ay, accelz, az);

      // Gyro info
      int gyroy = (int16_t)(PS4.data.latestPacket[12 + 14] << 8) | PS4.data.latestPacket[12 + 13];
      int gyroz = (int16_t)(PS4.data.latestPacket[12 + 16] << 8) | PS4.data.latestPacket[12 + 15];
      int gyrox = (int16_t)(PS4.data.latestPacket[12 + 18] << 8) | PS4.data.latestPacket[12 + 17];

      float gx = (float) gyrox * RAD_TO_DEG / 1024;
      float gy = (float) gyroy * RAD_TO_DEG / 1024;
      float gz = (float) gyroz * RAD_TO_DEG / 1024;
      Serial.printf(" - %4d(%3.2f) %4d(%3.2f) %4d(%3.2f)\n", gyrox, gx, gyroy, gy, gyroz, gz);

Note: most of these lines add 12 to an index as the arrays of data we are processing are indexed slightly differently: Most of this code comes from the file: https://github.com/PaulStoffregen/USBHost_t36/blob/master/examples/Joystick/ps4Helpers.ino And the indexes I am working off of are shown in the file: https://github.com/PaulStoffregen/USBHost_t36/blob/master/joystick.cpp#L1021

Not sure if you wish to integrate any of this in your library and/or potentially document it somewhere for sketches to do something similar.

Kurt

TomOnderwater commented 1 year ago

cool! I was looking for this