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,302 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_VCELL = 0x02;
|
|
4
|
+
const _REG_SOC = 0x04;
|
|
5
|
+
const _REG_MODE = 0x06;
|
|
6
|
+
const _REG_VERSION = 0x08;
|
|
7
|
+
const _REG_HIBRT = 0x0A;
|
|
8
|
+
const _REG_CONFIG = 0x0C;
|
|
9
|
+
const _REG_VALRT = 0x14;
|
|
10
|
+
const _REG_CRATE = 0x16;
|
|
11
|
+
const _REG_VRESET = 0x18;
|
|
12
|
+
const _REG_STATUS = 0x1A;
|
|
13
|
+
const _REG_CMD = 0xFE;
|
|
14
|
+
|
|
15
|
+
const _SOURCES = 0x3F00;
|
|
16
|
+
|
|
17
|
+
function _sleep(ms) {
|
|
18
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function _clampByte(v) {
|
|
22
|
+
return Math.max(0, Math.min(255, Math.round(v)));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* MAX17048 / MAX17049 lithium fuel gauge (ModelGauge) — minimal interface.
|
|
27
|
+
*
|
|
28
|
+
* Reports the cell voltage and the state of charge. The constructor makes no bus access; the chip
|
|
29
|
+
* runs with its power-on defaults. The first SOC result is valid about 1 s after power-up.
|
|
30
|
+
*/
|
|
31
|
+
class MAX17048Minimal {
|
|
32
|
+
/**
|
|
33
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (address 0x36).
|
|
34
|
+
* @param {number} [cells=1] - 1 for the MAX17048, 2 for the MAX17049 (scales the voltage).
|
|
35
|
+
*/
|
|
36
|
+
constructor(connection, cells = 1) {
|
|
37
|
+
this._conn = connection;
|
|
38
|
+
this._cells = cells === 2 ? 2 : 1;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async _readWord(reg) {
|
|
42
|
+
return (await this._conn.readReg(reg, 2)).readUInt16BE(0);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async _writeWord(reg, value) {
|
|
46
|
+
const buf = Buffer.alloc(2);
|
|
47
|
+
buf.writeUInt16BE(value & 0xFFFF, 0);
|
|
48
|
+
await this._conn.writeReg(reg, buf);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @returns {Promise<number>} Cell voltage in volts ((VCELL >> 4) × 1.25 mV × cells). */
|
|
52
|
+
async voltage() {
|
|
53
|
+
return ((await this._readWord(_REG_VCELL)) >> 4) * 1.25e-3 * this._cells;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** @returns {Promise<number>} State of charge in %, clamped to 0–100 (SOC / 256). */
|
|
57
|
+
async percent() {
|
|
58
|
+
return Math.max(0, Math.min(100, (await this._readWord(_REG_SOC)) / 256));
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* MAX17048 / MAX17049 full interface — alerts, RCOMP, hibernate, sleep, reset.
|
|
64
|
+
*
|
|
65
|
+
* Interrupt sources (callback / pollInterrupt / enableInterrupt): SOURCE_RESET, SOURCE_OVERVOLTAGE,
|
|
66
|
+
* SOURCE_UNDERVOLTAGE, SOURCE_VRESET, SOURCE_LOW_SOC, SOURCE_SOC_CHANGE.
|
|
67
|
+
*/
|
|
68
|
+
class MAX17048Full extends MAX17048Minimal {
|
|
69
|
+
/**
|
|
70
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (address 0x36).
|
|
71
|
+
* @param {number} [cells=1] - 1 for the MAX17048, 2 for the MAX17049.
|
|
72
|
+
*/
|
|
73
|
+
constructor(connection, cells = 1) {
|
|
74
|
+
super(connection, cells);
|
|
75
|
+
this._callback = null;
|
|
76
|
+
this._intPinUsed = null;
|
|
77
|
+
this._pollTimer = null;
|
|
78
|
+
this._polling = false;
|
|
79
|
+
this._sleepEnabled = false;
|
|
80
|
+
this._edgeHandler = () => { this._service(); };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async _modify(reg, clear, set) {
|
|
84
|
+
await this._writeWord(reg, (((await this._readWord(reg)) & ~clear) | set) & 0xFFFF);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** @returns {Promise<number>} Raw 16-bit VCELL register. */
|
|
88
|
+
async rawVoltage() { return this._readWord(_REG_VCELL); }
|
|
89
|
+
|
|
90
|
+
/** @returns {Promise<number>} Raw 16-bit SOC register. */
|
|
91
|
+
async rawSoc() { return this._readWord(_REG_SOC); }
|
|
92
|
+
|
|
93
|
+
/** @returns {Promise<number>} Charge/discharge rate in %/h (signed CRATE × 0.208), positive while charging. */
|
|
94
|
+
async chargeRate() {
|
|
95
|
+
return (await this._conn.readReg(_REG_CRATE, 2)).readInt16BE(0) * 0.208;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** @returns {Promise<number>} VERSION register (0x001x). */
|
|
99
|
+
async version() { return this._readWord(_REG_VERSION); }
|
|
100
|
+
|
|
101
|
+
/** @returns {Promise<number>} Low byte of VRESET/ID (factory ID). */
|
|
102
|
+
async chipId() { return (await this._readWord(_REG_VRESET)) & 0xFF; }
|
|
103
|
+
|
|
104
|
+
/** Restart the SOC estimate from the present voltage (MODE.QuickStart); only if the first SOC is clearly wrong. @returns {Promise<void>} */
|
|
105
|
+
async quickStart() { await this._writeWord(_REG_MODE, 0x4000 | (this._sleepEnabled ? 0x2000 : 0)); }
|
|
106
|
+
|
|
107
|
+
/** Power-on reset (CMD ← 0x5400); tolerates the missing ACK and waits 1 s. @returns {Promise<void>} */
|
|
108
|
+
async reset() {
|
|
109
|
+
try { await this._writeWord(_REG_CMD, 0x5400); } catch (e) { /* the chip does not ACK */ }
|
|
110
|
+
await _sleep(1000);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Write RCOMP (CONFIG bits 15:8). @param {number} value - 0–255. @returns {Promise<void>} */
|
|
114
|
+
async setRcomp(value) { await this._modify(_REG_CONFIG, 0xFF00, (value & 0xFF) << 8); }
|
|
115
|
+
|
|
116
|
+
/** @returns {Promise<number>} RCOMP 0–255. */
|
|
117
|
+
async getRcomp() { return (await this._readWord(_REG_CONFIG)) >> 8; }
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Set RCOMP from the datasheet temperature formula with the default coefficients (RCOMP0 = 0x97).
|
|
121
|
+
* @param {number} celsius - Cell temperature in °C.
|
|
122
|
+
* @returns {Promise<void>}
|
|
123
|
+
*/
|
|
124
|
+
async compensateTemperature(celsius) {
|
|
125
|
+
const coefficient = celsius >= 20 ? -0.5 : -5.0;
|
|
126
|
+
await this.setRcomp(_clampByte(0x97 + (celsius - 20) * coefficient));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Set the empty-alert threshold (CONFIG.ATHD = 32 − percent). @param {number} percent - 1–32. @returns {Promise<void>} */
|
|
130
|
+
async setAlertSoc(percent) {
|
|
131
|
+
const p = Math.max(1, Math.min(32, Math.round(percent)));
|
|
132
|
+
await this._modify(_REG_CONFIG, 0x001F, 32 - p);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** @returns {Promise<number>} Empty-alert threshold in % (1–32). */
|
|
136
|
+
async getAlertSoc() { return 32 - ((await this._readWord(_REG_CONFIG)) & 0x1F); }
|
|
137
|
+
|
|
138
|
+
/** Enable the alert on a SOC change of at least 1 % (CONFIG.ALSC). @param {boolean} enabled @returns {Promise<void>} */
|
|
139
|
+
async setSocChangeAlert(enabled) { await this._modify(_REG_CONFIG, 0x0040, enabled ? 0x0040 : 0); }
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Set the voltage alert window in 20 mV steps (VALRT).
|
|
143
|
+
* @param {number} minV - Under-voltage threshold in V; 0 disables it.
|
|
144
|
+
* @param {number} maxV - Over-voltage threshold in V; 5.1 disables it.
|
|
145
|
+
* @returns {Promise<void>}
|
|
146
|
+
* @throws {RangeError} If minV > maxV.
|
|
147
|
+
*/
|
|
148
|
+
async setVoltageAlert(minV, maxV) {
|
|
149
|
+
if (minV > maxV) throw new RangeError('minV must not exceed maxV');
|
|
150
|
+
await this._writeWord(_REG_VALRT, (_clampByte(minV / 0.020) << 8) | _clampByte(maxV / 0.020));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** @returns {Promise<{minV: number, maxV: number}>} Voltage alert window in volts. */
|
|
154
|
+
async getVoltageAlert() {
|
|
155
|
+
const raw = await this._readWord(_REG_VALRT);
|
|
156
|
+
return { minV: (raw >> 8) * 0.020, maxV: (raw & 0xFF) * 0.020 };
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Set the reset voltage (VRESET bits 15:9, 40 mV steps). @param {number} volts - 0.0–5.08 V. @returns {Promise<void>} */
|
|
160
|
+
async setResetVoltage(volts) {
|
|
161
|
+
const steps = Math.max(0, Math.min(127, Math.round(volts / 0.040)));
|
|
162
|
+
await this._modify(_REG_VRESET, 0xFE00, steps << 9);
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** @returns {Promise<number>} Reset voltage in volts. */
|
|
166
|
+
async getResetVoltage() { return ((await this._readWord(_REG_VRESET)) >> 9) * 0.040; }
|
|
167
|
+
|
|
168
|
+
/** Let a voltage-reset event assert ALRT (STATUS.ENVR). @param {boolean} enabled @returns {Promise<void>} */
|
|
169
|
+
async enableVoltageResetAlert(enabled) { await this._modify(_REG_STATUS, 0x4000, enabled ? 0x4000 : 0); }
|
|
170
|
+
|
|
171
|
+
/** Disable the analog comparator in hibernate mode (VRESET/ID.DIS). @param {boolean} disabled @returns {Promise<void>} */
|
|
172
|
+
async setComparatorDisabled(disabled) { await this._modify(_REG_VRESET, 0x0100, disabled ? 0x0100 : 0); }
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Program the hibernate thresholds (HIBRT).
|
|
176
|
+
* @param {number} hibThrPctPerH - Hibernate below this |CRATE| in %/h (0.208 %/h steps).
|
|
177
|
+
* @param {number} actThrV - Leave hibernate above this |OCV − VCELL| in V (1.25 mV steps).
|
|
178
|
+
* @returns {Promise<void>}
|
|
179
|
+
*/
|
|
180
|
+
async setHibernateThresholds(hibThrPctPerH, actThrV) {
|
|
181
|
+
await this._writeWord(_REG_HIBRT, (_clampByte(hibThrPctPerH / 0.208) << 8) | _clampByte(actThrV / 0.00125));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** @returns {Promise<{hibThrPctPerH: number, actThrV: number}>} Hibernate thresholds. */
|
|
185
|
+
async getHibernateThresholds() {
|
|
186
|
+
const raw = await this._readWord(_REG_HIBRT);
|
|
187
|
+
return { hibThrPctPerH: (raw >> 8) * 0.208, actThrV: (raw & 0xFF) * 0.00125 };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Never hibernate (HIBRT ← 0x0000). @returns {Promise<void>} */
|
|
191
|
+
async disableHibernate() { await this._writeWord(_REG_HIBRT, 0x0000); }
|
|
192
|
+
|
|
193
|
+
/** Always stay in hibernate (HIBRT ← 0xFFFF). @returns {Promise<void>} */
|
|
194
|
+
async forceHibernate() { await this._writeWord(_REG_HIBRT, 0xFFFF); }
|
|
195
|
+
|
|
196
|
+
/** @returns {Promise<boolean>} MODE.HibStat; false if the write-only register cannot be read. */
|
|
197
|
+
async isHibernating() {
|
|
198
|
+
try { return ((await this._readWord(_REG_MODE)) & 0x1000) !== 0; } catch (e) { return false; }
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Allow or forbid sleep mode (MODE.EnSleep; MODE is written whole). @param {boolean} enabled @returns {Promise<void>} */
|
|
202
|
+
async enableSleep(enabled) {
|
|
203
|
+
this._sleepEnabled = !!enabled;
|
|
204
|
+
await this._writeWord(_REG_MODE, enabled ? 0x2000 : 0x0000);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/** Enter sleep mode (CONFIG.SLEEP = 1); needs enableSleep(true). @returns {Promise<void>} */
|
|
208
|
+
async sleep() { await this._modify(_REG_CONFIG, 0, 0x0080); }
|
|
209
|
+
|
|
210
|
+
/** Leave sleep mode (CONFIG.SLEEP = 0). @returns {Promise<void>} */
|
|
211
|
+
async wake() { await this._modify(_REG_CONFIG, 0x0080, 0); }
|
|
212
|
+
|
|
213
|
+
/** @returns {Promise<number>} Raw STATUS register. */
|
|
214
|
+
async readStatus() { return this._readWord(_REG_STATUS); }
|
|
215
|
+
|
|
216
|
+
/** Clear the given STATUS bits and CONFIG.ALRT (releases ALRT). @param {number} mask @returns {Promise<void>} */
|
|
217
|
+
async clearStatus(mask) {
|
|
218
|
+
await this._modify(_REG_STATUS, mask & 0xFFFF, 0);
|
|
219
|
+
await this._modify(_REG_CONFIG, 0x0020, 0);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** @returns {Promise<number>} Raw CONFIG register. */
|
|
223
|
+
async readConfig() { return this._readWord(_REG_CONFIG); }
|
|
224
|
+
|
|
225
|
+
/** Write the raw CONFIG register. @param {number} value @returns {Promise<void>} */
|
|
226
|
+
async writeConfig(value) { await this._writeWord(_REG_CONFIG, value); }
|
|
227
|
+
|
|
228
|
+
/** Enable an alert source with a control bit (SOURCE_SOC_CHANGE, SOURCE_VRESET). @param {number} source @returns {Promise<void>} */
|
|
229
|
+
async enableInterrupt(source) {
|
|
230
|
+
if (source === MAX17048Full.SOURCE_SOC_CHANGE) await this.setSocChangeAlert(true);
|
|
231
|
+
else if (source === MAX17048Full.SOURCE_VRESET) await this.enableVoltageResetAlert(true);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Disable an alert source (control bit, or VALRT.MIN = 0 / VALRT.MAX = 0xFF). @param {number} source @returns {Promise<void>} */
|
|
235
|
+
async disableInterrupt(source) {
|
|
236
|
+
if (source === MAX17048Full.SOURCE_SOC_CHANGE) await this.setSocChangeAlert(false);
|
|
237
|
+
else if (source === MAX17048Full.SOURCE_VRESET) await this.enableVoltageResetAlert(false);
|
|
238
|
+
else if (source === MAX17048Full.SOURCE_UNDERVOLTAGE) await this._modify(_REG_VALRT, 0xFF00, 0);
|
|
239
|
+
else if (source === MAX17048Full.SOURCE_OVERVOLTAGE) await this._modify(_REG_VALRT, 0x00FF, 0x00FF);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Subscribe to ALRT (falling edge). `callback(status)` receives the STATUS word masked to the SOURCE_* bits.
|
|
244
|
+
* Delivery is via the INT line (options.intPin or connection.intPin) or a 1 s polling timer.
|
|
245
|
+
* @param {function} callback - Called with (status).
|
|
246
|
+
* @param {object} [options]
|
|
247
|
+
* @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
|
|
248
|
+
* @returns {Promise<void>}
|
|
249
|
+
*/
|
|
250
|
+
async onInterrupt(callback, options = {}) {
|
|
251
|
+
this._callback = callback;
|
|
252
|
+
const pin = options.intPin ?? this._conn.intPin ?? null;
|
|
253
|
+
this._intPinUsed = pin;
|
|
254
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
255
|
+
if (pin) {
|
|
256
|
+
await pin.onEdge(this._edgeHandler, 'falling');
|
|
257
|
+
} else {
|
|
258
|
+
this._pollTimer = setInterval(() => { this._service(); }, 1000);
|
|
259
|
+
if (this._pollTimer.unref) this._pollTimer.unref();
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Unsubscribe and stop delivery. @returns {Promise<void>} */
|
|
264
|
+
async offInterrupt() {
|
|
265
|
+
if (this._intPinUsed) {
|
|
266
|
+
await this._intPinUsed.offEdge(this._edgeHandler);
|
|
267
|
+
this._intPinUsed = null;
|
|
268
|
+
}
|
|
269
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
270
|
+
this._callback = null;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
async _service() {
|
|
274
|
+
if (this._polling) return;
|
|
275
|
+
this._polling = true;
|
|
276
|
+
try {
|
|
277
|
+
const status = await this.pollInterrupt();
|
|
278
|
+
if (status && this._callback) this._callback(status);
|
|
279
|
+
} finally {
|
|
280
|
+
this._polling = false;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Read STATUS, clear the reported flags and CONFIG.ALRT.
|
|
286
|
+
* @returns {Promise<number>} STATUS word masked to the SOURCE_* bits (0 = no alert).
|
|
287
|
+
*/
|
|
288
|
+
async pollInterrupt() {
|
|
289
|
+
const flags = (await this._readWord(_REG_STATUS)) & _SOURCES;
|
|
290
|
+
if (flags) await this.clearStatus(flags);
|
|
291
|
+
return flags;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
MAX17048Full.SOURCE_RESET = 0x0100;
|
|
296
|
+
MAX17048Full.SOURCE_OVERVOLTAGE = 0x0200;
|
|
297
|
+
MAX17048Full.SOURCE_UNDERVOLTAGE = 0x0400;
|
|
298
|
+
MAX17048Full.SOURCE_VRESET = 0x0800;
|
|
299
|
+
MAX17048Full.SOURCE_LOW_SOC = 0x1000;
|
|
300
|
+
MAX17048Full.SOURCE_SOC_CHANGE = 0x2000;
|
|
301
|
+
|
|
302
|
+
module.exports = { MAX17048Minimal, MAX17048Full };
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { toSigned } = require('../../connection/register');
|
|
4
|
+
|
|
5
|
+
const _REG_INTERRUPT_CFG = 0x0B;
|
|
6
|
+
const _REG_THS_P_L = 0x0C;
|
|
7
|
+
const _REG_THS_P_H = 0x0D;
|
|
8
|
+
const _REG_WHO_AM_I = 0x0F;
|
|
9
|
+
const _REG_CTRL_REG1 = 0x10;
|
|
10
|
+
const _REG_CTRL_REG2 = 0x11;
|
|
11
|
+
const _REG_CTRL_REG3 = 0x12;
|
|
12
|
+
const _REG_FIFO_CTRL = 0x14;
|
|
13
|
+
const _REG_RPDS_L = 0x18;
|
|
14
|
+
const _REG_RPDS_H = 0x19;
|
|
15
|
+
const _REG_RES_CONF = 0x1A;
|
|
16
|
+
const _REG_INT_SOURCE = 0x25;
|
|
17
|
+
const _REG_FIFO_STATUS = 0x26;
|
|
18
|
+
const _REG_STATUS = 0x27;
|
|
19
|
+
const _REG_PRESS_XL = 0x28;
|
|
20
|
+
const _REG_LPFP_RES = 0x33;
|
|
21
|
+
|
|
22
|
+
const _CHIP_ID = 0xB1;
|
|
23
|
+
const _STATUS_P_DA = 0x01;
|
|
24
|
+
const _STATUS_T_DA = 0x02;
|
|
25
|
+
const _CTRL_REG1_DEFAULT = 0x12;
|
|
26
|
+
const _CTRL_REG2_RESET = 0x04;
|
|
27
|
+
const _CTRL_REG2_DEFAULT = 0x10;
|
|
28
|
+
|
|
29
|
+
function _delay(ms) {
|
|
30
|
+
const start = Date.now();
|
|
31
|
+
while (Date.now() - start < ms) {}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function _signExtend24(v) { return toSigned(v, 24); }
|
|
35
|
+
function _signExtend16(v) { return toSigned(v, 16); }
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* LPS22HB MEMS nano absolute pressure sensor - minimal interface.
|
|
39
|
+
*
|
|
40
|
+
* Provides calibrated pressure (Pa) and temperature (C) with no configuration
|
|
41
|
+
* beyond the connection. I2C address is 0x5C (SA0=GND) or 0x5D (SA0=VDD).
|
|
42
|
+
*
|
|
43
|
+
* Default: ODR=1 Hz, BDU=1, EN_LPFP=0, IF_ADD_INC=1.
|
|
44
|
+
*
|
|
45
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
|
|
46
|
+
*/
|
|
47
|
+
class LPS22HBMinimal {
|
|
48
|
+
constructor(connection) {
|
|
49
|
+
this._conn = connection;
|
|
50
|
+
this._init();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async _init() {
|
|
54
|
+
let chipId = 0;
|
|
55
|
+
try {
|
|
56
|
+
const buf = await this._readReg(_REG_WHO_AM_I, 1);
|
|
57
|
+
chipId = buf[0];
|
|
58
|
+
} catch (e) {}
|
|
59
|
+
if (chipId !== _CHIP_ID) {
|
|
60
|
+
throw new Error('Unexpected LPS22HB chip ID 0x' + chipId.toString(16) + ', expected 0x' + _CHIP_ID.toString(16));
|
|
61
|
+
}
|
|
62
|
+
await this._writeReg(_REG_CTRL_REG2, _CTRL_REG2_RESET);
|
|
63
|
+
_delay(1);
|
|
64
|
+
await this._writeReg(_REG_CTRL_REG2, _CTRL_REG2_DEFAULT);
|
|
65
|
+
await this._writeReg(_REG_CTRL_REG1, _CTRL_REG1_DEFAULT);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async _writeReg(reg, value) {
|
|
69
|
+
await this._conn.writeReg(reg, value);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async _readReg(reg, n) {
|
|
73
|
+
return this._conn.readReg(reg, n);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async _waitStatus(mask) {
|
|
77
|
+
for (let i = 0; i < 50; i++) {
|
|
78
|
+
const buf = await this._readReg(_REG_STATUS, 1);
|
|
79
|
+
if ((buf[0] & mask) === mask) return;
|
|
80
|
+
_delay(5);
|
|
81
|
+
}
|
|
82
|
+
throw new Error('LPS22HB data not ready');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async pressure() {
|
|
86
|
+
await this._waitStatus(_STATUS_P_DA);
|
|
87
|
+
const raw = await this._readReg(_REG_PRESS_XL, 5);
|
|
88
|
+
const v = (raw[2] << 16) | (raw[1] << 8) | raw[0];
|
|
89
|
+
return _signExtend24(v) * 100.0 / 4096.0;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async temperature() {
|
|
93
|
+
await this._waitStatus(_STATUS_T_DA);
|
|
94
|
+
const raw = await this._readReg(_REG_PRESS_XL, 5);
|
|
95
|
+
const v = (raw[4] << 8) | raw[3];
|
|
96
|
+
return _signExtend16(v) / 100.0;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* LPS22HB full interface - extends LPS22HBMinimal.
|
|
102
|
+
*/
|
|
103
|
+
class LPS22HBFull extends LPS22HBMinimal {
|
|
104
|
+
static ODR_POWER_DOWN = 0;
|
|
105
|
+
static ODR_1_HZ = 1;
|
|
106
|
+
static ODR_10_HZ = 2;
|
|
107
|
+
static ODR_25_HZ = 3;
|
|
108
|
+
static ODR_50_HZ = 4;
|
|
109
|
+
static ODR_75_HZ = 5;
|
|
110
|
+
static LPFP_BW_ODR_9 = 0;
|
|
111
|
+
static LPFP_BW_ODR_20 = 1;
|
|
112
|
+
static FIFO_MODE_BYPASS = 0;
|
|
113
|
+
static FIFO_MODE_FIFO = 1;
|
|
114
|
+
static FIFO_MODE_STREAM = 2;
|
|
115
|
+
static FIFO_MODE_STREAM_TO_FIFO = 3;
|
|
116
|
+
static FIFO_MODE_BYPASS_TO_STREAM = 4;
|
|
117
|
+
static FIFO_MODE_DYNAMIC_STREAM = 6;
|
|
118
|
+
static FIFO_MODE_BYPASS_TO_FIFO = 7;
|
|
119
|
+
static INT_S_DATA_SIGNALS = 0;
|
|
120
|
+
static INT_S_PRESSURE_HIGH = 1;
|
|
121
|
+
static INT_S_PRESSURE_LOW = 2;
|
|
122
|
+
static INT_S_PRESSURE_BOTH = 3;
|
|
123
|
+
|
|
124
|
+
constructor(connection) {
|
|
125
|
+
super(connection);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async configure(odr, bdu, enLpfp, lpfpCfg, lcEn, sim) {
|
|
129
|
+
const ctrl1 = ((odr & 7) << 4)
|
|
130
|
+
| (enLpfp ? (1 << 3) : 0)
|
|
131
|
+
| ((lpfpCfg & 1) << 2)
|
|
132
|
+
| (bdu ? (1 << 1) : 0)
|
|
133
|
+
| (sim ? 1 : 0);
|
|
134
|
+
await this._writeReg(_REG_CTRL_REG1, ctrl1);
|
|
135
|
+
const cur = await this._readReg(_REG_RES_CONF, 1);
|
|
136
|
+
await this._writeReg(_REG_RES_CONF, (cur[0] & 0xFE) | (lcEn ? 1 : 0));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async oneShot() {
|
|
140
|
+
const cur = await this._readReg(_REG_CTRL_REG2, 1);
|
|
141
|
+
await this._writeReg(_REG_CTRL_REG2, cur[0] | 0x01);
|
|
142
|
+
for (let i = 0; i < 50; i++) {
|
|
143
|
+
const statusBuf = await this._readReg(_REG_STATUS, 1);
|
|
144
|
+
if ((statusBuf[0] & 0x03) === 0x03) {
|
|
145
|
+
return { pressure_Pa: await this.pressure(),
|
|
146
|
+
temperature_C: await this.temperature() };
|
|
147
|
+
}
|
|
148
|
+
_delay(5);
|
|
149
|
+
}
|
|
150
|
+
throw new Error('LPS22HB one-shot timeout');
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async status() {
|
|
154
|
+
const buf = await this._readReg(_REG_STATUS, 1);
|
|
155
|
+
return buf[0];
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async reset() {
|
|
159
|
+
await this._writeReg(_REG_CTRL_REG2, _CTRL_REG2_RESET);
|
|
160
|
+
for (let i = 0; i < 50; i++) {
|
|
161
|
+
const buf = await this._readReg(_REG_CTRL_REG2, 1);
|
|
162
|
+
if (!(buf[0] & 0x04)) break;
|
|
163
|
+
_delay(1);
|
|
164
|
+
}
|
|
165
|
+
await this._writeReg(_REG_CTRL_REG2, _CTRL_REG2_DEFAULT);
|
|
166
|
+
await this._writeReg(_REG_CTRL_REG1, _CTRL_REG1_DEFAULT);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async reboot() {
|
|
170
|
+
await this._writeReg(_REG_CTRL_REG2, 0x80);
|
|
171
|
+
for (let i = 0; i < 100; i++) {
|
|
172
|
+
const buf = await this._readReg(_REG_INT_SOURCE, 1);
|
|
173
|
+
if (!(buf[0] & 0x80)) break;
|
|
174
|
+
_delay(5);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async setPressureOffset(offsetHPa) {
|
|
179
|
+
let raw = Math.round(offsetHPa * 16);
|
|
180
|
+
if (raw < 0) raw += 0x10000;
|
|
181
|
+
await this._writeReg(_REG_RPDS_L, raw & 0xFF);
|
|
182
|
+
await this._writeReg(_REG_RPDS_H, (raw >> 8) & 0xFF);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
async setAutozero() {
|
|
186
|
+
const cur = await this._readReg(_REG_INTERRUPT_CFG, 1);
|
|
187
|
+
await this._writeReg(_REG_INTERRUPT_CFG, cur[0] | 0x20);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async clearAutozero() {
|
|
191
|
+
const cur = await this._readReg(_REG_INTERRUPT_CFG, 1);
|
|
192
|
+
await this._writeReg(_REG_INTERRUPT_CFG, cur[0] | 0x10);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async setAutorifp() {
|
|
196
|
+
const cur = await this._readReg(_REG_INTERRUPT_CFG, 1);
|
|
197
|
+
await this._writeReg(_REG_INTERRUPT_CFG, cur[0] | 0x80);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async clearAutorifp() {
|
|
201
|
+
const cur = await this._readReg(_REG_INTERRUPT_CFG, 1);
|
|
202
|
+
await this._writeReg(_REG_INTERRUPT_CFG, cur[0] | 0x40);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async configureInterrupt(drdy, fFth, fOvr, fFss5, intS, activeLow, openDrain) {
|
|
206
|
+
const ctrl3 = (activeLow ? 0x80 : 0)
|
|
207
|
+
| (openDrain ? 0x40 : 0)
|
|
208
|
+
| (fFss5 ? 0x20 : 0)
|
|
209
|
+
| (fFth ? 0x10 : 0)
|
|
210
|
+
| (fOvr ? 0x08 : 0)
|
|
211
|
+
| (drdy ? 0x04 : 0)
|
|
212
|
+
| (intS & 0x03);
|
|
213
|
+
await this._writeReg(_REG_CTRL_REG3, ctrl3);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async configurePressureInterrupt(highEn, lowEn, thresholdHPa, latch) {
|
|
217
|
+
const rawThs = Math.round(thresholdHPa * 16) & 0xFFFF;
|
|
218
|
+
await this._writeReg(_REG_THS_P_L, rawThs & 0xFF);
|
|
219
|
+
await this._writeReg(_REG_THS_P_H, (rawThs >> 8) & 0xFF);
|
|
220
|
+
const cur = await this._readReg(_REG_INTERRUPT_CFG, 1);
|
|
221
|
+
const newCfg = (cur[0] & 0xF0)
|
|
222
|
+
| (latch ? 0x04 : 0)
|
|
223
|
+
| (highEn ? 0x02 : 0)
|
|
224
|
+
| (lowEn ? 0x01 : 0);
|
|
225
|
+
await this._writeReg(_REG_INTERRUPT_CFG, newCfg);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
async interruptStatus() {
|
|
229
|
+
const buf = await this._readReg(_REG_INT_SOURCE, 1);
|
|
230
|
+
return buf[0];
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
async enableFifo(mode, watermark) {
|
|
234
|
+
if (mode === 5) throw new Error('FIFO mode 5 is reserved');
|
|
235
|
+
const ctrl = ((mode & 7) << 5) | (watermark & 0x1F);
|
|
236
|
+
await this._writeReg(_REG_FIFO_CTRL, ctrl);
|
|
237
|
+
const cur = await this._readReg(_REG_CTRL_REG2, 1);
|
|
238
|
+
await this._writeReg(_REG_CTRL_REG2, cur[0] | 0x40);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async disableFifo() {
|
|
242
|
+
const cur = await this._readReg(_REG_CTRL_REG2, 1);
|
|
243
|
+
await this._writeReg(_REG_CTRL_REG2, cur[0] & ~0x40);
|
|
244
|
+
await this._writeReg(_REG_FIFO_CTRL, 0);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async fifoStatus() {
|
|
248
|
+
const buf = await this._readReg(_REG_FIFO_STATUS, 1);
|
|
249
|
+
return buf[0];
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async resetLpf() {
|
|
253
|
+
await this._readReg(_REG_LPFP_RES, 1);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
module.exports = { LPS22HBMinimal, LPS22HBFull };
|