@sensslen/node-gamepad 1.0.9-beta.24 → 1.0.9-beta.241
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/NodeGamepad.d.ts +1 -2
- package/NodeGamepad.js +99 -124
- package/index.js +1 -1
- package/package.json +3 -3
package/NodeGamepad.d.ts
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { HID } from 'node-hid';
|
|
2
1
|
import { IConfig } from './IConfig';
|
|
3
2
|
import { EventEmitter } from 'events';
|
|
4
3
|
import { ILogger } from './ILogger';
|
|
5
4
|
export declare class NodeGamepad extends EventEmitter {
|
|
6
5
|
private config;
|
|
7
6
|
private logger?;
|
|
8
|
-
|
|
7
|
+
private _usb?;
|
|
9
8
|
private _running;
|
|
10
9
|
private _joystickStates;
|
|
11
10
|
private _buttonStates;
|
package/NodeGamepad.js
CHANGED
|
@@ -1,157 +1,137 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
3
|
exports.NodeGamepad = void 0;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
NodeGamepad.prototype.start = function () {
|
|
4
|
+
const node_hid_1 = require("node-hid");
|
|
5
|
+
const events_1 = require("events");
|
|
6
|
+
const mathjs_1 = require("mathjs");
|
|
7
|
+
class NodeGamepad extends events_1.EventEmitter {
|
|
8
|
+
constructor(config, logger) {
|
|
9
|
+
super();
|
|
10
|
+
this.config = config;
|
|
11
|
+
this.logger = logger;
|
|
12
|
+
this._usb = undefined;
|
|
13
|
+
this._running = false;
|
|
14
|
+
this._joystickStates = {};
|
|
15
|
+
this._buttonStates = {};
|
|
16
|
+
this._statusStates = {};
|
|
17
|
+
this._scaleStates = {};
|
|
18
|
+
this._connectionRetryTimeoutInMs = 500;
|
|
19
|
+
}
|
|
20
|
+
start() {
|
|
38
21
|
if (this._running) {
|
|
39
22
|
return;
|
|
40
23
|
}
|
|
41
|
-
this.log(
|
|
24
|
+
this.log(`Starting connection procedure to device:${JSON.stringify(this.toIDeviceSpec(this.config))}`);
|
|
42
25
|
this.registerProgramExitEvents();
|
|
43
26
|
this._running = true;
|
|
44
27
|
this.connect();
|
|
45
|
-
}
|
|
46
|
-
|
|
28
|
+
}
|
|
29
|
+
stop() {
|
|
47
30
|
this._running = false;
|
|
48
31
|
this.stopConnectionProcess();
|
|
49
32
|
this.closeUsbDevice();
|
|
50
|
-
}
|
|
51
|
-
|
|
33
|
+
}
|
|
34
|
+
rumble(_intensity, _duration) {
|
|
52
35
|
// todo
|
|
53
|
-
}
|
|
54
|
-
|
|
36
|
+
}
|
|
37
|
+
logDebug(toLog) {
|
|
55
38
|
var _a;
|
|
56
39
|
if ((_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug) {
|
|
57
|
-
this.logger.debug(
|
|
40
|
+
this.logger.debug(`NodeGamepad (Debug):${toLog}`);
|
|
58
41
|
}
|
|
59
|
-
}
|
|
60
|
-
|
|
42
|
+
}
|
|
43
|
+
log(toLog) {
|
|
61
44
|
if (this.logger) {
|
|
62
|
-
this.logger.info(
|
|
45
|
+
this.logger.info(`NodeGamepad:${toLog}`);
|
|
63
46
|
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
var _this = this;
|
|
47
|
+
}
|
|
48
|
+
connect() {
|
|
67
49
|
if (!this._running) {
|
|
68
50
|
return;
|
|
69
51
|
}
|
|
70
|
-
|
|
52
|
+
let deviceList = (0, node_hid_1.devices)(this.config.vendorID, this.config.productID);
|
|
71
53
|
if (this.config.serialNumber) {
|
|
72
|
-
deviceList = deviceList.filter(
|
|
54
|
+
deviceList = deviceList.filter((d) => d.serialNumber === this.config.serialNumber);
|
|
73
55
|
}
|
|
74
56
|
if (deviceList.length < 1) {
|
|
75
57
|
this.logDebug('Device not found, trying again later');
|
|
76
|
-
this._connectRetryTimeout = setTimeout(
|
|
58
|
+
this._connectRetryTimeout = setTimeout(() => this.connect(), this._connectionRetryTimeoutInMs);
|
|
77
59
|
return;
|
|
78
60
|
}
|
|
79
|
-
|
|
61
|
+
const deviceToConnectTo = deviceList[0];
|
|
80
62
|
if ((deviceToConnectTo === null || deviceToConnectTo === void 0 ? void 0 : deviceToConnectTo.path) === undefined) {
|
|
81
63
|
this.logDebug('Failed to connect. Checking again later.');
|
|
82
|
-
this._connectRetryTimeout = setTimeout(
|
|
64
|
+
this._connectRetryTimeout = setTimeout(() => this.connect(), this._connectionRetryTimeoutInMs);
|
|
83
65
|
return;
|
|
84
66
|
}
|
|
85
|
-
this.logDebug(
|
|
67
|
+
this.logDebug(`connecting to:${JSON.stringify(deviceToConnectTo)}`);
|
|
86
68
|
try {
|
|
87
69
|
this._usb = new node_hid_1.HID(deviceToConnectTo.path);
|
|
88
70
|
this.log('connected');
|
|
89
71
|
this.emit('connected');
|
|
90
72
|
this._connectRetryTimeout = undefined;
|
|
91
|
-
this._usb.on('data',
|
|
92
|
-
this._usb.on('error',
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
setTimeout(
|
|
97
|
-
|
|
98
|
-
|
|
73
|
+
this._usb.on('data', (data) => this.onControllerFrame(data));
|
|
74
|
+
this._usb.on('error', (error) => {
|
|
75
|
+
this.log(`Error occurred:${JSON.stringify(error)}`);
|
|
76
|
+
this.emit('disconnected');
|
|
77
|
+
this.closeUsbDevice();
|
|
78
|
+
setTimeout(() => {
|
|
79
|
+
this.log('reconnecting');
|
|
80
|
+
this.connect();
|
|
99
81
|
}, 0);
|
|
100
82
|
});
|
|
101
83
|
}
|
|
102
84
|
catch (error) {
|
|
103
|
-
|
|
104
|
-
this.log(
|
|
85
|
+
const typedError = error;
|
|
86
|
+
this.log(`Connecting failed: ${typedError.message}`);
|
|
105
87
|
this.log('trying again later.');
|
|
106
88
|
this.closeUsbDevice();
|
|
107
|
-
this._connectRetryTimeout = setTimeout(
|
|
89
|
+
this._connectRetryTimeout = setTimeout(() => this.connect(), this._connectionRetryTimeoutInMs);
|
|
108
90
|
}
|
|
109
|
-
}
|
|
110
|
-
|
|
91
|
+
}
|
|
92
|
+
stopConnectionProcess() {
|
|
111
93
|
if (this._connectRetryTimeout) {
|
|
112
94
|
clearTimeout(this._connectRetryTimeout);
|
|
113
95
|
this._connectRetryTimeout = undefined;
|
|
114
96
|
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
_this.stop();
|
|
97
|
+
}
|
|
98
|
+
registerProgramExitEvents() {
|
|
99
|
+
process.on('exit', () => {
|
|
100
|
+
this.logDebug('exit encontered, cleaning up');
|
|
101
|
+
this.stop();
|
|
121
102
|
});
|
|
122
|
-
}
|
|
123
|
-
|
|
103
|
+
}
|
|
104
|
+
closeUsbDevice() {
|
|
124
105
|
if (this._usb) {
|
|
125
106
|
this._usb.close();
|
|
126
107
|
this._usb = undefined;
|
|
127
108
|
}
|
|
128
|
-
}
|
|
129
|
-
|
|
109
|
+
}
|
|
110
|
+
toIDeviceSpec(spec) {
|
|
130
111
|
return {
|
|
131
112
|
vendorID: spec.vendorID,
|
|
132
113
|
productID: spec.productID,
|
|
133
114
|
serialNumber: spec.serialNumber,
|
|
134
115
|
};
|
|
135
|
-
}
|
|
136
|
-
|
|
116
|
+
}
|
|
117
|
+
onControllerFrame(data) {
|
|
137
118
|
this.logDebug(JSON.stringify(data));
|
|
138
119
|
this.processJoysticks(data);
|
|
139
120
|
this.processButtons(data);
|
|
140
121
|
this.processStates(data);
|
|
141
122
|
this.processScales(data);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
var _this = this;
|
|
123
|
+
}
|
|
124
|
+
processJoysticks(data) {
|
|
145
125
|
var _a;
|
|
146
|
-
(_a = this.config.joysticks) === null || _a === void 0 ? void 0 : _a.forEach(
|
|
126
|
+
(_a = this.config.joysticks) === null || _a === void 0 ? void 0 : _a.forEach((joystick) => {
|
|
147
127
|
if (data.length > joystick.x.pin || data.length > joystick.y.pin) {
|
|
148
|
-
|
|
128
|
+
this.processJoystick(joystick, data);
|
|
149
129
|
}
|
|
150
130
|
});
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
131
|
+
}
|
|
132
|
+
processJoystick(joystick, data) {
|
|
133
|
+
const oldState = this._joystickStates[joystick.name];
|
|
134
|
+
const newState = {
|
|
155
135
|
x: data[joystick.x.pin],
|
|
156
136
|
y: data[joystick.y.pin],
|
|
157
137
|
};
|
|
@@ -159,74 +139,69 @@ var NodeGamepad = /** @class */ (function (_super) {
|
|
|
159
139
|
this.emit(joystick.name + ':move', newState);
|
|
160
140
|
}
|
|
161
141
|
this._joystickStates[joystick.name] = newState;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
var _this = this;
|
|
142
|
+
}
|
|
143
|
+
processButtons(data) {
|
|
165
144
|
var _a;
|
|
166
|
-
(_a = this.config.buttons) === null || _a === void 0 ? void 0 : _a.forEach(
|
|
145
|
+
(_a = this.config.buttons) === null || _a === void 0 ? void 0 : _a.forEach((button) => {
|
|
167
146
|
if (data.length > button.pin) {
|
|
168
|
-
|
|
147
|
+
this.processButton(data, button);
|
|
169
148
|
}
|
|
170
149
|
});
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
150
|
+
}
|
|
151
|
+
processButton(data, config) {
|
|
152
|
+
const oldState = this._buttonStates[config.name];
|
|
153
|
+
const newState = (0, mathjs_1.evaluate)(config.value, { value: data[config.pin] });
|
|
175
154
|
if (oldState == undefined) {
|
|
176
155
|
if (newState) {
|
|
177
156
|
this.emit(config.name + ':press');
|
|
178
157
|
}
|
|
179
158
|
}
|
|
180
159
|
else if (oldState !== newState) {
|
|
181
|
-
|
|
160
|
+
const emitEvent = newState ? `${config.name}:press` : `${config.name}:release`;
|
|
182
161
|
this.emit(emitEvent);
|
|
183
162
|
}
|
|
184
163
|
this._buttonStates[config.name] = newState;
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
var _this = this;
|
|
164
|
+
}
|
|
165
|
+
processScales(data) {
|
|
188
166
|
var _a;
|
|
189
|
-
(_a = this.config.scales) === null || _a === void 0 ? void 0 : _a.forEach(
|
|
167
|
+
(_a = this.config.scales) === null || _a === void 0 ? void 0 : _a.forEach((scale) => {
|
|
190
168
|
if (data.length > scale.pin) {
|
|
191
|
-
|
|
169
|
+
this.processScale(scale, data);
|
|
192
170
|
}
|
|
193
171
|
});
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
172
|
+
}
|
|
173
|
+
processScale(config, data) {
|
|
174
|
+
const oldState = this._scaleStates[config.name];
|
|
175
|
+
const newState = data[config.pin];
|
|
198
176
|
if (oldState !== newState) {
|
|
199
177
|
this.emit(config.name + ':change', newState);
|
|
200
178
|
}
|
|
201
179
|
this._scaleStates[config.name] = newState;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
var _this = this;
|
|
180
|
+
}
|
|
181
|
+
processStates(data) {
|
|
205
182
|
var _a;
|
|
206
|
-
(_a = this.config.status) === null || _a === void 0 ? void 0 : _a.forEach(
|
|
183
|
+
(_a = this.config.status) === null || _a === void 0 ? void 0 : _a.forEach((status) => {
|
|
207
184
|
if (data.length > status.pin) {
|
|
208
|
-
|
|
185
|
+
this.processState(status, data);
|
|
209
186
|
}
|
|
210
187
|
});
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
188
|
+
}
|
|
189
|
+
processState(config, data) {
|
|
190
|
+
const oldState = this._statusStates[config.name];
|
|
191
|
+
const newState = data[config.pin];
|
|
215
192
|
if (oldState !== newState) {
|
|
216
193
|
this.emit(config.name + ':change', this.getStateName(config.states, newState));
|
|
217
194
|
}
|
|
218
195
|
this._statusStates[config.name] = newState;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
for (
|
|
222
|
-
var state = states_1[_i];
|
|
196
|
+
}
|
|
197
|
+
getStateName(states, value) {
|
|
198
|
+
for (const state of states) {
|
|
223
199
|
if (state.value === value) {
|
|
224
200
|
return state.state;
|
|
225
201
|
}
|
|
226
202
|
}
|
|
227
|
-
return
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
}(events_1.EventEmitter));
|
|
203
|
+
return `unknown state:${value}`;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
231
206
|
exports.NodeGamepad = NodeGamepad;
|
|
232
207
|
//# sourceMappingURL=NodeGamepad.js.map
|
package/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.NodeGamepad = void 0;
|
|
4
|
-
|
|
4
|
+
const NodeGamepad_1 = require("./NodeGamepad");
|
|
5
5
|
Object.defineProperty(exports, "NodeGamepad", { enumerable: true, get: function () { return NodeGamepad_1.NodeGamepad; } });
|
|
6
6
|
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sensslen/node-gamepad",
|
|
3
|
-
"version": "1.0.9-beta.
|
|
3
|
+
"version": "1.0.9-beta.241",
|
|
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+ssh://git@github.com/sensslen/node-gamepad.git"
|
|
12
12
|
},
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"author": {
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"types": "index.d.ts",
|
|
21
21
|
"scripts": {},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"mathjs": "^
|
|
23
|
+
"mathjs": "^15.0.0",
|
|
24
24
|
"node-hid": "^3.1.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {}
|