@sensslen/node-gamepad 1.0.9-beta.213 → 1.0.9-beta.217

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 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
- protected _usb?: HID;
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
- 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._running = false;
30
- _this._joystickStates = {};
31
- _this._buttonStates = {};
32
- _this._statusStates = {};
33
- _this._scaleStates = {};
34
- _this._connectionRetryTimeoutInMs = 500;
35
- return _this;
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("Starting connection procedure to device:".concat(JSON.stringify(this.toIDeviceSpec(this.config))));
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
- NodeGamepad.prototype.stop = function () {
28
+ }
29
+ stop() {
47
30
  this._running = false;
48
31
  this.stopConnectionProcess();
49
32
  this.closeUsbDevice();
50
- };
51
- NodeGamepad.prototype.rumble = function (_intensity, _duration) {
33
+ }
34
+ rumble(_intensity, _duration) {
52
35
  // todo
53
- };
54
- NodeGamepad.prototype.logDebug = function (toLog) {
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("NodeGamepad (Debug):".concat(toLog));
40
+ this.logger.debug(`NodeGamepad (Debug):${toLog}`);
58
41
  }
59
- };
60
- NodeGamepad.prototype.log = function (toLog) {
42
+ }
43
+ log(toLog) {
61
44
  if (this.logger) {
62
- this.logger.info("NodeGamepad:".concat(toLog));
45
+ this.logger.info(`NodeGamepad:${toLog}`);
63
46
  }
64
- };
65
- NodeGamepad.prototype.connect = function () {
66
- var _this = this;
47
+ }
48
+ connect() {
67
49
  if (!this._running) {
68
50
  return;
69
51
  }
70
- var deviceList = (0, node_hid_1.devices)(this.config.vendorID, this.config.productID);
52
+ let deviceList = (0, node_hid_1.devices)(this.config.vendorID, this.config.productID);
71
53
  if (this.config.serialNumber) {
72
- deviceList = deviceList.filter(function (d) { return d.serialNumber === _this.config.serialNumber; });
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(function () { return _this.connect(); }, this._connectionRetryTimeoutInMs);
58
+ this._connectRetryTimeout = setTimeout(() => this.connect(), this._connectionRetryTimeoutInMs);
77
59
  return;
78
60
  }
79
- var deviceToConnectTo = deviceList[0];
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(function () { return _this.connect(); }, this._connectionRetryTimeoutInMs);
64
+ this._connectRetryTimeout = setTimeout(() => this.connect(), this._connectionRetryTimeoutInMs);
83
65
  return;
84
66
  }
85
- this.logDebug("connecting to:".concat(JSON.stringify(deviceToConnectTo)));
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', function (data) { return _this.onControllerFrame(data); });
92
- this._usb.on('error', function (error) {
93
- _this.log("Error occurred:".concat(JSON.stringify(error)));
94
- _this.emit('disconnected');
95
- _this.closeUsbDevice();
96
- setTimeout(function () {
97
- _this.log('reconnecting');
98
- _this.connect();
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
- var typedError = error;
104
- this.log("Connecting failed: ".concat(typedError.message));
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(function () { return _this.connect(); }, this._connectionRetryTimeoutInMs);
89
+ this._connectRetryTimeout = setTimeout(() => this.connect(), this._connectionRetryTimeoutInMs);
108
90
  }
109
- };
110
- NodeGamepad.prototype.stopConnectionProcess = function () {
91
+ }
92
+ stopConnectionProcess() {
111
93
  if (this._connectRetryTimeout) {
112
94
  clearTimeout(this._connectRetryTimeout);
113
95
  this._connectRetryTimeout = undefined;
114
96
  }
115
- };
116
- NodeGamepad.prototype.registerProgramExitEvents = function () {
117
- var _this = this;
118
- process.on('exit', function () {
119
- _this.logDebug('exit encontered, cleaning up');
120
- _this.stop();
97
+ }
98
+ registerProgramExitEvents() {
99
+ process.on('exit', () => {
100
+ this.logDebug('exit encontered, cleaning up');
101
+ this.stop();
121
102
  });
122
- };
123
- NodeGamepad.prototype.closeUsbDevice = function () {
103
+ }
104
+ closeUsbDevice() {
124
105
  if (this._usb) {
125
106
  this._usb.close();
126
107
  this._usb = undefined;
127
108
  }
128
- };
129
- NodeGamepad.prototype.toIDeviceSpec = function (spec) {
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
- NodeGamepad.prototype.onControllerFrame = function (data) {
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
- NodeGamepad.prototype.processJoysticks = function (data) {
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(function (joystick) {
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
- _this.processJoystick(joystick, data);
128
+ this.processJoystick(joystick, data);
149
129
  }
150
130
  });
151
- };
152
- NodeGamepad.prototype.processJoystick = function (joystick, data) {
153
- var oldState = this._joystickStates[joystick.name];
154
- var newState = {
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
- NodeGamepad.prototype.processButtons = function (data) {
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(function (button) {
145
+ (_a = this.config.buttons) === null || _a === void 0 ? void 0 : _a.forEach((button) => {
167
146
  if (data.length > button.pin) {
168
- _this.processButton(data, button);
147
+ this.processButton(data, button);
169
148
  }
170
149
  });
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] });
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
- var emitEvent = newState ? "".concat(config.name, ":press") : "".concat(config.name, ":release");
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
- NodeGamepad.prototype.processScales = function (data) {
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(function (scale) {
167
+ (_a = this.config.scales) === null || _a === void 0 ? void 0 : _a.forEach((scale) => {
190
168
  if (data.length > scale.pin) {
191
- _this.processScale(scale, data);
169
+ this.processScale(scale, data);
192
170
  }
193
171
  });
194
- };
195
- NodeGamepad.prototype.processScale = function (config, data) {
196
- var oldState = this._scaleStates[config.name];
197
- var newState = data[config.pin];
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
- NodeGamepad.prototype.processStates = function (data) {
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(function (status) {
183
+ (_a = this.config.status) === null || _a === void 0 ? void 0 : _a.forEach((status) => {
207
184
  if (data.length > status.pin) {
208
- _this.processState(status, data);
185
+ this.processState(status, data);
209
186
  }
210
187
  });
211
- };
212
- NodeGamepad.prototype.processState = function (config, data) {
213
- var oldState = this._statusStates[config.name];
214
- var newState = data[config.pin];
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
- NodeGamepad.prototype.getStateName = function (states, value) {
221
- for (var _i = 0, states_1 = states; _i < states_1.length; _i++) {
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 "unknown state:".concat(value);
228
- };
229
- return NodeGamepad;
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
- var NodeGamepad_1 = require("./NodeGamepad");
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.213",
3
+ "version": "1.0.9-beta.217",
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": {