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.
Files changed (79) hide show
  1. package/README.md +19 -0
  2. package/examples/color/as7262/complete.js +60 -0
  3. package/examples/color/as7262/demo.js +65 -0
  4. package/examples/color/as7262/minimal.js +23 -0
  5. package/examples/environmental/aht10/complete.js +37 -0
  6. package/examples/environmental/aht10/demo.js +43 -0
  7. package/examples/environmental/aht10/minimal.js +12 -0
  8. package/examples/environmental/aht20/complete.js +41 -0
  9. package/examples/environmental/aht20/demo.js +49 -0
  10. package/examples/environmental/aht20/minimal.js +12 -0
  11. package/examples/environmental/am2320/complete.js +34 -0
  12. package/examples/environmental/am2320/demo.js +46 -0
  13. package/examples/environmental/am2320/minimal.js +21 -0
  14. package/examples/humidity/hdc1008/complete.js +39 -0
  15. package/examples/humidity/hdc1008/demo.js +46 -0
  16. package/examples/humidity/hdc1008/minimal.js +22 -0
  17. package/examples/humidity/hdc1080/complete.js +34 -0
  18. package/examples/humidity/hdc1080/demo.js +46 -0
  19. package/examples/humidity/hdc1080/minimal.js +22 -0
  20. package/examples/humidity/hs30xx/complete.js +43 -0
  21. package/examples/humidity/hs30xx/demo.js +40 -0
  22. package/examples/humidity/hs30xx/minimal.js +21 -0
  23. package/examples/humidity/hs40xx/complete.js +54 -0
  24. package/examples/humidity/hs40xx/demo.js +38 -0
  25. package/examples/humidity/hs40xx/minimal.js +21 -0
  26. package/examples/io_expander/tca9554/complete.js +63 -0
  27. package/examples/io_expander/tca9554/demo.js +58 -0
  28. package/examples/io_expander/tca9554/minimal.js +20 -0
  29. package/examples/led/ht16k33/complete.js +45 -0
  30. package/examples/led/ht16k33/demo.js +54 -0
  31. package/examples/led/ht16k33/minimal.js +24 -0
  32. package/examples/led/is31fl3731/complete.js +60 -0
  33. package/examples/led/is31fl3731/demo.js +72 -0
  34. package/examples/led/is31fl3731/minimal.js +26 -0
  35. package/examples/led/pca9685/complete.js +43 -0
  36. package/examples/led/pca9685/demo.js +77 -0
  37. package/examples/led/pca9685/minimal.js +24 -0
  38. package/examples/power/ina260/complete.js +38 -0
  39. package/examples/power/ina260/demo.js +32 -0
  40. package/examples/power/ina260/minimal.js +11 -0
  41. package/examples/power/lc709203f/complete.js +57 -0
  42. package/examples/power/lc709203f/demo.js +34 -0
  43. package/examples/power/lc709203f/minimal.js +11 -0
  44. package/examples/power/lm25066/complete.js +54 -0
  45. package/examples/power/lm25066/demo.js +41 -0
  46. package/examples/power/lm25066/minimal.js +12 -0
  47. package/examples/power/ltc4151/complete.js +40 -0
  48. package/examples/power/ltc4151/demo.js +49 -0
  49. package/examples/power/ltc4151/minimal.js +11 -0
  50. package/examples/power/max17048/complete.js +68 -0
  51. package/examples/power/max17048/demo.js +56 -0
  52. package/examples/power/max17048/minimal.js +19 -0
  53. package/examples/pressure/lps22hb/complete.js +43 -0
  54. package/examples/pressure/lps22hb/demo.js +43 -0
  55. package/examples/pressure/lps22hb/minimal.js +19 -0
  56. package/examples/pressure/mpl115a2/complete.js +25 -0
  57. package/examples/pressure/mpl115a2/demo.js +30 -0
  58. package/examples/pressure/mpl115a2/minimal.js +20 -0
  59. package/package.json +1 -1
  60. package/src/chips/color/as7262.js +419 -0
  61. package/src/chips/environmental/aht10.js +186 -0
  62. package/src/chips/environmental/aht20.js +226 -0
  63. package/src/chips/environmental/am2320.js +237 -0
  64. package/src/chips/humidity/hdc1008.js +262 -0
  65. package/src/chips/humidity/hdc1080.js +208 -0
  66. package/src/chips/humidity/hs30xx.js +254 -0
  67. package/src/chips/humidity/hs40xx.js +382 -0
  68. package/src/chips/io_expander/tca9554.js +407 -0
  69. package/src/chips/led/ht16k33.js +273 -0
  70. package/src/chips/led/is31fl3731.js +501 -0
  71. package/src/chips/led/pca9685.js +350 -0
  72. package/src/chips/power/ina260.js +229 -0
  73. package/src/chips/power/lc709203f.js +376 -0
  74. package/src/chips/power/lm25066.js +370 -0
  75. package/src/chips/power/ltc4151.js +297 -0
  76. package/src/chips/power/max17048.js +302 -0
  77. package/src/chips/pressure/lps22hb.js +257 -0
  78. package/src/chips/pressure/mpl115a2.js +171 -0
  79. package/src/discovery/registry.js +56 -14
@@ -0,0 +1,370 @@
1
+ 'use strict';
2
+
3
+ const CMD = {
4
+ OPERATION: 0x01, CLEAR_FAULTS: 0x03, CAPABILITY: 0x19, VOUT_UV_WARN: 0x43, OT_FAULT: 0x4F,
5
+ OT_WARN: 0x51, VIN_OV_WARN: 0x57, VIN_UV_WARN: 0x58, STATUS_BYTE: 0x78, STATUS_WORD: 0x79,
6
+ STATUS_VOUT: 0x7A, STATUS_INPUT: 0x7C, STATUS_TEMP: 0x7D, STATUS_CML: 0x7E, STATUS_MFR: 0x80,
7
+ READ_VIN: 0x88, READ_VOUT: 0x8B, READ_TEMP: 0x8D, MFR_ID: 0x99, MFR_MODEL: 0x9A,
8
+ MFR_REVISION: 0x9B, READ_VAUX: 0xD0, READ_IIN: 0xD1, READ_PIN: 0xD2, IIN_WARN: 0xD3,
9
+ PIN_WARN: 0xD4, READ_PIN_PEAK: 0xD5, CLEAR_PIN_PEAK: 0xD6, GATE_MASK: 0xD7, ALERT_MASK: 0xD8,
10
+ DEVICE_SETUP: 0xD9, BLOCK_READ: 0xDA, SAMPLES_FOR_AVG: 0xDB, READ_AVG_VIN: 0xDC,
11
+ READ_AVG_VOUT: 0xDD, READ_AVG_IIN: 0xDE, READ_AVG_PIN: 0xDF, BLACK_BOX: 0xE0,
12
+ DIAGNOSTIC: 0xE1, AVG_BLOCK_READ: 0xE2,
13
+ };
14
+
15
+ // PMBus DIRECT coefficients [m, b, R]
16
+ const VOLT = [22070, -1800, -2];
17
+ const VAUX = [3546, -3, 0];
18
+ const TEMP = [16000, 0, -3];
19
+
20
+ function fromY(y, [m, b, r]) {
21
+ return (y * Math.pow(10, -r) - b) / m;
22
+ }
23
+
24
+ function toY(x, [m, b, r]) {
25
+ const y = Math.round((m * x + b) * Math.pow(10, r));
26
+ return Math.max(0, Math.min(0x0FFE, y));
27
+ }
28
+
29
+ function signed16(word) {
30
+ return word & 0x8000 ? word - 0x10000 : word;
31
+ }
32
+
33
+ /**
34
+ * LM25066 hot-swap controller / PMBus power monitor - minimal interface.
35
+ *
36
+ * Reads the monitored rail: input voltage, output voltage, input current, input power and MOSFET
37
+ * temperature. The constructor performs no bus access and never writes the chip. Telemetry is PMBus
38
+ * DIRECT; the current and power coefficients depend on the sense resistor and the CL pin.
39
+ */
40
+ class LM25066Minimal {
41
+ /**
42
+ * @param {object} connection - RegisterConnection (I2C/SMBus, 1-byte command; address 0x10-0x17, 0x40-0x47, 0x50-0x5A).
43
+ * @param {number} [rSenseMohm=1.0] - Sense resistor in milliohms.
44
+ * @param {boolean} [currentLimitHigh=false] - CL pin at VDD (46 mV) when true, GND (25 mV) when false.
45
+ */
46
+ constructor(connection, rSenseMohm = 1.0, currentLimitHigh = false) {
47
+ this._conn = connection;
48
+ this._rs = rSenseMohm;
49
+ this._setCl(currentLimitHigh);
50
+ }
51
+
52
+ _setCl(high) {
53
+ this._clHigh = !!high;
54
+ const rs = this._rs;
55
+ if (this._clHigh) {
56
+ this._amp = [6854 * rs, -3100, -2];
57
+ this._watt = [369 * rs, -1900, -2];
58
+ } else {
59
+ this._amp = [13661 * rs, -5200, -2];
60
+ this._watt = [736 * rs, -3300, -2];
61
+ }
62
+ }
63
+
64
+ async _readWord(cmd) {
65
+ const raw = await this._conn.readReg(cmd, 2);
66
+ return raw[0] | (raw[1] << 8);
67
+ }
68
+
69
+ async _writeWord(cmd, value) {
70
+ await this._conn.writeReg(cmd, Buffer.from([value & 0xFF, (value >> 8) & 0xFF]));
71
+ }
72
+
73
+ async _readByte(cmd) {
74
+ return (await this._conn.readReg(cmd, 1))[0];
75
+ }
76
+
77
+ async _readY(cmd) {
78
+ return (await this._readWord(cmd)) & 0x0FFF;
79
+ }
80
+
81
+ /** @returns {Promise<number>} Input voltage VIN in volts. */
82
+ async voltageIn() { return fromY(await this._readY(CMD.READ_VIN), VOLT); }
83
+
84
+ /** @returns {Promise<number>} Output voltage VOUT in volts. */
85
+ async voltageOut() { return fromY(await this._readY(CMD.READ_VOUT), VOLT); }
86
+
87
+ /** @returns {Promise<number>} Instantaneous input current in amperes. */
88
+ async current() { return fromY(await this._readY(CMD.READ_IIN), this._amp); }
89
+
90
+ /** @returns {Promise<number>} Instantaneous input power in watts. */
91
+ async power() { return fromY(await this._readY(CMD.READ_PIN), this._watt); }
92
+
93
+ /** @returns {Promise<number>} External MOSFET temperature in degrees Celsius. */
94
+ async temperature() { return signed16(await this._readWord(CMD.READ_TEMP)) / 16; }
95
+ }
96
+
97
+ /**
98
+ * LM25066 full interface - averaging, limits, status, control, alerts.
99
+ *
100
+ * WARNING: operationOff() cuts the load rail and setGateMask() can stop hardware faults from
101
+ * switching the MOSFET off (which may destroy it). Neither is ever called by the constructor.
102
+ * SOURCE_* constants are ALERT_MASK bit positions (1 = masked).
103
+ */
104
+ class LM25066Full extends LM25066Minimal {
105
+ constructor(connection, rSenseMohm = 1.0, currentLimitHigh = false) {
106
+ super(connection, rSenseMohm, currentLimitHigh);
107
+ this._callback = null;
108
+ this._intPinUsed = null;
109
+ this._pollTimer = null;
110
+ this._polling = false;
111
+ this._edgeHandler = () => { this._service(); };
112
+ }
113
+
114
+ async _block(cmd) {
115
+ const raw = await this._conn.readReg(cmd, 13);
116
+ if (raw[0] !== 12) throw new Error(`LM25066 block read count ${raw[0]} != 12`);
117
+ const w = [];
118
+ for (let i = 0; i < 6; i++) w.push(raw[1 + 2 * i] | (raw[2 + 2 * i] << 8));
119
+ return {
120
+ diagnostic: w[0],
121
+ current: fromY(w[1] & 0x0FFF, this._amp),
122
+ voltageOut: fromY(w[2] & 0x0FFF, VOLT),
123
+ voltageIn: fromY(w[3] & 0x0FFF, VOLT),
124
+ power: fromY(w[4] & 0x0FFF, this._watt),
125
+ temperature: signed16(w[5]) / 16,
126
+ };
127
+ }
128
+
129
+ async _text(cmd, n) {
130
+ const raw = await this._conn.readReg(cmd, n + 1);
131
+ if (raw[0] !== n) throw new Error(`LM25066 block read count ${raw[0]} != ${n}`);
132
+ return Buffer.from(raw.subarray(1)).filter((c) => c !== 0).toString('latin1');
133
+ }
134
+
135
+ /** @returns {Promise<number>} VAUX pin voltage in volts (0 ... 1.16 V). */
136
+ async voltageAux() { return fromY(await this._readY(CMD.READ_VAUX), VAUX); }
137
+
138
+ /** @returns {Promise<object>} BLOCK_READ: {diagnostic, current, voltageOut, voltageIn, power, temperature}. */
139
+ async readBlock() { return this._block(CMD.BLOCK_READ); }
140
+
141
+ /** @returns {Promise<object>} AVG_BLOCK_READ: same keys as readBlock(). */
142
+ async readAverage() { return this._block(CMD.AVG_BLOCK_READ); }
143
+
144
+ /** @returns {Promise<object>} BLACK_BOX_READ snapshot latched at the first SMBA since clearFaults(). */
145
+ async blackBox() { return this._block(CMD.BLACK_BOX); }
146
+
147
+ /** @returns {Promise<number>} Averaged VIN in volts. */
148
+ async voltageInAvg() { return fromY(await this._readY(CMD.READ_AVG_VIN), VOLT); }
149
+
150
+ /** @returns {Promise<number>} Averaged VOUT in volts. */
151
+ async voltageOutAvg() { return fromY(await this._readY(CMD.READ_AVG_VOUT), VOLT); }
152
+
153
+ /** @returns {Promise<number>} Averaged input current in amperes. */
154
+ async currentAvg() { return fromY(await this._readY(CMD.READ_AVG_IIN), this._amp); }
155
+
156
+ /** @returns {Promise<number>} Averaged input power in watts. */
157
+ async powerAvg() { return fromY(await this._readY(CMD.READ_AVG_PIN), this._watt); }
158
+
159
+ /** @returns {Promise<number>} Peak input power since reset / clearPowerPeak(), in watts. */
160
+ async powerPeak() { return fromY(await this._readY(CMD.READ_PIN_PEAK), this._watt); }
161
+
162
+ /** Reset the peak-power register (CLEAR_PIN_PEAK, send byte). @returns {Promise<void>} */
163
+ async clearPowerPeak() { await this._conn.writeReg(CMD.CLEAR_PIN_PEAK, Buffer.alloc(0)); }
164
+
165
+ /**
166
+ * Average over 2^exponent samples.
167
+ * @param {number} exponent - 0-12.
168
+ * @returns {Promise<void>}
169
+ * @throws {RangeError} If exponent is outside 0-12.
170
+ */
171
+ async setAveraging(exponent) {
172
+ if (!Number.isInteger(exponent) || exponent < 0 || exponent > 12) throw new RangeError('exponent must be 0-12');
173
+ await this._conn.writeReg(CMD.SAMPLES_FOR_AVG, Buffer.from([exponent]));
174
+ }
175
+
176
+ /** @returns {Promise<number>} SAMPLES_FOR_AVG exponent. */
177
+ async getAveraging() { return this._readByte(CMD.SAMPLES_FOR_AVG); }
178
+
179
+ async _setLimit(cmd, value, coef, disabled) {
180
+ await this._writeWord(cmd, value === null || value === undefined ? disabled : toY(value, coef));
181
+ }
182
+
183
+ async _getLimit(cmd, coef, disabled) {
184
+ const y = (await this._readWord(cmd)) & 0x0FFF;
185
+ return y === disabled ? null : fromY(y, coef);
186
+ }
187
+
188
+ /** VIN over-voltage warning in volts (null disables). @param {number|null} volts @returns {Promise<void>} */
189
+ async setVinOvWarn(volts) { await this._setLimit(CMD.VIN_OV_WARN, volts, VOLT, 0x0FFF); }
190
+
191
+ /** VIN under-voltage warning in volts (null disables). @param {number|null} volts @returns {Promise<void>} */
192
+ async setVinUvWarn(volts) { await this._setLimit(CMD.VIN_UV_WARN, volts, VOLT, 0x0000); }
193
+
194
+ /** VOUT under-voltage warning in volts (null disables). @param {number|null} volts @returns {Promise<void>} */
195
+ async setVoutUvWarn(volts) { await this._setLimit(CMD.VOUT_UV_WARN, volts, VOLT, 0x0000); }
196
+
197
+ /** Over-temperature warning in Celsius (null disables). @param {number|null} celsius @returns {Promise<void>} */
198
+ async setOtWarn(celsius) { await this._setLimit(CMD.OT_WARN, celsius, TEMP, 0x0FFF); }
199
+
200
+ /** Over-temperature fault in Celsius (null disables). @param {number|null} celsius @returns {Promise<void>} */
201
+ async setOtFault(celsius) { await this._setLimit(CMD.OT_FAULT, celsius, TEMP, 0x0FFF); }
202
+
203
+ /** Input over-current warning in amperes (null disables). @param {number|null} amps @returns {Promise<void>} */
204
+ async setIinWarn(amps) { await this._setLimit(CMD.IIN_WARN, amps, this._amp, 0x0FFF); }
205
+
206
+ /** Input over-power warning in watts (null disables). @param {number|null} watts @returns {Promise<void>} */
207
+ async setPinWarn(watts) { await this._setLimit(CMD.PIN_WARN, watts, this._watt, 0x0FFF); }
208
+
209
+ /**
210
+ * Read all warning / fault thresholds.
211
+ * @returns {Promise<object>} {vinOvWarn, vinUvWarn, voutUvWarn, otWarn, otFault, iinWarn, pinWarn}; null where disabled.
212
+ */
213
+ async getLimits() {
214
+ return {
215
+ vinOvWarn: await this._getLimit(CMD.VIN_OV_WARN, VOLT, 0x0FFF),
216
+ vinUvWarn: await this._getLimit(CMD.VIN_UV_WARN, VOLT, 0x0000),
217
+ voutUvWarn: await this._getLimit(CMD.VOUT_UV_WARN, VOLT, 0x0000),
218
+ otWarn: await this._getLimit(CMD.OT_WARN, TEMP, 0x0FFF),
219
+ otFault: await this._getLimit(CMD.OT_FAULT, TEMP, 0x0FFF),
220
+ iinWarn: await this._getLimit(CMD.IIN_WARN, this._amp, 0x0FFF),
221
+ pinWarn: await this._getLimit(CMD.PIN_WARN, this._watt, 0x0FFF),
222
+ };
223
+ }
224
+
225
+ /** @returns {Promise<number>} STATUS_BYTE. */
226
+ async statusByte() { return this._readByte(CMD.STATUS_BYTE); }
227
+ /** @returns {Promise<number>} STATUS_WORD. */
228
+ async statusWord() { return this._readWord(CMD.STATUS_WORD); }
229
+ /** @returns {Promise<number>} STATUS_VOUT. */
230
+ async statusVout() { return this._readByte(CMD.STATUS_VOUT); }
231
+ /** @returns {Promise<number>} STATUS_INPUT. */
232
+ async statusInput() { return this._readByte(CMD.STATUS_INPUT); }
233
+ /** @returns {Promise<number>} STATUS_TEMPERATURE. */
234
+ async statusTemperature() { return this._readByte(CMD.STATUS_TEMP); }
235
+ /** @returns {Promise<number>} STATUS_CML. */
236
+ async statusCml() { return this._readByte(CMD.STATUS_CML); }
237
+ /** @returns {Promise<number>} STATUS_MFR_SPECIFIC. */
238
+ async statusMfr() { return this._readByte(CMD.STATUS_MFR); }
239
+ /** @returns {Promise<number>} READ_DIAGNOSTIC_WORD. */
240
+ async diagnosticWord() { return this._readWord(CMD.DIAGNOSTIC); }
241
+
242
+ /** CLEAR_FAULTS (send byte); does not switch the MOSFET on. @returns {Promise<void>} */
243
+ async clearFaults() { await this._conn.writeReg(CMD.CLEAR_FAULTS, Buffer.alloc(0)); }
244
+
245
+ /** OPERATION = 0x80: switches the load on. @returns {Promise<void>} */
246
+ async operationOn() { await this._conn.writeReg(CMD.OPERATION, Buffer.from([0x80])); }
247
+
248
+ /** OPERATION = 0x00: CUTS THE LOAD RAIL. @returns {Promise<void>} */
249
+ async operationOff() { await this._conn.writeReg(CMD.OPERATION, Buffer.from([0x00])); }
250
+
251
+ /** @returns {Promise<boolean>} True if OPERATION reads 0x80 and the STATUS_BYTE OFF bit is clear. */
252
+ async isOn() {
253
+ return (await this._readByte(CMD.OPERATION)) === 0x80 && !((await this._readByte(CMD.STATUS_BYTE)) & 0x40);
254
+ }
255
+
256
+ /** @returns {Promise<number>} DEVICE_SETUP. */
257
+ async deviceSetup() { return this._readByte(CMD.DEVICE_SETUP); }
258
+
259
+ /**
260
+ * Override the RETRY / CL / CB pin settings from software; only the given options are overridden.
261
+ * Updates the driver coefficients when currentLimitHigh is given.
262
+ * @param {object} [options]
263
+ * @param {number} [options.retryCode] - 0-7 (bits 7:5; 0 pin, 1 none, 2 = 1 retry, 3 = 2, 4 = 4, 5 = 8, 6 = 16, 7 = unlimited).
264
+ * @param {boolean} [options.currentLimitHigh] - true = 46 mV, false = 25 mV.
265
+ * @param {boolean} [options.cbRatioHigh] - true = 3.6x, false = 1.8x.
266
+ * @returns {Promise<void>}
267
+ * @throws {RangeError} If retryCode is outside 0-7.
268
+ */
269
+ async configureDevice(options = {}) {
270
+ const { retryCode, currentLimitHigh, cbRatioHigh } = options;
271
+ let value = 0;
272
+ if (retryCode !== undefined && retryCode !== null) {
273
+ if (!Number.isInteger(retryCode) || retryCode < 0 || retryCode > 7) throw new RangeError('retryCode must be 0-7');
274
+ value |= retryCode << 5;
275
+ }
276
+ if (currentLimitHigh !== undefined && currentLimitHigh !== null) value |= 0x04 | (currentLimitHigh ? 0x10 : 0);
277
+ if (cbRatioHigh !== undefined && cbRatioHigh !== null) value |= 0x02 | (cbRatioHigh ? 0x08 : 0);
278
+ await this._conn.writeReg(CMD.DEVICE_SETUP, Buffer.from([value]));
279
+ if (currentLimitHigh !== undefined && currentLimitHigh !== null) this._setCl(currentLimitHigh);
280
+ }
281
+
282
+ /**
283
+ * Write GATE_MASK. DANGEROUS: can disable fault shut-off of the MOSFET.
284
+ * @param {number} mask - Bits 5, 4, 2 only.
285
+ * @returns {Promise<void>}
286
+ * @throws {RangeError} If other bits are set.
287
+ */
288
+ async setGateMask(mask) {
289
+ if (mask & ~0x34) throw new RangeError('only GATE_MASK bits 5, 4, 2 may be set');
290
+ await this._conn.writeReg(CMD.GATE_MASK, Buffer.from([mask]));
291
+ }
292
+
293
+ /** @returns {Promise<number>} GATE_MASK. */
294
+ async getGateMask() { return this._readByte(CMD.GATE_MASK); }
295
+
296
+ /** @returns {Promise<number>} CAPABILITY. */
297
+ async capability() { return this._readByte(CMD.CAPABILITY); }
298
+
299
+ /** @returns {Promise<string>} MFR_ID. */
300
+ async manufacturerId() { return this._text(CMD.MFR_ID, 3); }
301
+
302
+ /** @returns {Promise<string>} MFR_MODEL. */
303
+ async model() { return this._text(CMD.MFR_MODEL, 8); }
304
+
305
+ /** @returns {Promise<string>} MFR_REVISION. */
306
+ async revision() { return this._text(CMD.MFR_REVISION, 2); }
307
+
308
+ /** Unmask alert source bit(s) in ALERT_MASK. @param {number} source @returns {Promise<void>} */
309
+ async enableInterrupt(source) {
310
+ await this._writeWord(CMD.ALERT_MASK, (await this._readWord(CMD.ALERT_MASK)) & ~source & 0xFFFF);
311
+ }
312
+
313
+ /** Mask alert source bit(s) in ALERT_MASK. @param {number} source @returns {Promise<void>} */
314
+ async disableInterrupt(source) {
315
+ await this._writeWord(CMD.ALERT_MASK, (await this._readWord(CMD.ALERT_MASK)) | source);
316
+ }
317
+
318
+ async _service() {
319
+ if (this._polling) return;
320
+ this._polling = true;
321
+ try {
322
+ const status = await this.pollInterrupt();
323
+ if (status && this._callback) this._callback(status);
324
+ } finally {
325
+ this._polling = false;
326
+ }
327
+ }
328
+
329
+ /**
330
+ * Subscribe to SMBA (falling edge); falls back to a 1 s poll without an interrupt pin.
331
+ * @param {function(number): void} callback - Receives READ_DIAGNOSTIC_WORD.
332
+ * @param {object} [options]
333
+ * @param {object} [options.intPin] - InputPin overriding connection.intPin.
334
+ * @returns {Promise<void>}
335
+ */
336
+ async onInterrupt(callback, options = {}) {
337
+ this._callback = callback;
338
+ const intPin = options.intPin ?? this._conn.intPin ?? null;
339
+ this._intPinUsed = intPin;
340
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
341
+ if (intPin) {
342
+ await intPin.onEdge(this._edgeHandler, 'falling');
343
+ } else {
344
+ this._pollTimer = setInterval(() => { this._service(); }, 1000);
345
+ }
346
+ }
347
+
348
+ /** Unsubscribe and stop delivery. @returns {Promise<void>} */
349
+ async offInterrupt() {
350
+ if (this._intPinUsed) {
351
+ await this._intPinUsed.offEdge(this._edgeHandler);
352
+ this._intPinUsed = null;
353
+ }
354
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
355
+ this._callback = null;
356
+ }
357
+
358
+ /** Read READ_DIAGNOSTIC_WORD (does not clear; call clearFaults()). @returns {Promise<number>} */
359
+ async pollInterrupt() { return this._readWord(CMD.DIAGNOSTIC); }
360
+ }
361
+
362
+ Object.assign(LM25066Full, {
363
+ SOURCE_VOUT_UV_WARN: 0x8000, SOURCE_IIN_WARN: 0x4000, SOURCE_VIN_UV_WARN: 0x2000,
364
+ SOURCE_VIN_OV_WARN: 0x1000, SOURCE_POWER_GOOD: 0x0800, SOURCE_OT_WARN: 0x0400,
365
+ SOURCE_PIN_OP_WARN: 0x0100, SOURCE_MOSFET_SHORTED: 0x0040, SOURCE_VIN_UV_FAULT: 0x0020,
366
+ SOURCE_VIN_OV_FAULT: 0x0010, SOURCE_IIN_FAULT: 0x0008, SOURCE_OT_FAULT: 0x0004,
367
+ SOURCE_CML_FAULT: 0x0002, SOURCE_CB_FAULT: 0x0001,
368
+ });
369
+
370
+ module.exports = { LM25066Minimal, LM25066Full };
@@ -0,0 +1,297 @@
1
+ 'use strict';
2
+
3
+ const _REG_SENSE = 0x00;
4
+ const _REG_VIN = 0x02;
5
+ const _REG_ADIN = 0x04;
6
+ const _REG_CONTROL = 0x06;
7
+
8
+ const _LSB_SENSE = 20e-6;
9
+ const _LSB_VIN = 0.025;
10
+ const _LSB_ADIN = 0.0005;
11
+
12
+ const _CTRL_SNAPSHOT = 0x80;
13
+ const _CTRL_TEST = 0x10;
14
+ const _CTRL_PAGE_RW = 0x08;
15
+ const _CTRL_STUCK_BUS = 0x04;
16
+
17
+ const _SNAPSHOT_TIMEOUT_MS = 100;
18
+
19
+ function _code(high, low) { return (high << 4) | (low >> 4); }
20
+ function _sleep(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); }
21
+
22
+ /**
23
+ * LTC4151 7–80 V high-side current and voltage monitor — minimal interface.
24
+ *
25
+ * Provides supply voltage, load current and power with one call each. The chip
26
+ * scans SENSE, VIN and ADIN continuously after power-up, so no configuration is
27
+ * needed beyond the connection and the sense resistor; the constructor makes
28
+ * no bus access.
29
+ *
30
+ * Default configuration (chip power-on state, not rewritten):
31
+ * - CONTROL = 0x0C: continuous scan, page read/write and stuck-bus timer enabled
32
+ * - 20 mΩ sense resistor (full-scale current 4.096 A)
33
+ */
34
+ class LTC4151Minimal {
35
+ /**
36
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x67–0x6F).
37
+ * @param {number} [rSense=0.02] - Sense resistor in ohms.
38
+ */
39
+ constructor(connection, rSense = 0.02) {
40
+ if (!(rSense > 0)) throw new RangeError('rSense must be > 0');
41
+ this._conn = connection;
42
+ this._rSense = rSense;
43
+ }
44
+
45
+ /**
46
+ * Read the supply voltage at VIN.
47
+ * @returns {Promise<number>} VIN in volts (code × 25 mV).
48
+ */
49
+ async voltage() {
50
+ const d = await this._conn.readReg(_REG_VIN, 2);
51
+ return _code(d[0], d[1]) * _LSB_VIN;
52
+ }
53
+
54
+ /**
55
+ * Read the load current through the sense resistor.
56
+ * @returns {Promise<number>} Current in amperes (sense voltage / rSense).
57
+ */
58
+ async current() {
59
+ const d = await this._conn.readReg(_REG_SENSE, 2);
60
+ return _code(d[0], d[1]) * _LSB_SENSE / this._rSense;
61
+ }
62
+
63
+ /**
64
+ * Read the load power from one consistent block read of SENSE and VIN.
65
+ * @returns {Promise<number>} Power in watts (VIN × current).
66
+ */
67
+ async power() {
68
+ const d = await this._conn.readReg(_REG_SENSE, 4);
69
+ return _code(d[2], d[3]) * _LSB_VIN * (_code(d[0], d[1]) * _LSB_SENSE / this._rSense);
70
+ }
71
+ }
72
+
73
+ /**
74
+ * LTC4151 full interface — extends LTC4151Minimal with all channels, snapshot mode and CONTROL access.
75
+ *
76
+ * Channel constants (pass to readRaw, snapshot, isBusy):
77
+ * - LTC4151Full.SENSE = 0, LTC4151Full.VIN = 1, LTC4151Full.ADIN = 2
78
+ */
79
+ class LTC4151Full extends LTC4151Minimal {
80
+ static SENSE = 0;
81
+ static VIN = 1;
82
+ static ADIN = 2;
83
+
84
+ /**
85
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x67–0x6F).
86
+ * @param {number} [rSense=0.02] - Sense resistor in ohms.
87
+ */
88
+ constructor(connection, rSense = 0.02) {
89
+ super(connection, rSense);
90
+ this._pageRw = true;
91
+ }
92
+
93
+ async _readBlock(reg, length) {
94
+ if (this._pageRw || length === 1) return this._conn.readReg(reg, length);
95
+ const out = Buffer.alloc(length);
96
+ for (let i = 0; i < length; i++) {
97
+ out[i] = (await this._conn.readReg(reg + i, 1))[0];
98
+ }
99
+ return out;
100
+ }
101
+
102
+ async _readControlRaw() {
103
+ return (await this._conn.readReg(_REG_CONTROL, 1))[0];
104
+ }
105
+
106
+ async _writeControlRaw(value) {
107
+ await this._conn.writeReg(_REG_CONTROL, Buffer.from([value & 0xFF]));
108
+ }
109
+
110
+ static _checkChannel(channel) {
111
+ if (channel !== 0 && channel !== 1 && channel !== 2) throw new RangeError('channel must be 0, 1 or 2');
112
+ }
113
+
114
+ /**
115
+ * Read the voltage across the sense resistor.
116
+ * @returns {Promise<number>} SENSE+ − SENSE− in volts (code × 20 µV).
117
+ */
118
+ async senseVoltage() {
119
+ const d = await this._readBlock(_REG_SENSE, 2);
120
+ return _code(d[0], d[1]) * _LSB_SENSE;
121
+ }
122
+
123
+ /**
124
+ * Read the ADIN pin voltage.
125
+ * @returns {Promise<number>} ADIN in volts (code × 0.5 mV).
126
+ */
127
+ async adinVoltage() {
128
+ const d = await this._readBlock(_REG_ADIN, 2);
129
+ return _code(d[0], d[1]) * _LSB_ADIN;
130
+ }
131
+
132
+ /**
133
+ * Read the supply voltage at VIN.
134
+ * @returns {Promise<number>} VIN in volts (code × 25 mV).
135
+ */
136
+ async voltage() {
137
+ const d = await this._readBlock(_REG_VIN, 2);
138
+ return _code(d[0], d[1]) * _LSB_VIN;
139
+ }
140
+
141
+ /**
142
+ * Read the load current through the sense resistor.
143
+ * @returns {Promise<number>} Current in amperes (sense voltage / rSense).
144
+ */
145
+ async current() { return (await this.senseVoltage()) / this._rSense; }
146
+
147
+ /**
148
+ * Read the load power from one consistent block read of SENSE and VIN.
149
+ * @returns {Promise<number>} Power in watts (VIN × current).
150
+ */
151
+ async power() {
152
+ const d = await this._readBlock(_REG_SENSE, 4);
153
+ return _code(d[2], d[3]) * _LSB_VIN * (_code(d[0], d[1]) * _LSB_SENSE / this._rSense);
154
+ }
155
+
156
+ /**
157
+ * Read all three channels from one block read of registers 0x00–0x05.
158
+ * @returns {Promise<{vin: number, senseV: number, current: number, power: number, adin: number}>}
159
+ * VIN in V, sense voltage in V, current in A, power in W, ADIN in V.
160
+ */
161
+ async readAll() {
162
+ const d = await this._readBlock(_REG_SENSE, 6);
163
+ const senseV = _code(d[0], d[1]) * _LSB_SENSE;
164
+ const vin = _code(d[2], d[3]) * _LSB_VIN;
165
+ const adin = _code(d[4], d[5]) * _LSB_ADIN;
166
+ const current = senseV / this._rSense;
167
+ return { vin, senseV, current, power: vin * current, adin };
168
+ }
169
+
170
+ /**
171
+ * Read the 12-bit code of a channel.
172
+ * @param {number} channel - SENSE (0), VIN (1) or ADIN (2).
173
+ * @returns {Promise<number>} 12-bit conversion code, 0–4095.
174
+ */
175
+ async readRaw(channel) {
176
+ LTC4151Full._checkChannel(channel);
177
+ const d = await this._readBlock(channel * 2, 2);
178
+ return _code(d[0], d[1]);
179
+ }
180
+
181
+ /**
182
+ * Set the sense resistor value used for current and power.
183
+ * @param {number} ohms - Sense resistor in ohms; must be greater than 0.
184
+ */
185
+ setSenseResistor(ohms) {
186
+ if (!(ohms > 0)) throw new RangeError('ohms must be > 0');
187
+ this._rSense = ohms;
188
+ }
189
+
190
+ /**
191
+ * Get the sense resistor value.
192
+ * @returns {number} Sense resistor in ohms.
193
+ */
194
+ getSenseResistor() { return this._rSense; }
195
+
196
+ /**
197
+ * Run one snapshot conversion of a channel, then return to continuous scan.
198
+ *
199
+ * Clears the snapshot bit first, then writes the channel with the snapshot bit
200
+ * set, polls the channel's BUSY bit (timeout 100 ms), reads the result and
201
+ * restores continuous scan.
202
+ *
203
+ * @param {number} channel - SENSE (0), VIN (1) or ADIN (2).
204
+ * @returns {Promise<number>} Converted value in volts (sense voltage, VIN or ADIN).
205
+ */
206
+ async snapshot(channel) {
207
+ LTC4151Full._checkChannel(channel);
208
+ const base = (await this._readControlRaw()) & (_CTRL_PAGE_RW | _CTRL_STUCK_BUS);
209
+ await this._writeControlRaw(base);
210
+ await this._writeControlRaw(base | _CTRL_SNAPSHOT | (channel << 5));
211
+ const start = Date.now();
212
+ while (await this.isBusy(channel)) {
213
+ if (Date.now() - start > _SNAPSHOT_TIMEOUT_MS) {
214
+ await this._writeControlRaw(base);
215
+ throw new Error('LTC4151 snapshot timeout');
216
+ }
217
+ await _sleep(2);
218
+ }
219
+ const code = await this.readRaw(channel);
220
+ await this._writeControlRaw(base);
221
+ return code * [_LSB_SENSE, _LSB_VIN, _LSB_ADIN][channel];
222
+ }
223
+
224
+ /** Switch to continuous scan mode (snapshot bit cleared). */
225
+ async setContinuous() {
226
+ const value = (await this._readControlRaw()) & ~(_CTRL_SNAPSHOT | _CTRL_TEST);
227
+ await this._writeControlRaw(value);
228
+ }
229
+
230
+ /**
231
+ * Read the snapshot bit of CONTROL.
232
+ * @returns {Promise<boolean>} True if the chip is in snapshot mode.
233
+ */
234
+ async isSnapshotMode() { return ((await this._readControlRaw()) & _CTRL_SNAPSHOT) !== 0; }
235
+
236
+ /**
237
+ * Read the BUSY bit of a channel (snapshot mode).
238
+ * @param {number} channel - SENSE (0), VIN (1) or ADIN (2).
239
+ * @returns {Promise<boolean>} True while that channel is being converted.
240
+ */
241
+ async isBusy(channel) {
242
+ LTC4151Full._checkChannel(channel);
243
+ return (((await this._conn.readReg(channel * 2 + 1, 1))[0]) & 0x08) !== 0;
244
+ }
245
+
246
+ /**
247
+ * Enable or disable the stuck-bus reset timer (CONTROL.G2, read-modify-write).
248
+ * @param {boolean} enabled - True to enable (power-on default).
249
+ */
250
+ async setStuckBusTimer(enabled) {
251
+ let value = (await this._readControlRaw()) & ~_CTRL_TEST;
252
+ value = enabled ? (value | _CTRL_STUCK_BUS) : (value & ~_CTRL_STUCK_BUS);
253
+ await this._writeControlRaw(value);
254
+ }
255
+
256
+ /**
257
+ * Enable or disable I²C page read/write (CONTROL.G3, read-modify-write).
258
+ * When disabled the chip only supports byte/word transfers, so this driver
259
+ * then reads each register separately.
260
+ * @param {boolean} enabled - True to enable (power-on default).
261
+ */
262
+ async setPageRw(enabled) {
263
+ let value = (await this._readControlRaw()) & ~_CTRL_TEST;
264
+ value = enabled ? (value | _CTRL_PAGE_RW) : (value & ~_CTRL_PAGE_RW);
265
+ await this._writeControlRaw(value);
266
+ this._pageRw = !!enabled;
267
+ }
268
+
269
+ /**
270
+ * Read the CONTROL register.
271
+ * @returns {Promise<number>} Raw CONTROL value (power-on default 0x0C).
272
+ */
273
+ async readControl() { return this._readControlRaw(); }
274
+
275
+ /**
276
+ * Write the CONTROL register. Test mode (bit 4) is always masked to 0.
277
+ * @param {number} value - Raw CONTROL value.
278
+ */
279
+ async writeControl(value) {
280
+ const v = value & ~_CTRL_TEST & 0xFF;
281
+ await this._writeControlRaw(v);
282
+ this._pageRw = (v & _CTRL_PAGE_RW) !== 0;
283
+ }
284
+
285
+ /**
286
+ * Convert an ADIN/VIN code pair to temperature (datasheet NTC example circuit).
287
+ * Valid for roughly 20–60 °C and only for the datasheet circuit.
288
+ * @param {number} adinCode - 12-bit ADIN code.
289
+ * @param {number} vinCode - 12-bit VIN code.
290
+ * @returns {number} Temperature in °C, 58.82 × (adinCode / vinCode − 0.1066).
291
+ */
292
+ static temperatureFromAdin(adinCode, vinCode) {
293
+ return 58.82 * (adinCode / vinCode - 0.1066);
294
+ }
295
+ }
296
+
297
+ module.exports = { LTC4151Minimal, LTC4151Full };