-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
108 lines (92 loc) · 2.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var usb = require('usb');
// Guitar Hero X-plorer:
// Product ID: 0x4748 18248
// Vendor ID: 0x1430 (RedOctane)
// Version: 31.22
// Serial Number: 0ADD7D8
// Speed: Up to 12 Mb/sec
// Manufacturer: RedOctane Inc(c)2006
// Location ID: 0x14100000 / 3
// Current Available (mA): 500
// Current Required (mA): 500
var definitions = {
"buttons": {
"green": [3, 0x10],
"red": [3, 0x20],
"yellow": [3, 0x80],
"blue": [3, 0x40],
"orange": [3, 0x01],
"back": [2, 0x20],
"start": [2, 0x10],
"up": [2, 0x02],
"down": [2, 0x01],
"left": [2, 0x04],
"right": [2, 0x08],
"xbox": [3, 0x04]
},
"ranges": {
"x": 4,
"y": 5,
"whammy": 10,
}
}
function GuitarController(end) {
var me = {};
me.buttons = definitions.buttons;
me.ranges = definitions.ranges;
me.controlState = new Buffer(12);
end.on("data", function(data) {
// early bolt for optimization improvements
var same = true;
for (var i = 0; i < 12; i++) {
if (me.controlState[i] !== data[i]) {
same = false;
break;
}
}
if (same) return;
for (type in me.ranges) {
var address = me.ranges[type];
if (me.controlState[address] !== data[address]) {
end.emit(type, data[address]);
}
}
// check buttons
for (key in me.buttons) {
var address = me.buttons[key];
var chunk = address[0];
var mask = address[1];
// check if different from controlState
if ((me.controlState[chunk] & mask) != (data[chunk] & mask)) {
if ((data[chunk] & mask) === mask) {
end.emit(key + ".press");
} else {
end.emit(key + ".release");
}
}
}
// save state to compare against next frame, update cache
data.copy(me.controlState);
});
}
module.exports = function() {
var controllers = [];
usb.getDeviceList().forEach(function(device) {
if (device.deviceDescriptor.idProduct !== 18248) return;
device.__open();
device.__claimInterface(0);
device.open();
device.interfaces.forEach(function(inter) {
inter.claim();
inter.endpoints.forEach(function(end) {
if (end.direction !== "in") return;
end.startPoll(7, end.descriptor.wMaxPacketSize);
GuitarController(end);
controllers.push(end);
});
});
});
return controllers;
}
module.exports.buttons = definitions.buttons;
module.exports.ranges = definitions.ranges;