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.
- package/README.md +17 -0
- package/examples/gas/ccs811/complete.js +87 -0
- package/examples/gas/ccs811/demo.js +108 -0
- package/examples/gas/ccs811/minimal.js +20 -0
- package/examples/gas/rrh46410/complete.js +78 -0
- package/examples/gas/rrh46410/demo.js +86 -0
- package/examples/gas/rrh46410/minimal.js +21 -0
- package/examples/gas/rrh62000/complete.js +74 -0
- package/examples/gas/rrh62000/demo.js +87 -0
- package/examples/gas/rrh62000/minimal.js +22 -0
- package/examples/gas/scd30/complete.js +73 -0
- package/examples/gas/scd30/demo.js +102 -0
- package/examples/gas/scd30/minimal.js +20 -0
- package/examples/gas/scd40/complete.js +76 -0
- package/examples/gas/scd40/demo.js +109 -0
- package/examples/gas/scd40/minimal.js +20 -0
- package/examples/light/si1133/complete.js +89 -0
- package/examples/light/si1133/demo.js +93 -0
- package/examples/light/si1133/minimal.js +22 -0
- package/examples/light/si1145/complete.js +93 -0
- package/examples/light/si1145/demo.js +98 -0
- package/examples/light/si1145/minimal.js +22 -0
- package/examples/light/tsl2561/complete.js +74 -0
- package/examples/light/tsl2561/demo.js +46 -0
- package/examples/light/tsl2561/minimal.js +22 -0
- package/examples/light/tsl2591/complete.js +90 -0
- package/examples/light/tsl2591/demo.js +119 -0
- package/examples/light/tsl2591/minimal.js +22 -0
- package/examples/light/vcnl4040/complete.js +102 -0
- package/examples/light/vcnl4040/demo.js +125 -0
- package/examples/light/vcnl4040/minimal.js +25 -0
- package/examples/light/vcnl4200/complete.js +101 -0
- package/examples/light/vcnl4200/demo.js +126 -0
- package/examples/light/vcnl4200/minimal.js +25 -0
- package/examples/pressure/spl06/complete.js +81 -0
- package/examples/pressure/spl06/demo.js +106 -0
- package/examples/pressure/spl06/minimal.js +22 -0
- package/examples/rfid/pn532/complete.js +99 -0
- package/examples/rfid/pn532/demo.js +121 -0
- package/examples/rfid/pn532/minimal.js +22 -0
- package/examples/rfid/st25dv/complete.js +94 -0
- package/examples/rfid/st25dv/demo.js +121 -0
- package/examples/rfid/st25dv/minimal.js +21 -0
- package/examples/rtc/bq32000/complete.js +53 -0
- package/examples/rtc/bq32000/demo.js +77 -0
- package/examples/rtc/bq32000/minimal.js +20 -0
- package/examples/rtc/ds1307/complete.js +54 -0
- package/examples/rtc/ds1307/demo.js +96 -0
- package/examples/rtc/ds1307/minimal.js +20 -0
- package/examples/rtc/ds1371/complete.js +53 -0
- package/examples/rtc/ds1371/demo.js +85 -0
- package/examples/rtc/ds1371/minimal.js +25 -0
- package/package.json +1 -1
- package/src/chips/gas/ccs811.js +400 -0
- package/src/chips/gas/rrh46410.js +534 -0
- package/src/chips/gas/rrh62000.js +418 -0
- package/src/chips/gas/scd30.js +368 -0
- package/src/chips/gas/scd40.js +451 -0
- package/src/chips/light/si1133.js +583 -0
- package/src/chips/light/si1145.js +630 -0
- package/src/chips/light/tsl2561.js +445 -0
- package/src/chips/light/tsl2591.js +561 -0
- package/src/chips/light/vcnl4040.js +576 -0
- package/src/chips/light/vcnl4200.js +592 -0
- package/src/chips/pressure/spl06.js +461 -0
- package/src/chips/rfid/pn532.js +513 -0
- package/src/chips/rfid/st25dv.js +628 -0
- package/src/chips/rtc/bq32000.js +334 -0
- package/src/chips/rtc/ds1307.js +327 -0
- package/src/chips/rtc/ds1371.js +453 -0
- package/src/connection/i2c.js +46 -13
- package/src/connection/smbus.js +21 -8
- package/src/discovery/registry.js +17 -17
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
* SCD40 photoacoustic CO₂, temperature and humidity sensor (Sensirion).
|
|
5
|
+
*
|
|
6
|
+
* Command protocol on I²C (fixed address 0x62): 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 after the command execution time as 16-bit words, each followed by a CRC-8
|
|
9
|
+
* (polynomial 0x31, init 0xFF). There is no register file, so the driver uses a plain
|
|
10
|
+
* Connection. Keep the bus at 100 kHz or below.
|
|
11
|
+
*
|
|
12
|
+
* Settings commands are only accepted in idle mode; the Full driver stops (500 ms, the pending
|
|
13
|
+
* sample is lost) and restarts a running measurement around every idle-only command. Settings
|
|
14
|
+
* live in RAM until persistSettings() stores them in the EEPROM.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const CMD_START_PERIODIC = 0x21B1;
|
|
18
|
+
const CMD_READ_MEASUREMENT = 0xEC05;
|
|
19
|
+
const CMD_STOP_PERIODIC = 0x3F86;
|
|
20
|
+
const CMD_SET_TEMPERATURE_OFFSET = 0x241D;
|
|
21
|
+
const CMD_GET_TEMPERATURE_OFFSET = 0x2318;
|
|
22
|
+
const CMD_SET_ALTITUDE = 0x2427;
|
|
23
|
+
const CMD_GET_ALTITUDE = 0x2322;
|
|
24
|
+
const CMD_SET_AMBIENT_PRESSURE = 0xE000;
|
|
25
|
+
const CMD_FORCED_RECALIBRATION = 0x362F;
|
|
26
|
+
const CMD_SET_ASC = 0x2416;
|
|
27
|
+
const CMD_GET_ASC = 0x2313;
|
|
28
|
+
const CMD_START_LOW_POWER_PERIODIC = 0x21AC;
|
|
29
|
+
const CMD_DATA_READY = 0xE4B8;
|
|
30
|
+
const CMD_PERSIST_SETTINGS = 0x3615;
|
|
31
|
+
const CMD_SERIAL_NUMBER = 0x3682;
|
|
32
|
+
const CMD_SELF_TEST = 0x3639;
|
|
33
|
+
const CMD_FACTORY_RESET = 0x3632;
|
|
34
|
+
const CMD_REINIT = 0x3646;
|
|
35
|
+
|
|
36
|
+
const MODE_IDLE = 0;
|
|
37
|
+
const MODE_PERIODIC = 1;
|
|
38
|
+
const MODE_LOW_POWER = 2;
|
|
39
|
+
|
|
40
|
+
const CMD_MS = 1;
|
|
41
|
+
const STOP_MS = 500;
|
|
42
|
+
const FRC_MS = 400;
|
|
43
|
+
const PERSIST_MS = 800;
|
|
44
|
+
const SELF_TEST_MS = 10000;
|
|
45
|
+
const FACTORY_RESET_MS = 1200;
|
|
46
|
+
const REINIT_MS = 20;
|
|
47
|
+
const POLL_MS = 100;
|
|
48
|
+
const TIMEOUT_MS = 6000;
|
|
49
|
+
const TIMEOUT_LOW_POWER_MS = 35000;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Sensirion CRC-8 (polynomial 0x31, init 0xFF, no reflection, no final XOR).
|
|
53
|
+
* @param {Buffer|number[]} data - Bytes to check (normally one 16-bit word).
|
|
54
|
+
* @returns {number} The CRC (`crc8([0xBE, 0xEF]) === 0x92`).
|
|
55
|
+
*/
|
|
56
|
+
function crc8(data) {
|
|
57
|
+
let crc = 0xFF;
|
|
58
|
+
for (const b of data) {
|
|
59
|
+
crc ^= b;
|
|
60
|
+
for (let i = 0; i < 8; i++) crc = crc & 0x80 ? ((crc << 1) ^ 0x31) & 0xFF : (crc << 1) & 0xFF;
|
|
61
|
+
}
|
|
62
|
+
return crc;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** SCD40 error: CRC mismatch, short read, no new measurement within the timeout, or failed FRC. */
|
|
66
|
+
class SCD40Error extends Error {
|
|
67
|
+
constructor(message) {
|
|
68
|
+
super(message);
|
|
69
|
+
this.name = 'SCD40Error';
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const isInt = (v, lo, hi) => Number.isInteger(v) && v >= lo && v <= hi;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* SCD40 CO₂ sensor — minimal interface.
|
|
77
|
+
*
|
|
78
|
+
* `init()` stops a measurement left running (500 ms), reads the serial number (presence check)
|
|
79
|
+
* and starts the standard periodic measurement (a sample every 5 s); `read()` waits for the
|
|
80
|
+
* next sample. Chip defaults (not rewritten): temperature offset 4 °C, altitude 0 m, ASC on.
|
|
81
|
+
* The constructor does not touch the bus.
|
|
82
|
+
*
|
|
83
|
+
* @param {import('../../connection/base').Connection} connection - Plain I²C Connection bound to 0x62.
|
|
84
|
+
*/
|
|
85
|
+
class SCD40Minimal {
|
|
86
|
+
constructor(connection) {
|
|
87
|
+
this._connection = connection;
|
|
88
|
+
this._mode = MODE_IDLE;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Wait `ms` milliseconds (overridable in tests). */
|
|
92
|
+
_delay(ms) {
|
|
93
|
+
return ms > 0 ? new Promise((resolve) => setTimeout(resolve, ms)) : Promise.resolve();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Monotonic milliseconds (overridable in tests). */
|
|
97
|
+
_now() {
|
|
98
|
+
return Date.now();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
_frame(command, argument) {
|
|
102
|
+
const b = [(command >> 8) & 0xFF, command & 0xFF];
|
|
103
|
+
if (argument !== undefined) {
|
|
104
|
+
const a = [(argument >> 8) & 0xFF, argument & 0xFF];
|
|
105
|
+
b.push(...a, crc8(a));
|
|
106
|
+
}
|
|
107
|
+
return Buffer.from(b);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async _command(command, argument) {
|
|
111
|
+
await this._connection.write(this._frame(command, argument));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async _readResponse(count) {
|
|
115
|
+
const d = Buffer.from(await this._connection.read(3 * count));
|
|
116
|
+
if (d.length !== 3 * count) throw new SCD40Error('SCD40 short read');
|
|
117
|
+
const words = [];
|
|
118
|
+
for (let i = 0; i < count; i++) {
|
|
119
|
+
if (crc8(d.subarray(3 * i, 3 * i + 2)) !== d[3 * i + 2]) throw new SCD40Error('SCD40 CRC mismatch');
|
|
120
|
+
words.push((d[3 * i] << 8) | d[3 * i + 1]);
|
|
121
|
+
}
|
|
122
|
+
return words;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async _readWords(command, count, delayMs = CMD_MS) {
|
|
126
|
+
await this._command(command);
|
|
127
|
+
await this._delay(delayMs);
|
|
128
|
+
return this._readResponse(count);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
async _start(mode) {
|
|
132
|
+
await this._command(mode === MODE_LOW_POWER ? CMD_START_LOW_POWER_PERIODIC : CMD_START_PERIODIC);
|
|
133
|
+
this._mode = mode;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
async _stop() {
|
|
137
|
+
await this._command(CMD_STOP_PERIODIC);
|
|
138
|
+
await this._delay(STOP_MS);
|
|
139
|
+
this._mode = MODE_IDLE;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
async _dataReady() {
|
|
143
|
+
return ((await this._readWords(CMD_DATA_READY, 1))[0] & 0x07FF) !== 0;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async _readMeasurement() {
|
|
147
|
+
const w = await this._readWords(CMD_READ_MEASUREMENT, 3);
|
|
148
|
+
return [w[0], -45 + (175 * w[1]) / 65536, (100 * w[2]) / 65536];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Stop a running measurement (500 ms), read the serial number and start the standard
|
|
153
|
+
* periodic measurement.
|
|
154
|
+
* @returns {Promise<void>}
|
|
155
|
+
* @throws {SCD40Error} On a CRC mismatch (bus errors reject from the connection).
|
|
156
|
+
*/
|
|
157
|
+
async init() {
|
|
158
|
+
this._mode = MODE_IDLE;
|
|
159
|
+
await this._command(CMD_STOP_PERIODIC);
|
|
160
|
+
await this._delay(STOP_MS);
|
|
161
|
+
await this._readWords(CMD_SERIAL_NUMBER, 3);
|
|
162
|
+
await this._start(MODE_PERIODIC);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Wait for the next sample and return CO₂, temperature and humidity.
|
|
167
|
+
*
|
|
168
|
+
* Polls the data-ready status every 100 ms for up to 6 s (35 s in low-power periodic
|
|
169
|
+
* mode), reads the 9-byte measurement and verifies all three CRCs.
|
|
170
|
+
* @returns {Promise<{co2_ppm: number, temperature_c: number, humidity_rh: number}>} CO₂ in
|
|
171
|
+
* ppm, °C, %RH.
|
|
172
|
+
* @throws {SCD40Error} On a timeout or a CRC mismatch (no partial values).
|
|
173
|
+
*/
|
|
174
|
+
async read() {
|
|
175
|
+
const start = this._now();
|
|
176
|
+
const timeout = this._mode === MODE_LOW_POWER ? TIMEOUT_LOW_POWER_MS : TIMEOUT_MS;
|
|
177
|
+
while (!(await this._dataReady())) {
|
|
178
|
+
if (this._now() - start >= timeout) throw new SCD40Error(`SCD40 no new measurement within ${timeout} ms`);
|
|
179
|
+
await this._delay(POLL_MS);
|
|
180
|
+
}
|
|
181
|
+
const [co2, t, rh] = await this._readMeasurement();
|
|
182
|
+
return { co2_ppm: co2, temperature_c: t, humidity_rh: rh };
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* SCD40 full interface — extends SCD40Minimal with single values, the data-ready status,
|
|
188
|
+
* standard and low-power periodic modes, temperature offset, altitude and ambient pressure
|
|
189
|
+
* compensation, automatic self-calibration, forced recalibration, EEPROM persistence, serial
|
|
190
|
+
* number, self test, factory reset, reinit and raw command access.
|
|
191
|
+
*
|
|
192
|
+
* Idle-only commands (every setter and getter except setAmbientPressure(), plus FRC, persist,
|
|
193
|
+
* serial number, self test, factory reset and reinit) stop a running measurement first
|
|
194
|
+
* (500 ms, the pending sample is lost) and restart it in the same mode afterwards.
|
|
195
|
+
*
|
|
196
|
+
* @param {import('../../connection/base').Connection} connection - Plain I²C Connection bound to 0x62.
|
|
197
|
+
*/
|
|
198
|
+
class SCD40Full extends SCD40Minimal {
|
|
199
|
+
async _idle(fn) {
|
|
200
|
+
const mode = this._mode;
|
|
201
|
+
if (mode !== MODE_IDLE) await this._stop();
|
|
202
|
+
try {
|
|
203
|
+
return await fn();
|
|
204
|
+
} finally {
|
|
205
|
+
if (mode !== MODE_IDLE) await this._start(mode);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
_writeIdle(command, value, delayMs = CMD_MS) {
|
|
210
|
+
return this._idle(async () => {
|
|
211
|
+
await this._command(command, value);
|
|
212
|
+
await this._delay(delayMs);
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
_readIdle(command, count = 1, delayMs = CMD_MS) {
|
|
217
|
+
return this._idle(() => this._readWords(command, count, delayMs));
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Wait for a fresh sample and return the CO₂ concentration.
|
|
222
|
+
* @returns {Promise<number>} CO₂ in ppm.
|
|
223
|
+
*/
|
|
224
|
+
async readCo2() {
|
|
225
|
+
return (await this.read()).co2_ppm;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Wait for a fresh sample and return the temperature (offset applied).
|
|
230
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
231
|
+
*/
|
|
232
|
+
async readTemperature() {
|
|
233
|
+
return (await this.read()).temperature_c;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Wait for a fresh sample and return the relative humidity.
|
|
238
|
+
* @returns {Promise<number>} Relative humidity in %RH.
|
|
239
|
+
*/
|
|
240
|
+
async readHumidity() {
|
|
241
|
+
return (await this.read()).humidity_rh;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Data-ready status (0xE4B8): ready when the 11 low bits are not all 0.
|
|
246
|
+
* @returns {Promise<boolean>} True when a new measurement can be read.
|
|
247
|
+
*/
|
|
248
|
+
dataReady() {
|
|
249
|
+
return this._dataReady();
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Read the measurement without waiting (0xEC05); call after dataReady(). Each sample can be
|
|
254
|
+
* read only once.
|
|
255
|
+
* @returns {Promise<number[]>} `[co2_ppm, temperature_c, humidity_rh]`.
|
|
256
|
+
* @throws {SCD40Error} On a CRC mismatch.
|
|
257
|
+
*/
|
|
258
|
+
readMeasurement() {
|
|
259
|
+
return this._readMeasurement();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Start the standard periodic measurement (0x21B1, a sample every 5 s); a running
|
|
264
|
+
* measurement is stopped first (500 ms).
|
|
265
|
+
* @returns {Promise<void>}
|
|
266
|
+
*/
|
|
267
|
+
async startPeriodic() {
|
|
268
|
+
if (this._mode !== MODE_IDLE) await this._stop();
|
|
269
|
+
await this._start(MODE_PERIODIC);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Start the low-power periodic measurement (0x21AC, about every 30 s); a running
|
|
274
|
+
* measurement is stopped first (500 ms).
|
|
275
|
+
* @returns {Promise<void>}
|
|
276
|
+
*/
|
|
277
|
+
async startLowPowerPeriodic() {
|
|
278
|
+
if (this._mode !== MODE_IDLE) await this._stop();
|
|
279
|
+
await this._start(MODE_LOW_POWER);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Stop the periodic measurement (0x3F86) and wait 500 ms.
|
|
284
|
+
* @returns {Promise<void>}
|
|
285
|
+
*/
|
|
286
|
+
stopPeriodic() {
|
|
287
|
+
return this._stop();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Set the temperature offset (0x241D, idle only); only temperature and humidity are
|
|
292
|
+
* affected. Sent as round(kelvin × 65536 / 175); the chip default is 4 K.
|
|
293
|
+
* @param {number} kelvin - Offset in K, 0 to about 175.
|
|
294
|
+
* @returns {Promise<void>}
|
|
295
|
+
* @throws {RangeError} If the offset does not fit the 16-bit word.
|
|
296
|
+
*/
|
|
297
|
+
async setTemperatureOffset(kelvin) {
|
|
298
|
+
const value = kelvin >= 0 ? Math.floor((kelvin * 65536) / 175 + 0.5) : -1;
|
|
299
|
+
if (!isInt(value, 0, 0xFFFF)) throw new RangeError('kelvin must be 0..174.99');
|
|
300
|
+
await this._writeIdle(CMD_SET_TEMPERATURE_OFFSET, value);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Read the temperature offset (0x2318, idle only).
|
|
305
|
+
* @returns {Promise<number>} Offset in K.
|
|
306
|
+
*/
|
|
307
|
+
async getTemperatureOffset() {
|
|
308
|
+
return (175 * (await this._readIdle(CMD_GET_TEMPERATURE_OFFSET))[0]) / 65536;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Set the sensor altitude (0x2427, idle only).
|
|
313
|
+
* @param {number} metres - Altitude above sea level, 0–65535 m.
|
|
314
|
+
* @returns {Promise<void>}
|
|
315
|
+
* @throws {RangeError} If metres is out of range.
|
|
316
|
+
*/
|
|
317
|
+
async setAltitude(metres) {
|
|
318
|
+
if (!isInt(metres, 0, 0xFFFF)) throw new RangeError('metres must be 0..65535');
|
|
319
|
+
await this._writeIdle(CMD_SET_ALTITUDE, metres);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Read the sensor altitude (0x2322, idle only).
|
|
324
|
+
* @returns {Promise<number>} Altitude in m.
|
|
325
|
+
*/
|
|
326
|
+
async getAltitude() {
|
|
327
|
+
return (await this._readIdle(CMD_GET_ALTITUDE))[0];
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Set the ambient pressure (0xE000, allowed during measurement); overrides the altitude
|
|
332
|
+
* compensation until the next power cycle or reinit(). Sent as round(pressurePa / 100) hPa.
|
|
333
|
+
* @param {number} pressurePa - Ambient pressure in Pa, 0–6553549.
|
|
334
|
+
* @returns {Promise<void>}
|
|
335
|
+
* @throws {RangeError} If the rounded hPa value does not fit 16 bits.
|
|
336
|
+
*/
|
|
337
|
+
async setAmbientPressure(pressurePa) {
|
|
338
|
+
const value = Number.isFinite(pressurePa) && pressurePa >= 0 ? Math.floor((Math.trunc(pressurePa) + 50) / 100) : -1;
|
|
339
|
+
if (!isInt(value, 0, 0xFFFF)) throw new RangeError('pressurePa must be 0..6553549');
|
|
340
|
+
await this._command(CMD_SET_AMBIENT_PRESSURE, value);
|
|
341
|
+
await this._delay(CMD_MS);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Enable or disable automatic self-calibration (0x2416, idle only).
|
|
346
|
+
* @param {boolean} enabled - True to enable (chip default; needs fresh air once a week).
|
|
347
|
+
* @returns {Promise<void>}
|
|
348
|
+
*/
|
|
349
|
+
async setAutoCalibration(enabled) {
|
|
350
|
+
await this._writeIdle(CMD_SET_ASC, enabled ? 1 : 0);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Read the automatic self-calibration state (0x2313, idle only).
|
|
355
|
+
* @returns {Promise<boolean>} True when ASC is enabled.
|
|
356
|
+
*/
|
|
357
|
+
async getAutoCalibration() {
|
|
358
|
+
return (await this._readIdle(CMD_GET_ASC))[0] !== 0;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Perform a forced recalibration (0x362F, idle only, 400 ms) after > 3 minutes in the
|
|
363
|
+
* target mode at a homogeneous, known concentration.
|
|
364
|
+
* @param {number} ppm - Reference CO₂ concentration in ppm, 0–65535.
|
|
365
|
+
* @returns {Promise<number>} FRC correction in ppm (response word − 0x8000).
|
|
366
|
+
* @throws {RangeError} If ppm is out of range.
|
|
367
|
+
* @throws {SCD40Error} If the chip reports a failed recalibration (0xFFFF).
|
|
368
|
+
*/
|
|
369
|
+
async forceRecalibration(ppm) {
|
|
370
|
+
if (!isInt(ppm, 0, 0xFFFF)) throw new RangeError('ppm must be 0..65535');
|
|
371
|
+
const word = await this._idle(async () => {
|
|
372
|
+
await this._command(CMD_FORCED_RECALIBRATION, ppm);
|
|
373
|
+
await this._delay(FRC_MS);
|
|
374
|
+
return (await this._readResponse(1))[0];
|
|
375
|
+
});
|
|
376
|
+
if (word === 0xFFFF) throw new SCD40Error('SCD40 forced recalibration failed');
|
|
377
|
+
return word - 0x8000;
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Store offset, altitude and ASC in the EEPROM (0x3615, idle only, 800 ms); the EEPROM
|
|
382
|
+
* endures at least 2000 write cycles.
|
|
383
|
+
* @returns {Promise<void>}
|
|
384
|
+
*/
|
|
385
|
+
persistSettings() {
|
|
386
|
+
return this._writeIdle(CMD_PERSIST_SETTINGS, undefined, PERSIST_MS);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Read the 48-bit serial number (0x3682, idle only).
|
|
391
|
+
* @returns {Promise<number>} `word0 << 32 | word1 << 16 | word2` (exact in a Number).
|
|
392
|
+
*/
|
|
393
|
+
async serialNumber() {
|
|
394
|
+
const w = await this._readIdle(CMD_SERIAL_NUMBER, 3);
|
|
395
|
+
return w[0] * 2 ** 32 + w[1] * 2 ** 16 + w[2];
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Run the self test (0x3639, idle only, blocks 10 s).
|
|
400
|
+
* @returns {Promise<boolean>} True when no malfunction was detected.
|
|
401
|
+
*/
|
|
402
|
+
async selfTest() {
|
|
403
|
+
return (await this._readIdle(CMD_SELF_TEST, 1, SELF_TEST_MS))[0] === 0;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Reset the EEPROM settings and the FRC/ASC history (0x3632, idle only, 1.2 s).
|
|
408
|
+
* @returns {Promise<void>}
|
|
409
|
+
*/
|
|
410
|
+
factoryReset() {
|
|
411
|
+
return this._writeIdle(CMD_FACTORY_RESET, undefined, FACTORY_RESET_MS);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Reload the settings from the EEPROM (0x3646, idle only, 20 ms).
|
|
416
|
+
* @returns {Promise<void>}
|
|
417
|
+
*/
|
|
418
|
+
reinit() {
|
|
419
|
+
return this._writeIdle(CMD_REINIT, undefined, REINIT_MS);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Raw read: write a 16-bit command, wait 1 ms and read one CRC-checked word. No idle
|
|
424
|
+
* handling.
|
|
425
|
+
* @param {number} command - 16-bit command code.
|
|
426
|
+
* @returns {Promise<number>} The response word.
|
|
427
|
+
* @throws {RangeError} If command is not 0–0xFFFF.
|
|
428
|
+
* @throws {SCD40Error} On a CRC mismatch.
|
|
429
|
+
*/
|
|
430
|
+
async readWord(command) {
|
|
431
|
+
if (!isInt(command, 0, 0xFFFF)) throw new RangeError('command must be 0..0xFFFF');
|
|
432
|
+
return (await this._readWords(command, 1))[0];
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Raw write: a 16-bit command with a 16-bit argument and its CRC. No idle handling.
|
|
437
|
+
* @param {number} command - 16-bit command code.
|
|
438
|
+
* @param {number} value - 16-bit argument.
|
|
439
|
+
* @returns {Promise<void>}
|
|
440
|
+
* @throws {RangeError} If command or value is not 0–0xFFFF.
|
|
441
|
+
*/
|
|
442
|
+
async writeWord(command, value) {
|
|
443
|
+
if (!isInt(command, 0, 0xFFFF) || !isInt(value, 0, 0xFFFF)) {
|
|
444
|
+
throw new RangeError('command and value must be 0..0xFFFF');
|
|
445
|
+
}
|
|
446
|
+
await this._command(command, value);
|
|
447
|
+
await this._delay(CMD_MS);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
module.exports = { SCD40Minimal, SCD40Full, SCD40Error, crc8 };
|