periph 1.6.0 → 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 (47) hide show
  1. package/README.md +11 -0
  2. package/examples/gas/rrh62000/complete.js +74 -0
  3. package/examples/gas/rrh62000/demo.js +87 -0
  4. package/examples/gas/rrh62000/minimal.js +22 -0
  5. package/examples/gas/scd30/complete.js +73 -0
  6. package/examples/gas/scd30/demo.js +102 -0
  7. package/examples/gas/scd30/minimal.js +20 -0
  8. package/examples/gas/scd40/complete.js +76 -0
  9. package/examples/gas/scd40/demo.js +109 -0
  10. package/examples/gas/scd40/minimal.js +20 -0
  11. package/examples/light/tsl2561/complete.js +74 -0
  12. package/examples/light/tsl2561/demo.js +46 -0
  13. package/examples/light/tsl2561/minimal.js +22 -0
  14. package/examples/light/tsl2591/complete.js +90 -0
  15. package/examples/light/tsl2591/demo.js +119 -0
  16. package/examples/light/tsl2591/minimal.js +22 -0
  17. package/examples/light/vcnl4040/complete.js +102 -0
  18. package/examples/light/vcnl4040/demo.js +125 -0
  19. package/examples/light/vcnl4040/minimal.js +25 -0
  20. package/examples/light/vcnl4200/complete.js +101 -0
  21. package/examples/light/vcnl4200/demo.js +126 -0
  22. package/examples/light/vcnl4200/minimal.js +25 -0
  23. package/examples/rfid/st25dv/complete.js +94 -0
  24. package/examples/rfid/st25dv/demo.js +121 -0
  25. package/examples/rfid/st25dv/minimal.js +21 -0
  26. package/examples/rtc/bq32000/complete.js +53 -0
  27. package/examples/rtc/bq32000/demo.js +77 -0
  28. package/examples/rtc/bq32000/minimal.js +20 -0
  29. package/examples/rtc/ds1307/complete.js +54 -0
  30. package/examples/rtc/ds1307/demo.js +96 -0
  31. package/examples/rtc/ds1307/minimal.js +20 -0
  32. package/examples/rtc/ds1371/complete.js +53 -0
  33. package/examples/rtc/ds1371/demo.js +85 -0
  34. package/examples/rtc/ds1371/minimal.js +25 -0
  35. package/package.json +1 -1
  36. package/src/chips/gas/rrh62000.js +418 -0
  37. package/src/chips/gas/scd30.js +368 -0
  38. package/src/chips/gas/scd40.js +451 -0
  39. package/src/chips/light/tsl2561.js +445 -0
  40. package/src/chips/light/tsl2591.js +561 -0
  41. package/src/chips/light/vcnl4040.js +576 -0
  42. package/src/chips/light/vcnl4200.js +592 -0
  43. package/src/chips/rfid/st25dv.js +628 -0
  44. package/src/chips/rtc/bq32000.js +334 -0
  45. package/src/chips/rtc/ds1307.js +327 -0
  46. package/src/chips/rtc/ds1371.js +453 -0
  47. package/src/discovery/registry.js +11 -11
@@ -0,0 +1,418 @@
1
+ 'use strict';
2
+
3
+ /*
4
+ * RRH62000 all-in-one air-quality module (Renesas).
5
+ *
6
+ * Laser particulate-matter sensor, HS40xx temperature/humidity sensor and ZMOD4410 TVOC/IAQ
7
+ * sensor behind a fan. Over I²C (address 0x69, SEL pin to GND) the module is register
8
+ * addressed: a one-byte command code is the register pointer; reading 0x00 returns the
9
+ * 37-byte measurement block with a CRC-8. The module must not be accessed more often than
10
+ * once per second (tRD); the driver rate-limits itself.
11
+ */
12
+
13
+ const CMD_READ = 0x00;
14
+ const CMD_DATA = 0x40;
15
+ const CMD_SLEEP = 0x50;
16
+ const CMD_CLEAN = 0x51;
17
+ const CMD_RESET = 0x52;
18
+ const CMD_MAVE = 0x53;
19
+ const CMD_TINTC_H = 0x5A;
20
+ const CMD_TINTC_L = 0x5B;
21
+ const CMD_TCLEAN = 0x5C;
22
+ const CMD_SPEEDFAN = 0x63;
23
+ const CMD_RMOX = 0x71;
24
+ const CMD_UID = 0x72;
25
+ const CMD_ARGVER = 0x73;
26
+ const CMD_CSTATUS = 0x74;
27
+ const CMD_FWVER = 0x75;
28
+
29
+ const BLOCK_LENGTH = 37;
30
+ const _STARTUP_MS = 1000;
31
+ const _VALID_MS = 3300;
32
+ const _DATA_TIMEOUT_MS = 4000;
33
+
34
+ const _sleep = (ms) => (ms > 0 ? new Promise((resolve) => setTimeout(resolve, ms)) : Promise.resolve());
35
+ const _now = () => Date.now();
36
+
37
+ /**
38
+ * CRC-8 of the measurement block (polynomial 0x31, init 0xFF, no reflection, no final XOR).
39
+ * @param {Buffer|number[]} data - Bytes to check (bytes 0–35 of the block).
40
+ * @returns {number} The CRC (`crc8([0xBE, 0xEF]) === 0x92`).
41
+ */
42
+ function crc8(data) {
43
+ let crc = 0xFF;
44
+ for (const b of data) {
45
+ crc ^= b;
46
+ for (let i = 0; i < 8; i++) crc = crc & 0x80 ? ((crc << 1) ^ 0x31) & 0xFF : (crc << 1) & 0xFF;
47
+ }
48
+ return crc;
49
+ }
50
+
51
+ /** RRH62000 data error: CRC mismatch, invalid (start-up) sample or timeout. */
52
+ class RRH62000Error extends Error {
53
+ constructor(message) {
54
+ super(message);
55
+ this.name = 'RRH62000Error';
56
+ }
57
+ }
58
+
59
+ /**
60
+ * RRH62000 air-quality module (Renesas) — minimal interface.
61
+ *
62
+ * One call returns PM1/PM2.5/PM10 (KCl reference), temperature, humidity, TVOC, estimated CO₂
63
+ * and the UBA IAQ index. The module defaults (10 moving averages, fan 86 %, cleaning every
64
+ * 7 days for 10 s) are kept. The start-up time (1 s for I²C, 3.3 s for valid data) is
65
+ * counted from construction, `wake()` and `softReset()`. The constructor does not touch the bus.
66
+ *
67
+ * @param {import('../../connection/register_connection').RegisterConnection} connection -
68
+ * RegisterConnection (1-byte register addresses) bound to 0x69.
69
+ * @param {{accessIntervalMs?: number}} [options] - `accessIntervalMs`: minimum spacing between
70
+ * two accesses (the module needs ≥ 1000 ms; lower it only for mocks).
71
+ */
72
+ class RRH62000Minimal {
73
+ constructor(connection, { accessIntervalMs = 1000 } = {}) {
74
+ this._connection = connection;
75
+ this._interval = accessIntervalMs;
76
+ this._lastAccess = 0;
77
+ this._validAt = 0;
78
+ this._dataTimeoutMs = _DATA_TIMEOUT_MS;
79
+ this._markStartup();
80
+ }
81
+
82
+ _markStartup() {
83
+ const now = _now();
84
+ const scale = this._interval / 1000;
85
+ this._lastAccess = now + Math.round(_STARTUP_MS * scale) - this._interval;
86
+ this._validAt = now + Math.round(_VALID_MS * scale);
87
+ }
88
+
89
+ async _waitAccess() {
90
+ await _sleep(this._interval - (_now() - this._lastAccess));
91
+ }
92
+
93
+ async _read(cmd, length) {
94
+ await this._waitAccess();
95
+ try {
96
+ return Buffer.from(await this._connection.readReg(cmd, length));
97
+ } finally {
98
+ this._lastAccess = _now();
99
+ }
100
+ }
101
+
102
+ async _write(cmd, value) {
103
+ await this._waitAccess();
104
+ try {
105
+ await this._connection.writeReg(cmd, Buffer.from([value & 0xFF]));
106
+ } finally {
107
+ this._lastAccess = _now();
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Wait for the start-up time (1 s from construction) and read the firmware version as a
113
+ * presence check.
114
+ * @returns {Promise<void>}
115
+ */
116
+ async init() {
117
+ await this._read(CMD_FWVER, 2);
118
+ }
119
+
120
+ /**
121
+ * Wait for a new sample (DATA polled once per second, up to 4 s) and return the main values.
122
+ * @returns {Promise<{pm1_ug_m3: number, pm2_5_ug_m3: number, pm10_ug_m3: number,
123
+ * temperature_c: number, humidity_rh: number, tvoc: number, eco2_ppm: number, iaq: number}>}
124
+ * PM in µg/m³ (KCl reference), °C, %RH, TVOC in µg/m³, eCO₂ in ppm, IAQ 1.00–5.00.
125
+ * @throws {RRH62000Error} On a timeout, a CRC mismatch or a start-up (all 0xFF) block.
126
+ */
127
+ async read() {
128
+ const m = await this._readBlock(true);
129
+ return {
130
+ pm1_ug_m3: m.pm1_ug_m3,
131
+ pm2_5_ug_m3: m.pm2_5_ug_m3,
132
+ pm10_ug_m3: m.pm10_ug_m3,
133
+ temperature_c: m.temperature_c,
134
+ humidity_rh: m.humidity_rh,
135
+ tvoc: m.tvoc,
136
+ eco2_ppm: m.eco2_ppm,
137
+ iaq: m.iaq,
138
+ };
139
+ }
140
+
141
+ async _waitData() {
142
+ const start = _now();
143
+ for (;;) {
144
+ if ((await this._read(CMD_DATA, 1))[0] === 0x01) return;
145
+ if (_now() - start >= this._dataTimeoutMs) throw new RRH62000Error('RRH62000 no new data within 4 s');
146
+ }
147
+ }
148
+
149
+ async _readBlock(wait) {
150
+ await _sleep(this._validAt - _now());
151
+ if (wait) await this._waitData();
152
+ return RRH62000Minimal.decode(await this._read(CMD_READ, BLOCK_LENGTH));
153
+ }
154
+
155
+ /**
156
+ * Decode a 37-byte measurement block.
157
+ * @param {Buffer|number[]} block - The raw block read from 0x00.
158
+ * @returns {object} All values (see {@link RRH62000Full#readAll}).
159
+ * @throws {RRH62000Error} On a wrong length, an all-0xFF block or a CRC mismatch.
160
+ */
161
+ static decode(block) {
162
+ const b = Buffer.from(block);
163
+ if (b.length !== BLOCK_LENGTH) throw new RRH62000Error('RRH62000 block must be 37 bytes');
164
+ if (b.subarray(0, 36).every((x) => x === 0xFF)) throw new RRH62000Error('RRH62000 sample invalid (start-up)');
165
+ if (crc8(b.subarray(0, 36)) !== b[36]) throw new RRH62000Error('RRH62000 CRC error');
166
+ const w = (i) => b.readUInt16BE(i);
167
+ const status = b[0] | (b[1] << 8);
168
+ return {
169
+ status,
170
+ high_concentration: Boolean(status & 0x01),
171
+ dust_accumulation: Boolean(status & 0x02),
172
+ fan_speed: Boolean(status & 0x04),
173
+ fan: Boolean(status & 0x08),
174
+ nc_0_3: w(2) / 10,
175
+ nc_0_5: w(4) / 10,
176
+ nc_1: w(6) / 10,
177
+ nc_2_5: w(8) / 10,
178
+ nc_4: w(10) / 10,
179
+ pm1_ug_m3: w(12) / 10,
180
+ pm2_5_ug_m3: w(14) / 10,
181
+ pm10_ug_m3: w(16) / 10,
182
+ pm1_smoke_ug_m3: w(18) / 10,
183
+ pm2_5_smoke_ug_m3: w(20) / 10,
184
+ pm10_smoke_ug_m3: w(22) / 10,
185
+ temperature_c: b.readInt16BE(24) / 100,
186
+ humidity_rh: w(26) / 100,
187
+ tvoc: w(28) * 10,
188
+ eco2_ppm: w(30),
189
+ iaq: w(32) / 100,
190
+ };
191
+ }
192
+ }
193
+
194
+ /**
195
+ * RRH62000 full interface — extends RRH62000Minimal with the complete block, status flags,
196
+ * particle bins, sleep, fan cleaning, reset, settings (moving average, fan speed, cleaning
197
+ * interval and time), MOX resistance and identification.
198
+ *
199
+ * @param {import('../../connection/register_connection').RegisterConnection} connection -
200
+ * RegisterConnection bound to 0x69.
201
+ * @param {{accessIntervalMs?: number}} [options] - Minimum spacing between two accesses.
202
+ */
203
+ class RRH62000Full extends RRH62000Minimal {
204
+ /**
205
+ * Wait for a new sample and return the complete decoded block.
206
+ * @returns {Promise<object>} `status` (raw word), `high_concentration`, `dust_accumulation`,
207
+ * `fan_speed`, `fan` (flags), `nc_0_3`, `nc_0_5`, `nc_1`, `nc_2_5`, `nc_4` (cm⁻³),
208
+ * `pm1_ug_m3`, `pm2_5_ug_m3`, `pm10_ug_m3` (KCl), `pm1_smoke_ug_m3`, `pm2_5_smoke_ug_m3`,
209
+ * `pm10_smoke_ug_m3` (cigarette smoke), `temperature_c`, `humidity_rh`, `tvoc` (µg/m³),
210
+ * `eco2_ppm`, `iaq`.
211
+ * @throws {RRH62000Error} On a timeout, a CRC mismatch or a start-up block.
212
+ */
213
+ async readAll() {
214
+ return this._readBlock(true);
215
+ }
216
+
217
+ /**
218
+ * Read the current block (without waiting for new data) and decode its status word.
219
+ * @returns {Promise<{raw: number, high_concentration: boolean, dust_accumulation: boolean,
220
+ * fan_speed: boolean, fan: boolean}>}
221
+ */
222
+ async readStatus() {
223
+ const m = await this._readBlock(false);
224
+ return {
225
+ raw: m.status,
226
+ high_concentration: m.high_concentration,
227
+ dust_accumulation: m.dust_accumulation,
228
+ fan_speed: m.fan_speed,
229
+ fan: m.fan,
230
+ };
231
+ }
232
+
233
+ /**
234
+ * Read the current block (without waiting for new data) and return the particle values.
235
+ * @returns {Promise<object>} The six PM values (µg/m³, KCl and smoke) and the five
236
+ * number-concentration bins (cm⁻³).
237
+ */
238
+ async readParticles() {
239
+ const m = await this._readBlock(false);
240
+ const out = {};
241
+ for (const k of ['pm1_ug_m3', 'pm2_5_ug_m3', 'pm10_ug_m3', 'pm1_smoke_ug_m3', 'pm2_5_smoke_ug_m3',
242
+ 'pm10_smoke_ug_m3', 'nc_0_3', 'nc_0_5', 'nc_1', 'nc_2_5', 'nc_4']) out[k] = m[k];
243
+ return out;
244
+ }
245
+
246
+ /**
247
+ * Check the DATA flag (0x40).
248
+ * @returns {Promise<boolean>} True when new data has arrived.
249
+ */
250
+ async dataReady() {
251
+ return (await this._read(CMD_DATA, 1))[0] === 0x01;
252
+ }
253
+
254
+ /**
255
+ * Put the module to sleep (0x50 ← 0x00).
256
+ * @returns {Promise<void>}
257
+ */
258
+ async sleep() {
259
+ await this._write(CMD_SLEEP, 0x00);
260
+ }
261
+
262
+ /**
263
+ * Wake the module up (0x50 ← 0x80) and restart the start-up timers.
264
+ * @returns {Promise<void>}
265
+ */
266
+ async wake() {
267
+ await this._write(CMD_SLEEP, 0x80);
268
+ this._markStartup();
269
+ }
270
+
271
+ /**
272
+ * Start the fan dust-cleaning (0x51 ← 0x01).
273
+ * @returns {Promise<void>}
274
+ */
275
+ async startCleaning() {
276
+ await this._write(CMD_CLEAN, 0x01);
277
+ }
278
+
279
+ /**
280
+ * Read the cleaning status (0x74).
281
+ * @returns {Promise<boolean>} True when the cleaning is completed.
282
+ */
283
+ async cleaningDone() {
284
+ return (await this._read(CMD_CSTATUS, 1))[0] === 0x01;
285
+ }
286
+
287
+ /**
288
+ * Reset the module (0x52 ← 0x81, same as power-on) and restart the start-up timers.
289
+ * A missing acknowledge during the reset is ignored.
290
+ * @returns {Promise<void>}
291
+ */
292
+ async softReset() {
293
+ try {
294
+ await this._write(CMD_RESET, 0x81);
295
+ } catch (e) {
296
+ // the module may not acknowledge while resetting
297
+ }
298
+ this._markStartup();
299
+ }
300
+
301
+ /**
302
+ * Set the number of moving averages (0x53).
303
+ * @param {number} count - 1–60 (default 10).
304
+ * @returns {Promise<void>}
305
+ * @throws {RangeError} If count is out of range.
306
+ */
307
+ async setMovingAverage(count) {
308
+ if (!Number.isInteger(count) || count < 1 || count > 60) throw new RangeError('count must be 1..60');
309
+ await this._write(CMD_MAVE, count);
310
+ }
311
+
312
+ /**
313
+ * Read the number of moving averages.
314
+ * @returns {Promise<number>} 1–60.
315
+ */
316
+ async getMovingAverage() {
317
+ return (await this._read(CMD_MAVE, 1))[0];
318
+ }
319
+
320
+ /**
321
+ * Set the fan speed (0x63).
322
+ * @param {number} percent - 60–100 % (default 86).
323
+ * @returns {Promise<void>}
324
+ * @throws {RangeError} If percent is out of range.
325
+ */
326
+ async setFanSpeed(percent) {
327
+ if (!Number.isInteger(percent) || percent < 60 || percent > 100) throw new RangeError('percent must be 60..100');
328
+ await this._write(CMD_SPEEDFAN, percent);
329
+ }
330
+
331
+ /**
332
+ * Read the fan speed.
333
+ * @returns {Promise<number>} Percent (60–100).
334
+ */
335
+ async getFanSpeed() {
336
+ return (await this._read(CMD_SPEEDFAN, 1))[0];
337
+ }
338
+
339
+ /**
340
+ * Set the automatic cleaning interval (0x5A/0x5B, 16-bit count of 30 s units).
341
+ * @param {number} seconds - 0–1 814 400 s, a multiple of 30 (default 604 800 = 7 days).
342
+ * @returns {Promise<void>}
343
+ * @throws {RangeError} If seconds is out of range or not a multiple of 30.
344
+ */
345
+ async setCleaningInterval(seconds) {
346
+ if (!Number.isInteger(seconds) || seconds < 0 || seconds > 1814400 || seconds % 30) {
347
+ throw new RangeError('seconds must be 0..1814400 and a multiple of 30');
348
+ }
349
+ const units = seconds / 30;
350
+ await this._write(CMD_TINTC_H, units >> 8);
351
+ await this._write(CMD_TINTC_L, units & 0xFF);
352
+ }
353
+
354
+ /**
355
+ * Read the automatic cleaning interval.
356
+ * @returns {Promise<number>} Seconds.
357
+ */
358
+ async getCleaningInterval() {
359
+ const hi = (await this._read(CMD_TINTC_H, 1))[0];
360
+ const lo = (await this._read(CMD_TINTC_L, 1))[0];
361
+ return ((hi << 8) | lo) * 30;
362
+ }
363
+
364
+ /**
365
+ * Set the fan auto-cleaning time (0x5C).
366
+ * @param {number} seconds - 0–60 s (default 10).
367
+ * @returns {Promise<void>}
368
+ * @throws {RangeError} If seconds is out of range.
369
+ */
370
+ async setCleaningTime(seconds) {
371
+ if (!Number.isInteger(seconds) || seconds < 0 || seconds > 60) throw new RangeError('seconds must be 0..60');
372
+ await this._write(CMD_TCLEAN, seconds);
373
+ }
374
+
375
+ /**
376
+ * Read the fan auto-cleaning time.
377
+ * @returns {Promise<number>} Seconds (0–60).
378
+ */
379
+ async getCleaningTime() {
380
+ return (await this._read(CMD_TCLEAN, 1))[0];
381
+ }
382
+
383
+ /**
384
+ * Read the MOX sensor resistance (0x71, 4 bytes big-endian).
385
+ * @returns {Promise<number>} Ohms.
386
+ */
387
+ async readMoxResistance() {
388
+ return (await this._read(CMD_RMOX, 4)).readUInt32BE(0);
389
+ }
390
+
391
+ /**
392
+ * Read the unique ID (0x72).
393
+ * @returns {Promise<Buffer>} 6 bytes.
394
+ */
395
+ async uniqueId() {
396
+ return this._read(CMD_UID, 6);
397
+ }
398
+
399
+ /**
400
+ * Read the TVOC algorithm version (0x73).
401
+ * @returns {Promise<number[]>} `[major, minor, patch]`.
402
+ */
403
+ async algorithmVersion() {
404
+ const d = await this._read(CMD_ARGVER, 3);
405
+ return [d[0], d[1], d[2]];
406
+ }
407
+
408
+ /**
409
+ * Read the firmware version (0x75).
410
+ * @returns {Promise<number[]>} `[major, minor]`.
411
+ */
412
+ async firmwareVersion() {
413
+ const d = await this._read(CMD_FWVER, 2);
414
+ return [d[0], d[1]];
415
+ }
416
+ }
417
+
418
+ module.exports = { RRH62000Minimal, RRH62000Full, RRH62000Error, crc8 };