node-web-gpio 1.1.63 → 1.1.65
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/dist/index.d.ts +2 -2
- package/dist/index.js +56 -23
- package/package.json +6 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { EventEmitter } from
|
|
1
|
+
import { EventEmitter } from "node:events";
|
|
2
2
|
/** ポート番号 */
|
|
3
3
|
type PortNumber = number;
|
|
4
4
|
/** ポート名 */
|
|
@@ -6,7 +6,7 @@ type PortName = string;
|
|
|
6
6
|
/** ピン名 */
|
|
7
7
|
type PinName = string;
|
|
8
8
|
/** 入出力方向 */
|
|
9
|
-
type DirectionMode =
|
|
9
|
+
type DirectionMode = "in" | "out";
|
|
10
10
|
/** GPIO 値 0: LOW / 1: HIGH */
|
|
11
11
|
type GPIOValue = 0 | 1;
|
|
12
12
|
/**
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
36
|
exports.OperationError = exports.InvalidAccessError = exports.GPIOPort = exports.GPIOPortMap = exports.GPIOAccess = void 0;
|
|
4
37
|
exports.requestGPIOAccess = requestGPIOAccess;
|
|
5
38
|
const node_events_1 = require("node:events");
|
|
6
39
|
const node_fs_1 = require("node:fs");
|
|
7
|
-
const os = require("node:os");
|
|
8
|
-
const path = require("node:path");
|
|
40
|
+
const os = __importStar(require("node:os"));
|
|
41
|
+
const path = __importStar(require("node:path"));
|
|
9
42
|
/**
|
|
10
43
|
* Interval of file system polling, in milliseconds.
|
|
11
44
|
*/
|
|
@@ -13,7 +46,7 @@ const PollingInterval = 100;
|
|
|
13
46
|
/**
|
|
14
47
|
* GPIO パス
|
|
15
48
|
*/
|
|
16
|
-
const SysfsGPIOPath =
|
|
49
|
+
const SysfsGPIOPath = "/sys/class/gpio";
|
|
17
50
|
/**
|
|
18
51
|
* GPIO ポートマップサイズ
|
|
19
52
|
*/
|
|
@@ -39,8 +72,8 @@ function parseUint16(parseString) {
|
|
|
39
72
|
* GPIO0 オフセット
|
|
40
73
|
* @see {@link https://github.com/raspberrypi/linux/issues/6037}
|
|
41
74
|
*/
|
|
42
|
-
const GpioOffset = process.platform ===
|
|
43
|
-
os.release().localeCompare(
|
|
75
|
+
const GpioOffset = process.platform === "linux" &&
|
|
76
|
+
os.release().localeCompare("6.6", undefined, { numeric: true }) >= 0
|
|
44
77
|
? 512
|
|
45
78
|
: 0;
|
|
46
79
|
/**
|
|
@@ -59,10 +92,10 @@ class GPIOAccess extends node_events_1.EventEmitter {
|
|
|
59
92
|
super();
|
|
60
93
|
this._ports = ports == null ? new GPIOPortMap() : ports;
|
|
61
94
|
// biome-ignore lint/suspicious/useIterableCallbackReturn: port.on()の戻り値は使用しないため
|
|
62
|
-
this._ports.forEach((port) => port.on(
|
|
63
|
-
this.emit(
|
|
95
|
+
this._ports.forEach((port) => port.on("change", (event) => {
|
|
96
|
+
this.emit("change", event);
|
|
64
97
|
}));
|
|
65
|
-
this.on(
|
|
98
|
+
this.on("change", (event) => {
|
|
66
99
|
if (this.onchange !== undefined)
|
|
67
100
|
this.onchange(event);
|
|
68
101
|
});
|
|
@@ -80,7 +113,7 @@ class GPIOAccess extends node_events_1.EventEmitter {
|
|
|
80
113
|
* @return ポート開放結果
|
|
81
114
|
*/
|
|
82
115
|
async unexportAll() {
|
|
83
|
-
await Promise.all([...this.ports.values()].map((port) => port.exported ? port.unexport() : undefined));
|
|
116
|
+
await Promise.all([...this.ports.values()].map(async (port) => port.exported ? await port.unexport() : undefined));
|
|
84
117
|
}
|
|
85
118
|
}
|
|
86
119
|
exports.GPIOAccess = GPIOAccess;
|
|
@@ -118,10 +151,10 @@ class GPIOPort extends node_events_1.EventEmitter {
|
|
|
118
151
|
super();
|
|
119
152
|
this._portNumber = parseUint16(portNumber.toString()) + GpioOffset;
|
|
120
153
|
this._pollingInterval = PollingInterval;
|
|
121
|
-
this._direction = new OperationError(
|
|
122
|
-
this._exported = new OperationError(
|
|
154
|
+
this._direction = new OperationError("Unknown direction.");
|
|
155
|
+
this._exported = new OperationError("Unknown export.");
|
|
123
156
|
this._exportRetry = 0;
|
|
124
|
-
this.on(
|
|
157
|
+
this.on("change", (event) => {
|
|
125
158
|
if (this.onchange !== undefined)
|
|
126
159
|
this.onchange(event);
|
|
127
160
|
});
|
|
@@ -146,7 +179,7 @@ class GPIOPort extends node_events_1.EventEmitter {
|
|
|
146
179
|
*/
|
|
147
180
|
get pinName() {
|
|
148
181
|
// NOTE: Unknown pinName.
|
|
149
|
-
return
|
|
182
|
+
return "";
|
|
150
183
|
}
|
|
151
184
|
/**
|
|
152
185
|
* GPIO 入出力方向 getter
|
|
@@ -185,10 +218,10 @@ class GPIOPort extends node_events_1.EventEmitter {
|
|
|
185
218
|
try {
|
|
186
219
|
clearInterval(this._timeout);
|
|
187
220
|
if (!this.exported) {
|
|
188
|
-
await node_fs_1.promises.writeFile(path.join(SysfsGPIOPath,
|
|
221
|
+
await node_fs_1.promises.writeFile(path.join(SysfsGPIOPath, "export"), String(this.portNumber));
|
|
189
222
|
}
|
|
190
|
-
await node_fs_1.promises.writeFile(path.join(SysfsGPIOPath, this.portName,
|
|
191
|
-
if (direction ===
|
|
223
|
+
await node_fs_1.promises.writeFile(path.join(SysfsGPIOPath, this.portName, "direction"), direction);
|
|
224
|
+
if (direction === "in") {
|
|
192
225
|
this._timeout = setInterval(
|
|
193
226
|
// eslint-disable-next-line
|
|
194
227
|
this.read.bind(this), this._pollingInterval);
|
|
@@ -198,7 +231,7 @@ class GPIOPort extends node_events_1.EventEmitter {
|
|
|
198
231
|
catch (error) {
|
|
199
232
|
if (this._exportRetry < 10) {
|
|
200
233
|
await sleep(100);
|
|
201
|
-
console.warn(
|
|
234
|
+
console.warn("May be the first time port access. Retry..");
|
|
202
235
|
++this._exportRetry;
|
|
203
236
|
await this.export(direction);
|
|
204
237
|
}
|
|
@@ -217,7 +250,7 @@ class GPIOPort extends node_events_1.EventEmitter {
|
|
|
217
250
|
async unexport() {
|
|
218
251
|
clearInterval(this._timeout);
|
|
219
252
|
try {
|
|
220
|
-
await node_fs_1.promises.writeFile(path.join(SysfsGPIOPath,
|
|
253
|
+
await node_fs_1.promises.writeFile(path.join(SysfsGPIOPath, "unexport"), String(this.portNumber));
|
|
221
254
|
// biome-ignore lint/suspicious/noExplicitAny: エラーの型が不明なため、any型を使用してOperationErrorに変換する
|
|
222
255
|
}
|
|
223
256
|
catch (error) {
|
|
@@ -230,15 +263,15 @@ class GPIOPort extends node_events_1.EventEmitter {
|
|
|
230
263
|
* @return 読み取り処理の完了
|
|
231
264
|
*/
|
|
232
265
|
async read() {
|
|
233
|
-
if (!(this.exported && this.direction ===
|
|
266
|
+
if (!(this.exported && this.direction === "in")) {
|
|
234
267
|
throw new InvalidAccessError(`The exported must be true and value of direction must be "in".`);
|
|
235
268
|
}
|
|
236
269
|
try {
|
|
237
|
-
const buffer = await node_fs_1.promises.readFile(path.join(SysfsGPIOPath, this.portName,
|
|
270
|
+
const buffer = await node_fs_1.promises.readFile(path.join(SysfsGPIOPath, this.portName, "value"));
|
|
238
271
|
const value = parseUint16(buffer.toString());
|
|
239
272
|
if (this._value !== value) {
|
|
240
273
|
this._value = value;
|
|
241
|
-
this.emit(
|
|
274
|
+
this.emit("change", { value, port: this });
|
|
242
275
|
}
|
|
243
276
|
return value;
|
|
244
277
|
// biome-ignore lint/suspicious/noExplicitAny: エラーの型が不明なため、any型を使用してOperationErrorに変換する
|
|
@@ -252,11 +285,11 @@ class GPIOPort extends node_events_1.EventEmitter {
|
|
|
252
285
|
* @return 読み取り処理の完了
|
|
253
286
|
*/
|
|
254
287
|
async write(value) {
|
|
255
|
-
if (!(this.exported && this.direction ===
|
|
288
|
+
if (!(this.exported && this.direction === "out")) {
|
|
256
289
|
throw new InvalidAccessError(`The exported must be true and value of direction must be "out".`);
|
|
257
290
|
}
|
|
258
291
|
try {
|
|
259
|
-
await node_fs_1.promises.writeFile(path.join(SysfsGPIOPath, this.portName,
|
|
292
|
+
await node_fs_1.promises.writeFile(path.join(SysfsGPIOPath, this.portName, "value"), parseUint16(value.toString()).toString());
|
|
260
293
|
// biome-ignore lint/suspicious/noExplicitAny: エラーの型が不明なため、any型を使用してOperationErrorに変換する
|
|
261
294
|
}
|
|
262
295
|
catch (error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-web-gpio",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.65",
|
|
4
4
|
"description": "GPIO access with Node.js",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,18 +14,20 @@
|
|
|
14
14
|
"author": "Kohei Watanabe <kou029w@gmail.com>",
|
|
15
15
|
"license": "MIT",
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@
|
|
17
|
+
"@tsconfig/strictest": "2.0.8",
|
|
18
18
|
"@types/node": "22.19.13",
|
|
19
19
|
"husky": "9.1.7",
|
|
20
20
|
"lint-staged": "16.2.7",
|
|
21
|
-
"
|
|
21
|
+
"oxfmt": "0.53.0",
|
|
22
|
+
"oxlint": "1.68.0",
|
|
23
|
+
"oxlint-tsgolint": "0.23.0",
|
|
22
24
|
"typedoc": "0.28.15",
|
|
23
25
|
"typedoc-rhineai-theme": "1.2.0",
|
|
24
26
|
"typescript": "5.9.3"
|
|
25
27
|
},
|
|
26
28
|
"scripts": {
|
|
27
29
|
"build": "tsc",
|
|
28
|
-
"lint": "
|
|
30
|
+
"lint": "oxlint",
|
|
29
31
|
"prepare": "husky install && rm -rf dist && npm run build",
|
|
30
32
|
"precommit": "lint-staged",
|
|
31
33
|
"preversion": "npm run docs && git add docs",
|