Skip to content

Commit

Permalink
adds support for missing ds4 buttons, tests and coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Apr 17, 2014
1 parent e13105f commit b6199a5
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 6 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
node_modules
node_modules
*.log
coverage
*.iml
*.idea
.DS_Store
92 changes: 90 additions & 2 deletions controllerConfigurations/dualShock4-generic-driver.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@
{
"name": "leftAnalogBump",
"buttonBlock": 6,
"buttonValue": "0x40"
"buttonValue": "0x04"
},
{
"name": "rightAnalogBump",
"buttonBlock": 6,
"buttonValue": "0x80"
"buttonValue": "0x08"
},
{
"name": "psxButton",
Expand All @@ -55,6 +55,94 @@
"name": "touchPad",
"buttonBlock": 7,
"buttonValue": "0x02"
},
{
"name": "square",
"buttonBlock": 5,
"buttonValue": "0x10"
},
{
"name": "triangle",
"buttonBlock": 5,
"buttonValue": "0x80"
},
{
"name": "circle",
"buttonBlock": 5,
"buttonValue": "0x40"
},
{
"name": "x",
"buttonBlock": 5,
"buttonValue": "0x20"
},
{
"name": "dpadUp",
"buttonBlock": 5,
"buttonValue": "0x00",
"mask": "0xF"
},
{
"name": "dpadUpRight",
"buttonBlock": 5,
"buttonValue": "0x01",
"mask": "0xF"
},
{
"name": "dpadRight",
"buttonBlock": 5,
"buttonValue": "0x02",
"mask": "0xF"
},
{
"name": "dpadDownRight",
"buttonBlock": 5,
"buttonValue": "0x03",
"mask": "0xF"
},
{
"name": "dpadDown",
"buttonBlock": 5,
"buttonValue" : "0x04",
"mask": "0xF"
},
{
"name": "dpadDownLeft",
"buttonBlock": 5,
"buttonValue" : "0x05",
"mask": "0xF"
},
{
"name": "dpadLeft",
"buttonBlock": 5,
"buttonValue": "0x06",
"mask": "0xF"
},
{
"name": "dpadUpLeft",
"buttonBlock": 5,
"buttonValue": "0x07",
"mask": "0xF"
},
{
"name": "share",
"buttonBlock": 6,
"buttonValue": "0x10"
},
{
"name": "options",
"buttonBlock": 6,
"buttonValue": "0x20"
},
{
"name": "leftStick",
"buttonBlock": 6,
"buttonValue": "0x40"
},
{
"name": "rightStick",
"buttonBlock": 6,
"buttonValue": "0x80"
}
]
}
37 changes: 36 additions & 1 deletion lib/buttons.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,51 @@
// Module dependencies.

var isString = function(object) {
return object instanceof String || typeof object == "string";
};

//Proccess button events.
var Buttons = function (eventEmmiter, buttonConfiguration) {
'use strict';

// convert strings to numbers, e.g. "0x01" to 0x01
// must be converted because JSON doesn't allow numbers with leading zeros
buttonConfiguration.forEach(function(button) {
if(isString(button.buttonValue)) {
button.buttonValue = parseInt(button.buttonValue, 16);
}

if(isString(button.mask)) {
button.mask = parseInt(button.mask, 16);
} else if(!(button.mask instanceof Number)) {
button.mask = 0xFF;
}
});

var buffer = {};

//Private methods
var processButton = function (button, data) {
//make sure the data contains a value for the specified block
//and bitwise operation for the button value
if (data[button.buttonBlock] & button.buttonValue) {

var block = data[button.buttonBlock] & button.mask;
var hit = (block & button.buttonValue) == button.buttonValue;

// special case for the dualshock 4's dpadUp button as it causes the
// lower 8 bits of it's block to be zeroed
if(!button.buttonValue) {
hit = !block;
}

// special case for dualshock 4's dpad - they are not bitmasked values as
// they cannot be pressed together - ie. up, left and upleft are three
// different values - upleft is not equal to up & left
if(button.buttonBlock == 5 && block < 0x08) {
hit = block == button.buttonValue;
}

if (hit) {

//if the button is in the released state.
if (!buffer[button.name]) {
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,14 @@
"grunt-mocha-test": "~0.9.0",
"sinon": "~1.8.1"
},
"devDependencies": {},
"devDependencies": {
"mocha": "^1.0",
"sinon": "^1.8",
"istanbul": "^0.2"
},
"scripts": {
"test": "grunt test"
"test": "grunt test",
"coverage": "istanbul cover _mocha"
},
"repository": {
"type": "git",
Expand Down
44 changes: 44 additions & 0 deletions test/buttons.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ describe('the Buttons component', function () {
"buttonBlock": 0,
"buttonValue": "0x08",
"analogPin" : 1
},
{
"name": "dpadUp",
"buttonBlock": 5,
"buttonValue": "0x00",
"mask": "0xF"
},
{
"name": "dpadDown",
"buttonBlock": 5,
"buttonValue": "0x01",
"mask": "0xF"
}
],
instance = [{ name: 'process' }],
Expand Down Expand Up @@ -85,4 +97,36 @@ describe('the Buttons component', function () {
});
});

describe('ps4 dpad up button', function () {
it('should invoke the dpadUp:press', function () {
emitter.on('dpadUp:press', spy);
buttons.process([0, 0, 0, 0, 0, 0]);

assert.equal(spy.called, true);
});

it('should not invoke the dpadDown:press', function () {
emitter.on('dpadDown:press', spy);
buttons.process([0, 0, 0, 0, 0, 0]);

assert.equal(spy.called, false);
});
});

describe('ps4 dpad down button', function () {
it('should invoke the dpadDown:press', function () {
emitter.on('dpadDown:press', spy);
buttons.process([0, 0, 0, 0, 0, parseInt("00001001", 2)]);

assert.equal(spy.called, true);
});

it('should not invoke the dpadUp:press', function () {
emitter.on('dpadUp:press', spy);
buttons.process([0, 0, 0, 0, 0, parseInt("00001001", 2)]);

assert.equal(spy.called, false);
});
});

});

0 comments on commit b6199a5

Please sign in to comment.