-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
277 lines (236 loc) · 9.08 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/**
* Allows you to effortlessly interface your node applications with a variety of gamepad controllers.
*
* @example
* var controller = ( new Gamepad( 'ps3/dualshock3' ) ).connect();
* controller.on( 'x:press', function() {
* // do something here.
* } );
* @module Gamepad
*/
// load our dependencies into scope
const HID = require( 'node-hid' );
const EventEmitter = require( 'events' ).EventEmitter;
const util = require( 'util' );
const fs = require( 'fs' );
const colors = require( 'colors' );
const path = require( 'path' );
// export the constructor
module.exports = Gamepad;
// make sure the constructor inherits the properites necessary for EventEmitter to work
util.inherits( Gamepad, EventEmitter );
/**
* `Gamepad` is the base API object constructor.
*
* @class Gamepad
* @constructor
*
* @param {String} type The type of gamepad to be loaded. This can follow 2 forms: (1) vendor ID or (2) vendor ID/productID. Thus, "ps3" and "ps3/dualshock3" are both valid options.
* @param {Object} options A hash of options that can be set by the user.
* @param {Number} [options.vendorID] When this value is specified it will overwrite the existing `vendorID` that's loaded from the detected configuration.
* @param {Number} [options.productID] When this value is specified it will overwrite the existing `productID` that's loaded from the detected configuration.
*/
function Gamepad( type, options ) {
EventEmitter.call( this );
this._usb = null;
this._type = type;
this._config = {};
this._states = {};
this._options = options || {};
// Expand number of max listeners to 100 (so we can listen to up to 100 event types)
this.setMaxListeners(100)
// on process exit, disconnect from any devices we may be connected to.
process.on( 'exit', this.disconnect.bind( this ) );
}
// Prototype Methods
// =================
/**
* This function will load the configuration file for the specified controller type.
* If the configuration file for the specified controller type does not exist, we
* bail.
*
* @private
* @method _loadConfiguration
*/
Gamepad.prototype._loadConfiguration = function() {
var configPath = path.resolve( __dirname, './controllers/' + this._type + '.json' );
if( ! fs.existsSync( configPath ) ) {
console.log( ( 'The controller configuration for "' + this._type + '" does not exist.' ).red );
process.exit( 0 );
}
this._config = require( configPath );
// if the user specified a custom vendorID or productID, use that instead
if( this._options.vendorID ) {
this._config.vendorID = parseInt( this._options.vendorID, 10 );
}
if( this._options.productID ) {
this._config.productID = parseInt( this._options.productID, 10 );
}
};
/**
* Detects whether or not the specified string has a product ID in the form of
* "vendorID/productID" or not.
*
* @private
* @method _hasProductId
*
* @param {String} str The string we're using to check for a product ID.
* @return {Boolean} Indicates whether or not a product ID was detected.
*/
Gamepad.prototype._hasProductId = function( str ) {
return str.indexOf( '/' ) > -1;
};
/**
* Detects the configuration that will be used to load this controller type. If
* a controller configuration is already defined, we'll use it. Otherwise, we'll
* try to detect the specific controller configuration to use.
*
* @private
* @method _detectControllerConfiguration
*
* @return {Boolean} Indicates whether or not the controller configuration could be detected.
*/
Gamepad.prototype._detectControllerConfiguration = function() {
// check to see if a product ID was already specified in the product type.
if( this._hasProductId( this._type ) ) {
return true;
}
// check to see if the vendor exists
var platformPath = path.resolve( __dirname, './controllers/' + this._type + '/' );
if( ! fs.existsSync( platformPath ) ) {
console.log( ( 'The vendor "' + this._type + '" does not exist.' ).red );
process.exit( 0 );
}
// we know the vendor exists, so loop through HID devices and the
// configurations for this particular vendor while checking to see if any of
// them match each other (indicating that we have a configuration something
// that is currently plugged in).
//
// TODO: make this faster by looping through loaded controllers once instead
// of once per HID device.
var devices = HID.devices();
var files = fs.readdirSync( platformPath ), tmpConfig, tmpDevice;
for( var i = 0, len = files.length; i < len; i++ ) {
tmpConfig = platformPath + '/' + files[ i ];
tmpConfig = require( tmpConfig );
// check to see if this vendorID and productID exist
for( var j = 0, leng = devices.length; j < leng; j++ ) {
tmpDevice = devices[ j ];
if( tmpConfig.vendorID === tmpDevice.vendorId && tmpConfig.productID === tmpDevice.productId ) {
this._type = this._type + '/' + files[ i ].replace( '.json', '' );
return true;
}
}
}
return false;
};
Gamepad.prototype.connect = function() {
if( ! this._detectControllerConfiguration() ) {
console.log( ( 'A product for the vendor "' + this._type + '" could not be detected.' ).red );
process.exit( 0 );
}
this.emit( 'connecting' );
this._loadConfiguration();
this._usb = new HID.HID( this._config.vendorID, this._config.productID );
// Debug: Show each pin and current value
if (typeof this._options.debug === 'boolean' && this._options.debug === true) {
this._usb.on( 'data', function(data) {
// Build a message with input values from pins
// Example:
// Input pin values: 0:128 | 1:127 | 2:128 | 3:132 | 4:40 | 5:0 | 6:0 | 7:255 |
var logMessage = 'Input pin values: '
// Check up to 100 pins for input
for( var i = 0; i < 100; i ++ ) {
if (i in data) {
logMessage += i + ':' + data[i] + ' | '
}
}
console.log(logMessage);
});
}
this._usb.on( 'data', this._onControllerFrame.bind( this ) );
this.emit( 'connected' );
return this;
};
Gamepad.prototype._onControllerFrame = function( data ) {
this._processJoysticks( data );
this._processButtons( data );
this._processStatus( data );
};
Gamepad.prototype._processJoysticks = function( data ) {
if( ! this._config.joysticks ) {
return;
}
var joysticks = this._config.joysticks, joystick, currentState;
for( var i = 0, len = joysticks.length; i < len; i++ ) {
joystick = joysticks[ i ];
if( ! this._states[ joystick.name ] ) {
this._states[ joystick.name ] = {
x : data[ joystick.x.pin ],
y : data[ joystick.y.pin ]
};
continue;
}
currentState = this._states[ joystick.name ];
if( currentState.x !== data[ joystick.x.pin ] || currentState.y !== data[ joystick.y.pin ] ) {
currentState = {
x : data[ joystick.x.pin ],
y : data[ joystick.y.pin ]
};
this._states[ joystick.name ] = currentState;
this.emit( joystick.name + ':move', currentState );
}
}
};
Gamepad.prototype._processButtons = function( data ) {
if( ! this._config.buttons ) {
return;
}
var buttons = this._config.buttons, button, isPressed, currentState;
for( var i = 0, len = buttons.length; i < len; i ++ ) {
button = buttons[ i ];
isPressed = ( data[ button.pin ] & 0xff ) === button.value;
if( this._states[ button.name ] === undefined ) {
this._states[ button.name ] = isPressed;
if( isPressed ) {
this.emit( button.name + ':press' );
}
continue;
}
currentState = this._states[ button.name ];
if( isPressed && currentState !== isPressed ) {
this.emit( button.name + ':press' );
} else if( ! isPressed && currentState !== isPressed ) {
this.emit( button.name + ':release' );
}
this._states[ button.name ] = isPressed;
}
};
Gamepad.prototype._processStatus = function( data ) {
if( ! this._config.status ) {
return;
}
var statuses = this._config.status, status, state, states;
var currentState;
for( var i = 0, len = statuses.length; i < len; i++ ) {
status = statuses[ i ];
state = data[ status.pin ] & 0xff;
states = status.states;
for( var j = 0, length = states.length; j < length; j++ ) {
if( states[ j ].value === state ) {
state = states[ j ].state;
break;
}
}
currentState = this._states[ status.name ];
if( currentState !== state ) {
this.emit( status.name + ':change', state );
}
this._states[ status.name ] = state;
}
};
Gamepad.prototype.disconnect = function() {
if( this._usb ) {
this._usb.close();
}
};