node-web-gpio 1.0.12 → 1.0.14
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 +96 -0
- package/dist/index.js +102 -0
- package/package.json +6 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,53 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
|
+
/** ポート番号 */
|
|
3
4
|
declare type PortNumber = number;
|
|
5
|
+
/** ポート名 */
|
|
4
6
|
declare type PortName = string;
|
|
7
|
+
/** ピン名 */
|
|
5
8
|
declare type PinName = string;
|
|
9
|
+
/** 入出力方向 */
|
|
6
10
|
declare type DirectionMode = 'in' | 'out';
|
|
11
|
+
/** GPIO 値 0: LOW / 1: HIGH */
|
|
7
12
|
declare type GPIOValue = 0 | 1;
|
|
13
|
+
/**
|
|
14
|
+
* GPIO チェンジイベント
|
|
15
|
+
*/
|
|
8
16
|
interface GPIOChangeEvent {
|
|
17
|
+
/** 入出力値 */
|
|
9
18
|
readonly value: GPIOValue;
|
|
19
|
+
/** ポート */
|
|
10
20
|
readonly port: GPIOPort;
|
|
11
21
|
}
|
|
22
|
+
/**
|
|
23
|
+
* GPIO チェンジイベントハンドラ
|
|
24
|
+
*/
|
|
12
25
|
interface GPIOChangeEventHandler {
|
|
26
|
+
/** イベント */
|
|
13
27
|
(event: GPIOChangeEvent): void;
|
|
14
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* GPIO
|
|
31
|
+
*/
|
|
15
32
|
export declare class GPIOAccess extends EventEmitter {
|
|
33
|
+
/** ポート */
|
|
16
34
|
private readonly _ports;
|
|
35
|
+
/** GPIO チェンジイベントハンドラ */
|
|
17
36
|
onchange: GPIOChangeEventHandler | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Creates an instance of GPIOAccess.
|
|
39
|
+
* @param ports ポート番号
|
|
40
|
+
*/
|
|
18
41
|
constructor(ports?: GPIOPortMap);
|
|
42
|
+
/**
|
|
43
|
+
* ポート情報取得処理
|
|
44
|
+
* @return 現在のポート情報
|
|
45
|
+
*/
|
|
19
46
|
get ports(): GPIOPortMap;
|
|
20
47
|
/**
|
|
21
48
|
* Unexport all exported GPIO ports.
|
|
49
|
+
* 全てのポート開放をする
|
|
50
|
+
* @return ポート開放結果
|
|
22
51
|
*/
|
|
23
52
|
unexportAll(): Promise<void>;
|
|
24
53
|
}
|
|
@@ -27,30 +56,97 @@ export declare class GPIOAccess extends EventEmitter {
|
|
|
27
56
|
*/
|
|
28
57
|
export declare class GPIOPortMap extends Map<PortNumber, GPIOPort> {
|
|
29
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* GPIO ポート
|
|
61
|
+
*/
|
|
30
62
|
export declare class GPIOPort extends EventEmitter {
|
|
63
|
+
/** ポート番号 */
|
|
31
64
|
private readonly _portNumber;
|
|
65
|
+
/** ポーリング間隔 */
|
|
32
66
|
private readonly _pollingInterval;
|
|
67
|
+
/** 入出力方向 */
|
|
33
68
|
private _direction;
|
|
69
|
+
/** エクスポート */
|
|
34
70
|
private _exported;
|
|
71
|
+
/** エクスポートリトライ回数 */
|
|
35
72
|
private _exportRetry;
|
|
73
|
+
/** 入出力値 */
|
|
36
74
|
private _value;
|
|
75
|
+
/** タイムアウト値 */
|
|
37
76
|
private _timeout;
|
|
77
|
+
/** GPIO チェンジイベントハンドラ */
|
|
38
78
|
onchange: GPIOChangeEventHandler | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* Creates an instance of GPIOPort.
|
|
81
|
+
* @param portNumber ポート番号
|
|
82
|
+
*/
|
|
39
83
|
constructor(portNumber: PortNumber);
|
|
84
|
+
/**
|
|
85
|
+
* ポート番号取得処理
|
|
86
|
+
* @return 現在のポート番号
|
|
87
|
+
*/
|
|
40
88
|
get portNumber(): PortNumber;
|
|
89
|
+
/**
|
|
90
|
+
* ポート名取得処理
|
|
91
|
+
* @return 現在のポート名
|
|
92
|
+
*/
|
|
41
93
|
get portName(): PortName;
|
|
94
|
+
/**
|
|
95
|
+
* ピン名取得処理
|
|
96
|
+
* @return 現在のピン名
|
|
97
|
+
*/
|
|
42
98
|
get pinName(): PinName;
|
|
99
|
+
/**
|
|
100
|
+
* GPIO 入出力方向 getter
|
|
101
|
+
* @return 現在のGPIO 入出力方向
|
|
102
|
+
*/
|
|
43
103
|
get direction(): DirectionMode;
|
|
104
|
+
/**
|
|
105
|
+
* GPIO export の有無 getter
|
|
106
|
+
* @return 現在のGPIO 出力
|
|
107
|
+
*/
|
|
44
108
|
get exported(): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* GPIO 出力処理
|
|
111
|
+
* @param direction GPIO 入出力方向
|
|
112
|
+
* @return export 処理の完了
|
|
113
|
+
*/
|
|
45
114
|
export(direction: DirectionMode): Promise<void>;
|
|
115
|
+
/**
|
|
116
|
+
* Unexport exported GPIO ports.
|
|
117
|
+
* ポート開放をする
|
|
118
|
+
* @return ポート開放処理の完了
|
|
119
|
+
*/
|
|
46
120
|
unexport(): Promise<void>;
|
|
121
|
+
/**
|
|
122
|
+
* 入力値読み取り処理
|
|
123
|
+
* @return 読み取り処理の完了
|
|
124
|
+
*/
|
|
47
125
|
read(): Promise<GPIOValue>;
|
|
126
|
+
/**
|
|
127
|
+
* 出力値書き込み処理
|
|
128
|
+
* @return 読み取り処理の完了
|
|
129
|
+
*/
|
|
48
130
|
write(value: GPIOValue): Promise<void>;
|
|
49
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* 無効なアクセスエラー
|
|
134
|
+
*/
|
|
50
135
|
export declare class InvalidAccessError extends Error {
|
|
136
|
+
/**
|
|
137
|
+
* Creates an instance of InvalidAccessError.
|
|
138
|
+
* @param message エラーメッセージ
|
|
139
|
+
*/
|
|
51
140
|
constructor(message: string);
|
|
52
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* 操作エラー
|
|
144
|
+
*/
|
|
53
145
|
export declare class OperationError extends Error {
|
|
146
|
+
/**
|
|
147
|
+
* Creates an instance of OperationError.
|
|
148
|
+
* @param message エラーメッセージ
|
|
149
|
+
*/
|
|
54
150
|
constructor(message: string);
|
|
55
151
|
}
|
|
56
152
|
export declare function requestGPIOAccess(): Promise<GPIOAccess>;
|
package/dist/index.js
CHANGED
|
@@ -8,9 +8,24 @@ const path = require("path");
|
|
|
8
8
|
* Interval of file system polling, in milliseconds.
|
|
9
9
|
*/
|
|
10
10
|
const PollingInterval = 100;
|
|
11
|
+
/**
|
|
12
|
+
* GPIO パス
|
|
13
|
+
*/
|
|
11
14
|
const SysfsGPIOPath = '/sys/class/gpio';
|
|
15
|
+
/**
|
|
16
|
+
* GPIO ポートマップサイズ
|
|
17
|
+
*/
|
|
12
18
|
const GPIOPortMapSizeMax = 1024;
|
|
19
|
+
/**
|
|
20
|
+
* Uint16 Max サイズ
|
|
21
|
+
*/
|
|
13
22
|
const Uint16Max = 65535;
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
* Uint16型変換処理
|
|
26
|
+
* @param parseString 変換文字列
|
|
27
|
+
* @return Uint16型変換値
|
|
28
|
+
*/
|
|
14
29
|
function parseUint16(parseString) {
|
|
15
30
|
const n = Number.parseInt(parseString, 10);
|
|
16
31
|
if (0 <= n && n <= Uint16Max)
|
|
@@ -18,9 +33,18 @@ function parseUint16(parseString) {
|
|
|
18
33
|
else
|
|
19
34
|
throw new RangeError(`Must be between 0 and ${Uint16Max}.`);
|
|
20
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* GPIO
|
|
38
|
+
*/
|
|
21
39
|
class GPIOAccess extends events_1.EventEmitter {
|
|
40
|
+
/** ポート */
|
|
22
41
|
_ports;
|
|
42
|
+
/** GPIO チェンジイベントハンドラ */
|
|
23
43
|
onchange;
|
|
44
|
+
/**
|
|
45
|
+
* Creates an instance of GPIOAccess.
|
|
46
|
+
* @param ports ポート番号
|
|
47
|
+
*/
|
|
24
48
|
constructor(ports) {
|
|
25
49
|
super();
|
|
26
50
|
this._ports = ports == null ? new GPIOPortMap() : ports;
|
|
@@ -32,11 +56,17 @@ class GPIOAccess extends events_1.EventEmitter {
|
|
|
32
56
|
this.onchange(event);
|
|
33
57
|
});
|
|
34
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* ポート情報取得処理
|
|
61
|
+
* @return 現在のポート情報
|
|
62
|
+
*/
|
|
35
63
|
get ports() {
|
|
36
64
|
return this._ports;
|
|
37
65
|
}
|
|
38
66
|
/**
|
|
39
67
|
* Unexport all exported GPIO ports.
|
|
68
|
+
* 全てのポート開放をする
|
|
69
|
+
* @return ポート開放結果
|
|
40
70
|
*/
|
|
41
71
|
async unexportAll() {
|
|
42
72
|
await Promise.all([...this.ports.values()].map((port) => port.exported ? port.unexport() : undefined));
|
|
@@ -49,15 +79,30 @@ exports.GPIOAccess = GPIOAccess;
|
|
|
49
79
|
class GPIOPortMap extends Map {
|
|
50
80
|
}
|
|
51
81
|
exports.GPIOPortMap = GPIOPortMap;
|
|
82
|
+
/**
|
|
83
|
+
* GPIO ポート
|
|
84
|
+
*/
|
|
52
85
|
class GPIOPort extends events_1.EventEmitter {
|
|
86
|
+
/** ポート番号 */
|
|
53
87
|
_portNumber;
|
|
88
|
+
/** ポーリング間隔 */
|
|
54
89
|
_pollingInterval;
|
|
90
|
+
/** 入出力方向 */
|
|
55
91
|
_direction;
|
|
92
|
+
/** エクスポート */
|
|
56
93
|
_exported;
|
|
94
|
+
/** エクスポートリトライ回数 */
|
|
57
95
|
_exportRetry;
|
|
96
|
+
/** 入出力値 */
|
|
58
97
|
_value;
|
|
98
|
+
/** タイムアウト値 */
|
|
59
99
|
_timeout;
|
|
100
|
+
/** GPIO チェンジイベントハンドラ */
|
|
60
101
|
onchange;
|
|
102
|
+
/**
|
|
103
|
+
* Creates an instance of GPIOPort.
|
|
104
|
+
* @param portNumber ポート番号
|
|
105
|
+
*/
|
|
61
106
|
constructor(portNumber) {
|
|
62
107
|
super();
|
|
63
108
|
this._portNumber = parseUint16(portNumber.toString());
|
|
@@ -70,26 +115,51 @@ class GPIOPort extends events_1.EventEmitter {
|
|
|
70
115
|
this.onchange(event);
|
|
71
116
|
});
|
|
72
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* ポート番号取得処理
|
|
120
|
+
* @return 現在のポート番号
|
|
121
|
+
*/
|
|
73
122
|
get portNumber() {
|
|
74
123
|
return this._portNumber;
|
|
75
124
|
}
|
|
125
|
+
/**
|
|
126
|
+
* ポート名取得処理
|
|
127
|
+
* @return 現在のポート名
|
|
128
|
+
*/
|
|
76
129
|
get portName() {
|
|
77
130
|
return `gpio${this.portNumber}`;
|
|
78
131
|
}
|
|
132
|
+
/**
|
|
133
|
+
* ピン名取得処理
|
|
134
|
+
* @return 現在のピン名
|
|
135
|
+
*/
|
|
79
136
|
get pinName() {
|
|
80
137
|
// NOTE: Unknown pinName.
|
|
81
138
|
return '';
|
|
82
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* GPIO 入出力方向 getter
|
|
142
|
+
* @return 現在のGPIO 入出力方向
|
|
143
|
+
*/
|
|
83
144
|
get direction() {
|
|
84
145
|
if (this._direction instanceof OperationError)
|
|
85
146
|
throw this._direction;
|
|
86
147
|
return this._direction;
|
|
87
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* GPIO export の有無 getter
|
|
151
|
+
* @return 現在のGPIO 出力
|
|
152
|
+
*/
|
|
88
153
|
get exported() {
|
|
89
154
|
if (this._exported instanceof OperationError)
|
|
90
155
|
throw this._exported;
|
|
91
156
|
return this._exported;
|
|
92
157
|
}
|
|
158
|
+
/**
|
|
159
|
+
* GPIO 出力処理
|
|
160
|
+
* @param direction GPIO 入出力方向
|
|
161
|
+
* @return export 処理の完了
|
|
162
|
+
*/
|
|
93
163
|
async export(direction) {
|
|
94
164
|
if (!/^(in|out)$/.test(direction)) {
|
|
95
165
|
throw new InvalidAccessError(`Must be "in" or "out".`);
|
|
@@ -127,6 +197,11 @@ class GPIOPort extends events_1.EventEmitter {
|
|
|
127
197
|
this._direction = direction;
|
|
128
198
|
this._exported = true;
|
|
129
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Unexport exported GPIO ports.
|
|
202
|
+
* ポート開放をする
|
|
203
|
+
* @return ポート開放処理の完了
|
|
204
|
+
*/
|
|
130
205
|
async unexport() {
|
|
131
206
|
clearInterval(this._timeout);
|
|
132
207
|
try {
|
|
@@ -137,6 +212,10 @@ class GPIOPort extends events_1.EventEmitter {
|
|
|
137
212
|
}
|
|
138
213
|
this._exported = false;
|
|
139
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* 入力値読み取り処理
|
|
217
|
+
* @return 読み取り処理の完了
|
|
218
|
+
*/
|
|
140
219
|
async read() {
|
|
141
220
|
if (!(this.exported && this.direction === 'in')) {
|
|
142
221
|
throw new InvalidAccessError(`The exported must be true and value of direction must be "in".`);
|
|
@@ -154,6 +233,10 @@ class GPIOPort extends events_1.EventEmitter {
|
|
|
154
233
|
throw new OperationError(error);
|
|
155
234
|
}
|
|
156
235
|
}
|
|
236
|
+
/**
|
|
237
|
+
* 出力値書き込み処理
|
|
238
|
+
* @return 読み取り処理の完了
|
|
239
|
+
*/
|
|
157
240
|
async write(value) {
|
|
158
241
|
if (!(this.exported && this.direction === 'out')) {
|
|
159
242
|
throw new InvalidAccessError(`The exported must be true and value of direction must be "out".`);
|
|
@@ -167,14 +250,28 @@ class GPIOPort extends events_1.EventEmitter {
|
|
|
167
250
|
}
|
|
168
251
|
}
|
|
169
252
|
exports.GPIOPort = GPIOPort;
|
|
253
|
+
/**
|
|
254
|
+
* 無効なアクセスエラー
|
|
255
|
+
*/
|
|
170
256
|
class InvalidAccessError extends Error {
|
|
257
|
+
/**
|
|
258
|
+
* Creates an instance of InvalidAccessError.
|
|
259
|
+
* @param message エラーメッセージ
|
|
260
|
+
*/
|
|
171
261
|
constructor(message) {
|
|
172
262
|
super(message);
|
|
173
263
|
this.name = this.constructor.name;
|
|
174
264
|
}
|
|
175
265
|
}
|
|
176
266
|
exports.InvalidAccessError = InvalidAccessError;
|
|
267
|
+
/**
|
|
268
|
+
* 操作エラー
|
|
269
|
+
*/
|
|
177
270
|
class OperationError extends Error {
|
|
271
|
+
/**
|
|
272
|
+
* Creates an instance of OperationError.
|
|
273
|
+
* @param message エラーメッセージ
|
|
274
|
+
*/
|
|
178
275
|
constructor(message) {
|
|
179
276
|
super(message);
|
|
180
277
|
this.name = this.constructor.name;
|
|
@@ -191,6 +288,11 @@ async function requestGPIOAccess() {
|
|
|
191
288
|
return new GPIOAccess(ports);
|
|
192
289
|
}
|
|
193
290
|
exports.requestGPIOAccess = requestGPIOAccess;
|
|
291
|
+
/**
|
|
292
|
+
* 待機 関数
|
|
293
|
+
* @param ms スリープ時間(ミリ秒)
|
|
294
|
+
* @return 待機完了
|
|
295
|
+
*/
|
|
194
296
|
function sleep(ms) {
|
|
195
297
|
return new Promise((resolve) => {
|
|
196
298
|
return setTimeout(resolve, ms);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-web-gpio",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.14",
|
|
4
4
|
"description": "GPIO access with Node.js",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -21,13 +21,17 @@
|
|
|
21
21
|
"husky": "^7.0.4",
|
|
22
22
|
"lint-staged": "^12.1.2",
|
|
23
23
|
"prettier": "^2.5.1",
|
|
24
|
+
"typedoc": "^0.22.10",
|
|
24
25
|
"typescript": "^4.2.3"
|
|
25
26
|
},
|
|
26
27
|
"scripts": {
|
|
27
28
|
"build": "tsc",
|
|
28
29
|
"lint": "eslint index.ts",
|
|
29
30
|
"prepare": "husky install && rm -rf dist && npm run build",
|
|
30
|
-
"precommit": "lint-staged"
|
|
31
|
+
"precommit": "lint-staged",
|
|
32
|
+
"docs": "npm run remove:docs && npm run typedoc -- --options typedoc.json --exclude '**/*.spec.ts' ./ README.md",
|
|
33
|
+
"typedoc": "typedoc",
|
|
34
|
+
"remove:docs": "rm -rf docs"
|
|
31
35
|
},
|
|
32
36
|
"keywords": [
|
|
33
37
|
"gpio",
|