@sensslen/node-gamepad 1.0.4 → 1.0.5-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/IConfig.d.ts +30 -25
- package/NodeGamepad.d.ts +9 -5
- package/NodeGamepad.js +67 -42
- package/package.json +2 -2
- package/test.d.ts +0 -1
package/IConfig.d.ts
CHANGED
|
@@ -1,29 +1,34 @@
|
|
|
1
1
|
import { IDeviceSpec } from './IDeviceSpec';
|
|
2
|
-
export interface
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
x: {
|
|
6
|
-
pin: number;
|
|
7
|
-
};
|
|
8
|
-
y: {
|
|
9
|
-
pin: number;
|
|
10
|
-
};
|
|
11
|
-
}[];
|
|
12
|
-
buttons?: {
|
|
13
|
-
value: string;
|
|
14
|
-
pin: number;
|
|
15
|
-
name: string;
|
|
16
|
-
}[];
|
|
17
|
-
status?: {
|
|
18
|
-
name: string;
|
|
2
|
+
export interface IJoyStickConfig {
|
|
3
|
+
name: string;
|
|
4
|
+
x: {
|
|
19
5
|
pin: number;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
state: string;
|
|
23
|
-
}[];
|
|
24
|
-
}[];
|
|
25
|
-
scale?: {
|
|
6
|
+
};
|
|
7
|
+
y: {
|
|
26
8
|
pin: number;
|
|
27
|
-
|
|
28
|
-
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export interface IButtonConfig {
|
|
12
|
+
value: string;
|
|
13
|
+
pin: number;
|
|
14
|
+
name: string;
|
|
15
|
+
}
|
|
16
|
+
export interface IStatusConfig {
|
|
17
|
+
name: string;
|
|
18
|
+
pin: number;
|
|
19
|
+
states: IStateMappingConfig[];
|
|
20
|
+
}
|
|
21
|
+
export interface IStateMappingConfig {
|
|
22
|
+
value: number;
|
|
23
|
+
state: string;
|
|
24
|
+
}
|
|
25
|
+
export interface IScaleConfig {
|
|
26
|
+
pin: number;
|
|
27
|
+
name: string;
|
|
28
|
+
}
|
|
29
|
+
export interface IConfig extends IDeviceSpec {
|
|
30
|
+
joysticks?: IJoyStickConfig[];
|
|
31
|
+
buttons?: IButtonConfig[];
|
|
32
|
+
status?: IStatusConfig[];
|
|
33
|
+
scales?: IScaleConfig[];
|
|
29
34
|
}
|
package/NodeGamepad.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { HID } from 'node-hid';
|
|
3
|
-
import { EventEmitter } from 'events';
|
|
4
3
|
import { IConfig } from './IConfig';
|
|
4
|
+
import { EventEmitter } from 'events';
|
|
5
5
|
import { ILogger } from './ILogger';
|
|
6
6
|
export declare class NodeGamepad extends EventEmitter {
|
|
7
7
|
private config;
|
|
8
8
|
private logger?;
|
|
9
9
|
protected _usb?: HID;
|
|
10
|
-
private
|
|
10
|
+
private _running;
|
|
11
11
|
private _joystickStates;
|
|
12
12
|
private _buttonStates;
|
|
13
13
|
private _statusStates;
|
|
@@ -17,18 +17,22 @@ export declare class NodeGamepad extends EventEmitter {
|
|
|
17
17
|
constructor(config: IConfig, logger?: ILogger | undefined);
|
|
18
18
|
start(): void;
|
|
19
19
|
stop(): void;
|
|
20
|
-
rumble(_duration: number): void;
|
|
20
|
+
rumble(_intensity: number, _duration: number): void;
|
|
21
21
|
private logDebug;
|
|
22
22
|
private log;
|
|
23
23
|
private connect;
|
|
24
24
|
private stopConnectionProcess;
|
|
25
25
|
private registerProgramExitEvents;
|
|
26
|
-
private
|
|
26
|
+
private closeUsbDevice;
|
|
27
27
|
private toIDeviceSpec;
|
|
28
28
|
private onControllerFrame;
|
|
29
29
|
private processJoysticks;
|
|
30
|
+
private processJoystick;
|
|
30
31
|
private processButtons;
|
|
32
|
+
private processButton;
|
|
31
33
|
private processScales;
|
|
32
|
-
private
|
|
34
|
+
private processScale;
|
|
35
|
+
private processStates;
|
|
36
|
+
private processState;
|
|
33
37
|
private getStateName;
|
|
34
38
|
}
|
package/NodeGamepad.js
CHANGED
|
@@ -26,26 +26,30 @@ var NodeGamepad = /** @class */ (function (_super) {
|
|
|
26
26
|
_this.config = config;
|
|
27
27
|
_this.logger = logger;
|
|
28
28
|
_this._usb = undefined;
|
|
29
|
-
_this.
|
|
29
|
+
_this._running = false;
|
|
30
30
|
_this._joystickStates = {};
|
|
31
31
|
_this._buttonStates = {};
|
|
32
32
|
_this._statusStates = {};
|
|
33
33
|
_this._scaleStates = {};
|
|
34
|
-
_this._connectionRetryTimeoutInMs =
|
|
34
|
+
_this._connectionRetryTimeoutInMs = 500;
|
|
35
35
|
return _this;
|
|
36
36
|
}
|
|
37
37
|
NodeGamepad.prototype.start = function () {
|
|
38
|
+
if (this._running) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
38
41
|
this.log("Starting connection procedure to device:".concat(JSON.stringify(this.toIDeviceSpec(this.config))));
|
|
39
42
|
this.registerProgramExitEvents();
|
|
43
|
+
this._running = true;
|
|
40
44
|
this.connect();
|
|
41
45
|
};
|
|
42
46
|
NodeGamepad.prototype.stop = function () {
|
|
43
|
-
this.
|
|
47
|
+
this._running = false;
|
|
44
48
|
this.stopConnectionProcess();
|
|
45
|
-
this.
|
|
49
|
+
this.closeUsbDevice();
|
|
46
50
|
};
|
|
47
|
-
NodeGamepad.prototype.rumble = function (_duration) {
|
|
48
|
-
//
|
|
51
|
+
NodeGamepad.prototype.rumble = function (_intensity, _duration) {
|
|
52
|
+
// todo
|
|
49
53
|
};
|
|
50
54
|
NodeGamepad.prototype.logDebug = function (toLog) {
|
|
51
55
|
var _a;
|
|
@@ -60,7 +64,7 @@ var NodeGamepad = /** @class */ (function (_super) {
|
|
|
60
64
|
};
|
|
61
65
|
NodeGamepad.prototype.connect = function () {
|
|
62
66
|
var _this = this;
|
|
63
|
-
if (this.
|
|
67
|
+
if (!this._running) {
|
|
64
68
|
return;
|
|
65
69
|
}
|
|
66
70
|
var deviceList = (0, node_hid_1.devices)(this.config.vendorID, this.config.productID);
|
|
@@ -87,10 +91,10 @@ var NodeGamepad = /** @class */ (function (_super) {
|
|
|
87
91
|
this._usb.on('data', function (data) { return _this.onControllerFrame(data); });
|
|
88
92
|
this._usb.on('error', function (error) {
|
|
89
93
|
_this.log("Error occurred:".concat(JSON.stringify(error)));
|
|
94
|
+
_this.emit('disconnected');
|
|
95
|
+
_this.closeUsbDevice();
|
|
90
96
|
setTimeout(function () {
|
|
91
97
|
_this.log('reconnecting');
|
|
92
|
-
_this.emit('disconnected');
|
|
93
|
-
_this.stopUsbRead();
|
|
94
98
|
_this.connect();
|
|
95
99
|
}, 0);
|
|
96
100
|
});
|
|
@@ -99,6 +103,7 @@ var NodeGamepad = /** @class */ (function (_super) {
|
|
|
99
103
|
var typedError = error;
|
|
100
104
|
this.log("Connecting failed: ".concat(typedError.message));
|
|
101
105
|
this.log('trying again later.');
|
|
106
|
+
this.closeUsbDevice();
|
|
102
107
|
this._connectRetryTimeout = setTimeout(function () { return _this.connect(); }, this._connectionRetryTimeoutInMs);
|
|
103
108
|
}
|
|
104
109
|
};
|
|
@@ -115,7 +120,7 @@ var NodeGamepad = /** @class */ (function (_super) {
|
|
|
115
120
|
_this.stop();
|
|
116
121
|
});
|
|
117
122
|
};
|
|
118
|
-
NodeGamepad.prototype.
|
|
123
|
+
NodeGamepad.prototype.closeUsbDevice = function () {
|
|
119
124
|
if (this._usb) {
|
|
120
125
|
this._usb.close();
|
|
121
126
|
this._usb = undefined;
|
|
@@ -132,66 +137,86 @@ var NodeGamepad = /** @class */ (function (_super) {
|
|
|
132
137
|
this.logDebug(JSON.stringify(data));
|
|
133
138
|
this.processJoysticks(data);
|
|
134
139
|
this.processButtons(data);
|
|
135
|
-
this.
|
|
140
|
+
this.processStates(data);
|
|
136
141
|
this.processScales(data);
|
|
137
142
|
};
|
|
138
143
|
NodeGamepad.prototype.processJoysticks = function (data) {
|
|
139
144
|
var _this = this;
|
|
140
145
|
var _a;
|
|
141
146
|
(_a = this.config.joysticks) === null || _a === void 0 ? void 0 : _a.forEach(function (joystick) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
x: data[joystick.x.pin],
|
|
145
|
-
y: data[joystick.y.pin],
|
|
146
|
-
};
|
|
147
|
-
if (oldState === undefined || oldState.x !== newState.x || oldState.y !== newState.y) {
|
|
148
|
-
_this.emit(joystick.name + ':move', newState);
|
|
147
|
+
if (data.length > joystick.x.pin || data.length > joystick.y.pin) {
|
|
148
|
+
_this.processJoystick(joystick, data);
|
|
149
149
|
}
|
|
150
|
-
_this._joystickStates[joystick.name] = newState;
|
|
151
150
|
});
|
|
152
151
|
};
|
|
152
|
+
NodeGamepad.prototype.processJoystick = function (joystick, data) {
|
|
153
|
+
var oldState = this._joystickStates[joystick.name];
|
|
154
|
+
var newState = {
|
|
155
|
+
x: data[joystick.x.pin],
|
|
156
|
+
y: data[joystick.y.pin],
|
|
157
|
+
};
|
|
158
|
+
if (oldState === undefined || oldState.x !== newState.x || oldState.y !== newState.y) {
|
|
159
|
+
this.emit(joystick.name + ':move', newState);
|
|
160
|
+
}
|
|
161
|
+
this._joystickStates[joystick.name] = newState;
|
|
162
|
+
};
|
|
153
163
|
NodeGamepad.prototype.processButtons = function (data) {
|
|
154
164
|
var _this = this;
|
|
155
165
|
var _a;
|
|
156
166
|
(_a = this.config.buttons) === null || _a === void 0 ? void 0 : _a.forEach(function (button) {
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (oldState == undefined) {
|
|
160
|
-
if (newState) {
|
|
161
|
-
_this.emit(button.name + ':press');
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
else if (oldState !== newState) {
|
|
165
|
-
var emitEvent = newState ? "".concat(button.name, ":press") : "".concat(button.name, ":release");
|
|
166
|
-
_this.emit(emitEvent);
|
|
167
|
+
if (data.length > button.pin) {
|
|
168
|
+
_this.processButton(data, button);
|
|
167
169
|
}
|
|
168
|
-
_this._buttonStates[button.name] = newState;
|
|
169
170
|
});
|
|
170
171
|
};
|
|
172
|
+
NodeGamepad.prototype.processButton = function (data, config) {
|
|
173
|
+
var oldState = this._buttonStates[config.name];
|
|
174
|
+
var newState = (0, mathjs_1.evaluate)(config.value, { value: data[config.pin] });
|
|
175
|
+
if (oldState == undefined) {
|
|
176
|
+
if (newState) {
|
|
177
|
+
this.emit(config.name + ':press');
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
else if (oldState !== newState) {
|
|
181
|
+
var emitEvent = newState ? "".concat(config.name, ":press") : "".concat(config.name, ":release");
|
|
182
|
+
this.emit(emitEvent);
|
|
183
|
+
}
|
|
184
|
+
this._buttonStates[config.name] = newState;
|
|
185
|
+
};
|
|
171
186
|
NodeGamepad.prototype.processScales = function (data) {
|
|
172
187
|
var _this = this;
|
|
173
188
|
var _a;
|
|
174
|
-
(_a = this.config.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
if (oldState !== newState) {
|
|
178
|
-
_this.emit(scale.name + ':change', newState);
|
|
189
|
+
(_a = this.config.scales) === null || _a === void 0 ? void 0 : _a.forEach(function (scale) {
|
|
190
|
+
if (data.length > scale.pin) {
|
|
191
|
+
_this.processScale(scale, data);
|
|
179
192
|
}
|
|
180
|
-
_this._scaleStates[scale.name] = newState;
|
|
181
193
|
});
|
|
182
194
|
};
|
|
183
|
-
NodeGamepad.prototype.
|
|
195
|
+
NodeGamepad.prototype.processScale = function (config, data) {
|
|
196
|
+
var oldState = this._scaleStates[config.name];
|
|
197
|
+
var newState = data[config.pin];
|
|
198
|
+
if (oldState !== newState) {
|
|
199
|
+
this.emit(config.name + ':change', newState);
|
|
200
|
+
}
|
|
201
|
+
this._scaleStates[config.name] = newState;
|
|
202
|
+
};
|
|
203
|
+
NodeGamepad.prototype.processStates = function (data) {
|
|
184
204
|
var _this = this;
|
|
185
205
|
var _a;
|
|
186
206
|
(_a = this.config.status) === null || _a === void 0 ? void 0 : _a.forEach(function (status) {
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
if (oldState !== newState) {
|
|
190
|
-
_this.emit(status.name + ':change', _this.getStateName(status.states, newState));
|
|
207
|
+
if (data.length > status.pin) {
|
|
208
|
+
_this.processState(status, data);
|
|
191
209
|
}
|
|
192
|
-
_this._statusStates[status.name] = newState;
|
|
193
210
|
});
|
|
194
211
|
};
|
|
212
|
+
NodeGamepad.prototype.processState = function (config, data) {
|
|
213
|
+
var oldState = this._statusStates[config.name];
|
|
214
|
+
var newState = data[config.pin];
|
|
215
|
+
if (oldState !== newState) {
|
|
216
|
+
this.emit(config.name + ':change', this.getStateName(config.states, newState));
|
|
217
|
+
}
|
|
218
|
+
this._statusStates[config.name] = newState;
|
|
219
|
+
};
|
|
195
220
|
NodeGamepad.prototype.getStateName = function (states, value) {
|
|
196
221
|
for (var _i = 0, states_1 = states; _i < states_1.length; _i++) {
|
|
197
222
|
var state = states_1[_i];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sensslen/node-gamepad",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5-beta.3",
|
|
4
4
|
"description": "node-gamepad is a package for node that allows you to effortlessly interface your node applications with a variety of gamepad controllers. This is a port of node-gamepad library to typescript by also removing some of the restrictions implied by this library (namely allowing gamepads to be subcleassed and also improving usb interaction)",
|
|
5
5
|
"homepage": "https://github.com/sensslen/node-gamepad",
|
|
6
6
|
"bugs": {
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "git@github.com:
|
|
11
|
+
"url": "git@github.com:sensslen/node-gamepad.git"
|
|
12
12
|
},
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"author": {
|
package/test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|