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

Adding support for multiple devices, serial number constructor and example code #36

Merged
merged 2 commits into from
Jun 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ xbox.on('not-found', function(){
});
```

Also see the `example.js` for usage.

## LEDs

Set LED pattern on controller:
Expand Down Expand Up @@ -97,14 +99,26 @@ xbox.within('righttrigger', [50,100], function(err, data){
});
```

## Third Party Controllers
## Multiple Devices

On startup, if a device is in use, it will be skipped and the next available device will be used. The serial number and
path will be printed to the console for debugging.

Use the printed serial number to specifically address devices if required, see Serial Numbers below.

If you have a third party controller with a different name you can specify the name when creating the controller:
## Serial Numbers

Each device has own serial number. On startup, the serial number of the device detected will be printed to the console.

To use a specific serial number, specify it when creating the controller:

```javascript
var xbox = new XboxController('flight stick');
var xbox = new XboxController('9BB004D0');
```

If you have a third party controller without 'controller' in the name, specify the device serial number to use that
device.

## Configuring a Third Party Controller

To configure a third party controller:
Expand Down
244 changes: 244 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@

var XboxController = require('xbox-controller');
var xbox = new XboxController();

console.log(xbox.serialNumber + ' online');


/*
* A,B,X,Y buttons
*/

xbox.on('a:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'a', action:'press' });
console.log(message);
});

xbox.on('a:release', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'a', action:'release' });
console.log(message);
});


xbox.on('b:press', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'b', action:'press' });
console.log(message);
});

xbox.on('b:release', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'b', action:'release' });
console.log(message);
});


xbox.on('x:press', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'x', action:'press' });
console.log(message);
});

xbox.on('x:release', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'x', action:'release' });
console.log(message);
});


xbox.on('y:press', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'y', action:'press' });
console.log(message);
});

xbox.on('y:release', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'y', action:'release' });
console.log(message);
});


/*
* Triggers
*/

xbox.on('lefttrigger', function(position){
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'lefttrigger', action:position });
console.log(message);

xbox.rumble(0,position);
});

xbox.on('righttrigger', function(position){
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'righttrigger', action:position });
console.log(message);

xbox.rumble(position,0);
});



/*
* Analog sticks
*/

xbox.on('left:move', function(position){
if(position.x != 0 || position.y != 0) {

var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'leftmove', action:position });
console.log(message);
}
});

xbox.on('right:move', function(position){
if(position.x != 0 || position.y != 0) {

var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'rightmove', action:position });
console.log(message);
}
});


xbox.on('leftstick:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'leftstick', action:'press' });
console.log(message);
});

xbox.on('leftstick:release', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'leftstick', action:'release' });
console.log(message);
});


xbox.on('rightstick:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'rightstick', action:'press' });
console.log(message);
});

xbox.on('rightstick:release', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'rightstick', action:'release' });
console.log(message);
});


/*
* Top buttons
*/

xbox.on('back:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'back', action:'press' });
console.log(message);
});

xbox.on('back:release', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'back', action:'release' });
console.log(message);
});


xbox.on('xboxbutton:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'xboxbutton', action:'press' });
console.log(message);
});

xbox.on('xboxbutton:release', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'xboxbutton', action:'release' });
console.log(message);
});


xbox.on('start:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'start', action:'press' });
console.log(message);
});

xbox.on('start:release', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'start', action:'release' });
console.log(message);
});


/*
* D pad
*/

xbox.on('dup:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'dup', action:'press' });
console.log(message);
});

xbox.on('dup:release', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'dup', action:'release' });
console.log(message);
});


xbox.on('ddown:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'ddown', action:'press' });
console.log(message);
});

xbox.on('ddown:release', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'ddown', action:'release' });
console.log(message);
});


xbox.on('dleft:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'dleft', action:'press' });
console.log(message);
});

xbox.on('dleft:release', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'dleft', action:'release' });
console.log(message);
});


xbox.on('dright:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'dright', action:'press' });
console.log(message);
});

xbox.on('dright:release', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'dright', action:'release' });
console.log(message);
});


/*
* Shoulder
*/

xbox.on('rightshoulder:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'rightshoulder', action:'press' });
console.log(message);
});

xbox.on('rightshoulder:release', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'rightshoulder', action:'release' });
console.log(message);
});


xbox.on('leftshoulder:press', function (key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'leftshoulder', action:'press' });
console.log(message);
});

xbox.on('leftshoulder:release', function(key) {
var message = JSON.stringify({ serialNumber: xbox.serialNumber, button: 'leftshoulder', action:'release' });
console.log(message);
});





/*
* Setup
*/

xbox.on('connected', function() {
console.log('Xbox controller connected');
xbox.setLed(LED)
});

xbox.on('not-found', function() {
console.log('Xbox controller could not be found');
});
46 changes: 38 additions & 8 deletions lib/xbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ function uint8Toint16(low, high) {
return int16View[0];
}

function XboxController(name) {
this.name = name || 'controller';
function XboxController(serialNumber) {

if( serialNumber) {
this.serialNumber = serialNumber.toUpperCase();
}

this.position = 0;

for (var button in buttons) {
Expand Down Expand Up @@ -263,13 +267,39 @@ util.inherits(XboxController, events.EventEmitter);
XboxController.prototype.loadController = function () {

HID.devices().forEach((function (d) {
var product = (typeof d === 'object' && d.product) || '';
if (product.toLowerCase().indexOf(this.name.toLowerCase()) !== -1) {
this.hid = new HID.HID(d.path);
console.log(chalk.green('notice: '), 'Xbox controller connected.');
this.emit('connected');
location = this.hid;


try {
if( this.serialNumber )
{
// If the serial number is set, look for device with that serial number.
var deviceSerialNumber = (typeof d === 'object' && d.serialNumber) || '';

if (deviceSerialNumber.indexOf(this.serialNumber) !== -1) {
console.log(chalk.green('notice: '), 'serial number found.');
this.hid = new HID.HID(d.path);
console.log(chalk.green('notice: '), 'Xbox controller connected (serial number: '
+ d.serialNumber + ', path: ' + d.path + ')');
this.emit('connected');
location = this.hid;
}
} else {
// The serial number is not set, grab the first device.
var product = (typeof d === 'object' && d.product) || '';
if (product.toLowerCase().indexOf('controller') !== -1) {
this.hid = new HID.HID(d.path);
console.log(chalk.green('notice: '), 'Xbox controller connected (serial number: '
+ d.serialNumber + ', path: ' + d.path + ')');
this.serialNumber = d.serialNumber;
this.emit('connected');
location = this.hid;
}
}
} catch (ex) {
console.log(chalk.green('notice: '), 'Xbox controller already in use (serial number: '
+ d.serialNumber + ', path: ' + d.path + ')' );
}

}).bind(this));

if (this.hid === false && !this._controllerLoadingInterval) {
Expand Down