node-web-i2c 1.1.18 → 1.1.21

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.
Files changed (4) hide show
  1. package/index.d.ts +90 -4
  2. package/index.js +101 -10
  3. package/index.ts +155 -17
  4. package/package.json +7 -3
package/index.d.ts CHANGED
@@ -1,38 +1,124 @@
1
+ /** ポート番号 */
1
2
  declare type PortNumber = number;
3
+ /** ポート名 */
2
4
  declare type PortName = string;
5
+ /** I2C Slave アドレス */
3
6
  declare type I2CSlaveAddress = number;
7
+ /**
8
+ * I2CAccess クラス
9
+ */
4
10
  export declare class I2CAccess {
5
11
  private readonly _ports;
12
+ /**
13
+ * Creates an instance of GPIOAccess.
14
+ * @param ports ポート番号
15
+ */
6
16
  constructor(ports?: I2CPortMap);
17
+ /**
18
+ * ポート情報取得処理
19
+ * @return 現在のポート情報
20
+ */
7
21
  get ports(): I2CPortMap;
8
22
  }
9
23
  /** Different from Web I2C API specification. */
10
24
  export declare class I2CPortMap extends Map<PortNumber, I2CPort> {
11
25
  getByName(portName: PortName): I2CPort | undefined;
12
26
  }
27
+ /**
28
+ * I2CPort クラス
29
+ */
13
30
  export declare class I2CPort {
14
31
  private readonly _portNumber;
32
+ /**
33
+ * Creates an instance of GPIOPort.
34
+ * @param portNumber ポート番号
35
+ */
15
36
  constructor(portNumber: PortNumber);
37
+ /**
38
+ * ポート番号取得処理
39
+ * @return 現在のポート番号
40
+ */
16
41
  get portNumber(): PortNumber;
42
+ /**
43
+ * ポート名取得処理
44
+ * @return 現在のポート名
45
+ */
17
46
  get portName(): string;
47
+ /**
48
+ * I2CSlave 接続デバイスオープン処理
49
+ * @param slaveAddress 接続デバイス情報のアドレス
50
+ * @return I2CSlaveDevice インスタンスの生成の完了
51
+ */
18
52
  open(slaveAddress: I2CSlaveAddress): Promise<I2CSlaveDevice>;
19
53
  }
54
+ /**
55
+ * I2CSlaveDevice クラス
56
+ */
20
57
  export interface I2CSlaveDevice {
58
+ /** I2C Slave アドレス */
21
59
  readonly slaveAddress: I2CSlaveAddress;
60
+ /**
61
+ * @function
62
+ * I2C 読み取り処理
63
+ * @param registerNumber 読み取りアドレス
64
+ */
22
65
  read8(registerNumber: number): Promise<number>;
66
+ /**
67
+ * @function
68
+ * I2C 読み取り処理
69
+ * @param registerNumber 読み取りアドレス
70
+ */
23
71
  read16(registerNumber: number): Promise<number>;
72
+ /**
73
+ * @function
74
+ * I2c s/I2c/I2C 書き込み処理
75
+ * @param registerNumber 書き込みアドレス
76
+ * @param value 書き込みの値(バイト)
77
+ */
24
78
  write8(registerNumber: number, value: number): Promise<number>;
79
+ /**
80
+ * @function
81
+ * I2c bytes 書き込み処理
82
+ * @param registerNumber 書き込みアドレス
83
+ * @param value 書き込みの値(ワード)
84
+ */
25
85
  write16(registerNumber: number, value: number): Promise<number>;
26
- /** Different from Web I2C API specification. */
86
+ /**
87
+ * @function
88
+ * I2c bytes 読み取りバイト処理
89
+ * Different from Web I2C API specification.
90
+ */
27
91
  readByte(): Promise<number>;
28
- /** Different from Web I2C API specification. */
92
+ /**
93
+ * @function
94
+ * I2c bytes 読み取りバイト処理
95
+ * Different from Web I2C API specification.
96
+ * @param length 読み取る配列の長さ
97
+ */
29
98
  readBytes(length: number): Promise<Uint8Array>;
30
- /** Different from Web I2C API specification. */
99
+ /**
100
+ * @function
101
+ * I2c bytes 書き込みバイト処理
102
+ * Different from Web I2C API specification.
103
+ * @param byte 書き込みの値
104
+ */
31
105
  writeByte(byte: number): Promise<number>;
32
- /** Different from Web I2C API specification. */
106
+ /**
107
+ * @function
108
+ * I2c bytes 書き込みバイト配列処理
109
+ * Different from Web I2C API specification.
110
+ * @param bytes 書き込みの値の配列
111
+ */
33
112
  writeBytes(bytes: Array<number>): Promise<Uint8Array>;
34
113
  }
114
+ /**
115
+ * 操作エラー
116
+ */
35
117
  export declare class OperationError extends Error {
118
+ /**
119
+ * Creates an instance of OperationError.
120
+ * @param message エラーメッセージ
121
+ */
36
122
  constructor(message: string);
37
123
  }
38
124
  export declare function requestI2CAccess(): Promise<I2CAccess>;
package/index.js CHANGED
@@ -2,8 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.requestI2CAccess = exports.OperationError = exports.I2CPort = exports.I2CPortMap = exports.I2CAccess = void 0;
4
4
  const i2c_bus_1 = require("i2c-bus");
5
+ /**
6
+ * I2C Port Map Max サイズ
7
+ */
5
8
  const I2CPortMapSizeMax = 32;
9
+ /**
10
+ * Uint16 Max サイズ
11
+ */
6
12
  const Uint16Max = 65535;
13
+ /**
14
+ *
15
+ * Uint16型変換処理
16
+ * @param parseString 変換文字列
17
+ * @return Uint16型変換値
18
+ */
7
19
  function parseUint16(parseString) {
8
20
  const n = Number.parseInt(parseString, 10);
9
21
  if (0 <= n && n <= Uint16Max)
@@ -11,11 +23,22 @@ function parseUint16(parseString) {
11
23
  else
12
24
  throw new RangeError(`Must be between 0 and ${Uint16Max}.`);
13
25
  }
26
+ /**
27
+ * I2CAccess クラス
28
+ */
14
29
  class I2CAccess {
15
30
  _ports;
31
+ /**
32
+ * Creates an instance of GPIOAccess.
33
+ * @param ports ポート番号
34
+ */
16
35
  constructor(ports) {
17
36
  this._ports = ports == null ? new I2CPortMap() : ports;
18
37
  }
38
+ /**
39
+ * ポート情報取得処理
40
+ * @return 現在のポート情報
41
+ */
19
42
  get ports() {
20
43
  return this._ports;
21
44
  }
@@ -29,48 +52,94 @@ class I2CPortMap extends Map {
29
52
  }
30
53
  }
31
54
  exports.I2CPortMap = I2CPortMap;
55
+ /**
56
+ * I2CPort クラス
57
+ */
32
58
  class I2CPort {
33
59
  _portNumber;
60
+ /**
61
+ * Creates an instance of GPIOPort.
62
+ * @param portNumber ポート番号
63
+ */
34
64
  constructor(portNumber) {
35
65
  this._portNumber = parseUint16(portNumber.toString());
36
66
  }
67
+ /**
68
+ * ポート番号取得処理
69
+ * @return 現在のポート番号
70
+ */
37
71
  get portNumber() {
38
72
  return this._portNumber;
39
73
  }
74
+ /**
75
+ * ポート名取得処理
76
+ * @return 現在のポート名
77
+ */
40
78
  get portName() {
41
79
  return `i2c-${this.portNumber}`;
42
80
  }
81
+ /**
82
+ * I2CSlave 接続デバイスオープン処理
83
+ * @param slaveAddress 接続デバイス情報のアドレス
84
+ * @return I2CSlaveDevice インスタンスの生成の完了
85
+ */
43
86
  async open(slaveAddress) {
44
87
  const bus = await (0, i2c_bus_1.openPromisified)(this.portNumber).catch((error) => {
45
88
  throw new OperationError(error);
46
89
  });
47
90
  return {
48
91
  slaveAddress,
49
- read8: (cmd) => bus.readByte(slaveAddress, cmd).catch((error) => {
92
+ /**
93
+ * @function
94
+ * I2C 読み取り処理
95
+ * @param registerNumber 読み取りアドレス
96
+ */
97
+ read8: (registerNumber) => bus.readByte(slaveAddress, registerNumber).catch((error) => {
50
98
  throw new OperationError(error);
51
99
  }),
52
- read16: (cmd) => bus.readWord(slaveAddress, cmd).catch((error) => {
100
+ /**
101
+ * @function
102
+ * I2C 読み取り処理
103
+ * @param registerNumber 読み取りアドレス
104
+ */
105
+ read16: (registerNumber) => bus.readWord(slaveAddress, registerNumber).catch((error) => {
53
106
  throw new OperationError(error);
54
107
  }),
55
- write8: async (cmd, byte) => {
108
+ /**
109
+ * @function
110
+ * I2c s/I2c/I2C 書き込み処理
111
+ * @param registerNumber 書き込みアドレス
112
+ * @param byte 書き込みの値(バイト)
113
+ */
114
+ write8: async (registerNumber, byte) => {
56
115
  try {
57
- await bus.writeByte(slaveAddress, cmd, byte);
116
+ await bus.writeByte(slaveAddress, registerNumber, byte);
58
117
  return byte;
59
118
  }
60
119
  catch (error) {
61
120
  throw new OperationError(error);
62
121
  }
63
122
  },
64
- write16: async (cmd, word) => {
123
+ /**
124
+ * @function
125
+ * I2c bytes 書き込み処理
126
+ * @param registerNumber 書き込みアドレス
127
+ * @param word 書き込みの値(ワード)
128
+ */
129
+ write16: async (registerNumber, word) => {
65
130
  try {
66
- await bus.writeWord(slaveAddress, cmd, word);
131
+ await bus.writeWord(slaveAddress, registerNumber, word);
67
132
  return word;
68
133
  }
69
134
  catch (error) {
70
135
  throw new OperationError(error);
71
136
  }
72
137
  },
73
- /** Different from Web I2C API specification. */
138
+ /**
139
+ * @function
140
+ * I2c bytes 読み取りバイト処理
141
+ * Different from Web I2C API specification.
142
+ */
74
143
  readByte: async () => {
75
144
  try {
76
145
  const byte = await bus.receiveByte(slaveAddress);
@@ -80,7 +149,12 @@ class I2CPort {
80
149
  throw new OperationError(error);
81
150
  }
82
151
  },
83
- /** Different from Web I2C API specification. */
152
+ /**
153
+ * @function
154
+ * I2c bytes 読み取りバイト処理
155
+ * Different from Web I2C API specification.
156
+ * @param length 読み取る配列の長さ
157
+ */
84
158
  readBytes: async (length) => {
85
159
  try {
86
160
  const { bytesRead, buffer } = await bus.i2cRead(slaveAddress, length, Buffer.allocUnsafe(length));
@@ -90,7 +164,12 @@ class I2CPort {
90
164
  throw new OperationError(error);
91
165
  }
92
166
  },
93
- /** Different from Web I2C API specification. */
167
+ /**
168
+ * @function
169
+ * I2c bytes 書き込みバイト処理
170
+ * Different from Web I2C API specification.
171
+ * @param byte 書き込みの値
172
+ */
94
173
  writeByte: async (byte) => {
95
174
  try {
96
175
  await bus.sendByte(slaveAddress, byte);
@@ -100,7 +179,12 @@ class I2CPort {
100
179
  throw new OperationError(error);
101
180
  }
102
181
  },
103
- /** Different from Web I2C API specification. */
182
+ /**
183
+ * @function
184
+ * I2c bytes 書き込み処理
185
+ * Different from Web I2C API specification.
186
+ * @param bytes 書き込みの値の配列
187
+ */
104
188
  writeBytes: async (bytes) => {
105
189
  try {
106
190
  const { bytesWritten, buffer } = await bus.i2cWrite(slaveAddress, bytes.length, Buffer.from(bytes));
@@ -114,7 +198,14 @@ class I2CPort {
114
198
  }
115
199
  }
116
200
  exports.I2CPort = I2CPort;
201
+ /**
202
+ * 操作エラー
203
+ */
117
204
  class OperationError extends Error {
205
+ /**
206
+ * Creates an instance of OperationError.
207
+ * @param message エラーメッセージ
208
+ */
118
209
  constructor(message) {
119
210
  super(message);
120
211
  this.name = this.constructor.name;
package/index.ts CHANGED
@@ -1,27 +1,53 @@
1
1
  import { openPromisified } from 'i2c-bus';
2
2
 
3
+ /**
4
+ * I2C Port Map Max サイズ
5
+ */
3
6
  const I2CPortMapSizeMax = 32;
4
7
 
8
+ /**
9
+ * Uint16 Max サイズ
10
+ */
5
11
  const Uint16Max = 65535;
6
12
 
13
+ /**
14
+ *
15
+ * Uint16型変換処理
16
+ * @param parseString 変換文字列
17
+ * @return Uint16型変換値
18
+ */
7
19
  function parseUint16(parseString: string) {
8
20
  const n = Number.parseInt(parseString, 10);
9
21
  if (0 <= n && n <= Uint16Max) return n;
10
22
  else throw new RangeError(`Must be between 0 and ${Uint16Max}.`);
11
23
  }
12
24
 
25
+ /** ポート番号 */
13
26
  type PortNumber = number;
27
+ /** ポート名 */
14
28
  type PortName = string;
15
29
 
30
+ /** I2C Slave アドレス */
16
31
  type I2CSlaveAddress = number;
17
32
 
33
+ /**
34
+ * I2CAccess クラス
35
+ */
18
36
  export class I2CAccess {
19
37
  private readonly _ports: I2CPortMap;
20
38
 
39
+ /**
40
+ * Creates an instance of GPIOAccess.
41
+ * @param ports ポート番号
42
+ */
21
43
  constructor(ports?: I2CPortMap) {
22
44
  this._ports = ports == null ? new I2CPortMap() : ports;
23
45
  }
24
46
 
47
+ /**
48
+ * ポート情報取得処理
49
+ * @return 現在のポート情報
50
+ */
25
51
  get ports(): I2CPortMap {
26
52
  return this._ports;
27
53
  }
@@ -35,21 +61,41 @@ export class I2CPortMap extends Map<PortNumber, I2CPort> {
35
61
  }
36
62
  }
37
63
 
64
+ /**
65
+ * I2CPort クラス
66
+ */
38
67
  export class I2CPort {
39
68
  private readonly _portNumber: PortNumber;
40
69
 
70
+ /**
71
+ * Creates an instance of GPIOPort.
72
+ * @param portNumber ポート番号
73
+ */
41
74
  constructor(portNumber: PortNumber) {
42
75
  this._portNumber = parseUint16(portNumber.toString());
43
76
  }
44
77
 
78
+ /**
79
+ * ポート番号取得処理
80
+ * @return 現在のポート番号
81
+ */
45
82
  get portNumber(): PortNumber {
46
83
  return this._portNumber;
47
84
  }
48
85
 
86
+ /**
87
+ * ポート名取得処理
88
+ * @return 現在のポート名
89
+ */
49
90
  get portName(): string {
50
91
  return `i2c-${this.portNumber}`;
51
92
  }
52
93
 
94
+ /**
95
+ * I2CSlave 接続デバイスオープン処理
96
+ * @param slaveAddress 接続デバイス情報のアドレス
97
+ * @return I2CSlaveDevice インスタンスの生成の完了
98
+ */
53
99
  async open(slaveAddress: I2CSlaveAddress): Promise<I2CSlaveDevice> {
54
100
  const bus = await openPromisified(this.portNumber).catch((error) => {
55
101
  throw new OperationError(error);
@@ -57,32 +103,57 @@ export class I2CPort {
57
103
 
58
104
  return {
59
105
  slaveAddress,
60
- read8: (cmd) =>
61
- bus.readByte(slaveAddress, cmd).catch((error) => {
106
+ /**
107
+ * @function
108
+ * I2C 読み取り処理
109
+ * @param registerNumber 読み取りアドレス
110
+ */
111
+ read8: (registerNumber) =>
112
+ bus.readByte(slaveAddress, registerNumber).catch((error) => {
62
113
  throw new OperationError(error);
63
114
  }),
64
- read16: (cmd) =>
65
- bus.readWord(slaveAddress, cmd).catch((error) => {
115
+ /**
116
+ * @function
117
+ * I2C 読み取り処理
118
+ * @param registerNumber 読み取りアドレス
119
+ */
120
+ read16: (registerNumber) =>
121
+ bus.readWord(slaveAddress, registerNumber).catch((error) => {
66
122
  throw new OperationError(error);
67
123
  }),
68
- write8: async (cmd, byte) => {
124
+ /**
125
+ * @function
126
+ * I2c s/I2c/I2C 書き込み処理
127
+ * @param registerNumber 書き込みアドレス
128
+ * @param byte 書き込みの値(バイト)
129
+ */
130
+ write8: async (registerNumber, byte) => {
69
131
  try {
70
- await bus.writeByte(slaveAddress, cmd, byte);
132
+ await bus.writeByte(slaveAddress, registerNumber, byte);
71
133
  return byte;
72
134
  } catch (error: any) {
73
135
  throw new OperationError(error);
74
136
  }
75
137
  },
76
- write16: async (cmd, word) => {
138
+ /**
139
+ * @function
140
+ * I2c bytes 書き込み処理
141
+ * @param registerNumber 書き込みアドレス
142
+ * @param word 書き込みの値(ワード)
143
+ */
144
+ write16: async (registerNumber, word) => {
77
145
  try {
78
- await bus.writeWord(slaveAddress, cmd, word);
146
+ await bus.writeWord(slaveAddress, registerNumber, word);
79
147
  return word;
80
148
  } catch (error: any) {
81
149
  throw new OperationError(error);
82
150
  }
83
151
  },
84
-
85
- /** Different from Web I2C API specification. */
152
+ /**
153
+ * @function
154
+ * I2c bytes 読み取りバイト処理
155
+ * Different from Web I2C API specification.
156
+ */
86
157
  readByte: async () => {
87
158
  try {
88
159
  const byte = await bus.receiveByte(slaveAddress);
@@ -91,7 +162,12 @@ export class I2CPort {
91
162
  throw new OperationError(error);
92
163
  }
93
164
  },
94
- /** Different from Web I2C API specification. */
165
+ /**
166
+ * @function
167
+ * I2c bytes 読み取りバイト処理
168
+ * Different from Web I2C API specification.
169
+ * @param length 読み取る配列の長さ
170
+ */
95
171
  readBytes: async (length) => {
96
172
  try {
97
173
  const { bytesRead, buffer } = await bus.i2cRead(
@@ -104,7 +180,12 @@ export class I2CPort {
104
180
  throw new OperationError(error);
105
181
  }
106
182
  },
107
- /** Different from Web I2C API specification. */
183
+ /**
184
+ * @function
185
+ * I2c bytes 書き込みバイト処理
186
+ * Different from Web I2C API specification.
187
+ * @param byte 書き込みの値
188
+ */
108
189
  writeByte: async (byte) => {
109
190
  try {
110
191
  await bus.sendByte(slaveAddress, byte);
@@ -113,7 +194,12 @@ export class I2CPort {
113
194
  throw new OperationError(error);
114
195
  }
115
196
  },
116
- /** Different from Web I2C API specification. */
197
+ /**
198
+ * @function
199
+ * I2c bytes 書き込み処理
200
+ * Different from Web I2C API specification.
201
+ * @param bytes 書き込みの値の配列
202
+ */
117
203
  writeBytes: async (bytes) => {
118
204
  try {
119
205
  const { bytesWritten, buffer } = await bus.i2cWrite(
@@ -130,25 +216,77 @@ export class I2CPort {
130
216
  }
131
217
  }
132
218
 
219
+ /**
220
+ * I2CSlaveDevice クラス
221
+ */
133
222
  export interface I2CSlaveDevice {
223
+ /** I2C Slave アドレス */
134
224
  readonly slaveAddress: I2CSlaveAddress;
135
225
 
226
+ /**
227
+ * @function
228
+ * I2C 読み取り処理
229
+ * @param registerNumber 読み取りアドレス
230
+ */
136
231
  read8(registerNumber: number): Promise<number>;
232
+ /**
233
+ * @function
234
+ * I2C 読み取り処理
235
+ * @param registerNumber 読み取りアドレス
236
+ */
137
237
  read16(registerNumber: number): Promise<number>;
238
+ /**
239
+ * @function
240
+ * I2c s/I2c/I2C 書き込み処理
241
+ * @param registerNumber 書き込みアドレス
242
+ * @param value 書き込みの値(バイト)
243
+ */
138
244
  write8(registerNumber: number, value: number): Promise<number>;
245
+ /**
246
+ * @function
247
+ * I2c bytes 書き込み処理
248
+ * @param registerNumber 書き込みアドレス
249
+ * @param value 書き込みの値(ワード)
250
+ */
139
251
  write16(registerNumber: number, value: number): Promise<number>;
140
252
 
141
- /** Different from Web I2C API specification. */
253
+ /**
254
+ * @function
255
+ * I2c bytes 読み取りバイト処理
256
+ * Different from Web I2C API specification.
257
+ */
142
258
  readByte(): Promise<number>;
143
- /** Different from Web I2C API specification. */
259
+ /**
260
+ * @function
261
+ * I2c bytes 読み取りバイト処理
262
+ * Different from Web I2C API specification.
263
+ * @param length 読み取る配列の長さ
264
+ */
144
265
  readBytes(length: number): Promise<Uint8Array>;
145
- /** Different from Web I2C API specification. */
266
+ /**
267
+ * @function
268
+ * I2c bytes 書き込みバイト処理
269
+ * Different from Web I2C API specification.
270
+ * @param byte 書き込みの値
271
+ */
146
272
  writeByte(byte: number): Promise<number>;
147
- /** Different from Web I2C API specification. */
273
+ /**
274
+ * @function
275
+ * I2c bytes 書き込みバイト配列処理
276
+ * Different from Web I2C API specification.
277
+ * @param bytes 書き込みの値の配列
278
+ */
148
279
  writeBytes(bytes: Array<number>): Promise<Uint8Array>;
149
280
  }
150
281
 
282
+ /**
283
+ * 操作エラー
284
+ */
151
285
  export class OperationError extends Error {
286
+ /**
287
+ * Creates an instance of OperationError.
288
+ * @param message エラーメッセージ
289
+ */
152
290
  constructor(message: string) {
153
291
  super(message);
154
292
  this.name = this.constructor.name;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-web-i2c",
3
- "version": "1.1.18",
3
+ "version": "1.1.21",
4
4
  "description": "I2C access with Node.js",
5
5
  "main": "index.js",
6
6
  "files": [
@@ -22,16 +22,20 @@
22
22
  "@typescript-eslint/eslint-plugin": "^5.6.0",
23
23
  "@typescript-eslint/parser": "^5.6.0",
24
24
  "eslint": "^8.4.1",
25
- "husky": "^7.0.4",
25
+ "husky": "^8.0.0",
26
26
  "lint-staged": "^12.1.2",
27
27
  "prettier": "^2.5.1",
28
+ "typedoc": "^0.22.10",
28
29
  "typescript": "^4.2.4"
29
30
  },
30
31
  "scripts": {
31
32
  "build": "tsc",
32
33
  "lint": "eslint index.ts",
33
34
  "prepare": "husky install && rm -rf dist && npm run build",
34
- "precommit": "lint-staged"
35
+ "precommit": "lint-staged",
36
+ "docs": "npm run remove:docs && npm run typedoc -- --options typedoc.json --exclude '**/*.spec.ts' ./ README.md",
37
+ "typedoc": "typedoc",
38
+ "remove:docs": "rm -rf docs"
35
39
  },
36
40
  "keywords": [
37
41
  "hardware",