@sensslen/node-gamepad 1.0.3 → 1.0.4

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 CHANGED
@@ -1,29 +1,29 @@
1
- import { IDeviceSpec } from './IDeviceSpec';
2
- export interface IConfig extends IDeviceSpec {
3
- joysticks?: {
4
- name: string;
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;
19
- pin: number;
20
- states: {
21
- value: number;
22
- state: string;
23
- }[];
24
- }[];
25
- scale?: {
26
- pin: number;
27
- name: string;
28
- }[];
29
- }
1
+ import { IDeviceSpec } from './IDeviceSpec';
2
+ export interface IConfig extends IDeviceSpec {
3
+ joysticks?: {
4
+ name: string;
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;
19
+ pin: number;
20
+ states: {
21
+ value: number;
22
+ state: string;
23
+ }[];
24
+ }[];
25
+ scale?: {
26
+ pin: number;
27
+ name: string;
28
+ }[];
29
+ }
package/IConfig.js CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=IConfig.js.map
package/IDeviceSpec.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export interface IDeviceSpec {
2
- vendorID: number;
3
- productID: number;
4
- serialNumber?: string;
5
- }
1
+ export interface IDeviceSpec {
2
+ vendorID: number;
3
+ productID: number;
4
+ serialNumber?: string;
5
+ }
package/IDeviceSpec.js CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=IDeviceSpec.js.map
package/ILogger.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- export interface ILogger {
2
- debug?: (toLog: string) => void;
3
- info(toLog: string): void;
4
- }
1
+ export interface ILogger {
2
+ debug?: (toLog: string) => void;
3
+ info(toLog: string): void;
4
+ }
package/ILogger.js CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=ILogger.js.map
@@ -1,4 +1,4 @@
1
- export interface JoyStickValue {
2
- x: number;
3
- y: number;
4
- }
1
+ export interface JoyStickValue {
2
+ x: number;
3
+ y: number;
4
+ }
package/JoyStickValue.js CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=JoyStickValue.js.map
package/NodeGamepad.d.ts CHANGED
@@ -1,34 +1,34 @@
1
- /// <reference types="node" />
2
- import { HID } from 'node-hid';
3
- import { EventEmitter } from 'events';
4
- import { IConfig } from './IConfig';
5
- import { ILogger } from './ILogger';
6
- export declare class NodeGamepad extends EventEmitter {
7
- private config;
8
- private logger?;
9
- protected _usb?: HID;
10
- private _stopped;
11
- private _joystickStates;
12
- private _buttonStates;
13
- private _statusStates;
14
- private _scaleStates;
15
- private _connectRetryTimeout?;
16
- private readonly _connectionRetryTimeoutInMs;
17
- constructor(config: IConfig, logger?: ILogger | undefined);
18
- start(): void;
19
- stop(): void;
20
- rumble(_duration: number): void;
21
- private logDebug;
22
- private log;
23
- private connect;
24
- private stopConnectionProcess;
25
- private registerStopProgramStopEvent;
26
- private disconnect;
27
- private toIDeviceSpec;
28
- private onControllerFrame;
29
- private processJoysticks;
30
- private processButtons;
31
- private processScales;
32
- private processStatus;
33
- private getStateName;
34
- }
1
+ /// <reference types="node" />
2
+ import { HID } from 'node-hid';
3
+ import { EventEmitter } from 'events';
4
+ import { IConfig } from './IConfig';
5
+ import { ILogger } from './ILogger';
6
+ export declare class NodeGamepad extends EventEmitter {
7
+ private config;
8
+ private logger?;
9
+ protected _usb?: HID;
10
+ private _stopped;
11
+ private _joystickStates;
12
+ private _buttonStates;
13
+ private _statusStates;
14
+ private _scaleStates;
15
+ private _connectRetryTimeout?;
16
+ private readonly _connectionRetryTimeoutInMs;
17
+ constructor(config: IConfig, logger?: ILogger | undefined);
18
+ start(): void;
19
+ stop(): void;
20
+ rumble(_duration: number): void;
21
+ private logDebug;
22
+ private log;
23
+ private connect;
24
+ private stopConnectionProcess;
25
+ private registerProgramExitEvents;
26
+ private stopUsbRead;
27
+ private toIDeviceSpec;
28
+ private onControllerFrame;
29
+ private processJoysticks;
30
+ private processButtons;
31
+ private processScales;
32
+ private processStatus;
33
+ private getStateName;
34
+ }
package/NodeGamepad.js CHANGED
@@ -1,210 +1,207 @@
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
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.NodeGamepad = void 0;
19
- var node_hid_1 = require("node-hid");
20
- var events_1 = require("events");
21
- var mathjs_1 = require("mathjs");
22
- var NodeGamepad = /** @class */ (function (_super) {
23
- __extends(NodeGamepad, _super);
24
- function NodeGamepad(config, logger) {
25
- var _this = _super.call(this) || this;
26
- _this.config = config;
27
- _this.logger = logger;
28
- _this._stopped = false;
29
- _this._joystickStates = {};
30
- _this._buttonStates = {};
31
- _this._statusStates = {};
32
- _this._scaleStates = {};
33
- _this._connectionRetryTimeoutInMs = 100;
34
- return _this;
35
- }
36
- NodeGamepad.prototype.start = function () {
37
- var _this = this;
38
- this.log("Starting connection procedure to device:" + JSON.stringify(this.toIDeviceSpec(this.config)));
39
- this.registerStopProgramStopEvent();
40
- this.connect();
41
- // on process exit, disconnect from any devices we may be connected to. and also stop any connection procedure
42
- process.on('exit', function () { return _this.stop(); });
43
- };
44
- NodeGamepad.prototype.stop = function () {
45
- if (!this._stopped) {
46
- this.stopConnectionProcess();
47
- this.disconnect();
48
- this._stopped = true;
49
- }
50
- };
51
- NodeGamepad.prototype.rumble = function (_duration) {
52
- // do nothing here intentionally. gamepads supporting rumbling do need to override this method
53
- };
54
- NodeGamepad.prototype.logDebug = function (toLog) {
55
- var _a;
56
- if ((_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug) {
57
- this.logger.debug("NodeGamepad (Debug):" + toLog);
58
- }
59
- };
60
- NodeGamepad.prototype.log = function (toLog) {
61
- if (this.logger) {
62
- this.logger.info("NodeGamepad:" + toLog);
63
- }
64
- };
65
- NodeGamepad.prototype.connect = function () {
66
- var _this = this;
67
- if (this._stopped) {
68
- return;
69
- }
70
- var deviceList = (0, node_hid_1.devices)(this.config.vendorID, this.config.productID);
71
- if (this.config.serialNumber) {
72
- deviceList = deviceList.filter(function (d) { return d.serialNumber === _this.config.serialNumber; });
73
- }
74
- if (deviceList.length < 1) {
75
- this.logDebug('Device not found, trying again later');
76
- this._connectRetryTimeout = setTimeout(function () { return _this.connect(); }, this._connectionRetryTimeoutInMs);
77
- return;
78
- }
79
- var deviceToConnectTo = deviceList[0];
80
- if ((deviceToConnectTo === null || deviceToConnectTo === void 0 ? void 0 : deviceToConnectTo.path) === undefined) {
81
- this.logDebug('Failed to connect. Checking again later.');
82
- this._connectRetryTimeout = setTimeout(function () { return _this.connect(); }, this._connectionRetryTimeoutInMs);
83
- return;
84
- }
85
- this.logDebug("connecting to:" + JSON.stringify(deviceToConnectTo));
86
- try {
87
- this._usb = new node_hid_1.HID(deviceToConnectTo.path);
88
- this.log('connected');
89
- this.emit('connected');
90
- this._connectRetryTimeout = undefined;
91
- this._usb.on('data', function (data) { return _this.onControllerFrame(data); });
92
- this._usb.on('error', function (error) {
93
- _this.log("Error occurred:" + JSON.stringify(error));
94
- setTimeout(function () {
95
- _this.log('reconnecting');
96
- _this.disconnect();
97
- _this.connect();
98
- }, 0);
99
- });
100
- }
101
- catch (error) {
102
- var typedError = error;
103
- this.log("Connecting failed: " + typedError.message);
104
- this.log('trying again later.');
105
- this._connectRetryTimeout = setTimeout(function () { return _this.connect(); }, this._connectionRetryTimeoutInMs);
106
- }
107
- };
108
- NodeGamepad.prototype.stopConnectionProcess = function () {
109
- if (this._connectRetryTimeout) {
110
- clearTimeout(this._connectRetryTimeout);
111
- this._connectRetryTimeout = undefined;
112
- }
113
- };
114
- NodeGamepad.prototype.registerStopProgramStopEvent = function () {
115
- var _this = this;
116
- process.on('SIGINT', function () { return _this.stop(); });
117
- process.on('exit', function () { return _this.stop(); });
118
- process.on('uncaughtException', function () { return _this.stop(); });
119
- };
120
- NodeGamepad.prototype.disconnect = function () {
121
- if (this._usb) {
122
- this.emit('disconnected');
123
- this._usb.close();
124
- this._usb = undefined;
125
- }
126
- };
127
- NodeGamepad.prototype.toIDeviceSpec = function (spec) {
128
- return {
129
- vendorID: spec.vendorID,
130
- productID: spec.productID,
131
- serialNumber: spec.serialNumber,
132
- };
133
- };
134
- NodeGamepad.prototype.onControllerFrame = function (data) {
135
- this.logDebug(JSON.stringify(data));
136
- this.processJoysticks(data);
137
- this.processButtons(data);
138
- this.processStatus(data);
139
- this.processScales(data);
140
- };
141
- NodeGamepad.prototype.processJoysticks = function (data) {
142
- var _this = this;
143
- var _a;
144
- (_a = this.config.joysticks) === null || _a === void 0 ? void 0 : _a.forEach(function (joystick) {
145
- var oldState = _this._joystickStates[joystick.name];
146
- var newState = {
147
- x: data[joystick.x.pin],
148
- y: data[joystick.y.pin],
149
- };
150
- if (oldState === undefined || oldState.x !== newState.x || oldState.y !== newState.y) {
151
- _this.emit(joystick.name + ':move', newState);
152
- }
153
- _this._joystickStates[joystick.name] = newState;
154
- });
155
- };
156
- NodeGamepad.prototype.processButtons = function (data) {
157
- var _this = this;
158
- var _a;
159
- (_a = this.config.buttons) === null || _a === void 0 ? void 0 : _a.forEach(function (button) {
160
- var oldState = _this._buttonStates[button.name];
161
- var newState = (0, mathjs_1.evaluate)(button.value, { value: data[button.pin] });
162
- if (oldState == undefined) {
163
- if (newState) {
164
- _this.emit(button.name + ':press');
165
- }
166
- }
167
- else if (oldState !== newState) {
168
- var emitEvent = newState ? button.name + ":press" : button.name + ":release";
169
- _this.emit(emitEvent);
170
- }
171
- _this._buttonStates[button.name] = newState;
172
- });
173
- };
174
- NodeGamepad.prototype.processScales = function (data) {
175
- var _this = this;
176
- var _a;
177
- (_a = this.config.scale) === null || _a === void 0 ? void 0 : _a.forEach(function (scale) {
178
- var oldState = _this._scaleStates[scale.name];
179
- var newState = data[scale.pin];
180
- if (oldState !== newState) {
181
- _this.emit(scale.name + ':change', newState);
182
- }
183
- _this._scaleStates[scale.name] = newState;
184
- });
185
- };
186
- NodeGamepad.prototype.processStatus = function (data) {
187
- var _this = this;
188
- var _a;
189
- (_a = this.config.status) === null || _a === void 0 ? void 0 : _a.forEach(function (status) {
190
- var oldState = _this._statusStates[status.name];
191
- var newState = data[status.pin];
192
- if (oldState !== newState) {
193
- _this.emit(status.name + ':change', _this.getStateName(status.states, newState));
194
- }
195
- _this._statusStates[status.name] = newState;
196
- });
197
- };
198
- NodeGamepad.prototype.getStateName = function (states, value) {
199
- for (var _i = 0, states_1 = states; _i < states_1.length; _i++) {
200
- var state = states_1[_i];
201
- if (state.value === value) {
202
- return state.state;
203
- }
204
- }
205
- return "unknown state:" + value;
206
- };
207
- return NodeGamepad;
208
- }(events_1.EventEmitter));
209
- exports.NodeGamepad = NodeGamepad;
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
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.NodeGamepad = void 0;
19
+ var node_hid_1 = require("node-hid");
20
+ var events_1 = require("events");
21
+ var mathjs_1 = require("mathjs");
22
+ var NodeGamepad = /** @class */ (function (_super) {
23
+ __extends(NodeGamepad, _super);
24
+ function NodeGamepad(config, logger) {
25
+ var _this = _super.call(this) || this;
26
+ _this.config = config;
27
+ _this.logger = logger;
28
+ _this._usb = undefined;
29
+ _this._stopped = false;
30
+ _this._joystickStates = {};
31
+ _this._buttonStates = {};
32
+ _this._statusStates = {};
33
+ _this._scaleStates = {};
34
+ _this._connectionRetryTimeoutInMs = 100;
35
+ return _this;
36
+ }
37
+ NodeGamepad.prototype.start = function () {
38
+ this.log("Starting connection procedure to device:".concat(JSON.stringify(this.toIDeviceSpec(this.config))));
39
+ this.registerProgramExitEvents();
40
+ this.connect();
41
+ };
42
+ NodeGamepad.prototype.stop = function () {
43
+ this._stopped = true;
44
+ this.stopConnectionProcess();
45
+ this.stopUsbRead();
46
+ };
47
+ NodeGamepad.prototype.rumble = function (_duration) {
48
+ // do nothing here intentionally. gamepads supporting rumbling do need to override this method
49
+ };
50
+ NodeGamepad.prototype.logDebug = function (toLog) {
51
+ var _a;
52
+ if ((_a = this.logger) === null || _a === void 0 ? void 0 : _a.debug) {
53
+ this.logger.debug("NodeGamepad (Debug):".concat(toLog));
54
+ }
55
+ };
56
+ NodeGamepad.prototype.log = function (toLog) {
57
+ if (this.logger) {
58
+ this.logger.info("NodeGamepad:".concat(toLog));
59
+ }
60
+ };
61
+ NodeGamepad.prototype.connect = function () {
62
+ var _this = this;
63
+ if (this._stopped) {
64
+ return;
65
+ }
66
+ var deviceList = (0, node_hid_1.devices)(this.config.vendorID, this.config.productID);
67
+ if (this.config.serialNumber) {
68
+ deviceList = deviceList.filter(function (d) { return d.serialNumber === _this.config.serialNumber; });
69
+ }
70
+ if (deviceList.length < 1) {
71
+ this.logDebug('Device not found, trying again later');
72
+ this._connectRetryTimeout = setTimeout(function () { return _this.connect(); }, this._connectionRetryTimeoutInMs);
73
+ return;
74
+ }
75
+ var deviceToConnectTo = deviceList[0];
76
+ if ((deviceToConnectTo === null || deviceToConnectTo === void 0 ? void 0 : deviceToConnectTo.path) === undefined) {
77
+ this.logDebug('Failed to connect. Checking again later.');
78
+ this._connectRetryTimeout = setTimeout(function () { return _this.connect(); }, this._connectionRetryTimeoutInMs);
79
+ return;
80
+ }
81
+ this.logDebug("connecting to:".concat(JSON.stringify(deviceToConnectTo)));
82
+ try {
83
+ this._usb = new node_hid_1.HID(deviceToConnectTo.path);
84
+ this.log('connected');
85
+ this.emit('connected');
86
+ this._connectRetryTimeout = undefined;
87
+ this._usb.on('data', function (data) { return _this.onControllerFrame(data); });
88
+ this._usb.on('error', function (error) {
89
+ _this.log("Error occurred:".concat(JSON.stringify(error)));
90
+ setTimeout(function () {
91
+ _this.log('reconnecting');
92
+ _this.emit('disconnected');
93
+ _this.stopUsbRead();
94
+ _this.connect();
95
+ }, 0);
96
+ });
97
+ }
98
+ catch (error) {
99
+ var typedError = error;
100
+ this.log("Connecting failed: ".concat(typedError.message));
101
+ this.log('trying again later.');
102
+ this._connectRetryTimeout = setTimeout(function () { return _this.connect(); }, this._connectionRetryTimeoutInMs);
103
+ }
104
+ };
105
+ NodeGamepad.prototype.stopConnectionProcess = function () {
106
+ if (this._connectRetryTimeout) {
107
+ clearTimeout(this._connectRetryTimeout);
108
+ this._connectRetryTimeout = undefined;
109
+ }
110
+ };
111
+ NodeGamepad.prototype.registerProgramExitEvents = function () {
112
+ var _this = this;
113
+ process.on('exit', function () {
114
+ _this.logDebug('exit encontered, cleaning up');
115
+ _this.stop();
116
+ });
117
+ };
118
+ NodeGamepad.prototype.stopUsbRead = function () {
119
+ if (this._usb) {
120
+ this._usb.close();
121
+ this._usb = undefined;
122
+ }
123
+ };
124
+ NodeGamepad.prototype.toIDeviceSpec = function (spec) {
125
+ return {
126
+ vendorID: spec.vendorID,
127
+ productID: spec.productID,
128
+ serialNumber: spec.serialNumber,
129
+ };
130
+ };
131
+ NodeGamepad.prototype.onControllerFrame = function (data) {
132
+ this.logDebug(JSON.stringify(data));
133
+ this.processJoysticks(data);
134
+ this.processButtons(data);
135
+ this.processStatus(data);
136
+ this.processScales(data);
137
+ };
138
+ NodeGamepad.prototype.processJoysticks = function (data) {
139
+ var _this = this;
140
+ var _a;
141
+ (_a = this.config.joysticks) === null || _a === void 0 ? void 0 : _a.forEach(function (joystick) {
142
+ var oldState = _this._joystickStates[joystick.name];
143
+ var newState = {
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);
149
+ }
150
+ _this._joystickStates[joystick.name] = newState;
151
+ });
152
+ };
153
+ NodeGamepad.prototype.processButtons = function (data) {
154
+ var _this = this;
155
+ var _a;
156
+ (_a = this.config.buttons) === null || _a === void 0 ? void 0 : _a.forEach(function (button) {
157
+ var oldState = _this._buttonStates[button.name];
158
+ var newState = (0, mathjs_1.evaluate)(button.value, { value: data[button.pin] });
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
+ }
168
+ _this._buttonStates[button.name] = newState;
169
+ });
170
+ };
171
+ NodeGamepad.prototype.processScales = function (data) {
172
+ var _this = this;
173
+ var _a;
174
+ (_a = this.config.scale) === null || _a === void 0 ? void 0 : _a.forEach(function (scale) {
175
+ var oldState = _this._scaleStates[scale.name];
176
+ var newState = data[scale.pin];
177
+ if (oldState !== newState) {
178
+ _this.emit(scale.name + ':change', newState);
179
+ }
180
+ _this._scaleStates[scale.name] = newState;
181
+ });
182
+ };
183
+ NodeGamepad.prototype.processStatus = function (data) {
184
+ var _this = this;
185
+ var _a;
186
+ (_a = this.config.status) === null || _a === void 0 ? void 0 : _a.forEach(function (status) {
187
+ var oldState = _this._statusStates[status.name];
188
+ var newState = data[status.pin];
189
+ if (oldState !== newState) {
190
+ _this.emit(status.name + ':change', _this.getStateName(status.states, newState));
191
+ }
192
+ _this._statusStates[status.name] = newState;
193
+ });
194
+ };
195
+ NodeGamepad.prototype.getStateName = function (states, value) {
196
+ for (var _i = 0, states_1 = states; _i < states_1.length; _i++) {
197
+ var state = states_1[_i];
198
+ if (state.value === value) {
199
+ return state.state;
200
+ }
201
+ }
202
+ return "unknown state:".concat(value);
203
+ };
204
+ return NodeGamepad;
205
+ }(events_1.EventEmitter));
206
+ exports.NodeGamepad = NodeGamepad;
210
207
  //# sourceMappingURL=NodeGamepad.js.map