Page 1 of 1

Hid protocol for a Gamepad

Posted: Mon Nov 05, 2012 9:12 am
by Feelg
In order to use an old Logitech Gamepad as a cheap input interface, to get the buttons events to trigger some scripts like reboot, umount USB disk, send report by email, .... I would like to be able to interpret the HID frames that I can read in /dev/input/js0

I can see that a have 16 bytes frames each time I click on a button, but I would like to understand what I get. I can't find on Internet a simple description of the frames, does one of you already did this ?


Re: Hid protocol for a Gamepad

Posted: Mon Nov 05, 2012 2:09 pm
by Feelg
Ok, I could make some progress, with my logitech rumblepad, I got frames of 8 bytes :


EE FF : Signed value in 2 complement
GG : 1 for push buttons and 2 for analog pads
HH : number of the button

This is quite nice for a first start.
The AA BB CC DD are quite dark for me.

Here my C++ code I used to reverse engenere the frames :

Code: Select all

#include <stdio.h>
#include <stdlib.h>

#define FS 8

int main(int argc, char* argv[])
   printf("Start PAD observer\n");

   FILE* f = fopen("/dev/input/js0", "r");
   if( f ) {
      unsigned char buf[256];
      int nb = 0;
      while( nb >= 0 ) {
         int i = 0;
         while( i < nb ) {
            printf("%02X ", buf[i]);
         if( nb == FS ) {
            short value = (buf[5] << 8) | buf[4];
            int btnum = buf[7];
            int group = buf[6];
            printf("  Group=%d BT=%d Value=%d\n", group, btnum, value);
         nb = fread(buf, 1, FS, f);

   } else {
      printf("Can't open file\n");
   printf("End PAD observer\n");

Re: Hid protocol for a Gamepad

Posted: Mon Nov 05, 2012 4:39 pm
by toxibunny
Aren't the first few digits in those types of results just some sort of timestamp?

Re: Hid protocol for a Gamepad

Posted: Tue Nov 06, 2012 4:58 pm
by Feelg
Thank you, it is exaclty this, timestamp :

Code: Select all

unsigned int tist = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
gives a number in milisecond, with the origin set to the machine startup.

Thank you.