• Back again: I updated the code to support the joysticks but still not luck with the troubles expressed above :/

    the current api

    gamepad.A.press(); // press a btn - 1st way
    gamepad.A.release(); // release a btn - 1st way
    gamepad.press([BUTTONS.A]); // release a btn - 2nd way
    gamepad.release([BUTTONS.A]); // release a btn - 2nd way
    gamepad.press([BUTTONS.A, BUTTONS.SELECT, BUTTONS.START]); // press multiple
    gamepad.release([BUTTONS.A, BUTTONS.SELECT, BUTTONS.START]); // release multiple
    gamepad.LJOY_X.move(45); // move joytick axis
    gamepad.set({START:1, SELECT: 1, LJOY_X: 120}); // set many stuff at once
    gamepad.unset(); // reset all to default
    gamepad.sendState(); // send current buttons/joysticks/triggers positions over usb
    
    var button = function(bLabel, bValue){ this.bLabel = bLabel; this.bValue = bValue & 0xFFFF; };
    button.prototype.bLabel = '';
    button.prototype.bValue = 0;
    // set corresponding bit in gamepad obj
    button.prototype.press = function(){
      //gamepad.btnState = ( gamepad.btnState | (1<<this.bValue) ) & 0xFFFF;
      //console.log('WTF: gamepad.btnState | ( (1<<this.bValue) & 0xFFFF) & 0xFFFF -->' + ( gamepad.btnState | ( (1<<this.bValue) & 0xFFFF) & 0xFFFF) );
    console.log('WTF: gamepad.btnState | ( (1<<this.bValue) & 0xFFFF) & 0xFFFF -->' + ( gamepad.btnState | 1<<this.bValue) );
      //gamepad.btnState = gamepad.btnState | ( (1<<this.bValue) & 0xFFFF) & 0xFFFF;
      gamepad.btnState = gamepad.btnState | (1<<this.bValue) & 0xFFFFFFFF;
      console.log('button const value: ' + this.bValue);
      console.log('gamepad state: 0b' + gamepad.btnState.toString(2) );
    }
    // unset corresponding bit in gamepad obj
    button.prototype.release = function(){
      gamepad.btnState = ( gamepad.btnState & ~(1<<this.bValue)) & 0xFFFF;
      console.log('button const value: ' + this.bValue);
      console.log('gamepad state: 0b' + gamepad.btnState.toString(2) );
    }
    
    
    var joystick = function(bLabel, objAttr){ this.bLabel = bLabel; this.objAttr = objAttr; };
    joystick.prototype.bLabel = '';
    joystick.prototype.objAttr = null;
    joystick.prototype.move = function(value){
      console.log('joystick ' + this.bLabel + ' value: ' + value);
      console.log('joystick ' + this.objAttr + ' value: ' + value);
      //gamepad[this.bLabel] = value;
      gamepad[this.objAttr] = value;
      //this.objAttr = value;
    };
    
    var gamepad = {
      lastBtnState: 0b0000000000000000,
      btnState: 0b0000000000000000,
      x1: 0,
      y1: 0,
      x2: 0,
      y2: 0
    }
    
    // pass an array of btn to be pressed
    // depending on the const(s) passed, we set the bits mapped from the const's value
    gamepad.press = function(btnsArr){
      for(var i=0; i< btnsArr.length; i++){
        this.btnState = ( this.btnState | (1<<btnsArr[i]) ) & 0xFFFF;
        console.log('gamepad state: 0b' + this.btnState.toString(2) );
      }
    };
    // pass an array of btn to be pressed
    // depending on the const(s) passed, we unset the bits mapped from the const's value
    gamepad.release = function(btnsArr){
      for(var i=0; i< btnsArr.length; i++){
        this.btnState = ( this.btnState & ~(1<<btnsArr[i]) ) & 0xFFFF;
        console.log('gamepad state: 0b' + this.btnState.toString(2) );
      }
    };
    // "populate" the gamepad object with buttons children offering press() & release() methods
    Object.keys(BUTTONS).forEach(function(bL­abel){
      gamepad[bLabel] = new button(bLabel, BUTTONS[bLabel]);
    });
    // "populate" the gamepad object with joysticks children offering move(), moveX() & moveY()  methods
    var JOYSTICKS = { LJOY_X: 'x1', LJOY_Y: 'y1', RJOY_X: 'x2', RJOY_Y: 'y2' };
    Object.keys(JOYSTICKS).forEach(function(­bLabel){
      //gamepad[bLabel] = new joystick(bLabel, gamepad[ JOYSTICKS[bLabel] ]);
      gamepad[bLabel] = new joystick(bLabel, JOYSTICKS[bLabel]);
    });
    // pass an array of elements to be set ( updated )
    gamepad.set = function(optsObj){
      var that = this;
      Object.keys(optsObj).forEach(function(op­t){
        //that[opt] = optsObj[opt]; // nope: overwrites our objs -> we want to
        // if the label is present in the BUTTONS consts, then we act depending on the value passed to set or unset correspondign bit
        if(typeof BUTTONS[opt] !== 'undefined'){
          if(optsObj[opt] === 1) that.btnState = that.btnState | (1<<BUTTONS[opt]);
          else that.btnState = that.btnState & ~(1<<BUTTONS[opt] ) & 0xFFFF;
        }
        // else, somehow map to the correct joystick & set its value directly
        // Thnk: accept 'LJOY':[x, y] ?
        else {
          (opt === 'LJOY_X') ? that.x1 = optsObj[opt] :
          (opt === 'LJOY_Y') ? that.y1 = optsObj[opt] :
          (opt === 'RJOY_X') ? that.x2 = optsObj[opt] :
          (opt === 'RJOY_Y') ? that.y2 = optsObj[opt] : null ;
        }
      });
    };
    // resets the gamepad ( unset all pressed stuff & cie - aka release all btns & center joysticks )
    gamepad.unset = function(optsObj){
      this.btnState = 0b0000000000000000;
      this.x1 = 0;
      this.y1 = 0;
      this.x2 = 0;
      this.y2 = 0;
    };
    // send the current state of the gamepad object over usb
    gamepad.sendState = function(){
      E.sendUSBHID([
        this.btnState & 0xFF,      // Byte0
        (this.btnState>>8) & 0xFF, // Byte1
        this.x1,                   // Byte2
        this.y1,                   // Byte3
        this.x2,                   // Byte4
        this.y2,                   // Byte5
      ]);
    };
    
About

Avatar for stephaneAG @stephaneAG started