Skip to content
This repository has been archived by the owner on Dec 20, 2018. It is now read-only.

Commit

Permalink
Merge pull request #19 from divanvisagie/master
Browse files Browse the repository at this point in the history
within function
  • Loading branch information
Andrew Nesbitt committed Aug 23, 2014
2 parents 1d5b375 + 08b2103 commit fd91767
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
28 changes: 28 additions & 0 deletions lib/mixins/within.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var mixins = {

within: function(eventName, range, callback) {
var upper;

if (typeof range === "number") {
upper = range;
range = [0, upper];
}

if (!Array.isArray(range)) {
this.emit("error", {
message: "range must be an array"
});
return;
}

this.on(eventName , function(value){
if (value >= range[0] && value <= range[1]) {
callback.call(this, null, value);
}
}.bind(this));

return this;
}
};

module.exports = mixins;
9 changes: 7 additions & 2 deletions lib/xbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ var HID = require('node-hid'),
path = require('path'),
triggers = require('./triggers.json'),
buttons = require('./buttons.json'),
within = require('./mixins/within'),
__ = require('lodash');
joysticks = require('./joysticks.json');


var dead = 6000;

function uint8Toint16(low, high) {
Expand Down Expand Up @@ -245,10 +248,10 @@ XboxController.configure = function () {
for (i = 0; i < configArray.length; i++) {
fs.writeFile(configArray[i], JSON.stringify(nameArray[i], null, 4), function (err) {
if (err) throw err;
console.log("Saved" + nameArray[i])
console.log('Saved' + nameArray[i])
});
}
console.log("Config complete");
console.log('Config complete');
break;
default:
console.log('error try again');
Expand Down Expand Up @@ -399,4 +402,6 @@ XboxController.prototype.rumble = function (left, right) {
this.sendCommand([0x00, 0x00, 0x04, left, right], 'Xbox controller wont rumble');
};

__.mixin(XboxController.prototype, within);

module.exports = XboxController;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
],
"main": "./lib/xbox.js",
"dependencies": {
"lodash": "latest",
"node-hid": "~>0.3.1",
"chalk": "~0.3.0"
},
Expand Down
5 changes: 5 additions & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,8 @@ xbox.on('left:move', function (position) {
xbox.on('right:move', function (position) {
console.log('right:move', position);
});


xbox.within('righttrigger', [50,100], function(err, data){
console.log('rightttrigger within 50 and 100', err, data);
});

0 comments on commit fd91767

Please sign in to comment.