periph 1.5.4 → 1.6.1

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 (73) hide show
  1. package/README.md +17 -0
  2. package/examples/gas/ccs811/complete.js +87 -0
  3. package/examples/gas/ccs811/demo.js +108 -0
  4. package/examples/gas/ccs811/minimal.js +20 -0
  5. package/examples/gas/rrh46410/complete.js +78 -0
  6. package/examples/gas/rrh46410/demo.js +86 -0
  7. package/examples/gas/rrh46410/minimal.js +21 -0
  8. package/examples/gas/rrh62000/complete.js +74 -0
  9. package/examples/gas/rrh62000/demo.js +87 -0
  10. package/examples/gas/rrh62000/minimal.js +22 -0
  11. package/examples/gas/scd30/complete.js +73 -0
  12. package/examples/gas/scd30/demo.js +102 -0
  13. package/examples/gas/scd30/minimal.js +20 -0
  14. package/examples/gas/scd40/complete.js +76 -0
  15. package/examples/gas/scd40/demo.js +109 -0
  16. package/examples/gas/scd40/minimal.js +20 -0
  17. package/examples/light/si1133/complete.js +89 -0
  18. package/examples/light/si1133/demo.js +93 -0
  19. package/examples/light/si1133/minimal.js +22 -0
  20. package/examples/light/si1145/complete.js +93 -0
  21. package/examples/light/si1145/demo.js +98 -0
  22. package/examples/light/si1145/minimal.js +22 -0
  23. package/examples/light/tsl2561/complete.js +74 -0
  24. package/examples/light/tsl2561/demo.js +46 -0
  25. package/examples/light/tsl2561/minimal.js +22 -0
  26. package/examples/light/tsl2591/complete.js +90 -0
  27. package/examples/light/tsl2591/demo.js +119 -0
  28. package/examples/light/tsl2591/minimal.js +22 -0
  29. package/examples/light/vcnl4040/complete.js +102 -0
  30. package/examples/light/vcnl4040/demo.js +125 -0
  31. package/examples/light/vcnl4040/minimal.js +25 -0
  32. package/examples/light/vcnl4200/complete.js +101 -0
  33. package/examples/light/vcnl4200/demo.js +126 -0
  34. package/examples/light/vcnl4200/minimal.js +25 -0
  35. package/examples/pressure/spl06/complete.js +81 -0
  36. package/examples/pressure/spl06/demo.js +106 -0
  37. package/examples/pressure/spl06/minimal.js +22 -0
  38. package/examples/rfid/pn532/complete.js +99 -0
  39. package/examples/rfid/pn532/demo.js +121 -0
  40. package/examples/rfid/pn532/minimal.js +22 -0
  41. package/examples/rfid/st25dv/complete.js +94 -0
  42. package/examples/rfid/st25dv/demo.js +121 -0
  43. package/examples/rfid/st25dv/minimal.js +21 -0
  44. package/examples/rtc/bq32000/complete.js +53 -0
  45. package/examples/rtc/bq32000/demo.js +77 -0
  46. package/examples/rtc/bq32000/minimal.js +20 -0
  47. package/examples/rtc/ds1307/complete.js +54 -0
  48. package/examples/rtc/ds1307/demo.js +96 -0
  49. package/examples/rtc/ds1307/minimal.js +20 -0
  50. package/examples/rtc/ds1371/complete.js +53 -0
  51. package/examples/rtc/ds1371/demo.js +85 -0
  52. package/examples/rtc/ds1371/minimal.js +25 -0
  53. package/package.json +1 -1
  54. package/src/chips/gas/ccs811.js +400 -0
  55. package/src/chips/gas/rrh46410.js +534 -0
  56. package/src/chips/gas/rrh62000.js +418 -0
  57. package/src/chips/gas/scd30.js +368 -0
  58. package/src/chips/gas/scd40.js +451 -0
  59. package/src/chips/light/si1133.js +583 -0
  60. package/src/chips/light/si1145.js +630 -0
  61. package/src/chips/light/tsl2561.js +445 -0
  62. package/src/chips/light/tsl2591.js +561 -0
  63. package/src/chips/light/vcnl4040.js +576 -0
  64. package/src/chips/light/vcnl4200.js +592 -0
  65. package/src/chips/pressure/spl06.js +461 -0
  66. package/src/chips/rfid/pn532.js +513 -0
  67. package/src/chips/rfid/st25dv.js +628 -0
  68. package/src/chips/rtc/bq32000.js +334 -0
  69. package/src/chips/rtc/ds1307.js +327 -0
  70. package/src/chips/rtc/ds1371.js +453 -0
  71. package/src/connection/i2c.js +46 -13
  72. package/src/connection/smbus.js +21 -8
  73. package/src/discovery/registry.js +17 -17
@@ -0,0 +1,513 @@
1
+ 'use strict';
2
+
3
+ /*
4
+ * PN532 NFC/RFID controller (NXP), I²C frame protocol.
5
+ *
6
+ * The host exchanges frames with the PN532 firmware: a command frame
7
+ * `00 00 FF LEN LCS D4 code data DCS 00`, the chip's ACK frame `00 00 FF 00 FF 00`
8
+ * and a response frame (TFI 0xD5, code + 1). Every I²C read starts with a status
9
+ * byte (bit 0 = frame ready); a 1-byte read does not consume the frame. The driver
10
+ * reads the 6-byte response header, sends a NACK so the chip resends the response,
11
+ * and then reads exactly 1 + LEN + 7 bytes.
12
+ */
13
+
14
+ const _ACK = Buffer.from([0x00, 0x00, 0xFF, 0x00, 0xFF, 0x00]);
15
+ const _NACK = Buffer.from([0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00]);
16
+
17
+ const _TFI_HOST = 0xD4;
18
+ const _TFI_CHIP = 0xD5;
19
+ const _TFI_ERROR = 0x7F;
20
+
21
+ const _CMD_DIAGNOSE = 0x00;
22
+ const _CMD_GET_FIRMWARE_VERSION = 0x02;
23
+ const _CMD_GET_GENERAL_STATUS = 0x04;
24
+ const _CMD_READ_REGISTER = 0x06;
25
+ const _CMD_WRITE_REGISTER = 0x08;
26
+ const _CMD_SAM_CONFIGURATION = 0x14;
27
+ const _CMD_POWER_DOWN = 0x16;
28
+ const _CMD_RF_CONFIGURATION = 0x32;
29
+ const _CMD_IN_DATA_EXCHANGE = 0x40;
30
+ const _CMD_IN_COMMUNICATE_THRU = 0x42;
31
+ const _CMD_IN_DESELECT = 0x44;
32
+ const _CMD_IN_LIST_PASSIVE_TARGET = 0x4A;
33
+ const _CMD_IN_RELEASE = 0x52;
34
+ const _CMD_IN_SELECT = 0x54;
35
+
36
+ const _IC = 0x32;
37
+ const _WRITE_RETRIES = 10;
38
+ const _ACK_TIMEOUT = 0.015;
39
+ const _ACK_ATTEMPTS = 3;
40
+ const _PATTERN = Buffer.from('PERIPH01', 'ascii');
41
+
42
+ const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
43
+
44
+ /**
45
+ * PN532 protocol or status error.
46
+ * @property {number|null} code - PN532 status error code (`Status & 0x3F`), or null for
47
+ * framing / timeout errors.
48
+ */
49
+ class PN532Error extends Error {
50
+ constructor(message, code = null) {
51
+ super(message);
52
+ this.name = 'PN532Error';
53
+ this.code = code;
54
+ }
55
+ }
56
+
57
+ class _Timeout extends PN532Error {}
58
+
59
+ /**
60
+ * Build a host command frame.
61
+ * @param {number} command - Command code.
62
+ * @param {Buffer|number[]} data - Parameters.
63
+ * @returns {Buffer} The frame.
64
+ */
65
+ function _frame(command, data) {
66
+ const body = Buffer.concat([Buffer.from([_TFI_HOST, command]), Buffer.from(data)]);
67
+ let sum = 0;
68
+ for (const b of body) sum = (sum + b) & 0xFF;
69
+ return Buffer.concat([
70
+ Buffer.from([0x00, 0x00, 0xFF, body.length, (0x100 - body.length) & 0xFF]),
71
+ body,
72
+ Buffer.from([(0x100 - sum) & 0xFF, 0x00]),
73
+ ]);
74
+ }
75
+
76
+ /**
77
+ * PN532 NFC/RFID reader — minimal interface. Detects one ISO/IEC 14443 A card or tag
78
+ * at 106 kbps and returns its UID.
79
+ *
80
+ * `init()` checks GetFirmwareVersion (IC 0x32), sets SAMConfiguration normal mode with the
81
+ * IRQ pin enabled and MxRtyPassiveActivation = 2 so a read without a card returns. Writes are
82
+ * retried up to 10 times (2 ms apart) to wake the chip; the ACK is awaited 15 ms and the frame
83
+ * resent up to 3 times. The chip must be wired for I²C (I0 low, I1 high); address 0x24.
84
+ *
85
+ * @param {object} connection - Plain I²C connection bound to address 0x24.
86
+ */
87
+ class PN532Minimal {
88
+ constructor(connection) {
89
+ this._conn = connection;
90
+ }
91
+
92
+ /**
93
+ * Wake the chip, check the firmware and configure SAM and retries.
94
+ * @returns {Promise<void>}
95
+ * @throws {PN532Error} If the chip does not answer or IC is not 0x32.
96
+ */
97
+ async init() {
98
+ const d = await this._command(_CMD_GET_FIRMWARE_VERSION);
99
+ if (d.length < 4 || d[0] !== _IC) {
100
+ throw new PN532Error(`Unexpected PN532 IC 0x${(d[0] || 0).toString(16)}, expected 0x32`);
101
+ }
102
+ await this._command(_CMD_SAM_CONFIGURATION, [0x01, 0x14, 0x01]);
103
+ await this._command(_CMD_RF_CONFIGURATION, [0x05, 0xFF, 0x01, 0x02]);
104
+ }
105
+
106
+ /**
107
+ * Detect one ISO/IEC 14443 A card and return its UID; the target is released.
108
+ * @param {number} [timeout=1.0] - Response timeout in s; a timeout aborts the command.
109
+ * @returns {Promise<Buffer|null>} NFCID1 (4, 7 or 10 bytes), or null when no card answered.
110
+ */
111
+ async readUid(timeout = 1.0) {
112
+ const d = await this._list(1, 0x00, [], timeout);
113
+ if (d === null || d.length < 6 || d[0] === 0) return null;
114
+ const uid = Buffer.from(d.subarray(6, 6 + d[5]));
115
+ await this._command(_CMD_IN_RELEASE, [d[1]]);
116
+ return uid;
117
+ }
118
+
119
+ async _list(maxTargets, baud, initiatorData, timeout) {
120
+ await this._start(_CMD_IN_LIST_PASSIVE_TARGET, Buffer.concat([Buffer.from([maxTargets, baud]), Buffer.from(initiatorData)]));
121
+ try {
122
+ return await this._response(_CMD_IN_LIST_PASSIVE_TARGET, timeout);
123
+ } catch (e) {
124
+ if (e instanceof _Timeout) return null;
125
+ throw e;
126
+ }
127
+ }
128
+
129
+ async _write(data) {
130
+ for (let attempt = 0; ; attempt++) {
131
+ try {
132
+ await this._conn.write(data);
133
+ return;
134
+ } catch (e) {
135
+ if (attempt >= _WRITE_RETRIES - 1) throw e;
136
+ await _sleep(2);
137
+ }
138
+ }
139
+ }
140
+
141
+ async _ready() {
142
+ const s = await this._conn.read(1);
143
+ return (s[0] & 0x01) !== 0;
144
+ }
145
+
146
+ async _wait(timeout) {
147
+ const polls = Math.floor(timeout * 1000) + 1;
148
+ for (let i = 0; i < polls; i++) {
149
+ if (await this._ready()) return true;
150
+ await _sleep(1);
151
+ }
152
+ return false;
153
+ }
154
+
155
+ async _start(command, data = []) {
156
+ const frame = _frame(command, data);
157
+ for (let attempt = 0; attempt < _ACK_ATTEMPTS; attempt++) {
158
+ await this._write(frame);
159
+ if (await this._wait(_ACK_TIMEOUT)) {
160
+ const ack = await this._conn.read(7);
161
+ if (ack.subarray(1, 7).equals(_ACK)) return;
162
+ }
163
+ }
164
+ throw new PN532Error(`PN532 did not ACK command 0x${command.toString(16)}`);
165
+ }
166
+
167
+ async _response(command, timeout) {
168
+ if (!(await this._wait(timeout))) {
169
+ await this._write(_ACK); // abort the running command
170
+ throw new _Timeout(`PN532 response timeout (command 0x${command.toString(16)})`);
171
+ }
172
+ const header = await this._conn.read(6);
173
+ if (header[1] !== 0x00 || header[2] !== 0x00 || header[3] !== 0xFF) throw new PN532Error('PN532 bad frame preamble');
174
+ const length = header[4];
175
+ if (((length + header[5]) & 0xFF) !== 0) throw new PN532Error('PN532 bad length checksum');
176
+ await this._write(_NACK); // have the chip resend the whole frame
177
+ if (!(await this._wait(timeout))) throw new _Timeout(`PN532 response timeout (command 0x${command.toString(16)})`);
178
+ const frame = await this._conn.read(1 + length + 7);
179
+ const body = frame.subarray(6, 6 + length);
180
+ if (length === 1 && body[0] === _TFI_ERROR) throw new PN532Error(`PN532 error frame (command 0x${command.toString(16)})`);
181
+ if (length < 2 || body[0] !== _TFI_CHIP || body[1] !== command + 1) {
182
+ throw new PN532Error(`PN532 unexpected response to command 0x${command.toString(16)}`);
183
+ }
184
+ let sum = frame[6 + length];
185
+ for (const b of body) sum = (sum + b) & 0xFF;
186
+ if (sum !== 0) throw new PN532Error('PN532 bad data checksum');
187
+ return Buffer.from(body.subarray(2));
188
+ }
189
+
190
+ async _command(command, data = [], timeout = 1.0) {
191
+ await this._start(command, data);
192
+ return this._response(command, timeout);
193
+ }
194
+ }
195
+
196
+ /**
197
+ * PN532 NFC/RFID controller — full interface. Adds target listing at every baud rate, data
198
+ * exchange, MIFARE Classic and NTAG helpers, RF and retry configuration, register access,
199
+ * power-down, the self test, raw commands and the IRQ interrupt API.
200
+ *
201
+ * @param {object} connection - Plain I²C connection bound to address 0x24.
202
+ */
203
+ class PN532Full extends PN532Minimal {
204
+ constructor(connection) {
205
+ super(connection);
206
+ this._callback = null;
207
+ this._intPinUsed = null;
208
+ this._pollTimer = null;
209
+ this._polling = false;
210
+ this._wasReady = false;
211
+ this._edgeHandler = () => { if (this._callback) this._callback(); };
212
+ }
213
+
214
+ /**
215
+ * GetFirmwareVersion.
216
+ * @returns {Promise<{ic: number, version: number, revision: number, support: number,
217
+ * iso14443a: boolean, iso14443b: boolean, iso18092: boolean}>}
218
+ */
219
+ async firmwareVersion() {
220
+ const d = await this._command(_CMD_GET_FIRMWARE_VERSION);
221
+ return {
222
+ ic: d[0], version: d[1], revision: d[2], support: d[3],
223
+ iso14443a: (d[3] & 0x01) !== 0,
224
+ iso14443b: (d[3] & 0x02) !== 0,
225
+ iso18092: (d[3] & 0x04) !== 0,
226
+ };
227
+ }
228
+
229
+ /**
230
+ * GetGeneralStatus (per-target entries are skipped).
231
+ * @returns {Promise<{error: number, field: boolean, targets: number, samStatus: number}>}
232
+ */
233
+ async generalStatus() {
234
+ const d = await this._command(_CMD_GET_GENERAL_STATUS);
235
+ const n = d[2];
236
+ const samIndex = 3 + 4 * n;
237
+ return { error: d[0], field: d[1] !== 0, targets: n, samStatus: samIndex < d.length ? d[samIndex] : 0 };
238
+ }
239
+
240
+ /**
241
+ * InListPassiveTarget.
242
+ * @param {object} [options]
243
+ * @param {number} [options.baud=PN532Full.BAUD_106_A] - BAUD_106_A, BAUD_212_F, BAUD_424_F or BAUD_106_B.
244
+ * @param {number} [options.maxTargets=1] - 1 or 2.
245
+ * @param {Buffer|number[]} [options.initiatorData=[]] - Optional InitiatorData.
246
+ * @param {number} [options.timeout=1.0] - Response timeout in s; a timeout aborts the command.
247
+ * @returns {Promise<Array<{tg: number, sensRes: number, selRes: number, uid: Buffer, ats: Buffer}>>}
248
+ * Targets found; empty when none answered.
249
+ */
250
+ async listPassiveTargets({ baud = 0x00, maxTargets = 1, initiatorData = [], timeout = 1.0 } = {}) {
251
+ if (maxTargets !== 1 && maxTargets !== 2) throw new RangeError('maxTargets must be 1 or 2');
252
+ if (baud < 0 || baud > 4) throw new RangeError('baud must be 0..4');
253
+ const d = await this._list(maxTargets, baud, initiatorData, timeout);
254
+ if (d === null || d.length === 0) return [];
255
+ const targets = [];
256
+ let i = 1;
257
+ for (let k = 0; k < d[0] && i < d.length; k++) {
258
+ const tg = d[i];
259
+ if (baud === PN532Full.BAUD_106_A) {
260
+ const sensRes = (d[i + 1] << 8) | d[i + 2];
261
+ const selRes = d[i + 3];
262
+ const n = d[i + 4];
263
+ const uid = Buffer.from(d.subarray(i + 5, i + 5 + n));
264
+ i += 5 + n;
265
+ let ats = Buffer.alloc(0);
266
+ if ((selRes & 0x20) && i < d.length) {
267
+ const tl = d[i];
268
+ ats = Buffer.from(d.subarray(i, i + tl));
269
+ i += tl;
270
+ }
271
+ targets.push({ tg, sensRes, selRes, uid, ats });
272
+ } else if (baud === PN532Full.BAUD_212_F || baud === PN532Full.BAUD_424_F) {
273
+ const len = d[i + 1];
274
+ const pol = d.subarray(i + 2, i + 1 + len);
275
+ targets.push({ tg, sensRes: 0, selRes: 0, uid: Buffer.from(pol.subarray(1, 9)), ats: Buffer.from(pol.subarray(9)) });
276
+ i += 1 + len;
277
+ } else {
278
+ const atqb = d.subarray(i + 1, i + 13);
279
+ const len = d[i + 13];
280
+ const attrib = Buffer.from(d.subarray(i + 14, i + 14 + len));
281
+ targets.push({ tg, sensRes: 0, selRes: 0, uid: Buffer.from(atqb.subarray(1, 5)), ats: attrib });
282
+ i += 14 + len;
283
+ }
284
+ }
285
+ return targets;
286
+ }
287
+
288
+ async _statusCommand(command, data) {
289
+ const d = await this._command(command, data);
290
+ if (d.length === 0) throw new PN532Error('PN532 empty status response');
291
+ const code = d[0] & 0x3F;
292
+ if (code) throw new PN532Error(`PN532 status error 0x${code.toString(16)} (command 0x${command.toString(16)})`, code);
293
+ return Buffer.from(d.subarray(1));
294
+ }
295
+
296
+ /**
297
+ * InDataExchange with an activated target.
298
+ * @param {number} target - Logical target number.
299
+ * @param {Buffer|number[]} data - Data to send.
300
+ * @returns {Promise<Buffer>} Data received.
301
+ * @throws {PN532Error} On a non-zero status (`code` holds it).
302
+ */
303
+ async dataExchange(target, data) {
304
+ return this._statusCommand(_CMD_IN_DATA_EXCHANGE, Buffer.concat([Buffer.from([target]), Buffer.from(data)]));
305
+ }
306
+
307
+ /**
308
+ * InCommunicateThru: raw RF exchange with the current target.
309
+ * @param {Buffer|number[]} data - Raw bytes to transmit.
310
+ * @returns {Promise<Buffer>} Raw bytes received.
311
+ */
312
+ async communicateThru(data) {
313
+ return this._statusCommand(_CMD_IN_COMMUNICATE_THRU, data);
314
+ }
315
+
316
+ /** InRelease. @param {number} [target=0] - 0 = all. @returns {Promise<void>} */
317
+ async release(target = 0) { await this._statusCommand(_CMD_IN_RELEASE, [target]); }
318
+
319
+ /** InSelect a listed target. @param {number} [target=0] @returns {Promise<void>} */
320
+ async select(target = 0) { await this._statusCommand(_CMD_IN_SELECT, [target]); }
321
+
322
+ /** InDeselect (keeps the target's information). @param {number} [target=0] - 0 = all. @returns {Promise<void>} */
323
+ async deselect(target = 0) { await this._statusCommand(_CMD_IN_DESELECT, [target]); }
324
+
325
+ /**
326
+ * Authenticate a MIFARE Classic sector (0x60 key A / 0x61 key B).
327
+ * @param {number} target - Logical target number.
328
+ * @param {number} block - Any block of the sector.
329
+ * @param {Buffer|number[]} key - 6-byte key.
330
+ * @param {Buffer|number[]} uid - Card UID; its last 4 bytes are used.
331
+ * @param {boolean} [keyB=false] - Use key B.
332
+ * @returns {Promise<void>}
333
+ */
334
+ async mifareAuthenticate(target, block, key, uid, keyB = false) {
335
+ const k = Buffer.from(key);
336
+ const u = Buffer.from(uid);
337
+ if (k.length !== 6) throw new RangeError('key must be 6 bytes');
338
+ if (u.length < 4) throw new RangeError('uid must have at least 4 bytes');
339
+ await this.dataExchange(target, Buffer.concat([Buffer.from([keyB ? 0x61 : 0x60, block & 0xFF]), k, u.subarray(u.length - 4)]));
340
+ }
341
+
342
+ /** Read a 16-byte MIFARE Classic block (0x30). @param {number} target @param {number} block @returns {Promise<Buffer>} 16 bytes. */
343
+ async mifareReadBlock(target, block) {
344
+ return (await this.dataExchange(target, [0x30, block & 0xFF])).subarray(0, 16);
345
+ }
346
+
347
+ /**
348
+ * Write a 16-byte MIFARE Classic block (0xA0).
349
+ * @param {number} target @param {number} block @param {Buffer|number[]} data - 16 bytes.
350
+ * @returns {Promise<void>}
351
+ */
352
+ async mifareWriteBlock(target, block, data) {
353
+ const b = Buffer.from(data);
354
+ if (b.length !== 16) throw new RangeError('data must be 16 bytes');
355
+ await this.dataExchange(target, Buffer.concat([Buffer.from([0xA0, block & 0xFF]), b]));
356
+ }
357
+
358
+ /** Read four NTAG / Ultralight pages from `page` (0x30). @param {number} target @param {number} page @returns {Promise<Buffer>} 16 bytes. */
359
+ async ntagReadPage(target, page) {
360
+ return (await this.dataExchange(target, [0x30, page & 0xFF])).subarray(0, 16);
361
+ }
362
+
363
+ /**
364
+ * Write one 4-byte NTAG / Ultralight page (0xA2).
365
+ * @param {number} target @param {number} page @param {Buffer|number[]} data - 4 bytes.
366
+ * @returns {Promise<void>}
367
+ */
368
+ async ntagWritePage(target, page, data) {
369
+ const b = Buffer.from(data);
370
+ if (b.length !== 4) throw new RangeError('data must be 4 bytes');
371
+ await this.dataExchange(target, Buffer.concat([Buffer.from([0xA2, page & 0xFF]), b]));
372
+ }
373
+
374
+ /** RFConfiguration item 0x01. @param {boolean} on @param {boolean} [autoRfca=false] @returns {Promise<void>} */
375
+ async setRfField(on, autoRfca = false) {
376
+ await this._command(_CMD_RF_CONFIGURATION, [0x01, (on ? 0x02 : 0x00) | (autoRfca ? 0x01 : 0x00)]);
377
+ }
378
+
379
+ /**
380
+ * RFConfiguration item 0x05: retry counts, 0xFF = infinite.
381
+ * @param {object} [options]
382
+ * @param {number} [options.passiveActivation=2] - MxRtyPassiveActivation.
383
+ * @param {number} [options.atr=0xFF] - MxRtyATR.
384
+ * @param {number} [options.psl=1] - MxRtyPSL.
385
+ * @returns {Promise<void>}
386
+ */
387
+ async setMaxRetries({ passiveActivation = 2, atr = 0xFF, psl = 1 } = {}) {
388
+ await this._command(_CMD_RF_CONFIGURATION, [0x05, atr & 0xFF, psl & 0xFF, passiveActivation & 0xFF]);
389
+ }
390
+
391
+ /** ReadRegister (16-bit address). @param {number} address @returns {Promise<number>} */
392
+ async readRegister(address) {
393
+ const d = await this._command(_CMD_READ_REGISTER, [(address >> 8) & 0xFF, address & 0xFF]);
394
+ return d[0];
395
+ }
396
+
397
+ /** WriteRegister (16-bit address). @param {number} address @param {number} value @returns {Promise<void>} */
398
+ async writeRegister(address, value) {
399
+ await this._command(_CMD_WRITE_REGISTER, [(address >> 8) & 0xFF, address & 0xFF, value & 0xFF]);
400
+ }
401
+
402
+ /** PowerDown. @param {number} [wakeupSources=0x20] - WakeUpEnable mask (0x20 = I²C). @returns {Promise<void>} */
403
+ async powerDown(wakeupSources = 0x20) {
404
+ await this._statusCommand(_CMD_POWER_DOWN, [wakeupSources & 0xFF]);
405
+ }
406
+
407
+ /**
408
+ * Diagnose self test.
409
+ * @param {number} [test=0x00] - 0x00 communication line (echo), 0x01 ROM, 0x02 RAM, ...
410
+ * @returns {Promise<boolean>} True when the test passed.
411
+ */
412
+ async selfTest(test = 0x00) {
413
+ if (test === 0x00) {
414
+ const req = Buffer.concat([Buffer.from([0x00]), _PATTERN]);
415
+ return (await this._command(_CMD_DIAGNOSE, req)).equals(req);
416
+ }
417
+ const d = await this._command(_CMD_DIAGNOSE, [test & 0xFF]);
418
+ return d.length > 0 && d[0] === 0x00;
419
+ }
420
+
421
+ /**
422
+ * Send a raw command and read its response.
423
+ * @param {number} command - Command code.
424
+ * @param {Buffer|number[]} [data=[]] - Parameters.
425
+ * @param {number} [timeout=1.0] - Response timeout in s.
426
+ * @returns {Promise<Buffer>} Response data after the response code.
427
+ */
428
+ async sendCommand(command, data = [], timeout = 1.0) {
429
+ return this._command(command, data, timeout);
430
+ }
431
+
432
+ /** Write a command frame and read its ACK, without waiting for the response. @param {number} command @param {Buffer|number[]} [data=[]] @returns {Promise<void>} */
433
+ async startCommand(command, data = []) {
434
+ await this._start(command, data);
435
+ }
436
+
437
+ /**
438
+ * Read the response to a command started with `startCommand`.
439
+ * @param {number} command - The started command code.
440
+ * @param {number} [timeout=1.0] - Response timeout in s; a timeout aborts the command.
441
+ * @returns {Promise<Buffer>} Response data after the response code.
442
+ */
443
+ async readResponse(command, timeout = 1.0) {
444
+ return this._response(command, timeout);
445
+ }
446
+
447
+ /** Poll the status byte every 1 ms until a frame is ready. @param {number} timeout - s. @returns {Promise<boolean>} */
448
+ async waitReady(timeout) {
449
+ return this._wait(timeout);
450
+ }
451
+
452
+ /** True when the status byte reports a ready ACK or response frame. @returns {Promise<boolean>} */
453
+ async pollInterrupt() {
454
+ return this._ready();
455
+ }
456
+
457
+ /**
458
+ * Call `callback()` when the chip has a frame ready. Uses `intPin` (or the connection's
459
+ * `intPin`) on its falling edge, otherwise a 10 ms status-byte poll that fires on each
460
+ * not-ready → ready transition. Read the frame with `readResponse`.
461
+ * @param {function(): void} callback - Called with no arguments.
462
+ * @param {object|null} [intPin=null] - Optional InputPin wired to IRQ.
463
+ * @returns {Promise<void>}
464
+ */
465
+ async onInterrupt(callback, intPin = null) {
466
+ await this.offInterrupt();
467
+ this._callback = callback;
468
+ const ipin = intPin ?? this._conn.intPin ?? null;
469
+ if (ipin) {
470
+ this._intPinUsed = ipin;
471
+ await ipin.onEdge(this._edgeHandler, 'falling');
472
+ } else {
473
+ this._wasReady = false;
474
+ this._pollTimer = setInterval(() => { this._service(); }, 10);
475
+ if (this._pollTimer.unref) this._pollTimer.unref();
476
+ }
477
+ }
478
+
479
+ /** Unsubscribe. @returns {Promise<void>} */
480
+ async offInterrupt() {
481
+ if (this._intPinUsed) {
482
+ await this._intPinUsed.offEdge(this._edgeHandler);
483
+ this._intPinUsed = null;
484
+ }
485
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
486
+ this._callback = null;
487
+ }
488
+
489
+ async _service() {
490
+ if (this._polling) return;
491
+ this._polling = true;
492
+ try {
493
+ const now = await this._ready();
494
+ if (now && !this._wasReady && this._callback) this._callback();
495
+ this._wasReady = now;
496
+ } catch (e) {
497
+ /* a failed poll is retried on the next tick */
498
+ } finally {
499
+ this._polling = false;
500
+ }
501
+ }
502
+ }
503
+
504
+ /** 106 kbps ISO/IEC 14443 A. */
505
+ PN532Full.BAUD_106_A = 0x00;
506
+ /** 212 kbps FeliCa. */
507
+ PN532Full.BAUD_212_F = 0x01;
508
+ /** 424 kbps FeliCa. */
509
+ PN532Full.BAUD_424_F = 0x02;
510
+ /** 106 kbps ISO/IEC 14443 B. */
511
+ PN532Full.BAUD_106_B = 0x03;
512
+
513
+ module.exports = { PN532Minimal, PN532Full, PN532Error, _frame };