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,350 @@
1
+ 'use strict';
2
+
3
+ const _REG_MODE1 = 0x00;
4
+ const _REG_MODE2 = 0x01;
5
+ const _REG_SUBADR1 = 0x02;
6
+ const _REG_ALLCALLADR = 0x05;
7
+ const _REG_LED0 = 0x06;
8
+ const _REG_ALL_LED = 0xFA;
9
+ const _REG_PRE_SCALE = 0xFE;
10
+
11
+ const _RESTART = 0x80;
12
+ const _EXTCLK = 0x40;
13
+ const _AI = 0x20;
14
+ const _SLEEP = 0x10;
15
+
16
+ const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
17
+
18
+ function _check(value, name, max) {
19
+ if (!Number.isInteger(value) || value < 0 || value > max) throw new RangeError(`${name} must be in 0..${max}`);
20
+ }
21
+
22
+ function _dutyBytes(duty) {
23
+ if (typeof duty !== 'number' || !(duty >= 0 && duty <= 1)) throw new RangeError('duty must be in 0.0..1.0');
24
+ const off = Math.round(duty * 4096);
25
+ if (off <= 0) return Buffer.from([0, 0, 0, 0x10]);
26
+ if (off >= 4096) return Buffer.from([0, 0x10, 0, 0]);
27
+ return Buffer.from([0, 0, off & 0xFF, off >> 8]);
28
+ }
29
+
30
+ function _pwmBytes(on, off) {
31
+ _check(on, 'on', 4095);
32
+ _check(off, 'off', 4095);
33
+ if (on === off) throw new RangeError('on must differ from off');
34
+ return Buffer.from([on & 0xFF, on >> 8, off & 0xFF, off >> 8]);
35
+ }
36
+
37
+ /**
38
+ * PCA9685 16-channel, 12-bit PWM LED/servo controller — minimal interface.
39
+ *
40
+ * Dims LEDs / drives servos on 16 channels with one call each. The constructor starts the
41
+ * initialization (sleep, PRE_SCALE = 5 for 1 kHz, then wake with auto-increment on and ALLCALL off,
42
+ * 1 ms oscillator wait); every bus method waits for it and `ready` exposes it. Outputs are totem-pole,
43
+ * not inverted, and change on the I²C STOP; all channels keep their power-on state (off).
44
+ * Bus calls are serialised internally.
45
+ */
46
+ class PCA9685Minimal {
47
+ /**
48
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (address 0x40-0x6F or 0x71-0x77).
49
+ */
50
+ constructor(connection) {
51
+ this._conn = connection;
52
+ this._chain = Promise.resolve();
53
+ this._ready = this._exec(async () => {
54
+ await this._conn.writeReg(_REG_MODE1, _AI | _SLEEP);
55
+ await this._conn.writeReg(_REG_PRE_SCALE, 5);
56
+ await this._conn.writeReg(_REG_MODE1, _AI);
57
+ await _sleep(1);
58
+ });
59
+ }
60
+
61
+ /** Wait for the init sequence started by the constructor. @returns {Promise<void>} */
62
+ get ready() { return this._ready; }
63
+
64
+ _exec(fn) {
65
+ const p = this._chain.then(fn);
66
+ this._chain = p.catch(() => {});
67
+ return p;
68
+ }
69
+
70
+ /**
71
+ * Set the duty cycle of one channel (ON = 0, OFF = round(duty * 4096)); 0.0 writes full OFF and 1.0 full ON.
72
+ * @param {number} channel - 0-15.
73
+ * @param {number} duty - 0.0-1.0.
74
+ * @returns {Promise<void>}
75
+ */
76
+ setDuty(channel, duty) {
77
+ _check(channel, 'channel', 15);
78
+ const data = _dutyBytes(duty);
79
+ return this._exec(() => this._conn.writeReg(_REG_LED0 + 4 * channel, data));
80
+ }
81
+ }
82
+
83
+ /**
84
+ * PCA9685 full interface — phase-shifted PWM, frequency, servo pulses, modes, addresses.
85
+ */
86
+ class PCA9685Full extends PCA9685Minimal {
87
+ async _mode1() { return (await this._conn.readReg(_REG_MODE1, 1))[0]; }
88
+
89
+ /**
90
+ * Write the ON and OFF counts of one channel in one transaction (phase-shifted PWM).
91
+ * @param {number} channel - 0-15.
92
+ * @param {number} on - 0-4095.
93
+ * @param {number} off - 0-4095, must differ from `on`.
94
+ * @returns {Promise<void>}
95
+ */
96
+ setPwm(channel, on, off) {
97
+ _check(channel, 'channel', 15);
98
+ const data = _pwmBytes(on, off);
99
+ return this._exec(() => this._conn.writeReg(_REG_LED0 + 4 * channel, data));
100
+ }
101
+
102
+ /**
103
+ * Read the ON and OFF counts of one channel (without the full-ON/OFF flags).
104
+ * @param {number} channel - 0-15.
105
+ * @returns {Promise<{on: number, off: number}>}
106
+ */
107
+ getPwm(channel) {
108
+ _check(channel, 'channel', 15);
109
+ return this._exec(async () => {
110
+ const d = await this._conn.readReg(_REG_LED0 + 4 * channel, 4);
111
+ return { on: ((d[1] & 0x0F) << 8) | d[0], off: ((d[3] & 0x0F) << 8) | d[2] };
112
+ });
113
+ }
114
+
115
+ /**
116
+ * Force a channel permanently high (full-ON bit set, full-OFF bit cleared).
117
+ * @param {number} channel - 0-15.
118
+ * @returns {Promise<void>}
119
+ */
120
+ setFullOn(channel) {
121
+ _check(channel, 'channel', 15);
122
+ return this._exec(() => this._conn.writeReg(_REG_LED0 + 4 * channel, Buffer.from([0, 0x10, 0, 0])));
123
+ }
124
+
125
+ /**
126
+ * Force a channel permanently low (full-OFF bit set; it wins over full ON).
127
+ * @param {number} channel - 0-15.
128
+ * @returns {Promise<void>}
129
+ */
130
+ setFullOff(channel) {
131
+ _check(channel, 'channel', 15);
132
+ return this._exec(() => this._conn.writeReg(_REG_LED0 + 4 * channel, Buffer.from([0, 0, 0, 0x10])));
133
+ }
134
+
135
+ /**
136
+ * Set the same duty cycle on all 16 channels (ALL_LED registers).
137
+ * @param {number} duty - 0.0-1.0.
138
+ * @returns {Promise<void>}
139
+ */
140
+ setAllDuty(duty) {
141
+ const data = _dutyBytes(duty);
142
+ return this._exec(() => this._conn.writeReg(_REG_ALL_LED, data));
143
+ }
144
+
145
+ /**
146
+ * Set the same ON and OFF counts on all 16 channels.
147
+ * @param {number} on - 0-4095.
148
+ * @param {number} off - 0-4095, must differ from `on`.
149
+ * @returns {Promise<void>}
150
+ */
151
+ setAllPwm(on, off) {
152
+ const data = _pwmBytes(on, off);
153
+ return this._exec(() => this._conn.writeReg(_REG_ALL_LED, data));
154
+ }
155
+
156
+ /**
157
+ * Set the PWM frequency: PRE_SCALE = round(oscHz / (4096 * hz)) - 1, clamped 3-255. Sleeps the chip,
158
+ * writes PRE_SCALE, wakes it, waits 500 us and restarts running channels.
159
+ * @param {number} hz - Wanted frequency in Hz (24-1526 at 25 MHz).
160
+ * @param {number} [oscHz=25000000] - Oscillator frequency in Hz.
161
+ * @returns {Promise<void>}
162
+ */
163
+ setFrequency(hz, oscHz = 25000000) {
164
+ if (!(hz > 0) || !(oscHz > 0)) throw new RangeError('hz and oscHz must be positive');
165
+ const prescale = Math.max(3, Math.min(255, Math.round(oscHz / (4096 * hz)) - 1));
166
+ return this._exec(async () => {
167
+ const old = await this._mode1();
168
+ const awake = old & ~(_RESTART | _SLEEP) & 0xFF;
169
+ await this._conn.writeReg(_REG_MODE1, (old & 0x7F) | _SLEEP);
170
+ await this._conn.writeReg(_REG_PRE_SCALE, prescale);
171
+ await this._conn.writeReg(_REG_MODE1, awake);
172
+ await _sleep(1);
173
+ if (old & _RESTART) await this._conn.writeReg(_REG_MODE1, awake | _RESTART);
174
+ });
175
+ }
176
+
177
+ /**
178
+ * Read PRE_SCALE and return the PWM frequency.
179
+ * @param {number} [oscHz=25000000] - Oscillator frequency in Hz.
180
+ * @returns {Promise<number>} oscHz / (4096 * (PRE_SCALE + 1)) in Hz.
181
+ */
182
+ getFrequency(oscHz = 25000000) {
183
+ return this._exec(async () => oscHz / (4096 * ((await this._conn.readReg(_REG_PRE_SCALE, 1))[0] + 1)));
184
+ }
185
+
186
+ /**
187
+ * Set a pulse width in seconds (servo/ESC mode): OFF = round(seconds * frequency * 4096).
188
+ * Uses the frequency programmed in PRE_SCALE (25 MHz oscillator).
189
+ * @param {number} channel - 0-15.
190
+ * @param {number} seconds - Pulse width, 0 up to one PWM period.
191
+ * @returns {Promise<void>}
192
+ */
193
+ setPulseWidth(channel, seconds) {
194
+ _check(channel, 'channel', 15);
195
+ if (!(seconds >= 0)) throw new RangeError('seconds must be >= 0');
196
+ return this._exec(async () => {
197
+ const hz = 25000000 / (4096 * ((await this._conn.readReg(_REG_PRE_SCALE, 1))[0] + 1));
198
+ const data = _dutyBytes(Math.min(1, seconds * hz));
199
+ await this._conn.writeReg(_REG_LED0 + 4 * channel, data);
200
+ });
201
+ }
202
+
203
+ /** Enter low-power mode (MODE1.SLEEP = 1, oscillator off). @returns {Promise<void>} */
204
+ sleep() {
205
+ return this._exec(async () => {
206
+ await this._conn.writeReg(_REG_MODE1, ((await this._mode1()) & 0x7F) | _SLEEP);
207
+ });
208
+ }
209
+
210
+ /** Leave low-power mode, wait 500 us and restart channels that were running. @returns {Promise<void>} */
211
+ wake() {
212
+ return this._exec(async () => {
213
+ const old = await this._mode1();
214
+ await this._conn.writeReg(_REG_MODE1, old & ~(_RESTART | _SLEEP) & 0xFF);
215
+ await _sleep(1);
216
+ if (old & _RESTART) await this._conn.writeReg(_REG_MODE1, (old & ~_SLEEP & 0xFF) | _RESTART);
217
+ });
218
+ }
219
+
220
+ /** Datasheet restart sequence (read MODE1, clear SLEEP, wait 500 us, write RESTART). @returns {Promise<void>} */
221
+ restart() {
222
+ return this._exec(async () => {
223
+ const old = await this._mode1();
224
+ await this._conn.writeReg(_REG_MODE1, old & ~(_RESTART | _SLEEP) & 0xFF);
225
+ await _sleep(1);
226
+ await this._conn.writeReg(_REG_MODE1, (old & ~_SLEEP & 0xFF) | _RESTART);
227
+ });
228
+ }
229
+
230
+ /**
231
+ * Configure MODE2: output driver, inversion and the output state while OE = 1.
232
+ * @param {boolean} [openDrain=false] - true for open-drain, false for totem-pole.
233
+ * @param {boolean} [invert=false] - Invert the output logic.
234
+ * @param {number} [offState=PCA9685Full.OFF_LOW] - OFF_LOW, OFF_HIGH_OR_HIGHZ or OFF_HIGHZ.
235
+ * @returns {Promise<void>}
236
+ */
237
+ setOutputMode(openDrain = false, invert = false, offState = PCA9685Full.OFF_LOW) {
238
+ _check(offState, 'offState', 2);
239
+ const mode2 = (invert ? 0x10 : 0) | (openDrain ? 0 : 0x04) | offState;
240
+ return this._exec(async () => {
241
+ const old = (await this._conn.readReg(_REG_MODE2, 1))[0];
242
+ await this._conn.writeReg(_REG_MODE2, (old & 0x08) | mode2);
243
+ });
244
+ }
245
+
246
+ /**
247
+ * Select when outputs change: on ACK (true, MODE2.OCH = 1) or on STOP (false, default).
248
+ * @param {boolean} enabled
249
+ * @returns {Promise<void>}
250
+ */
251
+ setUpdateOnAck(enabled) {
252
+ return this._exec(async () => {
253
+ const old = (await this._conn.readReg(_REG_MODE2, 1))[0];
254
+ await this._conn.writeReg(_REG_MODE2, enabled ? (old | 0x08) : (old & ~0x08 & 0xFF));
255
+ });
256
+ }
257
+
258
+ /**
259
+ * Switch to the EXTCLK pin as clock (sticky until power cycle / software reset). Pass the external
260
+ * clock frequency as `oscHz` to setFrequency() / getFrequency() afterwards.
261
+ * @returns {Promise<void>}
262
+ */
263
+ useExternalClock() {
264
+ return this._exec(async () => {
265
+ const sleeping = ((await this._mode1()) & 0x7F) | _SLEEP;
266
+ await this._conn.writeReg(_REG_MODE1, sleeping);
267
+ await this._conn.writeReg(_REG_MODE1, sleeping | _EXTCLK);
268
+ await this._conn.writeReg(_REG_MODE1, (sleeping | _EXTCLK) & ~_SLEEP & 0xFF);
269
+ await _sleep(1);
270
+ });
271
+ }
272
+
273
+ /**
274
+ * Enable or disable the LED All Call address and set its value.
275
+ * @param {boolean} enabled
276
+ * @param {number} [address=0x70] - 7-bit address.
277
+ * @returns {Promise<void>}
278
+ */
279
+ setAllcall(enabled, address = 0x70) {
280
+ _check(address, 'address', 0x7F);
281
+ return this._exec(async () => {
282
+ await this._conn.writeReg(_REG_ALLCALLADR, address << 1);
283
+ const old = await this._mode1();
284
+ await this._conn.writeReg(_REG_MODE1, (old & 0x7E) | (enabled ? 1 : 0));
285
+ });
286
+ }
287
+
288
+ /**
289
+ * Program and enable/disable one of the three LED Sub Call addresses.
290
+ * @param {number} index - 1-3.
291
+ * @param {number} address - 7-bit address.
292
+ * @param {boolean} [enabled=true]
293
+ * @returns {Promise<void>}
294
+ */
295
+ setSubaddress(index, address, enabled = true) {
296
+ if (!Number.isInteger(index) || index < 1 || index > 3) throw new RangeError('index must be in 1..3');
297
+ _check(address, 'address', 0x7F);
298
+ const mask = 1 << (4 - index);
299
+ return this._exec(async () => {
300
+ await this._conn.writeReg(_REG_SUBADR1 + index - 1, address << 1);
301
+ const old = (await this._mode1()) & 0x7F;
302
+ await this._conn.writeReg(_REG_MODE1, enabled ? (old | mask) : (old & ~mask & 0xFF));
303
+ });
304
+ }
305
+
306
+ /**
307
+ * Return the chip registers to their power-on values by writing the documented defaults
308
+ * (MODE1 = 0x11, MODE2 = 0x04, PRE_SCALE = 0x1E, outputs off). No general-call software reset.
309
+ * @returns {Promise<void>}
310
+ */
311
+ reset() {
312
+ return this._exec(async () => {
313
+ await this._conn.writeReg(_REG_MODE1, 0x10);
314
+ await this._conn.writeReg(_REG_PRE_SCALE, 0x1E);
315
+ await this._conn.writeReg(_REG_MODE2, 0x04);
316
+ await this._conn.writeReg(_REG_ALL_LED, Buffer.from([0, 0, 0, 0x10]));
317
+ await this._conn.writeReg(_REG_SUBADR1, Buffer.from([0xE2, 0xE4, 0xE8, 0xE0]));
318
+ await this._conn.writeReg(_REG_MODE1, 0x11);
319
+ });
320
+ }
321
+
322
+ /**
323
+ * Send the Software Reset call (0x06) over a connection bound to the general call address 0x00.
324
+ * Resets every PCA9685 on the bus; wait at least 500 us before using the chips again.
325
+ * @param {object} generalCall - Connection bound to I²C address 0x00.
326
+ * @returns {Promise<void>}
327
+ */
328
+ static softwareReset(generalCall) {
329
+ return generalCall.write(Buffer.from([0x06]));
330
+ }
331
+
332
+ /**
333
+ * Read the raw mode registers.
334
+ * @returns {Promise<{mode1: number, mode2: number}>}
335
+ */
336
+ readMode() {
337
+ return this._exec(async () => {
338
+ const d = await this._conn.readReg(_REG_MODE1, 2);
339
+ return { mode1: d[0], mode2: d[1] };
340
+ });
341
+ }
342
+ }
343
+
344
+ PCA9685Minimal.CHANNELS = 16;
345
+ PCA9685Minimal.ADDRESS = 0x40;
346
+ PCA9685Full.OFF_LOW = 0;
347
+ PCA9685Full.OFF_HIGH_OR_HIGHZ = 1;
348
+ PCA9685Full.OFF_HIGHZ = 2;
349
+
350
+ module.exports = { PCA9685Minimal, PCA9685Full };
@@ -0,0 +1,229 @@
1
+ 'use strict';
2
+
3
+ const _REG_CONFIG = 0x00;
4
+ const _REG_CURRENT = 0x01;
5
+ const _REG_BUS = 0x02;
6
+ const _REG_POWER = 0x03;
7
+ const _REG_MASK = 0x06;
8
+ const _REG_ALERT = 0x07;
9
+ const _REG_MFR_ID = 0xFE;
10
+ const _REG_DIE_ID = 0xFF;
11
+
12
+ const _CONFIG_BASE = 0x6000; // bits 14:12 must be written as 110
13
+
14
+ /**
15
+ * INA260 36V, 16-bit current/voltage/power monitor with integrated shunt — minimal interface.
16
+ *
17
+ * Provides bus voltage, current, and power readings with no configuration
18
+ * beyond the connection. The shunt is integrated and the current/power
19
+ * registers have fixed LSBs, so no calibration is needed and the constructor
20
+ * makes no bus access.
21
+ *
22
+ * Default configuration (chip power-on state, not rewritten):
23
+ * - MODE = 7: shunt current + bus voltage, continuous
24
+ * - VBUSCT = 4: 1.1 ms bus voltage conversion time
25
+ * - ISHCT = 4: 1.1 ms shunt current conversion time
26
+ * - AVG = 0: 1 sample (no averaging)
27
+ */
28
+ class INA260Minimal {
29
+ /**
30
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (writeRead, write).
31
+ */
32
+ constructor(connection) {
33
+ this._conn = connection;
34
+ }
35
+
36
+ async _writeReg(reg, value) {
37
+ await this._conn.writeReg(reg, Buffer.from([(value >> 8) & 0xFF, value & 0xFF]));
38
+ }
39
+
40
+ async _readReg(reg) {
41
+ return (await this._conn.readReg(reg, 2)).readUInt16BE(0);
42
+ }
43
+
44
+ async _readRegSigned(reg) {
45
+ return (await this._conn.readReg(reg, 2)).readInt16BE(0);
46
+ }
47
+
48
+ /**
49
+ * Read bus voltage.
50
+ * @returns {Promise<number>} Bus voltage in volts (raw × 1.25 mV LSB).
51
+ */
52
+ async voltage() { return (await this._readReg(_REG_BUS)) * 1.25e-3; }
53
+
54
+ /**
55
+ * Read current through the integrated shunt.
56
+ * @returns {Promise<number>} Current in amperes, signed (raw × 1.25 mA LSB); positive flows IN+ → IN−.
57
+ */
58
+ async current() { return (await this._readRegSigned(_REG_CURRENT)) * 1.25e-3; }
59
+
60
+ /**
61
+ * Read calculated power.
62
+ * @returns {Promise<number>} Power in watts (raw × 10 mW LSB), always positive.
63
+ */
64
+ async power() { return (await this._readReg(_REG_POWER)) * 10e-3; }
65
+ }
66
+
67
+ /**
68
+ * INA260 full interface — extends INA260Minimal with configuration and alert support.
69
+ *
70
+ * Adds Configuration Register programming, triggered conversions,
71
+ * conversion-ready and overflow status, alert configuration, reset, and
72
+ * shutdown/wake.
73
+ *
74
+ * Alert function constants (pass to setAlert):
75
+ * - INA260Full.OCL — over-current limit
76
+ * - INA260Full.UCL — under-current limit
77
+ * - INA260Full.BOL — bus voltage over-limit
78
+ * - INA260Full.BUL — bus voltage under-limit
79
+ * - INA260Full.POL — power over-limit
80
+ * - INA260Full.CNVR — conversion ready
81
+ */
82
+ class INA260Full extends INA260Minimal {
83
+ static OCL = 0x8000;
84
+ static UCL = 0x4000;
85
+ static BOL = 0x2000;
86
+ static BUL = 0x1000;
87
+ static POL = 0x0800;
88
+ static CNVR = 0x0400;
89
+ static AFF = 0x0010;
90
+
91
+ static AVG_1 = 0; static AVG_4 = 1; static AVG_16 = 2; static AVG_64 = 3;
92
+ static AVG_128 = 4; static AVG_256 = 5; static AVG_512 = 6; static AVG_1024 = 7;
93
+ static CT_140US = 0; static CT_204US = 1; static CT_332US = 2; static CT_588US = 3;
94
+ static CT_1100US = 4; static CT_2116US = 5; static CT_4156US = 6; static CT_8244US = 7;
95
+ static MODE_POWER_DOWN = 0;
96
+ static MODE_SHUNT_TRIGGERED = 1;
97
+ static MODE_BUS_TRIGGERED = 2;
98
+ static MODE_SHUNT_BUS_TRIGGERED = 3;
99
+ static MODE_SHUNT_CONT = 5;
100
+ static MODE_BUS_CONT = 6;
101
+ static MODE_SHUNT_BUS_CONT = 7;
102
+
103
+ /**
104
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
105
+ */
106
+ constructor(connection) {
107
+ super(connection);
108
+ this._avg = 0;
109
+ this._vbusCt = 4;
110
+ this._ishCt = 4;
111
+ this._mode = 7;
112
+ }
113
+
114
+ async _writeConfig(mode) {
115
+ const config = _CONFIG_BASE | (this._avg << 9) | (this._vbusCt << 6) | (this._ishCt << 3) | (mode & 0x07);
116
+ await this._writeReg(_REG_CONFIG, config);
117
+ }
118
+
119
+ /**
120
+ * Write the Configuration Register (bits 14:12 kept at 110).
121
+ * @param {number} [avg=0] - Averaging count selector 0–7 (AVG_1 … AVG_1024).
122
+ * @param {number} [vbusCt=4] - Bus voltage conversion time selector 0–7 (default 4 = 1.1 ms).
123
+ * @param {number} [ishCt=4] - Shunt current conversion time selector 0–7 (default 4 = 1.1 ms).
124
+ * @param {number} [mode=7] - Operating mode 0–7 (7 = shunt+bus continuous).
125
+ * @returns {Promise<void>}
126
+ */
127
+ async configure(avg = 0, vbusCt = 4, ishCt = 4, mode = 7) {
128
+ this._avg = avg & 0x07;
129
+ this._vbusCt = vbusCt & 0x07;
130
+ this._ishCt = ishCt & 0x07;
131
+ this._mode = mode & 0x07;
132
+ await this._writeConfig(this._mode);
133
+ }
134
+
135
+ /**
136
+ * Start one triggered conversion with the current settings (rewrites the
137
+ * Configuration Register). Poll conversionReady() for completion.
138
+ * @param {number} [mode=3] - MODE_SHUNT_TRIGGERED, MODE_BUS_TRIGGERED or MODE_SHUNT_BUS_TRIGGERED.
139
+ * @returns {Promise<void>}
140
+ */
141
+ async trigger(mode = INA260Full.MODE_SHUNT_BUS_TRIGGERED) { await this._writeConfig(mode); }
142
+
143
+ /**
144
+ * Read the Conversion Ready Flag (CVRF) from the Mask/Enable Register.
145
+ *
146
+ * Note: Reading Mask/Enable clears CVRF. Read it last if also checking other flags.
147
+ *
148
+ * @returns {Promise<boolean>} True if a conversion completed since the last Mask/Enable read.
149
+ */
150
+ async conversionReady() { return !!((await this._readReg(_REG_MASK)) & 0x0008); }
151
+
152
+ /**
153
+ * Read the Math Overflow Flag (OVF) from the Mask/Enable Register.
154
+ * @returns {Promise<boolean>} True if an arithmetic overflow occurred in the power calculation.
155
+ */
156
+ async overflow() { return !!((await this._readReg(_REG_MASK)) & 0x0004); }
157
+
158
+ /**
159
+ * Configure the alert pin function and threshold.
160
+ *
161
+ * Writes the Alert Limit register first, then Mask/Enable. Only one alert
162
+ * function can be active at a time.
163
+ *
164
+ * @param {number} fn - Alert function constant (OCL/UCL/BOL/BUL/POL/CNVR).
165
+ * @param {number} [limit=0] - Threshold in natural units (A for current, V for voltage, W for power); ignored for CNVR.
166
+ * @param {number} [polarity=0] - 0 = active-low (default), 1 = active-high.
167
+ * @param {number} [latch=0] - 0 = transparent (default), 1 = latch until Mask/Enable is read.
168
+ * @returns {Promise<void>}
169
+ */
170
+ async setAlert(fn, limit = 0, polarity = 0, latch = 0) {
171
+ let raw = null;
172
+ if (fn === INA260Full.OCL || fn === INA260Full.UCL ||
173
+ fn === INA260Full.BOL || fn === INA260Full.BUL) raw = Math.round(limit / 1.25e-3);
174
+ else if (fn === INA260Full.POL) raw = Math.round(limit / 10e-3);
175
+ if (raw !== null) await this._writeReg(_REG_ALERT, raw & 0xFFFF);
176
+ const mask = fn | ((polarity & 1) << 1) | (latch & 1);
177
+ await this._writeReg(_REG_MASK, mask);
178
+ }
179
+
180
+ /**
181
+ * Disable all alert functions (write Mask/Enable = 0).
182
+ * @returns {Promise<void>}
183
+ */
184
+ async clearAlert() { await this._writeReg(_REG_MASK, 0); }
185
+
186
+ /**
187
+ * Read the Mask/Enable Register (clears CVRF and, in latch mode, AFF).
188
+ * @returns {Promise<number>} Raw 16-bit value containing alert and status flags.
189
+ */
190
+ async alertFlags() { return this._readReg(_REG_MASK); }
191
+
192
+ /**
193
+ * Reset all registers to power-on defaults (RST bit).
194
+ * @returns {Promise<void>}
195
+ */
196
+ async reset() {
197
+ await this._writeReg(_REG_CONFIG, 0x8000);
198
+ this._avg = 0;
199
+ this._vbusCt = 4;
200
+ this._ishCt = 4;
201
+ this._mode = 7;
202
+ }
203
+
204
+ /**
205
+ * Enter power-down mode (MODE = 000); the previous mode is kept for wake().
206
+ * @returns {Promise<void>}
207
+ */
208
+ async shutdown() { await this._writeConfig(INA260Full.MODE_POWER_DOWN); }
209
+
210
+ /**
211
+ * Restore the operating mode that was active before shutdown().
212
+ * @returns {Promise<void>}
213
+ */
214
+ async wake() { await this._writeConfig(this._mode); }
215
+
216
+ /**
217
+ * Read the Manufacturer ID register.
218
+ * @returns {Promise<number>} Manufacturer ID; expect 0x5449 (Texas Instruments).
219
+ */
220
+ async manufacturerId() { return this._readReg(_REG_MFR_ID); }
221
+
222
+ /**
223
+ * Read the Die ID register.
224
+ * @returns {Promise<number>} Die ID; (value >> 4) is 0x227, the low nibble is the revision.
225
+ */
226
+ async dieId() { return this._readReg(_REG_DIE_ID); }
227
+ }
228
+
229
+ module.exports = { INA260Minimal, INA260Full };