Generic Devices PS4 Dualshock

Ageorgios,

I thought you may be interested in my version of button mappings for the DualShock. Since the controller is capable of having multiple buttons selected at once, I created a structure that allows one to determine any/all buttons that are being pressed. I also included the joystick positions and the trigger positions, though I'm not sure how I would utilize those with the current Generic Device support.

function analyzeDeviceInput(targetDevice, reportID, reportDataHex) {

    let reportBuffer = buffer.Buffer.from(reportDataHex, 'hex');
    
    // Initialize a button object to track the state of each button.
    let buttons = {};
    
    //--------------------------------------------------------------------------------
    // Get the integer values of the joysticks;
    // These are values from 0 to 255.
    // Note: Joystick drift could cause this script to be called a lot.
    //       If you don't intend to use it, don't enable those bytes for monitoring.
    //--------------------------------------------------------------------------------
    buttons['left_x'] = reportBuffer.readUInt8(1);
    buttons['left_y'] = reportBuffer.readUInt8(2);
    buttons['right_x'] = reportBuffer.readUInt8(3);
    buttons['right_y'] = reportBuffer.readUInt8(4);

    //--------------------------------------------------------------------------------
    // Get the integer values of the L2 and R2 Triggers;
    // These are values from 0 to 255.
    //--------------------------------------------------------------------------------
    buttons['l2_value'] = reportBuffer.readUInt8(8);
    buttons['r2_value'] = reportBuffer.readUInt8(9);
    
    //--------------------------------------------------------------------------------
    // Interpret the Index 5 Buttons.
    //--------------------------------------------------------------------------------
     
    // Get the integer value of Index 5.
    let val = reportBuffer.readUInt8(5);

    // Convert it to binary. The controller uses a bitmask to track the button states.
    let binVal = val.toString(2).padStart(8,'0');

    // Get the four right-most bits. These are for the dpad.
    let dpadVal = parseInt(binVal.substring(4,8),2)

    // Determine the state of each button.
    buttons['triangle'] = (binVal.substring(0,1) == '1');
    buttons['circle'] = (binVal.substring(1,2) == '1');
    buttons['x'] = (binVal.substring(2,3) == '1');
    buttons['square'] = (binVal.substring(3,4) == '1');
    buttons['up'] = (dpadVal == 0 || dpadVal == 1 || dpadVal == 7);
    buttons['right'] = (dpadVal == 1 || dpadVal == 2 || dpadVal == 3);
    buttons['down'] = (dpadVal == 3 || dpadVal == 4 || dpadVal == 5);
    buttons['left'] = (dpadVal == 5 || dpadVal == 6 || dpadVal == 7);

    //--------------------------------------------------------------------------------
    // Interpret the Index 6 Buttons.
    //--------------------------------------------------------------------------------
     
    // Get the integer value of Index 6.
    val = reportBuffer.readUInt8(6);

    // Convert it to binary. The controller uses a bitmask to track the button states.
    binVal = val.toString(2).padStart(8,'0');
 
    // Determine the state of each button.
    buttons['r3'] = (binVal.substring(0,1) == '1');
    buttons['l3'] = (binVal.substring(1,2) == '1');
    buttons['options'] = (binVal.substring(2,3) == '1');
    buttons['share'] = (binVal.substring(3,4) == '1');
    buttons['r2'] = (binVal.substring(4,5) == '1');
    buttons['l2'] = (binVal.substring(5,6) == '1');
    buttons['r1'] = (binVal.substring(6,7) == '1');
    buttons['l1'] = (binVal.substring(7,8) == '1');

    //--------------------------------------------------------------------------------
    // Interpret the Index 7 Buttons.
    //--------------------------------------------------------------------------------
     
    // Get the integer value of Index 7.
    val = reportBuffer.readUInt8(7);

    // Convert it to binary. The controller uses a bitmask to track the button states.
    binVal = val.toString(2).padStart(2,'0');
    
    // Determine the state of each button.
    buttons['touchpad'] = (binVal.substring(0,1) == '1');
    buttons['home'] = (binVal.substring(1,2) == '1');
    
    //--------------------------------------------------------------------------------
    // Output the Button States.
    //--------------------------------------------------------------------------------
    log(JSON.stringify(buttons,null,4));

    //--------------------------------------------------------------------------------
    // Determine which triggers to fire.
    // This example shows a one-to-one mapping of the buttons to the trigger calls.
    // However, it could just as easily look for combinations of buttons
    // (e.g. L2 and X held down at the same time).
    //--------------------------------------------------------------------------------
    if(buttons['triangle']) bttTriggerDeviceTrigger(targetDevice, 'triangle');
    if(buttons['circle'])   bttTriggerDeviceTrigger(targetDevice, 'circle');
    if(buttons['x'])        bttTriggerDeviceTrigger(targetDevice, 'x');
    if(buttons['square'])   bttTriggerDeviceTrigger(targetDevice, 'square');
    if(buttons['up'])       bttTriggerDeviceTrigger(targetDevice, 'up');
    if(buttons['right'])    bttTriggerDeviceTrigger(targetDevice, 'right');
    if(buttons['down'])     bttTriggerDeviceTrigger(targetDevice, 'down');
    if(buttons['left'])     bttTriggerDeviceTrigger(targetDevice, 'left');
    if(buttons['l1'])       bttTriggerDeviceTrigger(targetDevice, 'l1');
    if(buttons['l2'])       bttTriggerDeviceTrigger(targetDevice, 'l2');
    if(buttons['l3'])       bttTriggerDeviceTrigger(targetDevice, 'l3');
    if(buttons['r1'])       bttTriggerDeviceTrigger(targetDevice, 'r1');
    if(buttons['r2'])       bttTriggerDeviceTrigger(targetDevice, 'r2');
    if(buttons['r3'])       bttTriggerDeviceTrigger(targetDevice, 'r3');
    if(buttons['options'])  bttTriggerDeviceTrigger(targetDevice, 'options');
    if(buttons['share'])    bttTriggerDeviceTrigger(targetDevice, 'share');
    if(buttons['touchpad']) bttTriggerDeviceTrigger(targetDevice, 'touchpad');
    if(buttons['home'])     bttTriggerDeviceTrigger(targetDevice, 'home');
    
}

1 Like