Skip to content

Instantly share code, notes, and snippets.

@elliots
Created July 22, 2014 07:16
Show Gist options
  • Save elliots/8a3daf84bc028dddb478 to your computer and use it in GitHub Desktop.
Save elliots/8a3daf84bc028dddb478 to your computer and use it in GitHub Desktop.
Node.JS Wii U Pro Controller and Wiimote events
'use strict';
/*
* This handles Wii U Pro Controllers and Wiimotes, but doesn't yet detect which it is =)
* `npm install node-hid pakkit`
*/
var HID = require('node-hid');
var pakkit = require('pakkit');
var packets = pakkit.export({
WII_U_PRO_CONTROLLER : {
buttons: {
mask: [
null, null, null, null, null, null,
'plus', 'minus', 'home',
null, null,
'dup', 'ddown', 'dleft', 'dright',
'a', 'b', 'x', 'y',
'l', 'r',
'zl', 'zr',
'leftstick', 'rightstick'
],
type: 'uint32le'
},
left: {
type: 'joystick'
},
right: {
type: 'joystick'
}
},
WIIMOTE_CONTROLLER : {
buttons: {
mask: [
'dleft', 'dright', 'dup', 'ddown', 'a', 'trigger',
'plus', 'minus', 'home',
'1', '2',
'z', 'c'
],
type: 'uint32le'
},
left: {
type: 'joystick'
}
}
}, {
joystick: {
read: function(parser, attribute) {
parser
.int8(attribute.name + 'X')
.int8(attribute.name + 'Y')
.tap(function() {
this.vars[attribute.name] = {
x: this.vars[attribute.name + 'X'],
y: this.vars[attribute.name + 'Y'] * -1
};
delete(this.vars[attribute.name + 'X']);
delete(this.vars[attribute.name + 'Y']);
});
}
}
});
/* Pro:
{
vendorId: 0,
productId: 0,
path: 'Virtual_0000_0000_fafafafc',
serialNumber: '00000000',
manufacturer: 'Alxn1',
product: 'Wiimote (34-af-2c-18-19-e1)',
release: 1,
interface: -1
}
Wiimote
{
vendorId: 0,
productId: 0,
path: 'Virtual_0000_0000_fafafafd',
serialNumber: '00000000',
manufacturer: 'Alxn1',
product: 'Wiimote (00-1f-32-95-3a-57)',
release: 1,
interface: -1 }
*/
HID.devices().forEach((function(d) {
if(d && d.product.toLowerCase().indexOf('wiimote') !== -1) {
console.log('Found a wiimote', d);
var hid = new HID.HID(d.path);
var read = function(error, data) {
var packet = packets.WIIMOTE_CONTROLLER.read(data);
console.log('Wiimote', packets.WIIMOTE_CONTROLLER.read(data));
console.log('Wii U Pro Controller', packets.WII_U_PRO_CONTROLLER.read(data));
hid.read(read);
};
hid.read(read);
}
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment