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

Commit fd91767

Browse files
author
Andrew Nesbitt
committed
Merge pull request #19 from divanvisagie/master
within function
2 parents 1d5b375 + 08b2103 commit fd91767

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

lib/mixins/within.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var mixins = {
2+
3+
within: function(eventName, range, callback) {
4+
var upper;
5+
6+
if (typeof range === "number") {
7+
upper = range;
8+
range = [0, upper];
9+
}
10+
11+
if (!Array.isArray(range)) {
12+
this.emit("error", {
13+
message: "range must be an array"
14+
});
15+
return;
16+
}
17+
18+
this.on(eventName , function(value){
19+
if (value >= range[0] && value <= range[1]) {
20+
callback.call(this, null, value);
21+
}
22+
}.bind(this));
23+
24+
return this;
25+
}
26+
};
27+
28+
module.exports = mixins;

lib/xbox.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ var HID = require('node-hid'),
66
path = require('path'),
77
triggers = require('./triggers.json'),
88
buttons = require('./buttons.json'),
9+
within = require('./mixins/within'),
10+
__ = require('lodash');
911
joysticks = require('./joysticks.json');
1012

13+
1114
var dead = 6000;
1215

1316
function uint8Toint16(low, high) {
@@ -245,10 +248,10 @@ XboxController.configure = function () {
245248
for (i = 0; i < configArray.length; i++) {
246249
fs.writeFile(configArray[i], JSON.stringify(nameArray[i], null, 4), function (err) {
247250
if (err) throw err;
248-
console.log("Saved" + nameArray[i])
251+
console.log('Saved' + nameArray[i])
249252
});
250253
}
251-
console.log("Config complete");
254+
console.log('Config complete');
252255
break;
253256
default:
254257
console.log('error try again');
@@ -399,4 +402,6 @@ XboxController.prototype.rumble = function (left, right) {
399402
this.sendCommand([0x00, 0x00, 0x04, left, right], 'Xbox controller wont rumble');
400403
};
401404

405+
__.mixin(XboxController.prototype, within);
406+
402407
module.exports = XboxController;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
],
3030
"main": "./lib/xbox.js",
3131
"dependencies": {
32+
"lodash": "latest",
3233
"node-hid": "~>0.3.1",
3334
"chalk": "~0.3.0"
3435
},

tests/test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,8 @@ xbox.on('left:move', function (position) {
2626
xbox.on('right:move', function (position) {
2727
console.log('right:move', position);
2828
});
29+
30+
31+
xbox.within('righttrigger', [50,100], function(err, data){
32+
console.log('rightttrigger within 50 and 100', err, data);
33+
});

0 commit comments

Comments
 (0)