-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
235 lines (195 loc) · 6.76 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
'use strict';
var HID = require('node-hid'),
util = require('util'),
EventEmitter = require('events').EventEmitter,
VS = require('./lib/value-stabilizer')
;
var Gamepad = function (hid) {
// Duplex.call(this);
EventEmitter.call(this);
// store hid
this._hid = hid;
// prepare stabilizers used for reducing unstable analog values
this._prepareValueStabilizers();
this._lastData = this._parseData(
this._stabLH.stabilize.bind(this._stabLH),
this._stabLV.stabilize.bind(this._stabLV),
this._stabRH.stabilize.bind(this._stabRH),
this._stabRV.stabilize.bind(this._stabRV),
new Buffer([0x80, 0x80, 0x82, 0x80, 0x80, 0x0f, 0x00, 0x40])
);
// listen to data and pass the stabilizers.
// function is curried to reduce lookups, considering
// that the 'data' event happens very often
hid.on('data', this._handleData.bind(
this,
this._stabLH.stabilize.bind(this._stabLH),
this._stabLV.stabilize.bind(this._stabLV),
this._stabRH.stabilize.bind(this._stabRH),
this._stabRV.stabilize.bind(this._stabRV)
));
hid.on('error', function (err) {
this.emit('error', err);
}.bind(this));
};
util.inherits(Gamepad, EventEmitter);
Gamepad.device = function (opt, callback) {
var hid,
gamepad
;
if (typeof opt === 'function') {
callback = opt;
opt = {};
}
opt = opt || {};
// opt.path = opt.path || 'USB_0079_0006_fd120000';
opt.path = opt.path || 'USB_0079_0006_14100000';
// try to get the hid
try {
hid = new HID.HID(opt.path);
} catch (err) {
return callback(err);
}
gamepad = new Gamepad(hid);
callback(null, gamepad);
};
Gamepad.prototype.value = function (property) {
return this._lastData[property];
};
// -----------------------------------------------------------------------------
Gamepad.prototype._write = function (chunk, encoding, callback) {
// TODO: implement this
};
Gamepad.prototype._read = function (size) {
// TODO: implement this
};
// -----------------------------------------------------------------------------
Gamepad.prototype._prepareValueStabilizers = function () {
// left stick stabilizer
this._stabLH = new VS({ min: 0, max: 255 });
this._stabLV = new VS({ min: 0, max: 255 });
// right stick stabilizer
this._stabRH = new VS({ min: 0, max: 255 });
this._stabRV = new VS({ min: 0, max: 255 });
};
Gamepad.prototype._handleData = function (stabLH, stabLV, stabRH, stabRV, data) {
var oldData = this._lastData,
newData,
inputChanges;
// parse data and replace last data with new
this._lastData = newData = this._parseData.apply(this, arguments);
// check for changes in any of the inputs and dispatch respective events
inputChanges = this._checkDataChanges(oldData, newData);
this._emitInputChanges(inputChanges);
this.emit('data', newData);
};
Gamepad.prototype._checkDataChanges = function (oldData, newData) {
var changes = [],
k,
newValue,
oldValue,
directionalChanged,
keyName,
i,
directionalKeys = ['up', 'right', 'left', 'down']
;
for (k in newData) {
oldValue = oldData[k];
newValue = newData[k];
switch (k) {
case 'rawData':
// ignore this property
continue;
case 'leftStick':
// check if direction changed
if (newValue.h !== oldValue.h || newValue.v !== oldValue.v) {
changes.push(['leftStickChange', {
h: newValue.h,
v: newValue.v
}]);
}
// check if press state changed
(oldValue.pressed !== newValue.pressed) && (changes.push(['leftStick' + (newValue ? 'Press' : 'Release')]));
break;
case 'rightStick':
// check if direction changed
if (newValue.h !== oldValue.h || newValue.v !== oldValue.v) {
changes.push(['rightStickChange', {
h: newValue.h,
v: newValue.v
}]);
}
// check if press state changed
(oldValue.pressed !== newValue.pressed) && (changes.push(['rightStick' + (newValue ? 'Press' : 'Release')]));
break;
case 'directional':
directionalChanged = false;
// check if direction changed
for (i in directionalKeys) {
keyName = directionalKeys[i];
// for each direction, if the press state changed
if (newValue[keyName] !== oldValue[keyName]) {
// add that button change
changes.push([keyName + (newValue[keyName] ? 'Press' : 'Release')]);
directionalChanged = true;
}
}
// if any direction changed
(directionalChanged) && changes.push(['directionalChange', newValue]);
break;
case 'analog':
(oldValue !== newValue) && (changes.push(['analogChange', newValue]));
break;
default:
// if something changed, store new value
(oldValue !== newValue) && (changes.push([k + (newValue ? 'Press' : 'Release')]));
}
}
return changes;
};
Gamepad.prototype._emitInputChanges = function (changes) {
var k;
for (k in changes) {
this.emit.apply(this, changes[k]);
}
};
Gamepad.prototype._parseData = function (stabLH, stabLV, stabRH, stabRV, data) {
// get directional and main buttons (square, circle, triangle and X)
var mainBtns = data[5],
// secondary buttons
secBtns = data[6],
padData
;
padData = {
rawData: data,
leftStick: {
h: stabLH(data[0]),
v: stabLV(data[1]),
pressed: !!(secBtns & 0x40)
},
rightStick: {
h: stabRH(data[3]),
v: stabRV(data[4]),
pressed: !!(secBtns & 0x80)
},
directional: {
up: (mainBtns >= 0 && mainBtns <= 1) || mainBtns === 7,
right: mainBtns >= 1 && mainBtns <= 3,
down: mainBtns >= 3 && mainBtns <= 5,
left: mainBtns >= 5 && mainBtns <= 7
},
triangle: !!(mainBtns & 0x10),
circle: !!(mainBtns & 0x20),
cross: !!(mainBtns & 0x40),
square: !!(mainBtns & 0x80),
start: !!(secBtns & 0x20),
select: !!(secBtns & 0x10),
analog: !(data[7] & 0x80),
l1: !!(secBtns & 0x01),
r1: !!(secBtns & 0x02),
l2: !!(secBtns & 0x04),
r2: !!(secBtns & 0x08)
};
return padData;
};
module.exports = Gamepad;