periph 1.4.0 → 1.5.0
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 +27 -0
- package/examples/adc_dac/ad5251/complete.js +66 -0
- package/examples/adc_dac/ad5251/demo.js +69 -0
- package/examples/adc_dac/ad5251/minimal.js +23 -0
- package/examples/adc_dac/ad5252/complete.js +66 -0
- package/examples/adc_dac/ad5252/demo.js +69 -0
- package/examples/adc_dac/ad5252/minimal.js +23 -0
- package/examples/adc_dac/ads7828/complete.js +46 -0
- package/examples/adc_dac/ads7828/demo.js +45 -0
- package/examples/adc_dac/ads7828/minimal.js +17 -0
- package/examples/adc_dac/cat5171/complete.js +52 -0
- package/examples/adc_dac/cat5171/demo.js +59 -0
- package/examples/adc_dac/cat5171/minimal.js +23 -0
- package/examples/adc_dac/cs43l22/complete.js +88 -0
- package/examples/adc_dac/cs43l22/demo.js +56 -0
- package/examples/adc_dac/cs43l22/minimal.js +20 -0
- package/examples/adc_dac/ds1881/complete.js +62 -0
- package/examples/adc_dac/ds1881/demo.js +61 -0
- package/examples/adc_dac/ds1881/minimal.js +25 -0
- package/examples/adc_dac/ds3502/complete.js +46 -0
- package/examples/adc_dac/ds3502/demo.js +54 -0
- package/examples/adc_dac/ds3502/minimal.js +28 -0
- package/examples/adc_dac/mcp3422/complete.js +48 -0
- package/examples/adc_dac/mcp3422/demo.js +49 -0
- package/examples/adc_dac/mcp3422/minimal.js +25 -0
- package/examples/display/sh1106/complete.js +101 -0
- package/examples/display/sh1106/demo.js +68 -0
- package/examples/display/sh1106/minimal.js +25 -0
- package/examples/display/ssd1305/complete.js +129 -0
- package/examples/display/ssd1305/demo.js +71 -0
- package/examples/display/ssd1305/minimal.js +25 -0
- package/examples/display/ssd1306/complete.js +115 -0
- package/examples/display/ssd1306/demo.js +70 -0
- package/examples/display/ssd1306/minimal.js +25 -0
- package/examples/environmental/am2315/complete.js +34 -0
- package/examples/environmental/am2315/demo.js +46 -0
- package/examples/environmental/am2315/minimal.js +21 -0
- package/examples/humidity/htu21df/complete.js +49 -0
- package/examples/humidity/htu21df/demo.js +50 -0
- package/examples/humidity/htu21df/minimal.js +22 -0
- package/examples/humidity/sht31/complete.js +53 -0
- package/examples/humidity/sht31/demo.js +53 -0
- package/examples/humidity/sht31/minimal.js +21 -0
- package/examples/humidity/si7021/complete.js +55 -0
- package/examples/humidity/si7021/demo.js +58 -0
- package/examples/humidity/si7021/minimal.js +21 -0
- package/examples/imu/bno055/complete.js +85 -0
- package/examples/imu/bno055/demo.js +70 -0
- package/examples/imu/bno055/minimal.js +20 -0
- package/examples/io_expander/pca6408a/complete.js +63 -0
- package/examples/io_expander/pca6408a/demo.js +58 -0
- package/examples/io_expander/pca6408a/minimal.js +20 -0
- package/examples/io_expander/pca6416a/complete.js +68 -0
- package/examples/io_expander/pca6416a/demo.js +60 -0
- package/examples/io_expander/pca6416a/minimal.js +20 -0
- package/examples/io_expander/pca9536/complete.js +63 -0
- package/examples/io_expander/pca9536/demo.js +57 -0
- package/examples/io_expander/pca9536/minimal.js +20 -0
- package/examples/io_expander/pca9539/complete.js +68 -0
- package/examples/io_expander/pca9539/demo.js +60 -0
- package/examples/io_expander/pca9539/minimal.js +20 -0
- package/examples/light/max44009/complete.js +56 -0
- package/examples/light/max44009/demo.js +49 -0
- package/examples/light/max44009/minimal.js +22 -0
- package/examples/memory/at24c02n/complete.js +39 -0
- package/examples/memory/at24c02n/demo.js +39 -0
- package/examples/memory/at24c02n/minimal.js +15 -0
- package/examples/memory/at24c64/complete.js +39 -0
- package/examples/memory/at24c64/demo.js +39 -0
- package/examples/memory/at24c64/minimal.js +15 -0
- package/examples/memory/cat24c512/complete.js +39 -0
- package/examples/memory/cat24c512/demo.js +39 -0
- package/examples/memory/cat24c512/minimal.js +15 -0
- package/examples/other/pca9541/complete.js +52 -0
- package/examples/other/pca9541/demo.js +61 -0
- package/examples/other/pca9541/minimal.js +23 -0
- package/examples/temperature/tmp102/complete.js +74 -0
- package/examples/temperature/tmp102/demo.js +71 -0
- package/examples/temperature/tmp102/minimal.js +20 -0
- package/examples/tof/vl6180x/complete.js +62 -0
- package/examples/tof/vl6180x/demo.js +77 -0
- package/examples/tof/vl6180x/minimal.js +23 -0
- package/package.json +1 -1
- package/src/chips/adc_dac/ad5251.js +345 -0
- package/src/chips/adc_dac/ad5252.js +345 -0
- package/src/chips/adc_dac/ads7828.js +225 -0
- package/src/chips/adc_dac/cat5171.js +213 -0
- package/src/chips/adc_dac/cs43l22.js +645 -0
- package/src/chips/adc_dac/ds1881.js +357 -0
- package/src/chips/adc_dac/ds3502.js +241 -0
- package/src/chips/adc_dac/mcp3422.js +312 -0
- package/src/chips/display/mono_oled.js +182 -0
- package/src/chips/display/sh1106.js +496 -0
- package/src/chips/display/ssd1305.js +655 -0
- package/src/chips/display/ssd1306.js +458 -0
- package/src/chips/environmental/am2315.js +237 -0
- package/src/chips/humidity/htu21df.js +222 -0
- package/src/chips/humidity/sht31.js +284 -0
- package/src/chips/humidity/si7021.js +282 -0
- package/src/chips/imu/bno055.js +496 -0
- package/src/chips/io_expander/pca6408a.js +407 -0
- package/src/chips/io_expander/pca6416a.js +455 -0
- package/src/chips/io_expander/pca9536.js +398 -0
- package/src/chips/io_expander/pca9539.js +457 -0
- package/src/chips/light/max44009.js +336 -0
- package/src/chips/memory/at24c02n.js +220 -0
- package/src/chips/memory/at24c64.js +220 -0
- package/src/chips/memory/cat24c512.js +221 -0
- package/src/chips/other/pca9541.js +315 -0
- package/src/chips/temperature/tmp102.js +361 -0
- package/src/chips/tof/vl6180x.js +580 -0
- package/src/discovery/registry.js +65 -13
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _CMD_RH_HOLD = 0xE5;
|
|
4
|
+
const _CMD_RH_POLL = 0xF5;
|
|
5
|
+
const _CMD_T_HOLD = 0xE3;
|
|
6
|
+
const _CMD_T_POLL = 0xF3;
|
|
7
|
+
const _CMD_T_PREVIOUS = 0xE0;
|
|
8
|
+
const _CMD_RESET = 0xFE;
|
|
9
|
+
const _CMD_WRITE_USER = 0xE6;
|
|
10
|
+
const _CMD_READ_USER = 0xE7;
|
|
11
|
+
const _POLL_ATTEMPTS = 50;
|
|
12
|
+
|
|
13
|
+
const _RESERVED = 0x3A;
|
|
14
|
+
const _HTRE = 0x04;
|
|
15
|
+
const _VDDS = 0x40;
|
|
16
|
+
|
|
17
|
+
// [rhBits, temperatureBits] indexed by RES1:RES0
|
|
18
|
+
const _RESOLUTIONS = [[12, 14], [8, 12], [10, 13], [11, 11]];
|
|
19
|
+
// typical conversion times in seconds, indexed by RES1:RES0
|
|
20
|
+
const _RH_TIME = [0.010, 0.0026, 0.0037, 0.0058];
|
|
21
|
+
const _T_TIME = [0.007, 0.0024, 0.004, 0.0015];
|
|
22
|
+
|
|
23
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* CRC-8 used by the Si7021: polynomial 0x31, init 0x00, no reflection, no final XOR.
|
|
27
|
+
* @param {Buffer|number[]} data
|
|
28
|
+
* @returns {number}
|
|
29
|
+
*/
|
|
30
|
+
function crc8(data) {
|
|
31
|
+
let crc = 0x00;
|
|
32
|
+
for (const byte of data) {
|
|
33
|
+
crc ^= byte;
|
|
34
|
+
for (let i = 0; i < 8; i++) crc = (crc & 0x80) ? ((crc << 1) ^ 0x31) & 0xFF : (crc << 1) & 0xFF;
|
|
35
|
+
}
|
|
36
|
+
return crc;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function _toHumidity(code) {
|
|
40
|
+
return Math.min(100.0, Math.max(0.0, 125.0 * code / 65536.0 - 6.0));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function _toTemperature(code) {
|
|
44
|
+
return 175.72 * code / 65536.0 - 46.85;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Si7021 humidity and temperature sensor — minimal interface.
|
|
49
|
+
*
|
|
50
|
+
* Each `read()` runs one relative-humidity measurement without hold master (the
|
|
51
|
+
* read header is polled until the chip ACKs, timeout 50 ms), verifies the CRC and
|
|
52
|
+
* then fetches the temperature of that same measurement with command 0xE0.
|
|
53
|
+
*
|
|
54
|
+
* Default configuration (power-on, nothing is written at construction):
|
|
55
|
+
* - 12-bit RH / 14-bit temperature, heater off
|
|
56
|
+
* - No-hold-master measurement (0xF5), polling the NACKed read header
|
|
57
|
+
* - CRC-8 verified on the RH result; relative humidity clamped to 0-100 %RH
|
|
58
|
+
*/
|
|
59
|
+
class Si7021Minimal {
|
|
60
|
+
/**
|
|
61
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x40).
|
|
62
|
+
*/
|
|
63
|
+
constructor(connection) {
|
|
64
|
+
this._conn = connection;
|
|
65
|
+
this._stretch = false;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async _command(code) {
|
|
69
|
+
await this._conn.write(Buffer.from([code]));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async _measure(holdCmd, pollCmd) {
|
|
73
|
+
let data = null;
|
|
74
|
+
if (this._stretch) {
|
|
75
|
+
await this._command(holdCmd);
|
|
76
|
+
data = Buffer.from(await this._conn.read(3));
|
|
77
|
+
} else {
|
|
78
|
+
await this._command(pollCmd);
|
|
79
|
+
for (let i = 0; i < _POLL_ATTEMPTS && data === null; i++) {
|
|
80
|
+
try {
|
|
81
|
+
data = Buffer.from(await this._conn.read(3));
|
|
82
|
+
} catch (e) {
|
|
83
|
+
await _sleep(1); // read header NACKed: not ready yet
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
if (data === null) throw new Error('Si7021 measurement did not complete');
|
|
87
|
+
}
|
|
88
|
+
if (crc8(data.subarray(0, 2)) !== data[2]) throw new Error('Si7021 CRC mismatch');
|
|
89
|
+
return (data[0] << 8) | data[1];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async _previousCode() {
|
|
93
|
+
await this._command(_CMD_T_PREVIOUS);
|
|
94
|
+
const data = Buffer.from(await this._conn.read(2));
|
|
95
|
+
return (data[0] << 8) | data[1];
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Run one RH measurement and read the temperature of the same conversion.
|
|
100
|
+
* @returns {Promise<[number, number]>} [temperature °C, relative humidity %RH (0-100)]
|
|
101
|
+
*/
|
|
102
|
+
async read() {
|
|
103
|
+
const rh = await this._measure(_CMD_RH_HOLD, _CMD_RH_POLL);
|
|
104
|
+
return [_toTemperature(await this._previousCode()), _toHumidity(rh)];
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Si7021 full interface — resolution, heater, hold master, ID and dew point.
|
|
110
|
+
*
|
|
111
|
+
* Extends Si7021Minimal with single values and raw codes, the four resolution pairs,
|
|
112
|
+
* the on-chip heater, the raw user register, clock stretching (hold master), software
|
|
113
|
+
* reset, electronic serial number, firmware revision, conversion time and the dew point.
|
|
114
|
+
*/
|
|
115
|
+
class Si7021Full extends Si7021Minimal {
|
|
116
|
+
/**
|
|
117
|
+
* Run one RH measurement and return the raw 16-bit code (status bits included).
|
|
118
|
+
* @returns {Promise<number>}
|
|
119
|
+
*/
|
|
120
|
+
async readRawHumidity() {
|
|
121
|
+
return this._measure(_CMD_RH_HOLD, _CMD_RH_POLL);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Run one temperature measurement and return the raw 16-bit code.
|
|
126
|
+
* @returns {Promise<number>}
|
|
127
|
+
*/
|
|
128
|
+
async readRawTemperature() {
|
|
129
|
+
return this._measure(_CMD_T_HOLD, _CMD_T_POLL);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Run one RH measurement.
|
|
134
|
+
* @returns {Promise<number>} Relative humidity in %RH, clamped to 0-100.
|
|
135
|
+
*/
|
|
136
|
+
async readHumidity() {
|
|
137
|
+
return _toHumidity(await this.readRawHumidity());
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Run a fresh temperature measurement (0xF3, or 0xE3 with clock stretching).
|
|
142
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
143
|
+
*/
|
|
144
|
+
async readTemperature() {
|
|
145
|
+
return _toTemperature(await this.readRawTemperature());
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Read the temperature of the last RH measurement (0xE0, no new conversion, no CRC).
|
|
150
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
151
|
+
*/
|
|
152
|
+
async readTemperaturePrevious() {
|
|
153
|
+
return _toTemperature(await this._previousCode());
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Read USER_REG_1 (command 0xE7).
|
|
158
|
+
* @returns {Promise<number>} Raw register value (0x3A after reset).
|
|
159
|
+
*/
|
|
160
|
+
async readUserRegister() {
|
|
161
|
+
return (await this._conn.writeRead(Buffer.from([_CMD_READ_USER]), 1))[0];
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Write USER_REG_1 (command 0xE6); the reserved bits and VDDS keep their current value.
|
|
166
|
+
* @param {number} value
|
|
167
|
+
*/
|
|
168
|
+
async writeUserRegister(value) {
|
|
169
|
+
const keep = _RESERVED | _VDDS;
|
|
170
|
+
const merged = ((await this.readUserRegister()) & keep) | (value & ~keep & 0xFF);
|
|
171
|
+
await this._conn.write(Buffer.from([_CMD_WRITE_USER, merged]));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Select the measurement resolution (read-modify-write of RES1:RES0).
|
|
176
|
+
* @param {number} rhBits - RH resolution in bits.
|
|
177
|
+
* @param {number} temperatureBits - Temperature resolution in bits; allowed pairs are
|
|
178
|
+
* (12, 14), (8, 12), (10, 13) and (11, 11).
|
|
179
|
+
* @throws {Error} If the pair is not one of the four allowed ones.
|
|
180
|
+
*/
|
|
181
|
+
async setResolution(rhBits, temperatureBits) {
|
|
182
|
+
const res = _RESOLUTIONS.findIndex(([r, t]) => r === rhBits && t === temperatureBits);
|
|
183
|
+
if (res < 0) throw new Error('resolution must be (12, 14), (8, 12), (10, 13) or (11, 11)');
|
|
184
|
+
const bits = ((res >> 1) << 7) | (res & 1);
|
|
185
|
+
await this.writeUserRegister(((await this.readUserRegister()) & ~0x81 & 0xFF) | bits);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Return the configured resolution.
|
|
190
|
+
* @returns {Promise<[number, number]>} [rhBits, temperatureBits]
|
|
191
|
+
*/
|
|
192
|
+
async getResolution() {
|
|
193
|
+
const reg = await this.readUserRegister();
|
|
194
|
+
return _RESOLUTIONS[(((reg >> 7) & 1) << 1) | (reg & 1)];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Switch the on-chip heater (HTRE, about 3.1 mA) on or off.
|
|
199
|
+
* @param {boolean} on
|
|
200
|
+
*/
|
|
201
|
+
async setHeater(on) {
|
|
202
|
+
const reg = await this.readUserRegister();
|
|
203
|
+
await this.writeUserRegister(on ? (reg | _HTRE) : (reg & ~_HTRE & 0xFF));
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* @returns {Promise<boolean>} True if the on-chip heater is enabled.
|
|
208
|
+
*/
|
|
209
|
+
async isHeaterOn() {
|
|
210
|
+
return ((await this.readUserRegister()) & _HTRE) !== 0;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* @returns {Promise<boolean>} True if the supply voltage is low (VDDS set, VDD below about 1.9 V).
|
|
215
|
+
*/
|
|
216
|
+
async vddLow() {
|
|
217
|
+
return ((await this.readUserRegister()) & _VDDS) !== 0;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Use the hold-master commands (0xE5 / 0xE3) instead of polling; the bus layer must support clock stretching.
|
|
222
|
+
* @param {boolean} on
|
|
223
|
+
*/
|
|
224
|
+
setClockStretching(on) {
|
|
225
|
+
this._stretch = Boolean(on);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/** Software reset (0xFE) and wait 15 ms; the user register returns to 0x3A. */
|
|
229
|
+
async softReset() {
|
|
230
|
+
await this._command(_CMD_RESET);
|
|
231
|
+
await _sleep(15);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Read the 64-bit electronic serial number (SNA_3..SNA_0, SNB_3..SNB_0).
|
|
236
|
+
* @returns {Promise<bigint>}
|
|
237
|
+
*/
|
|
238
|
+
async serialNumber() {
|
|
239
|
+
const a = Buffer.from(await this._conn.writeRead(Buffer.from([0xFA, 0x0F]), 8));
|
|
240
|
+
const b = Buffer.from(await this._conn.writeRead(Buffer.from([0xFC, 0xC9]), 6));
|
|
241
|
+
return Buffer.from([a[0], a[2], a[4], a[6], b[0], b[1], b[3], b[4]]).readBigUInt64BE(0);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Read SNB_3, the device type byte.
|
|
246
|
+
* @returns {Promise<number>} 0x15 for the Si7021 (0x14 Si7020, 0x0D Si7013).
|
|
247
|
+
*/
|
|
248
|
+
async deviceType() {
|
|
249
|
+
return (await this._conn.writeRead(Buffer.from([0xFC, 0xC9]), 6))[0];
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Read the firmware revision (command 0x84 0xB8).
|
|
254
|
+
* @returns {Promise<number>} 0xFF for firmware 1.0.
|
|
255
|
+
*/
|
|
256
|
+
async firmwareRevision() {
|
|
257
|
+
return (await this._conn.writeRead(Buffer.from([0x84, 0xB8]), 1))[0];
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Typical RH + temperature conversion time at the current resolution.
|
|
262
|
+
* @returns {Promise<number>} Time in seconds.
|
|
263
|
+
*/
|
|
264
|
+
async conversionTime() {
|
|
265
|
+
const reg = await this.readUserRegister();
|
|
266
|
+
const res = (((reg >> 7) & 1) << 1) | (reg & 1);
|
|
267
|
+
return _RH_TIME[res] + _T_TIME[res];
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Run one measurement and compute the dew point (Magnus formula).
|
|
272
|
+
* @returns {Promise<number>} Dew point in °C.
|
|
273
|
+
*/
|
|
274
|
+
async dewPoint() {
|
|
275
|
+
let [t, rh] = await this.read();
|
|
276
|
+
if (rh <= 0.0) rh = 0.01;
|
|
277
|
+
const gamma = Math.log(rh / 100.0) + (17.62 * t) / (243.12 + t);
|
|
278
|
+
return 243.12 * gamma / (17.62 - gamma);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
module.exports = { Si7021Minimal, Si7021Full, crc8 };
|