periph 1.5.0 → 1.5.2
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 +19 -0
- package/examples/color/as7262/complete.js +60 -0
- package/examples/color/as7262/demo.js +65 -0
- package/examples/color/as7262/minimal.js +23 -0
- package/examples/environmental/aht10/complete.js +37 -0
- package/examples/environmental/aht10/demo.js +43 -0
- package/examples/environmental/aht10/minimal.js +12 -0
- package/examples/environmental/aht20/complete.js +41 -0
- package/examples/environmental/aht20/demo.js +49 -0
- package/examples/environmental/aht20/minimal.js +12 -0
- package/examples/environmental/am2320/complete.js +34 -0
- package/examples/environmental/am2320/demo.js +46 -0
- package/examples/environmental/am2320/minimal.js +21 -0
- package/examples/humidity/hdc1008/complete.js +39 -0
- package/examples/humidity/hdc1008/demo.js +46 -0
- package/examples/humidity/hdc1008/minimal.js +22 -0
- package/examples/humidity/hdc1080/complete.js +34 -0
- package/examples/humidity/hdc1080/demo.js +46 -0
- package/examples/humidity/hdc1080/minimal.js +22 -0
- package/examples/humidity/hs30xx/complete.js +43 -0
- package/examples/humidity/hs30xx/demo.js +40 -0
- package/examples/humidity/hs30xx/minimal.js +21 -0
- package/examples/humidity/hs40xx/complete.js +54 -0
- package/examples/humidity/hs40xx/demo.js +38 -0
- package/examples/humidity/hs40xx/minimal.js +21 -0
- package/examples/io_expander/tca9554/complete.js +63 -0
- package/examples/io_expander/tca9554/demo.js +58 -0
- package/examples/io_expander/tca9554/minimal.js +20 -0
- package/examples/led/ht16k33/complete.js +45 -0
- package/examples/led/ht16k33/demo.js +54 -0
- package/examples/led/ht16k33/minimal.js +24 -0
- package/examples/led/is31fl3731/complete.js +60 -0
- package/examples/led/is31fl3731/demo.js +72 -0
- package/examples/led/is31fl3731/minimal.js +26 -0
- package/examples/led/pca9685/complete.js +43 -0
- package/examples/led/pca9685/demo.js +77 -0
- package/examples/led/pca9685/minimal.js +24 -0
- package/examples/power/ina260/complete.js +38 -0
- package/examples/power/ina260/demo.js +32 -0
- package/examples/power/ina260/minimal.js +11 -0
- package/examples/power/lc709203f/complete.js +57 -0
- package/examples/power/lc709203f/demo.js +34 -0
- package/examples/power/lc709203f/minimal.js +11 -0
- package/examples/power/lm25066/complete.js +54 -0
- package/examples/power/lm25066/demo.js +41 -0
- package/examples/power/lm25066/minimal.js +12 -0
- package/examples/power/ltc4151/complete.js +40 -0
- package/examples/power/ltc4151/demo.js +49 -0
- package/examples/power/ltc4151/minimal.js +11 -0
- package/examples/power/max17048/complete.js +68 -0
- package/examples/power/max17048/demo.js +56 -0
- package/examples/power/max17048/minimal.js +19 -0
- package/examples/pressure/lps22hb/complete.js +43 -0
- package/examples/pressure/lps22hb/demo.js +43 -0
- package/examples/pressure/lps22hb/minimal.js +19 -0
- package/examples/pressure/mpl115a2/complete.js +25 -0
- package/examples/pressure/mpl115a2/demo.js +30 -0
- package/examples/pressure/mpl115a2/minimal.js +20 -0
- package/package.json +1 -1
- package/src/chips/color/as7262.js +419 -0
- package/src/chips/environmental/aht10.js +186 -0
- package/src/chips/environmental/aht20.js +226 -0
- package/src/chips/environmental/am2320.js +237 -0
- package/src/chips/humidity/hdc1008.js +262 -0
- package/src/chips/humidity/hdc1080.js +208 -0
- package/src/chips/humidity/hs30xx.js +254 -0
- package/src/chips/humidity/hs40xx.js +382 -0
- package/src/chips/io_expander/tca9554.js +407 -0
- package/src/chips/led/ht16k33.js +273 -0
- package/src/chips/led/is31fl3731.js +501 -0
- package/src/chips/led/pca9685.js +350 -0
- package/src/chips/power/ina260.js +229 -0
- package/src/chips/power/lc709203f.js +376 -0
- package/src/chips/power/lm25066.js +370 -0
- package/src/chips/power/ltc4151.js +297 -0
- package/src/chips/power/max17048.js +302 -0
- package/src/chips/pressure/lps22hb.js +257 -0
- package/src/chips/pressure/mpl115a2.js +171 -0
- package/src/discovery/registry.js +56 -14
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_TEMPERATURE = 0x00;
|
|
4
|
+
const _REG_HUMIDITY = 0x01;
|
|
5
|
+
const _REG_CONFIG = 0x02;
|
|
6
|
+
const _REG_SERIAL1 = 0xFB;
|
|
7
|
+
|
|
8
|
+
const _RST = 0x8000;
|
|
9
|
+
const _HEAT = 0x2000;
|
|
10
|
+
const _MODE = 0x1000;
|
|
11
|
+
const _BTST = 0x0800;
|
|
12
|
+
const _TRES = 0x0400;
|
|
13
|
+
const _HRES_MASK = 0x0300;
|
|
14
|
+
const _CONFIG_DEFAULT = 0x1000;
|
|
15
|
+
const _POLL_ATTEMPTS = 50; // x 1 ms = 50 ms timeout
|
|
16
|
+
|
|
17
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
18
|
+
|
|
19
|
+
const _toTemperature = (raw) => raw / 65536.0 * 165.0 - 40.0;
|
|
20
|
+
const _toHumidity = (raw) => raw / 65536.0 * 100.0;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* HDC1008 humidity and temperature sensor (Texas Instruments) — minimal interface.
|
|
24
|
+
*
|
|
25
|
+
* `init()` writes the configuration 0x1000 (14-bit T and RH, sequence mode, heater off).
|
|
26
|
+
* `read()` triggers one sequence measurement (pointer 0x00), polls the NACKed read header
|
|
27
|
+
* (timeout 50 ms) and converts the temperature and humidity words. The constructor does not
|
|
28
|
+
* touch the bus.
|
|
29
|
+
*/
|
|
30
|
+
class HDC1008Minimal {
|
|
31
|
+
/**
|
|
32
|
+
* @param {import('../../connection/connection').Connection} connection - Plain I²C connection (0x40-0x43).
|
|
33
|
+
*/
|
|
34
|
+
constructor(connection) {
|
|
35
|
+
this._conn = connection;
|
|
36
|
+
this._config = _CONFIG_DEFAULT;
|
|
37
|
+
this._pending = null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Write the default configuration 0x1000 (14-bit, sequence mode, heater off). */
|
|
41
|
+
async init() {
|
|
42
|
+
this._config = _CONFIG_DEFAULT;
|
|
43
|
+
this._pending = null;
|
|
44
|
+
await this._writeConfig();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async _writeConfig() {
|
|
48
|
+
await this._conn.write(Buffer.from([_REG_CONFIG, (this._config >> 8) & 0xFF, this._config & 0xFF]));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async _triggerRead(pointer, n) {
|
|
52
|
+
this._pending = null;
|
|
53
|
+
await this._conn.write(Buffer.from([pointer]));
|
|
54
|
+
for (let i = 0; i < _POLL_ATTEMPTS; i++) {
|
|
55
|
+
try {
|
|
56
|
+
return Buffer.from(await this._conn.read(n));
|
|
57
|
+
} catch (e) {
|
|
58
|
+
await _sleep(1); // read header NACKed: not ready yet
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
throw new Error('HDC1008 measurement did not complete');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async _setMode(sequence) {
|
|
65
|
+
const want = sequence ? (this._config | _MODE) : (this._config & ~_MODE);
|
|
66
|
+
if (want !== this._config) {
|
|
67
|
+
this._config = want;
|
|
68
|
+
await this._writeConfig();
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async _measurePair() {
|
|
73
|
+
await this._setMode(true);
|
|
74
|
+
const d = await this._triggerRead(_REG_TEMPERATURE, 4);
|
|
75
|
+
return [(d[0] << 8) | d[1], (d[2] << 8) | d[3]];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Measure temperature and humidity (sequence mode, temperature first).
|
|
80
|
+
* @returns {Promise<[number, number]>} [temperature °C, relative humidity %RH]
|
|
81
|
+
*/
|
|
82
|
+
async read() {
|
|
83
|
+
const [t, h] = await this._measurePair();
|
|
84
|
+
return [_toTemperature(t), _toHumidity(h)];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* HDC1008 full interface — single values, resolution, heater, battery flag, serial ID and DRDYn.
|
|
90
|
+
*
|
|
91
|
+
* Extends HDC1008Minimal.
|
|
92
|
+
*/
|
|
93
|
+
class HDC1008Full extends HDC1008Minimal {
|
|
94
|
+
constructor(connection) {
|
|
95
|
+
super(connection);
|
|
96
|
+
this._callback = null;
|
|
97
|
+
this._edgeHandler = null;
|
|
98
|
+
this._pollTimer = null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Measure and return the raw 16-bit words.
|
|
103
|
+
* @returns {Promise<[number, number]>} [raw temperature, raw humidity], bits 1:0 zero
|
|
104
|
+
*/
|
|
105
|
+
async readRaw() {
|
|
106
|
+
return this._measurePair();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Measure temperature only (single mode).
|
|
111
|
+
* @returns {Promise<number>} °C
|
|
112
|
+
*/
|
|
113
|
+
async readTemperature() {
|
|
114
|
+
await this._setMode(false);
|
|
115
|
+
const d = await this._triggerRead(_REG_TEMPERATURE, 2);
|
|
116
|
+
return _toTemperature((d[0] << 8) | d[1]);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Measure humidity only (single mode).
|
|
121
|
+
* @returns {Promise<number>} %RH
|
|
122
|
+
*/
|
|
123
|
+
async readHumidity() {
|
|
124
|
+
await this._setMode(false);
|
|
125
|
+
const d = await this._triggerRead(_REG_HUMIDITY, 2);
|
|
126
|
+
return _toHumidity((d[0] << 8) | d[1]);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Select the conversion resolutions.
|
|
131
|
+
* @param {number} temperatureBits - 14 or 11.
|
|
132
|
+
* @param {number} humidityBits - 14, 11 or 8.
|
|
133
|
+
* @throws {RangeError} For any other value.
|
|
134
|
+
*/
|
|
135
|
+
async setResolution(temperatureBits, humidityBits) {
|
|
136
|
+
if (temperatureBits !== 14 && temperatureBits !== 11) throw new RangeError('temperatureBits must be 14 or 11');
|
|
137
|
+
if (![14, 11, 8].includes(humidityBits)) throw new RangeError('humidityBits must be 14, 11 or 8');
|
|
138
|
+
let cfg = this._config & ~(_TRES | _HRES_MASK);
|
|
139
|
+
if (temperatureBits === 11) cfg |= _TRES;
|
|
140
|
+
cfg |= { 14: 0x0000, 11: 0x0100, 8: 0x0200 }[humidityBits];
|
|
141
|
+
this._config = cfg;
|
|
142
|
+
await this._writeConfig();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Enable or disable the heater.
|
|
147
|
+
* @param {boolean} on
|
|
148
|
+
*/
|
|
149
|
+
async setHeater(on) {
|
|
150
|
+
this._config = on ? (this._config | _HEAT) : (this._config & ~_HEAT);
|
|
151
|
+
await this._writeConfig();
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** @returns {Promise<boolean>} True if the heater bit is set in the configuration register. */
|
|
155
|
+
async isHeaterOn() {
|
|
156
|
+
return ((await this.readConfig()) & _HEAT) !== 0;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** @returns {Promise<boolean>} True if VDD is below 2.8 V (BTST). */
|
|
160
|
+
async batteryLow() {
|
|
161
|
+
return ((await this.readConfig()) & _BTST) !== 0;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** @returns {Promise<number>} The 16-bit configuration word. */
|
|
165
|
+
async readConfig() {
|
|
166
|
+
await this._conn.write(Buffer.from([_REG_CONFIG]));
|
|
167
|
+
const d = Buffer.from(await this._conn.read(2));
|
|
168
|
+
return (d[0] << 8) | d[1];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Set RST, wait 15 ms, then rewrite the stored configuration. */
|
|
172
|
+
async softwareReset() {
|
|
173
|
+
await this._conn.write(Buffer.from([_REG_CONFIG, (_RST >> 8) & 0xFF, 0x00]));
|
|
174
|
+
await _sleep(15);
|
|
175
|
+
await this._writeConfig();
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** @returns {Promise<number>} The 40-bit serial number from registers 0xFB-0xFD. */
|
|
179
|
+
async serialId() {
|
|
180
|
+
const w = [];
|
|
181
|
+
for (let i = 0; i < 3; i++) {
|
|
182
|
+
await this._conn.write(Buffer.from([_REG_SERIAL1 + i]));
|
|
183
|
+
const d = Buffer.from(await this._conn.read(2));
|
|
184
|
+
w.push((d[0] << 8) | d[1]);
|
|
185
|
+
}
|
|
186
|
+
return w[0] * 2 ** 24 + w[1] * 2 ** 8 + (w[2] >> 7);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** @returns {number} Typical conversion time in seconds for the current resolutions (T + RH). */
|
|
190
|
+
conversionTime() {
|
|
191
|
+
const t = (this._config & _TRES) ? 0.00365 : 0.00635;
|
|
192
|
+
const h = { 0x0000: 0.0065, 0x0100: 0.00385, 0x0200: 0.0025 }[this._config & _HRES_MASK] ?? 0.0065;
|
|
193
|
+
return t + h;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Report whether a triggered measurement is ready: a read header that is ACKed means ready
|
|
198
|
+
* (the bytes are read and cached for the next callback), a NACK means not ready.
|
|
199
|
+
* @returns {Promise<boolean>}
|
|
200
|
+
*/
|
|
201
|
+
async pollInterrupt() {
|
|
202
|
+
if (this._pending) return true;
|
|
203
|
+
try {
|
|
204
|
+
this._pending = Buffer.from(await this._conn.read((this._config & _MODE) ? 4 : 2));
|
|
205
|
+
} catch (e) {
|
|
206
|
+
return false;
|
|
207
|
+
}
|
|
208
|
+
return true;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
_deliver() {
|
|
212
|
+
const d = this._pending;
|
|
213
|
+
this._pending = null;
|
|
214
|
+
if (!d || !this._callback) return;
|
|
215
|
+
if (d.length >= 4) this._callback([_toTemperature((d[0] << 8) | d[1]), _toHumidity((d[2] << 8) | d[3])]);
|
|
216
|
+
else this._callback([_toTemperature((d[0] << 8) | d[1])]);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Subscribe to DRDYn (falling edge via `connection.intPin`) or poll every 5 ms.
|
|
221
|
+
* The caller triggers the measurement; `callback([temperature_c, humidity_pct])` receives the result.
|
|
222
|
+
* @param {function(number[]): void} callback
|
|
223
|
+
*/
|
|
224
|
+
async onInterrupt(callback) {
|
|
225
|
+
this._callback = callback;
|
|
226
|
+
if (this._conn.intPin) {
|
|
227
|
+
this._edgeHandler = async () => {
|
|
228
|
+
if (await this.pollInterrupt()) this._deliver();
|
|
229
|
+
};
|
|
230
|
+
await this._conn.intPin.onEdge(this._edgeHandler, 'falling');
|
|
231
|
+
} else {
|
|
232
|
+
let busy = false;
|
|
233
|
+
this._pollTimer = setInterval(async () => {
|
|
234
|
+
if (busy) return;
|
|
235
|
+
busy = true;
|
|
236
|
+
try {
|
|
237
|
+
if (await this.pollInterrupt()) this._deliver();
|
|
238
|
+
} finally {
|
|
239
|
+
busy = false;
|
|
240
|
+
}
|
|
241
|
+
}, 5);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Unsubscribe and stop delivery. */
|
|
246
|
+
async offInterrupt() {
|
|
247
|
+
if (this._conn.intPin && this._edgeHandler) {
|
|
248
|
+
await this._conn.intPin.offEdge(this._edgeHandler);
|
|
249
|
+
this._edgeHandler = null;
|
|
250
|
+
}
|
|
251
|
+
if (this._pollTimer) {
|
|
252
|
+
clearInterval(this._pollTimer);
|
|
253
|
+
this._pollTimer = null;
|
|
254
|
+
}
|
|
255
|
+
this._callback = null;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** Default 7-bit I²C address (ADR1 = ADR0 = GND). Valid range 0x40–0x43. */
|
|
260
|
+
HDC1008Minimal.I2C_ADDRESS = 0x40;
|
|
261
|
+
|
|
262
|
+
module.exports = { HDC1008Minimal, HDC1008Full };
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_TEMPERATURE = 0x00;
|
|
4
|
+
const _REG_HUMIDITY = 0x01;
|
|
5
|
+
const _REG_CONFIG = 0x02;
|
|
6
|
+
const _REG_SERIAL1 = 0xFB;
|
|
7
|
+
const _REG_MANUFACTURER_ID = 0xFE;
|
|
8
|
+
const _REG_DEVICE_ID = 0xFF;
|
|
9
|
+
|
|
10
|
+
const _RST = 0x8000;
|
|
11
|
+
const _HEAT = 0x2000;
|
|
12
|
+
const _MODE = 0x1000;
|
|
13
|
+
const _BTST = 0x0800;
|
|
14
|
+
const _TRES = 0x0400;
|
|
15
|
+
const _HRES_MASK = 0x0300;
|
|
16
|
+
const _CONFIG_DEFAULT = 0x1000;
|
|
17
|
+
const _POLL_ATTEMPTS = 50; // x 1 ms = 50 ms timeout
|
|
18
|
+
|
|
19
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
20
|
+
|
|
21
|
+
const _toTemperature = (raw) => raw / 65536.0 * 165.0 - 40.0;
|
|
22
|
+
const _toHumidity = (raw) => raw / 65536.0 * 100.0;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* HDC1080 humidity and temperature sensor (Texas Instruments) — minimal interface.
|
|
26
|
+
*
|
|
27
|
+
* `init()` writes the configuration 0x1000 (14-bit T and RH, sequence mode, heater off).
|
|
28
|
+
* `read()` triggers one sequence measurement (pointer 0x00), polls the NACKed read header
|
|
29
|
+
* (timeout 50 ms) and converts the temperature and humidity words. The constructor does not
|
|
30
|
+
* touch the bus.
|
|
31
|
+
*/
|
|
32
|
+
class HDC1080Minimal {
|
|
33
|
+
/**
|
|
34
|
+
* @param {import('../../connection/connection').Connection} connection - Plain I²C connection (0x40).
|
|
35
|
+
*/
|
|
36
|
+
constructor(connection) {
|
|
37
|
+
this._conn = connection;
|
|
38
|
+
this._config = _CONFIG_DEFAULT;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Write the default configuration 0x1000 (14-bit, sequence mode, heater off). */
|
|
42
|
+
async init() {
|
|
43
|
+
this._config = _CONFIG_DEFAULT;
|
|
44
|
+
await this._writeConfig();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async _writeConfig() {
|
|
48
|
+
await this._conn.write(Buffer.from([_REG_CONFIG, (this._config >> 8) & 0xFF, this._config & 0xFF]));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async _triggerRead(pointer, n) {
|
|
52
|
+
await this._conn.write(Buffer.from([pointer]));
|
|
53
|
+
for (let i = 0; i < _POLL_ATTEMPTS; i++) {
|
|
54
|
+
try {
|
|
55
|
+
return Buffer.from(await this._conn.read(n));
|
|
56
|
+
} catch (e) {
|
|
57
|
+
await _sleep(1); // read header NACKed: not ready yet
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
throw new Error('HDC1080 measurement did not complete');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async _setMode(sequence) {
|
|
64
|
+
const want = sequence ? (this._config | _MODE) : (this._config & ~_MODE);
|
|
65
|
+
if (want !== this._config) {
|
|
66
|
+
this._config = want;
|
|
67
|
+
await this._writeConfig();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async _measurePair() {
|
|
72
|
+
await this._setMode(true);
|
|
73
|
+
const d = await this._triggerRead(_REG_TEMPERATURE, 4);
|
|
74
|
+
return [(d[0] << 8) | d[1], (d[2] << 8) | d[3]];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Measure temperature and humidity (sequence mode, temperature first).
|
|
79
|
+
* @returns {Promise<[number, number]>} [temperature °C, relative humidity %RH]
|
|
80
|
+
*/
|
|
81
|
+
async read() {
|
|
82
|
+
const [t, h] = await this._measurePair();
|
|
83
|
+
return [_toTemperature(t), _toHumidity(h)];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* HDC1080 full interface — single values, resolution, heater, battery flag, serial ID and the ID registers.
|
|
89
|
+
*
|
|
90
|
+
* Extends HDC1080Minimal.
|
|
91
|
+
*/
|
|
92
|
+
class HDC1080Full extends HDC1080Minimal {
|
|
93
|
+
/**
|
|
94
|
+
* Measure and return the raw 16-bit words.
|
|
95
|
+
* @returns {Promise<[number, number]>} [raw temperature, raw humidity], bits 1:0 zero
|
|
96
|
+
*/
|
|
97
|
+
async readRaw() {
|
|
98
|
+
return this._measurePair();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Measure temperature only (single mode).
|
|
103
|
+
* @returns {Promise<number>} °C
|
|
104
|
+
*/
|
|
105
|
+
async readTemperature() {
|
|
106
|
+
await this._setMode(false);
|
|
107
|
+
const d = await this._triggerRead(_REG_TEMPERATURE, 2);
|
|
108
|
+
return _toTemperature((d[0] << 8) | d[1]);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Measure humidity only (single mode).
|
|
113
|
+
* @returns {Promise<number>} %RH
|
|
114
|
+
*/
|
|
115
|
+
async readHumidity() {
|
|
116
|
+
await this._setMode(false);
|
|
117
|
+
const d = await this._triggerRead(_REG_HUMIDITY, 2);
|
|
118
|
+
return _toHumidity((d[0] << 8) | d[1]);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Select the conversion resolutions.
|
|
123
|
+
* @param {number} temperatureBits - 14 or 11.
|
|
124
|
+
* @param {number} humidityBits - 14, 11 or 8.
|
|
125
|
+
* @throws {RangeError} For any other value.
|
|
126
|
+
*/
|
|
127
|
+
async setResolution(temperatureBits, humidityBits) {
|
|
128
|
+
if (temperatureBits !== 14 && temperatureBits !== 11) throw new RangeError('temperatureBits must be 14 or 11');
|
|
129
|
+
if (![14, 11, 8].includes(humidityBits)) throw new RangeError('humidityBits must be 14, 11 or 8');
|
|
130
|
+
let cfg = this._config & ~(_TRES | _HRES_MASK);
|
|
131
|
+
if (temperatureBits === 11) cfg |= _TRES;
|
|
132
|
+
cfg |= { 14: 0x0000, 11: 0x0100, 8: 0x0200 }[humidityBits];
|
|
133
|
+
this._config = cfg;
|
|
134
|
+
await this._writeConfig();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Enable or disable the heater.
|
|
139
|
+
* @param {boolean} on
|
|
140
|
+
*/
|
|
141
|
+
async setHeater(on) {
|
|
142
|
+
this._config = on ? (this._config | _HEAT) : (this._config & ~_HEAT);
|
|
143
|
+
await this._writeConfig();
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** @returns {Promise<boolean>} True if the heater bit is set in the configuration register. */
|
|
147
|
+
async isHeaterOn() {
|
|
148
|
+
return ((await this.readConfig()) & _HEAT) !== 0;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** @returns {Promise<boolean>} True if VDD is below 2.8 V (BTST). */
|
|
152
|
+
async batteryLow() {
|
|
153
|
+
return ((await this.readConfig()) & _BTST) !== 0;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** @returns {Promise<number>} The 16-bit configuration word. */
|
|
157
|
+
async readConfig() {
|
|
158
|
+
await this._conn.write(Buffer.from([_REG_CONFIG]));
|
|
159
|
+
const d = Buffer.from(await this._conn.read(2));
|
|
160
|
+
return (d[0] << 8) | d[1];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Set RST, wait 15 ms, then rewrite the stored configuration. */
|
|
164
|
+
async softwareReset() {
|
|
165
|
+
await this._conn.write(Buffer.from([_REG_CONFIG, (_RST >> 8) & 0xFF, 0x00]));
|
|
166
|
+
await _sleep(15);
|
|
167
|
+
await this._writeConfig();
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** @returns {Promise<number>} The 40-bit serial number from registers 0xFB-0xFD. */
|
|
171
|
+
async serialId() {
|
|
172
|
+
const w = [];
|
|
173
|
+
for (let i = 0; i < 3; i++) {
|
|
174
|
+
await this._conn.write(Buffer.from([_REG_SERIAL1 + i]));
|
|
175
|
+
const d = Buffer.from(await this._conn.read(2));
|
|
176
|
+
w.push((d[0] << 8) | d[1]);
|
|
177
|
+
}
|
|
178
|
+
return w[0] * 2 ** 24 + w[1] * 2 ** 8 + (w[2] >> 7);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** @returns {number} Typical conversion time in seconds for the current resolutions (T + RH). */
|
|
182
|
+
conversionTime() {
|
|
183
|
+
const t = (this._config & _TRES) ? 0.00365 : 0.00635;
|
|
184
|
+
const h = { 0x0000: 0.0065, 0x0100: 0.00385, 0x0200: 0.0025 }[this._config & _HRES_MASK] ?? 0.0065;
|
|
185
|
+
return t + h;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async _readWord(reg) {
|
|
189
|
+
await this._conn.write(Buffer.from([reg]));
|
|
190
|
+
const d = Buffer.from(await this._conn.read(2));
|
|
191
|
+
return (d[0] << 8) | d[1];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** @returns {Promise<number>} The manufacturer ID register 0xFE (0x5449 = Texas Instruments). */
|
|
195
|
+
async manufacturerId() {
|
|
196
|
+
return this._readWord(_REG_MANUFACTURER_ID);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** @returns {Promise<number>} The device ID register 0xFF (0x1050 for the HDC1080). */
|
|
200
|
+
async deviceId() {
|
|
201
|
+
return this._readWord(_REG_DEVICE_ID);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/** Default 7-bit I²C address (fixed, no address pins). */
|
|
206
|
+
HDC1080Minimal.I2C_ADDRESS = 0x40;
|
|
207
|
+
|
|
208
|
+
module.exports = { HDC1080Minimal, HDC1080Full };
|