Sixaxis HID report documentation
================================
Antonio Ospite <ospite@studenti.unina.it>


Input report 0x01
-----------------

The Sixaxis raw input report is 49 bytes, it can be captured with hexdump:

  sudo cat /dev/hidraw1 | hexdump -v -e '49/1 "%02X " "\n"'

A more compact view to avoid line wrapping can be used

  sudo cat /dev/hidraw1 | hexdump -v -e '49/1 "%02X " "\n"' | \
  sed -e 's/\([[:xdigit:]]\{2\}\) \([[:xdigit:]]\{2\}\) \([[:xdigit:]]\{2\}\) \([[:xdigit:]]\{2\}\)/\1\2 \3\4/g'

.Note
sed is used here because 'hexdump' (or 'od') can't handle byte grouping with
odd byte counts.


Data description
~~~~~~~~~~~~~~~~
Analog buttons and axes range from 0x00 to 0xFF.

Motion sensors range from 0x0000 to 0x03FF.

 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
 ---------------------------------------------------------------------
 01 00 00 00 00 00 85 79 85 7D 00 00 00 00 00 00 00 00 00 00 00 00 ...
 `. `. `---4B----. `. `. `. `. `---4B----. `. `. `. `. `. `. `. `.
  |  |           |  |  |  |  |           |  |  |  |  |  |  |  |  |
  |  |           |  |  |  |  |           |  |  |  |  |  |  |  |  `- Analog R1
  |  |           |  |  |  |  |           |  |  |  |  |  |  |  |
  |  |           |  |  |  |  |           |  |  |  |  |  |  |  `- Analog L1
  |  |           |  |  |  |  |           |  |  |  |  |  |  |
  |  |           |  |  |  |  |           |  |  |  |  |  |  `- Analog R2
  |  |           |  |  |  |  |           |  |  |  |  |  |
  |  |           |  |  |  |  |           |  |  |  |  |  `- Analog L2
  |  |           |  |  |  |  |           |  |  |  |  |
  |  |           |  |  |  |  |           |  |  |  |  `- Analog DPad-Left
  |  |           |  |  |  |  |           |  |  |  |
  |  |           |  |  |  |  |           |  |  |  `- Analog DPad-Down
  |  |           |  |  |  |  |           |  |  |
  |  |           |  |  |  |  |           |  |  `- Analog DPad-Right
  |  |           |  |  |  |  |           |  |
  |  |           |  |  |  |  |           |  `- Analog DPad-Up
  |  |           |  |  |  |  |           |
  |  |           |  |  |  |  |           `- Constant to 0
  |  |           |  |  |  |  |
  |  |           |  |  |  |  `- Analog Right Y axis; top: 0x00, bottom: 0xFF
  |  |           |  |  |  |
  |  |           |  |  |  `- Analog Right X axis; left: 0x00, right: 0xFF
  |  |           |  |  |
  |  |           |  |  `- Analog Left Y axis; top: 0x00, bottom: 0xFF
  |  |           |  |
  |  |           |  `- Analog Left X axis; left: 0x00, right: 0xFF
  |  |           |
  |  |           `- Digital Buttons (4 bytes), see Buttons bitmap section
  |  |
  |  `- Unknown
  |
  `- Report id
.
.
 ... 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 ...
 ----------------------------------------------------------------
 ... 00 00 00 00 00 00 00 03 EE 16 FF CB 00 00 93 4B 77 01 DE ...
     `. `. `. `. `--3B--. `. `-----------9B-----------. `---.
      |  |  |  |        |  |                          |     |
      |  |  |  |        |  |                          |     `- Unused sensor?
      |  |  |  |        |  |                          |
      |  |  |  |        |  |                          `- Unknown (Battery?)
      |  |  |  |        |  |
      |  |  |  |        |  `- Charging status? (03:normal, 02:charging, ...)
      |  |  |  |        |
      |  |  |  |        `- Constant to 0 (3 bytes)
      |  |  |  |
      |  |  |  `- Analog Square
      |  |  |
      |  |  `- Analog Cross
      |  |
      |  `- Analog Circle
      |
      `- Analog Triangle
.
.
 ... 42 43 44 45 46 47 48 49
 ---------------------------
 ... FB 01 F5 01 91 01 ED 01
     `---. `---. `---. `---.
         |     |     |     |
         |     |     |     `- Gyro
         |     |     |
         |     |     `- Accel Z
         |     |
         |     `- Accel Y
         |
         `- Accel X

.Note
The device sends the Accel and Gyro data in MSB, the linux driver swaps
it because HID specifies that multi-byte values must be in LSB.
The data above shows the bytes already swapped.


Buttons bitmap (32 bits)
~~~~~~~~~~~~~~~~~~~~~~~~

Looking at the four bytes in LSB order they can be described as an array of 17
bits of variable data followed by 15 bits of constant data.

 DP = Directional Pad
 TB = Thumb Button
 TRIA = Triangle

      bit 7    bit 6    bit 5     bit 4   bit 3    bit 2    bit 1     bit 0
   +---------+--------+---------+-------+--------+---------+--------+--------+
 3 |         |        |         |       |        |         |        |        |
   +---------+--------+---------+-------+--------+---------+--------+--------+
 2 |         |        |         |       |        |         |        | PS     |
   +---------+--------+---------+-------+--------+---------+--------+--------+
 1 | Square  | Cross  | Circle  | TRIA  | R1     | L1      | R2     | L2     |
   +---------+--------+---------+-------+--------+---------+--------+--------+
 0 | DPLeft  | DPDown | DPRight | DPUp  | Start  | TBRight | TBLeft | Select |
   +---------+--------+---------+-------+--------+---------+--------+--------+


Motion sensors data
~~~~~~~~~~~~~~~~~~~

Motion sensors are sent as two bytes each, but they only use 10 significant
bits: looking at the two bytes in LSB they can be represented as a 10 bit
report variable followed by 6 bits of constant data.

      bit 7   bit 6   bit 5   bit 4   bit 3   bit 2   bit 1   bit 0
   +--------+-------+-------+-------+-------+-------+-------+-------+
 1 |        |       |       |       |       |       |   X   |   X   |
   +--------+-------+-------+-------+-------+-------+-------+-------+
 0 |    X   |   X   |   X   |   X   |   X   |   X   |   X   |   X   |
   +--------+-------+-------+-------+-------+-------+-------+-------+

Alternatively the data can be described as a report of 16bits with a Logical
Maximum of 0x3FF.

.Note
Logical min and max in HID specify the extremes of the raw data.
Physical min and max give a meaning to the data.
The HID terminology may be misleading, "physical" in HID refers to how the
user will interpret the data, not to how the machine receives it.

References
----------

Alternative sources about the data report:

* http://onakasuita.org/ps3/
* https://www.circuitsathome.com/mcu/ps3-and-wiimote-game-controllers-on-the-arduino-host-shield-part-2
