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,368 @@
1
+ 'use strict';
2
+
3
+ /*
4
+ * SCD30 NDIR CO₂, temperature and humidity sensor module (Sensirion).
5
+ *
6
+ * Command protocol on I²C (fixed address 0x61): a 16-bit command is written MSB first,
7
+ * optionally followed by a 16-bit argument and its CRC-8; a response is read in a separate
8
+ * transaction at least 3 ms later as 16-bit words, each followed by a CRC-8 (polynomial 0x31,
9
+ * init 0xFF). There is no register file, so the driver uses a plain Connection. The module
10
+ * stretches SCL for up to about 150 ms; the I²C master must support clock stretching (lower
11
+ * the bus to 50 kHz otherwise). Settings written by the setters are stored in non-volatile
12
+ * memory and never rewritten by init().
13
+ */
14
+
15
+ const CMD_START = 0x0010;
16
+ const CMD_STOP = 0x0104;
17
+ const CMD_INTERVAL = 0x4600;
18
+ const CMD_DATA_READY = 0x0202;
19
+ const CMD_READ_MEASUREMENT = 0x0300;
20
+ const CMD_ASC = 0x5306;
21
+ const CMD_FRC = 0x5204;
22
+ const CMD_TEMPERATURE_OFFSET = 0x5403;
23
+ const CMD_ALTITUDE = 0x5102;
24
+ const CMD_FIRMWARE = 0xD100;
25
+ const CMD_SOFT_RESET = 0xD304;
26
+
27
+ const _sleep = (ms) => (ms > 0 ? new Promise((resolve) => setTimeout(resolve, ms)) : Promise.resolve());
28
+
29
+ /**
30
+ * Sensirion CRC-8 (polynomial 0x31, init 0xFF, no reflection, no final XOR).
31
+ * @param {Buffer|number[]} data - Bytes to check (normally one 16-bit word).
32
+ * @returns {number} The CRC (`crc8([0xBE, 0xEF]) === 0x92`).
33
+ */
34
+ function crc8(data) {
35
+ let crc = 0xFF;
36
+ for (const b of data) {
37
+ crc ^= b;
38
+ for (let i = 0; i < 8; i++) crc = crc & 0x80 ? ((crc << 1) ^ 0x31) & 0xFF : (crc << 1) & 0xFF;
39
+ }
40
+ return crc;
41
+ }
42
+
43
+ /** SCD30 data error: CRC mismatch, short read or no new measurement within the timeout. */
44
+ class SCD30Error extends Error {
45
+ constructor(message) {
46
+ super(message);
47
+ this.name = 'SCD30Error';
48
+ }
49
+ }
50
+
51
+ const isInt = (v, lo, hi) => Number.isInteger(v) && v >= lo && v <= hi;
52
+
53
+ /**
54
+ * SCD30 CO₂ sensor module — minimal interface.
55
+ *
56
+ * `init()` reads the firmware version (presence check) and the stored measurement interval,
57
+ * then starts the continuous measurement without pressure compensation; `read()` waits for
58
+ * the next sample. Module defaults: interval 2 s, ASC off, no altitude or pressure
59
+ * compensation, temperature offset 0. The constructor does not touch the bus.
60
+ *
61
+ * @param {import('../../connection/base').Connection} connection - Plain I²C Connection bound
62
+ * to 0x61 (the master must support clock stretching).
63
+ */
64
+ class SCD30Minimal {
65
+ constructor(connection) {
66
+ this._connection = connection;
67
+ this._interval = 2;
68
+ this._pressurePa = 0;
69
+ this._running = false;
70
+ this._readDelayMs = 3;
71
+ this._pollMs = 100;
72
+ this._marginMs = 1000;
73
+ this._resetMs = 2000;
74
+ }
75
+
76
+ _frame(command, argument) {
77
+ const b = [(command >> 8) & 0xFF, command & 0xFF];
78
+ if (argument !== undefined) {
79
+ const a = [(argument >> 8) & 0xFF, argument & 0xFF];
80
+ b.push(...a, crc8(a));
81
+ }
82
+ return Buffer.from(b);
83
+ }
84
+
85
+ async _command(command, argument) {
86
+ await this._connection.write(this._frame(command, argument));
87
+ }
88
+
89
+ async _readWords(command, count) {
90
+ await this._command(command);
91
+ await _sleep(this._readDelayMs);
92
+ const d = Buffer.from(await this._connection.read(3 * count));
93
+ if (d.length !== 3 * count) throw new SCD30Error('SCD30 short read');
94
+ const words = [];
95
+ for (let i = 0; i < count; i++) {
96
+ if (crc8(d.subarray(3 * i, 3 * i + 2)) !== d[3 * i + 2]) throw new SCD30Error('SCD30 CRC mismatch');
97
+ words.push((d[3 * i] << 8) | d[3 * i + 1]);
98
+ }
99
+ return words;
100
+ }
101
+
102
+ async _start(pressurePa) {
103
+ await this._command(CMD_START, pressurePa ? Math.floor((pressurePa + 50) / 100) : 0);
104
+ this._pressurePa = pressurePa;
105
+ this._running = true;
106
+ }
107
+
108
+ async _dataReady() {
109
+ return (await this._readWords(CMD_DATA_READY, 1))[0] === 1;
110
+ }
111
+
112
+ async _readMeasurement() {
113
+ const w = await this._readWords(CMD_READ_MEASUREMENT, 6);
114
+ const b = Buffer.alloc(12);
115
+ w.forEach((v, i) => b.writeUInt16BE(v, 2 * i));
116
+ return [b.readFloatBE(0), b.readFloatBE(4), b.readFloatBE(8)];
117
+ }
118
+
119
+ /**
120
+ * Read the firmware version and the stored interval, then start the continuous measurement.
121
+ * @returns {Promise<void>}
122
+ * @throws {SCD30Error} On a CRC mismatch (bus errors reject from the connection).
123
+ */
124
+ async init() {
125
+ await this._readWords(CMD_FIRMWARE, 1);
126
+ const interval = (await this._readWords(CMD_INTERVAL, 1))[0];
127
+ this._interval = interval >= 2 && interval <= 1800 ? interval : 2;
128
+ await this._start(0);
129
+ }
130
+
131
+ /**
132
+ * Wait for the next sample and return CO₂, temperature and humidity.
133
+ *
134
+ * Polls the data-ready status every 100 ms for up to the measurement interval plus 1 s,
135
+ * reads the 18-byte measurement and verifies all six CRCs.
136
+ * @returns {Promise<{co2_ppm: number, temperature_c: number, humidity_rh: number}>} CO₂ in
137
+ * ppm, °C, %RH.
138
+ * @throws {SCD30Error} On a timeout or a CRC mismatch (no partial values).
139
+ */
140
+ async read() {
141
+ const start = Date.now();
142
+ const timeout = this._interval * 1000 + this._marginMs;
143
+ while (!(await this._dataReady())) {
144
+ if (Date.now() - start >= timeout) throw new SCD30Error(`SCD30 no new measurement within ${timeout} ms`);
145
+ await _sleep(this._pollMs);
146
+ }
147
+ const [co2, t, rh] = await this._readMeasurement();
148
+ return { co2_ppm: co2, temperature_c: t, humidity_rh: rh };
149
+ }
150
+ }
151
+
152
+ /**
153
+ * SCD30 full interface — extends SCD30Minimal with single values, the data-ready status,
154
+ * start/stop with ambient-pressure compensation, the measurement interval, automatic
155
+ * self-calibration, forced recalibration, the temperature offset, altitude compensation, the
156
+ * firmware version, soft reset and raw command access.
157
+ *
158
+ * @param {import('../../connection/base').Connection} connection - Plain I²C Connection bound to 0x61.
159
+ */
160
+ class SCD30Full extends SCD30Minimal {
161
+ /**
162
+ * Wait for a fresh sample and return the CO₂ concentration.
163
+ * @returns {Promise<number>} CO₂ in ppm.
164
+ */
165
+ async readCo2() {
166
+ return (await this.read()).co2_ppm;
167
+ }
168
+
169
+ /**
170
+ * Wait for a fresh sample and return the temperature (offset applied).
171
+ * @returns {Promise<number>} Temperature in °C.
172
+ */
173
+ async readTemperature() {
174
+ return (await this.read()).temperature_c;
175
+ }
176
+
177
+ /**
178
+ * Wait for a fresh sample and return the relative humidity.
179
+ * @returns {Promise<number>} Relative humidity in %RH.
180
+ */
181
+ async readHumidity() {
182
+ return (await this.read()).humidity_rh;
183
+ }
184
+
185
+ /**
186
+ * Data-ready status (0x0202).
187
+ * @returns {Promise<boolean>} True when a new measurement can be read.
188
+ */
189
+ async dataReady() {
190
+ return this._dataReady();
191
+ }
192
+
193
+ /**
194
+ * Read the current measurement without waiting (0x0300); call after `dataReady()`.
195
+ * @returns {Promise<number[]>} `[co2_ppm, temperature_c, humidity_rh]`.
196
+ * @throws {SCD30Error} On a CRC mismatch.
197
+ */
198
+ async readMeasurement() {
199
+ return this._readMeasurement();
200
+ }
201
+
202
+ /**
203
+ * Start the continuous measurement (0x0010).
204
+ * @param {number} [pressurePa=0] - Ambient pressure in Pa (70000–140000) for pressure
205
+ * compensation, or 0 to disable it; sent as mbar rounded to the nearest 100 Pa.
206
+ * @returns {Promise<void>}
207
+ * @throws {RangeError} For an out-of-range pressure (before any bus traffic).
208
+ */
209
+ async start(pressurePa = 0) {
210
+ if (pressurePa !== 0 && !isInt(pressurePa, 70000, 140000)) {
211
+ throw new RangeError('pressurePa must be 0 or 70000..140000');
212
+ }
213
+ await this._start(pressurePa);
214
+ }
215
+
216
+ /**
217
+ * Stop the continuous measurement (0x0104).
218
+ * @returns {Promise<void>}
219
+ */
220
+ async stop() {
221
+ await this._command(CMD_STOP);
222
+ this._running = false;
223
+ }
224
+
225
+ /**
226
+ * Set the measurement interval (0x4600, stored in non-volatile memory).
227
+ * @param {number} seconds - Interval, 2–1800 s.
228
+ * @returns {Promise<void>}
229
+ * @throws {RangeError} If out of range.
230
+ */
231
+ async setInterval(seconds) {
232
+ if (!isInt(seconds, 2, 1800)) throw new RangeError('seconds must be 2..1800');
233
+ await this._command(CMD_INTERVAL, seconds);
234
+ this._interval = seconds;
235
+ }
236
+
237
+ /**
238
+ * Read the measurement interval.
239
+ * @returns {Promise<number>} Interval in s.
240
+ */
241
+ async getInterval() {
242
+ return (await this._readWords(CMD_INTERVAL, 1))[0];
243
+ }
244
+
245
+ /**
246
+ * Enable or disable automatic self-calibration (0x5306); ASC needs fresh air weekly.
247
+ * @param {boolean} enabled - True to enable.
248
+ * @returns {Promise<void>}
249
+ */
250
+ async setAutoCalibration(enabled) {
251
+ await this._command(CMD_ASC, enabled ? 1 : 0);
252
+ }
253
+
254
+ /**
255
+ * Read the automatic self-calibration state.
256
+ * @returns {Promise<boolean>} True when ASC is active.
257
+ */
258
+ async getAutoCalibration() {
259
+ return (await this._readWords(CMD_ASC, 1))[0] === 1;
260
+ }
261
+
262
+ /**
263
+ * Apply a forced recalibration (0x5204) after ≥ 2 minutes at the reference concentration.
264
+ * @param {number} ppm - Reference CO₂ concentration, 400–2000 ppm.
265
+ * @returns {Promise<void>}
266
+ * @throws {RangeError} If out of range.
267
+ */
268
+ async setForcedCalibration(ppm) {
269
+ if (!isInt(ppm, 400, 2000)) throw new RangeError('ppm must be 400..2000');
270
+ await this._command(CMD_FRC, ppm);
271
+ }
272
+
273
+ /**
274
+ * Read the forced recalibration reference value.
275
+ * @returns {Promise<number>} Reference CO₂ in ppm (default 400).
276
+ */
277
+ async getForcedCalibration() {
278
+ return (await this._readWords(CMD_FRC, 1))[0];
279
+ }
280
+
281
+ /**
282
+ * Set the temperature offset for self-heating (0x5403, 0.01 K units).
283
+ * @param {number} kelvin - Offset 0–655.35 K, rounded to 0.01 K; a positive offset lowers
284
+ * the reported temperature.
285
+ * @returns {Promise<void>}
286
+ * @throws {RangeError} If out of range.
287
+ */
288
+ async setTemperatureOffset(kelvin) {
289
+ const value = Math.floor(kelvin * 100 + 0.5);
290
+ if (!(kelvin >= 0) || !isInt(value, 0, 0xFFFF)) throw new RangeError('kelvin must be 0..655.35');
291
+ await this._command(CMD_TEMPERATURE_OFFSET, value);
292
+ }
293
+
294
+ /**
295
+ * Read the temperature offset.
296
+ * @returns {Promise<number>} Offset in K.
297
+ */
298
+ async getTemperatureOffset() {
299
+ return (await this._readWords(CMD_TEMPERATURE_OFFSET, 1))[0] / 100;
300
+ }
301
+
302
+ /**
303
+ * Set the altitude compensation (0x5102); overridden by a non-zero start pressure.
304
+ * @param {number} metres - Altitude above sea level, 0–65535 m.
305
+ * @returns {Promise<void>}
306
+ * @throws {RangeError} If out of range.
307
+ */
308
+ async setAltitude(metres) {
309
+ if (!isInt(metres, 0, 0xFFFF)) throw new RangeError('metres must be 0..65535');
310
+ await this._command(CMD_ALTITUDE, metres);
311
+ }
312
+
313
+ /**
314
+ * Read the altitude compensation.
315
+ * @returns {Promise<number>} Altitude in m.
316
+ */
317
+ async getAltitude() {
318
+ return (await this._readWords(CMD_ALTITUDE, 1))[0];
319
+ }
320
+
321
+ /**
322
+ * Read the firmware version (0xD100).
323
+ * @returns {Promise<number[]>} `[major, minor]`.
324
+ */
325
+ async firmwareVersion() {
326
+ const w = (await this._readWords(CMD_FIRMWARE, 1))[0];
327
+ return [w >> 8, w & 0xFF];
328
+ }
329
+
330
+ /**
331
+ * Restart the module (0xD304), wait 2 s and restart a running measurement with the same
332
+ * ambient pressure. Stored settings survive.
333
+ * @returns {Promise<void>}
334
+ */
335
+ async softReset() {
336
+ await this._command(CMD_SOFT_RESET);
337
+ await _sleep(this._resetMs);
338
+ if (this._running) await this._start(this._pressurePa);
339
+ }
340
+
341
+ /**
342
+ * Raw read: write a 16-bit command, wait 3 ms and read one CRC-checked word.
343
+ * @param {number} command - 16-bit command code.
344
+ * @returns {Promise<number>} The response word.
345
+ * @throws {RangeError} If command is not 0–0xFFFF.
346
+ * @throws {SCD30Error} On a CRC mismatch.
347
+ */
348
+ async readWord(command) {
349
+ if (!isInt(command, 0, 0xFFFF)) throw new RangeError('command must be 0..0xFFFF');
350
+ return (await this._readWords(command, 1))[0];
351
+ }
352
+
353
+ /**
354
+ * Raw write: a 16-bit command with a 16-bit argument and its CRC.
355
+ * @param {number} command - 16-bit command code.
356
+ * @param {number} value - 16-bit argument.
357
+ * @returns {Promise<void>}
358
+ * @throws {RangeError} If command or value is not 0–0xFFFF.
359
+ */
360
+ async writeWord(command, value) {
361
+ if (!isInt(command, 0, 0xFFFF) || !isInt(value, 0, 0xFFFF)) {
362
+ throw new RangeError('command and value must be 0..0xFFFF');
363
+ }
364
+ await this._command(command, value);
365
+ }
366
+ }
367
+
368
+ module.exports = { SCD30Minimal, SCD30Full, SCD30Error, crc8 };