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

Commit 08b2103

Browse files
author
divanvisagie
committed
Added within functionality for range based inputs
1 parent f515ad9 commit 08b2103

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

lib/mixins/within.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var mixins = {
22

3-
within: function(range, unit, callback) {
3+
within: function(eventName, range, callback) {
44
var upper;
55

66
if (typeof range === "number") {
@@ -15,18 +15,7 @@ var mixins = {
1515
return;
1616
}
1717

18-
if (typeof unit === "function") {
19-
callback = unit;
20-
unit = "value";
21-
}
22-
23-
if (typeof this[unit] === "undefined") {
24-
return this;
25-
}
26-
27-
// Use the continuous read event for high resolution
28-
this.on("data", function() {
29-
var value = this[unit] | 0;
18+
this.on(eventName , function(value){
3019
if (value >= range[0] && value <= range[1]) {
3120
callback.call(this, null, value);
3221
}

lib/xbox.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ var HID = require('node-hid'),
77
triggers = require('./triggers.json'),
88
buttons = require('./buttons.json'),
99
within = require('./mixins/within'),
10+
__ = require('lodash');
1011
joysticks = require('./joysticks.json');
1112

13+
1214
var dead = 6000;
1315

1416
function uint8Toint16(low, high) {
@@ -400,4 +402,6 @@ XboxController.prototype.rumble = function (left, right) {
400402
this.sendCommand([0x00, 0x00, 0x04, left, right], 'Xbox controller wont rumble');
401403
};
402404

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

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)