• Hi there !

    I was finally able to find the time to implement a "standard" USB HID GamePad as an Espruino module ( js only for now ) :D
    Currently, the triggers are treated as digital buttons, but I'll soon take the time to update the code to support analog triggers ;)

    So, YES, Espruino's should now be able to play CoD & GTA ;p .. and make some perfects on guitar hero hardcore ? -> better: make it count & let's do an Espruino-adaptive-controller !!! ;P ( seriously, I'm on it ;) )

    => enjoy :)

    basic usage is as simple as Gordon's keyboard & mouse modules:

    var gamepad = require('USBGamepad');
    // debug easily using http://html5gamepad.com/ ;p
    gamepad.sendGamepadState(0b1111111111111­111, 127, 127, 127, 127);
    gamepad.sendGamepadState(0b1111111111111­111, -127, -127, -127, -127);
    

    I plan to:

    • maybe add possibility of using analog values for buttons as well ?
    • or so only for the "big" triggers ?

    I also consider writing a little wrapper to make it easier to work with if needed:

    // press & release buttons
    gamepad.press(<btnConst>); // press one
    gamepad.press([<btnConst>, <joyConst>, ..]); // press many
    gamepad.<btnConst>.press(); // press one
    gamepad.<joyConst>.press(); // same for joysticks buttons
    gamepad.<btnConst>.release // release one
    gamepad.release(<btnConst>, <joyConst>, ..]); // release many
    // joysticks only: range [-127..127]
    gamepad.<joyConst>.move(<xValue>, <yValue>);
    gamepad.<joyConst>.moveX(<value>);
    gamepad.<joyConst>.moveY(<value>);
    // triggers only: range [0..255]
    gamepad.<trigConst>.press(<value>); // if analog, 'll range [0..255]
    // global
    gamepad.set({ <const>:<value>, .. });
    // update
    gamepad.sendState(); // or '.update()' ?
    

    Advises are welcome to the above draf of api :)

    I also wonder if it'd be somewhat useful to retain the last gamepad state before any further update, as well as the timestamp & the timestamp diff with previous one ? ..

    The module should also work to send stuff to an RN42* ( or an HC-05 with its HID firmware ) or an Espruino board based on bluetooth

    • just add 0xFD, 0x06 & 0x01 before the stuff to be sent ;)

    I'll also try to get 2 gamepads & adding the possibility to pass a number of those to the module before it calls 'E.setUSBHID()' after a little rest ( digging through the USB HID docs can make one's eyes burn .. )

    On a near subject ( & sorry if I repeat myself ), I have a wip lib aimed to generate hid report descriptors ( currently just easing the mapping between constants & their values ): it's not much but it 'd have been very helpful to write the above ;/ ( I'll add the constants digged to it as soon as can do ;p )

    var reportDescriptorProto = [
      { USAGE_PAGE: 'Generic Desktop' },
      { USAGE: 'Mouse' },
      { COLLECTION: 'Application' },
        { USAGE: 'Pointer' },
        { COLLECTION: 'Physical' },
          { USAGE_PAGE: 'Button' },
          { USAGE_MINIMUM: 1 },
          { USAGE_MAXIMUM: 3 },
          { LOGICAL_MINIMUM: 0 },
          { LOGICAL_MAXIMUM: 1 },
          { REPORT_COUNT: 3 },
          { REPORT_SIZE: 1 },
          { INPUT: 'Data,Variable,Absolute' },
          { REPORT_COUNT: 1 },
          { REPORT_SIZE: 5 },
          { INPUT: 'Constant,Variable,Absolute', },
          { USAGE_PAGE: 'Generic Desktop' },
          { USAGE: 'X' },
          { USAGE: 'Y' },
          { LOGICAL_MINIMUM: -127 },
          { LOGICAL_MAXIMUM: 127 },
          { REPORT_SIZE: 8 },
          { REPORT_COUNT: 2 },
          { INPUT: 'Data,Variable,Relative' },
        { END_COLLECTION: null },
      { END_COLLECTION: null },
    ];
    
    var generatedDescriptor = genReportDescriptor(reportDescriptorProt­o);
    console.log(generatedDescriptor);
    
    // result ( make sure it's ok by drag& dropping in https://eleccelerator.com/usbdescreqpars­er/ ;p )
    /*
    ["0x05", "0x01", "0x09", "0x02", "0xa1", "0x01", "0x09", "0x01", "0xa1", "0x00", "0x05", "0x09", "0x19", "0x01", "0x29", "0x03", "0x15", "0x00", "0x25", "0x01", "0x95", "0x03", "0x75", "0x01", "0x81", "0x2", "0x95", "0x01", "0x75", "0x05", "0x81", "0x3", "0x05", "0x01", "0x09", "0x30", "0x09", "0x31", "0x15", "0x81", "0x25", "0x7f", "0x75", "0x08", "0x95", "0x02", "0x81", "0x6", "0xc0", "0xc0"]
    */
    

    Every contribution is welcome ;)
    https://github.com/stephaneAG/usbHidDesc­riptors

    Last but not least, if anyone has suggestions on the 'hid way' to get output descriptors working on Espruino, it 'd be daaaaaaaamn cool :) ( the goal is to be able to send stuff from host to Espruino - and for the moment, the 'stuff' is not quite defined ^^ )

    ps: while at it & if enough time to do so, I have some idea of a feedback mouse ;)
    +


    2 Attachments

About

Avatar for stephaneAG @stephaneAG started