periph 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/accelerometer/.gitkeep +0 -0
- package/examples/adc_dac/.gitkeep +0 -0
- package/examples/adc_dac/mcp4725/complete.js +34 -0
- package/examples/adc_dac/mcp4725/demo.js +43 -0
- package/examples/adc_dac/mcp4725/minimal.js +14 -0
- package/examples/color/.gitkeep +0 -0
- package/examples/comms/.gitkeep +0 -0
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gas/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/gpio/.gitkeep +0 -0
- package/examples/gyroscope/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/io_expander/mcp23017/complete.js +80 -0
- package/examples/io_expander/mcp23017/demo.js +59 -0
- package/examples/io_expander/mcp23017/minimal.js +16 -0
- package/examples/io_expander/pcf8574/complete.js +68 -0
- package/examples/io_expander/pcf8574/demo.js +41 -0
- package/examples/io_expander/pcf8574/minimal.js +16 -0
- package/examples/io_expander/pcf8575/complete.js +31 -0
- package/examples/io_expander/pcf8575/demo.js +33 -0
- package/examples/io_expander/pcf8575/minimal.js +20 -0
- package/examples/led/.gitkeep +0 -0
- package/examples/led/sk6812rgbw/complete.js +69 -0
- package/examples/led/sk6812rgbw/demo.js +85 -0
- package/examples/led/sk6812rgbw/minimal.js +25 -0
- package/examples/led/ws2812b/complete.js +64 -0
- package/examples/led/ws2812b/demo.js +101 -0
- package/examples/led/ws2812b/minimal.js +22 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/magnetometer/.gitkeep +0 -0
- package/examples/magnetometer/as5600/complete.js +43 -0
- package/examples/magnetometer/as5600/demo.js +49 -0
- package/examples/magnetometer/as5600/minimal.js +16 -0
- package/examples/memory/.gitkeep +0 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/other/.gitkeep +0 -0
- package/examples/power/ina219/complete.js +30 -0
- package/examples/power/ina219/demo.js +26 -0
- package/examples/power/ina219/minimal.js +17 -0
- package/examples/power/ina226/complete.js +33 -0
- package/examples/power/ina226/demo.js +30 -0
- package/examples/power/ina226/minimal.js +11 -0
- package/examples/power/ina3221/complete.js +65 -0
- package/examples/power/ina3221/demo.js +56 -0
- package/examples/power/ina3221/minimal.js +16 -0
- package/examples/pressure/.gitkeep +0 -0
- package/examples/pressure/bmp180/complete.js +26 -0
- package/examples/pressure/bmp180/demo.js +32 -0
- package/examples/pressure/bmp180/minimal.js +17 -0
- package/examples/pressure/bmp280/complete.js +28 -0
- package/examples/pressure/bmp280/demo.js +34 -0
- package/examples/pressure/bmp280/minimal.js +17 -0
- package/examples/rfid/.gitkeep +0 -0
- package/examples/rtc/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/package.json +20 -0
- package/src/chips/accelerometer/.gitkeep +0 -0
- package/src/chips/adc_dac/.gitkeep +0 -0
- package/src/chips/adc_dac/mcp4725.js +161 -0
- package/src/chips/color/.gitkeep +0 -0
- package/src/chips/comms/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gas/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/gpio/.gitkeep +0 -0
- package/src/chips/gyroscope/.gitkeep +0 -0
- package/src/chips/humidity/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/io_expander/mcp23017.js +396 -0
- package/src/chips/io_expander/pcf8574.js +298 -0
- package/src/chips/io_expander/pcf8575.js +295 -0
- package/src/chips/led/.gitkeep +0 -0
- package/src/chips/led/_color.js +25 -0
- package/src/chips/led/sk6812rgbw.js +183 -0
- package/src/chips/led/ws2812b.js +171 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/magnetometer/as5600.js +293 -0
- package/src/chips/memory/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/other/.gitkeep +0 -0
- package/src/chips/power/ina219.js +154 -0
- package/src/chips/power/ina226.js +208 -0
- package/src/chips/power/ina3221.js +341 -0
- package/src/chips/pressure/.gitkeep +0 -0
- package/src/chips/pressure/bmp180.js +234 -0
- package/src/chips/pressure/bmp280.js +328 -0
- package/src/chips/rfid/.gitkeep +0 -0
- package/src/chips/rtc/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
- package/src/index.js +2 -0
- package/src/transport/i2c.js +67 -0
- package/src/transport/neopixel.js +37 -0
- package/src/transport/smbus.js +117 -0
- package/src/transport/spi.js +72 -0
- package/src/transport/uart.js +145 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_ZMCO = 0x00;
|
|
4
|
+
const _REG_ZPOS_H = 0x01;
|
|
5
|
+
const _REG_ZPOS_L = 0x02;
|
|
6
|
+
const _REG_MPOS_H = 0x03;
|
|
7
|
+
const _REG_MPOS_L = 0x04;
|
|
8
|
+
const _REG_MANG_H = 0x05;
|
|
9
|
+
const _REG_MANG_L = 0x06;
|
|
10
|
+
const _REG_CONF_H = 0x07;
|
|
11
|
+
const _REG_CONF_L = 0x08;
|
|
12
|
+
const _REG_STATUS = 0x0B;
|
|
13
|
+
const _REG_RAW_ANGLE_H = 0x0C;
|
|
14
|
+
const _REG_RAW_ANGLE_L = 0x0D;
|
|
15
|
+
const _REG_ANGLE_H = 0x0E;
|
|
16
|
+
const _REG_ANGLE_L = 0x0F;
|
|
17
|
+
const _REG_AGC = 0x1A;
|
|
18
|
+
const _REG_MAGNITUDE_H = 0x1B;
|
|
19
|
+
const _REG_MAGNITUDE_L = 0x1C;
|
|
20
|
+
const _REG_BURN = 0xFF;
|
|
21
|
+
|
|
22
|
+
const _STATUS_MD = 0x08;
|
|
23
|
+
const _STATUS_ML = 0x10;
|
|
24
|
+
const _STATUS_MH = 0x20;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* AS5600 12-bit programmable contactless rotary position sensor — minimal interface.
|
|
28
|
+
*
|
|
29
|
+
* Reads the absolute angle in degrees with no configuration required beyond the
|
|
30
|
+
* transport. Verifies magnet presence at construction; throws if no magnet is detected.
|
|
31
|
+
*/
|
|
32
|
+
class AS5600Minimal {
|
|
33
|
+
/**
|
|
34
|
+
* @param {object} transport - Configured I²C transport pointing at the device (fixed address 0x36).
|
|
35
|
+
*/
|
|
36
|
+
constructor(transport) {
|
|
37
|
+
this._transport = transport;
|
|
38
|
+
const status = this._readReg8(_REG_STATUS);
|
|
39
|
+
if (!(status & _STATUS_MD)) {
|
|
40
|
+
throw new Error('AS5600: magnet not detected (MD=0)');
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
_readReg8(reg) {
|
|
45
|
+
return this._transport.writeRead(Buffer.from([reg]), 1)[0];
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
_readReg16(reg) {
|
|
49
|
+
const raw = this._transport.writeRead(Buffer.from([reg]), 2);
|
|
50
|
+
return (raw[0] << 8) | raw[1];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
_writeReg8(reg, value) {
|
|
54
|
+
const buf = Buffer.alloc(2);
|
|
55
|
+
buf[0] = reg;
|
|
56
|
+
buf[1] = value;
|
|
57
|
+
this._transport.write(buf);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
_writeReg16(reg, value) {
|
|
61
|
+
const buf = Buffer.alloc(3);
|
|
62
|
+
buf[0] = reg;
|
|
63
|
+
buf.writeUInt16BE(value, 1);
|
|
64
|
+
this._transport.write(buf);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Read the scaled absolute angle.
|
|
69
|
+
* @returns {number} Angle in degrees, 0.0–360.0 (exclusive).
|
|
70
|
+
*/
|
|
71
|
+
angle() {
|
|
72
|
+
return this.angleRaw() * 360.0 / 4096;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Read the scaled 12-bit angle count.
|
|
77
|
+
* @returns {number} Scaled angle count, 0–4095 (respects ZPOS/MPOS if programmed).
|
|
78
|
+
*/
|
|
79
|
+
angleRaw() {
|
|
80
|
+
const raw = this._readReg16(_REG_ANGLE_H);
|
|
81
|
+
return (raw >> 4) & 0x0FFF;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Check if a magnet is detected.
|
|
86
|
+
* @returns {boolean} True if STATUS.MD=1 (magnetic field >= 8 mT).
|
|
87
|
+
*/
|
|
88
|
+
isMagnetDetected() {
|
|
89
|
+
return !!(this._readReg8(_REG_STATUS) & _STATUS_MD);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Check if the magnet is too strong.
|
|
94
|
+
* @returns {boolean} True if STATUS.MH=1 (AGC minimum gain overflow, Bz > 90 mT).
|
|
95
|
+
*/
|
|
96
|
+
isMagnetTooStrong() {
|
|
97
|
+
return !!(this._readReg8(_REG_STATUS) & _STATUS_MH);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Check if the magnet is too weak.
|
|
102
|
+
* @returns {boolean} True if STATUS.ML=1 (AGC maximum gain overflow, Bz < 30 mT).
|
|
103
|
+
*/
|
|
104
|
+
isMagnetTooWeak() {
|
|
105
|
+
return !!(this._readReg8(_REG_STATUS) & _STATUS_ML);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* AS5600 full interface — extends AS5600Minimal with complete chip functionality.
|
|
111
|
+
*
|
|
112
|
+
* Adds raw angle readings, AGC/magnitude/status access, configuration,
|
|
113
|
+
* ZPOS/MPOS/MANG programming, and OTP burn commands.
|
|
114
|
+
*/
|
|
115
|
+
class AS5600Full extends AS5600Minimal {
|
|
116
|
+
static PM_NOM = 0;
|
|
117
|
+
static PM_LPM1 = 1;
|
|
118
|
+
static PM_LPM2 = 2;
|
|
119
|
+
static PM_LPM3 = 3;
|
|
120
|
+
|
|
121
|
+
static OUTS_ANALOG = 0;
|
|
122
|
+
static OUTS_ANALOG2 = 1;
|
|
123
|
+
static OUTS_PWM = 2;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* @param {object} transport - Configured I²C transport pointing at the device (fixed address 0x36).
|
|
127
|
+
*/
|
|
128
|
+
constructor(transport) {
|
|
129
|
+
super(transport);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Read the unscaled raw 12-bit angle count.
|
|
134
|
+
* @returns {number} Raw angle count, 0–4095 (unaffected by ZPOS/MPOS).
|
|
135
|
+
*/
|
|
136
|
+
rawAngle() {
|
|
137
|
+
const raw = this._readReg16(_REG_RAW_ANGLE_H);
|
|
138
|
+
return (raw >> 4) & 0x0FFF;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Read the unscaled raw angle in degrees.
|
|
143
|
+
* @returns {number} Raw angle in degrees, 0.0–360.0.
|
|
144
|
+
*/
|
|
145
|
+
rawAngleDegrees() {
|
|
146
|
+
return this.rawAngle() * 360.0 / 4096;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Read the automatic gain control value.
|
|
151
|
+
* @returns {number} AGC value (0–255 in 5 V mode; 0–127 in 3.3 V mode).
|
|
152
|
+
*/
|
|
153
|
+
agc() {
|
|
154
|
+
return this._readReg8(_REG_AGC);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Read the CORDIC magnitude value.
|
|
159
|
+
* @returns {number} 12-bit CORDIC magnitude value.
|
|
160
|
+
*/
|
|
161
|
+
magnitude() {
|
|
162
|
+
const raw = this._readReg16(_REG_MAGNITUDE_H);
|
|
163
|
+
return (raw >> 4) & 0x0FFF;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Read the raw STATUS register byte.
|
|
168
|
+
* @returns {number} Raw STATUS register (bits MH, ML, MD in positions 5, 4, 3).
|
|
169
|
+
*/
|
|
170
|
+
statusByte() {
|
|
171
|
+
return this._readReg8(_REG_STATUS);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Write the CONF_H and CONF_L registers.
|
|
176
|
+
*
|
|
177
|
+
* Reads the current CONF_H/CONF_L values first to preserve the reserved
|
|
178
|
+
* bits in CONF_H[7:6].
|
|
179
|
+
*
|
|
180
|
+
* @param {number} [pm=0] - Power mode 0–3 (0=NOM, 1=LPM1, 2=LPM2, 3=LPM3).
|
|
181
|
+
* @param {number} [hyst=0] - Hysteresis 0–3 (0=off, 1=1 LSB, 2=2 LSBs, 3=3 LSBs).
|
|
182
|
+
* @param {number} [outs=0] - Output stage 0–2 (0=analog 0–VDD, 1=analog 10–90%, 2=PWM).
|
|
183
|
+
* @param {number} [pwmf=0] - PWM frequency 0–3 (0=115 Hz, 1=230 Hz, 2=460 Hz, 3=920 Hz).
|
|
184
|
+
* @param {number} [sf=0] - Slow filter 0–3 (0=16x, 1=8x, 2=4x, 3=2x).
|
|
185
|
+
* @param {number} [fth=0] - Fast filter threshold 0–7.
|
|
186
|
+
* @param {boolean} [wd=false] - Watchdog enable.
|
|
187
|
+
*/
|
|
188
|
+
configure(pm = 0, hyst = 0, outs = 0, pwmf = 0, sf = 0, fth = 0, wd = false) {
|
|
189
|
+
let confH = this._readReg8(_REG_CONF_H);
|
|
190
|
+
let confL = this._readReg8(_REG_CONF_L);
|
|
191
|
+
confH = (confH & 0xC0) | ((wd ? 1 : 0) << 5) | ((fth & 0x07) << 2) | (sf & 0x03);
|
|
192
|
+
confL = ((pwmf & 0x03) << 6) | ((outs & 0x03) << 4) | ((hyst & 0x03) << 2) | (pm & 0x03);
|
|
193
|
+
this._writeReg16(_REG_CONF_H, (confH << 8) | confL);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Write the zero position (start angle) to volatile RAM.
|
|
198
|
+
* @param {number} pos - Zero position 0–4095. Lost on power cycle unless burned.
|
|
199
|
+
*/
|
|
200
|
+
setZeroPosition(pos) {
|
|
201
|
+
this._writeReg8(_REG_ZPOS_H, (pos >> 8) & 0x0F);
|
|
202
|
+
this._writeReg8(_REG_ZPOS_L, pos & 0xFF);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Write the maximum position (stop angle) to volatile RAM.
|
|
207
|
+
* @param {number} pos - Maximum position 0–4095. Lost on power cycle unless burned.
|
|
208
|
+
*/
|
|
209
|
+
setMaxPosition(pos) {
|
|
210
|
+
this._writeReg8(_REG_MPOS_H, (pos >> 8) & 0x0F);
|
|
211
|
+
this._writeReg8(_REG_MPOS_L, pos & 0xFF);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Write the maximum angle span to volatile RAM.
|
|
216
|
+
* @param {number} span - Angle span 0–4095 (must correspond to >= 18 degrees).
|
|
217
|
+
*/
|
|
218
|
+
setMaxAngle(span) {
|
|
219
|
+
this._writeReg8(_REG_MANG_H, (span >> 8) & 0x0F);
|
|
220
|
+
this._writeReg8(_REG_MANG_L, span & 0xFF);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Read the zero position (start angle).
|
|
225
|
+
* @returns {number} ZPOS value 0–4095.
|
|
226
|
+
*/
|
|
227
|
+
zeroPosition() {
|
|
228
|
+
const raw = this._readReg16(_REG_ZPOS_H);
|
|
229
|
+
return (raw >> 4) & 0x0FFF;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Read the maximum position (stop angle).
|
|
234
|
+
* @returns {number} MPOS value 0–4095.
|
|
235
|
+
*/
|
|
236
|
+
maxPosition() {
|
|
237
|
+
const raw = this._readReg16(_REG_MPOS_H);
|
|
238
|
+
return (raw >> 4) & 0x0FFF;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Read the maximum angle span.
|
|
243
|
+
* @returns {number} MANG value 0–4095.
|
|
244
|
+
*/
|
|
245
|
+
maxAngle() {
|
|
246
|
+
const raw = this._readReg16(_REG_MANG_H);
|
|
247
|
+
return (raw >> 4) & 0x0FFF;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Read the number of permanent ZPOS/MPOS burns already performed.
|
|
252
|
+
* @returns {number} ZMCO value 0–3. Remaining permanent writes = 3 - ZMCO.
|
|
253
|
+
*/
|
|
254
|
+
burnCount() {
|
|
255
|
+
return this._readReg8(_REG_ZMCO) & 0x03;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Permanently burn ZPOS and MPOS to OTP.
|
|
260
|
+
*
|
|
261
|
+
* Requires MD=1 (magnet present) and ZMCO < 3.
|
|
262
|
+
*
|
|
263
|
+
* @throws {Error} If magnet not detected or ZMCO >= 3.
|
|
264
|
+
*/
|
|
265
|
+
burnAngle() {
|
|
266
|
+
const status = this._readReg8(_REG_STATUS);
|
|
267
|
+
if (!(status & _STATUS_MD)) {
|
|
268
|
+
throw new Error('AS5600: cannot burn angle — magnet not detected');
|
|
269
|
+
}
|
|
270
|
+
const zmco = this._readReg8(_REG_ZMCO) & 0x03;
|
|
271
|
+
if (zmco >= 3) {
|
|
272
|
+
throw new Error('AS5600: cannot burn angle — ZMCO limit reached (3)');
|
|
273
|
+
}
|
|
274
|
+
this._writeReg8(_REG_BURN, 0x80);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Permanently burn MANG and CONF to OTP.
|
|
279
|
+
*
|
|
280
|
+
* Requires ZMCO=0 (ZPOS/MPOS never burned). Can only be executed once.
|
|
281
|
+
*
|
|
282
|
+
* @throws {Error} If ZMCO != 0.
|
|
283
|
+
*/
|
|
284
|
+
burnSetting() {
|
|
285
|
+
const zmco = this._readReg8(_REG_ZMCO) & 0x03;
|
|
286
|
+
if (zmco !== 0) {
|
|
287
|
+
throw new Error('AS5600: cannot burn setting — ZMCO must be 0');
|
|
288
|
+
}
|
|
289
|
+
this._writeReg8(_REG_BURN, 0x40);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
module.exports = { AS5600Minimal, AS5600Full };
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_CONFIG = 0x00;
|
|
4
|
+
const _REG_SHUNT = 0x01;
|
|
5
|
+
const _REG_BUS = 0x02;
|
|
6
|
+
const _REG_POWER = 0x03;
|
|
7
|
+
const _REG_CURRENT = 0x04;
|
|
8
|
+
const _REG_CAL = 0x05;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* INA219 26V, 12-bit current/voltage/power monitor — minimal interface.
|
|
12
|
+
*
|
|
13
|
+
* Provides bus voltage, shunt voltage, current, and power readings with no
|
|
14
|
+
* configuration beyond the transport and shunt resistor. Writes the
|
|
15
|
+
* Calibration Register automatically at construction.
|
|
16
|
+
*
|
|
17
|
+
* Default chip configuration (power-on defaults, not rewritten):
|
|
18
|
+
* - BRNG = 1: 32 V bus full-scale range
|
|
19
|
+
* - PG = 11: PGA ÷8, ±320 mV shunt full-scale
|
|
20
|
+
* - BADC = 0011: 12-bit, 532 µs
|
|
21
|
+
* - SADC = 0011: 12-bit, 532 µs
|
|
22
|
+
* - MODE = 111: shunt + bus, continuous
|
|
23
|
+
*/
|
|
24
|
+
class INA219Minimal {
|
|
25
|
+
/**
|
|
26
|
+
* @param {object} transport - Configured I²C or SMBus transport (writeRead, write).
|
|
27
|
+
* @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
28
|
+
* @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
|
|
29
|
+
*/
|
|
30
|
+
constructor(transport, rShunt = 0.1, maxCurrent = 2.0) {
|
|
31
|
+
this._transport = transport;
|
|
32
|
+
this._currentLsb = maxCurrent / 32768;
|
|
33
|
+
this._cal = Math.floor(0.04096 / (this._currentLsb * rShunt)) & 0xFFFE;
|
|
34
|
+
this._writeReg(_REG_CAL, this._cal);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
_writeReg(reg, value) {
|
|
38
|
+
const buf = Buffer.alloc(3);
|
|
39
|
+
buf[0] = reg;
|
|
40
|
+
buf.writeUInt16BE(value, 1);
|
|
41
|
+
this._transport.write(buf);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
_readReg(reg) {
|
|
45
|
+
return this._transport.writeRead(Buffer.from([reg]), 2).readUInt16BE(0);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
_readRegSigned(reg) {
|
|
49
|
+
return this._transport.writeRead(Buffer.from([reg]), 2).readInt16BE(0);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** @return {number} Bus voltage in volts ((raw >> 3) × 4 mV LSB). */
|
|
53
|
+
voltage() { return (this._readReg(_REG_BUS) >> 3) * 4e-3; }
|
|
54
|
+
|
|
55
|
+
/** @return {number} Shunt voltage in volts, signed (raw × 10 µV LSB). */
|
|
56
|
+
shuntVoltage() { return this._readRegSigned(_REG_SHUNT) * 10e-6; }
|
|
57
|
+
|
|
58
|
+
/** @return {number} Current in amperes, signed. */
|
|
59
|
+
current() { return this._readRegSigned(_REG_CURRENT) * this._currentLsb; }
|
|
60
|
+
|
|
61
|
+
/** @return {number} Power in watts (raw × 20 × current LSB). */
|
|
62
|
+
power() { return this._readReg(_REG_POWER) * 20 * this._currentLsb; }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* INA219 full interface — extends INA219Minimal with full configuration and power management.
|
|
67
|
+
*
|
|
68
|
+
* Adds Configuration Register programming, conversion-ready and overflow status,
|
|
69
|
+
* reset, and shutdown/wake.
|
|
70
|
+
*/
|
|
71
|
+
class INA219Full extends INA219Minimal {
|
|
72
|
+
static BRNG_16V = 0;
|
|
73
|
+
static BRNG_32V = 1;
|
|
74
|
+
|
|
75
|
+
static PGA_1 = 0;
|
|
76
|
+
static PGA_2 = 1;
|
|
77
|
+
static PGA_4 = 2;
|
|
78
|
+
static PGA_8 = 3;
|
|
79
|
+
|
|
80
|
+
static ADC_9BIT = 0x00;
|
|
81
|
+
static ADC_10BIT = 0x01;
|
|
82
|
+
static ADC_11BIT = 0x02;
|
|
83
|
+
static ADC_12BIT = 0x03;
|
|
84
|
+
static ADC_AVG_2 = 0x09;
|
|
85
|
+
static ADC_AVG_4 = 0x0A;
|
|
86
|
+
static ADC_AVG_8 = 0x0B;
|
|
87
|
+
static ADC_AVG_16 = 0x0C;
|
|
88
|
+
static ADC_AVG_32 = 0x0D;
|
|
89
|
+
static ADC_AVG_64 = 0x0E;
|
|
90
|
+
static ADC_AVG_128 = 0x0F;
|
|
91
|
+
|
|
92
|
+
static MODE_POWERDOWN = 0;
|
|
93
|
+
static MODE_SHUNT_TRIG = 1;
|
|
94
|
+
static MODE_BUS_TRIG = 2;
|
|
95
|
+
static MODE_SHUNT_BUS_TRIG = 3;
|
|
96
|
+
static MODE_ADC_OFF = 4;
|
|
97
|
+
static MODE_SHUNT_CONT = 5;
|
|
98
|
+
static MODE_BUS_CONT = 6;
|
|
99
|
+
static MODE_SHUNT_BUS_CONT = 7;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @param {object} transport - Configured I²C or SMBus transport.
|
|
103
|
+
* @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
104
|
+
* @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
|
|
105
|
+
*/
|
|
106
|
+
constructor(transport, rShunt = 0.1, maxCurrent = 2.0) {
|
|
107
|
+
super(transport, rShunt, maxCurrent);
|
|
108
|
+
this._savedMode = INA219Full.MODE_SHUNT_BUS_CONT;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Write the Configuration Register.
|
|
113
|
+
* @param {number} [brng=1] - Bus voltage range: 0 = 16 V FSR, 1 = 32 V FSR.
|
|
114
|
+
* @param {number} [pga=3] - Shunt PGA gain: 0 = ÷1, 1 = ÷2, 2 = ÷4, 3 = ÷8.
|
|
115
|
+
* @param {number} [badc=0x03] - Bus ADC resolution/averaging: 0x00–0x0F.
|
|
116
|
+
* @param {number} [sadc=0x03] - Shunt ADC resolution/averaging: 0x00–0x0F.
|
|
117
|
+
* @param {number} [mode=7] - Operating mode 0–7.
|
|
118
|
+
*/
|
|
119
|
+
configure(brng = 1, pga = 3, badc = 0x03, sadc = 0x03, mode = 7) {
|
|
120
|
+
const config = ((brng & 1) << 13) | ((pga & 3) << 11) | ((badc & 0x0F) << 7) | ((sadc & 0x0F) << 3) | (mode & 7);
|
|
121
|
+
this._savedMode = mode & 7;
|
|
122
|
+
this._writeReg(_REG_CONFIG, config);
|
|
123
|
+
this._writeReg(_REG_CAL, this._cal);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** @return {boolean} True if a conversion completed since the last read. */
|
|
127
|
+
conversionReady() { return !!(this._readReg(_REG_BUS) & 0x02); }
|
|
128
|
+
|
|
129
|
+
/** @return {boolean} True if an arithmetic overflow occurred. */
|
|
130
|
+
overflow() { return !!(this._readReg(_REG_BUS) & 0x01); }
|
|
131
|
+
|
|
132
|
+
reset() {
|
|
133
|
+
this._writeReg(_REG_CONFIG, 0x8000);
|
|
134
|
+
this._writeReg(_REG_CAL, this._cal);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
shutdown() {
|
|
138
|
+
const config = this._readReg(_REG_CONFIG);
|
|
139
|
+
this._savedMode = config & 7;
|
|
140
|
+
this._writeReg(_REG_CONFIG, config & 0xFFF8);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
wake() {
|
|
144
|
+
const config = this._readReg(_REG_CONFIG);
|
|
145
|
+
this._writeReg(_REG_CONFIG, (config & 0xFFF8) | this._savedMode);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
trigger() {
|
|
149
|
+
const config = this._readReg(_REG_CONFIG);
|
|
150
|
+
this._writeReg(_REG_CONFIG, config);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
module.exports = { INA219Minimal, INA219Full };
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_CONFIG = 0x00;
|
|
4
|
+
const _REG_SHUNT = 0x01;
|
|
5
|
+
const _REG_BUS = 0x02;
|
|
6
|
+
const _REG_POWER = 0x03;
|
|
7
|
+
const _REG_CURRENT = 0x04;
|
|
8
|
+
const _REG_CAL = 0x05;
|
|
9
|
+
const _REG_MASK = 0x06;
|
|
10
|
+
const _REG_ALERT = 0x07;
|
|
11
|
+
const _REG_MFR_ID = 0xFE;
|
|
12
|
+
const _REG_DIE_ID = 0xFF;
|
|
13
|
+
|
|
14
|
+
const _CONFIG_DEFAULT = 0x4127;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* INA226 36V, 16-bit current/voltage/power monitor — minimal interface.
|
|
18
|
+
*
|
|
19
|
+
* Provides bus voltage, shunt voltage, current, and power readings with no
|
|
20
|
+
* configuration beyond the transport and shunt resistor. Writes the
|
|
21
|
+
* Calibration Register automatically at construction.
|
|
22
|
+
*
|
|
23
|
+
* Default configuration (written at construction):
|
|
24
|
+
* - MODE = 7: shunt + bus, continuous
|
|
25
|
+
* - VBUSCT = 4: 1.1 ms bus voltage conversion time
|
|
26
|
+
* - VSHCT = 4: 1.1 ms shunt voltage conversion time
|
|
27
|
+
* - AVG = 0: 1 sample (no averaging)
|
|
28
|
+
*/
|
|
29
|
+
class INA226Minimal {
|
|
30
|
+
/**
|
|
31
|
+
* @param {object} transport - Configured I²C or SMBus transport (writeRead, write).
|
|
32
|
+
* @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
33
|
+
* @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
|
|
34
|
+
*/
|
|
35
|
+
constructor(transport, rShunt = 0.1, maxCurrent = 2.0) {
|
|
36
|
+
this._transport = transport;
|
|
37
|
+
this._currentLsb = maxCurrent / 32768;
|
|
38
|
+
this._cal = Math.floor(0.00512 / (this._currentLsb * rShunt));
|
|
39
|
+
this._writeReg(_REG_CONFIG, _CONFIG_DEFAULT);
|
|
40
|
+
this._writeReg(_REG_CAL, this._cal);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
_writeReg(reg, value) {
|
|
44
|
+
const buf = Buffer.alloc(3);
|
|
45
|
+
buf[0] = reg;
|
|
46
|
+
buf.writeUInt16BE(value, 1);
|
|
47
|
+
this._transport.write(buf);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_readReg(reg) {
|
|
51
|
+
return this._transport.writeRead(Buffer.from([reg]), 2).readUInt16BE(0);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
_readRegSigned(reg) {
|
|
55
|
+
return this._transport.writeRead(Buffer.from([reg]), 2).readInt16BE(0);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Read bus voltage.
|
|
60
|
+
* @returns {number} Bus voltage in volts (raw × 1.25 mV LSB).
|
|
61
|
+
*/
|
|
62
|
+
voltage() { return this._readReg(_REG_BUS) * 1.25e-3; }
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Read differential shunt voltage.
|
|
66
|
+
* @returns {number} Shunt voltage in volts, signed (raw × 2.5 µV LSB).
|
|
67
|
+
*/
|
|
68
|
+
shuntVoltage() { return this._readRegSigned(_REG_SHUNT) * 2.5e-6; }
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Read calculated current through the shunt.
|
|
72
|
+
* @returns {number} Current in amperes, signed.
|
|
73
|
+
*/
|
|
74
|
+
current() { return this._readRegSigned(_REG_CURRENT) * this._currentLsb; }
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Read calculated power.
|
|
78
|
+
* @returns {number} Power in watts (raw × 25 × current LSB).
|
|
79
|
+
*/
|
|
80
|
+
power() { return this._readReg(_REG_POWER) * 25 * this._currentLsb; }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* INA226 full interface — extends INA226Minimal with configuration and alert support.
|
|
85
|
+
*
|
|
86
|
+
* Adds Configuration Register programming, conversion-ready and overflow status,
|
|
87
|
+
* alert configuration, reset, and shutdown/wake.
|
|
88
|
+
*
|
|
89
|
+
* Alert function constants (pass to setAlert):
|
|
90
|
+
* - INA226Full.SOL — shunt voltage over-limit
|
|
91
|
+
* - INA226Full.SUL — shunt voltage under-limit
|
|
92
|
+
* - INA226Full.BOL — bus voltage over-limit
|
|
93
|
+
* - INA226Full.BUL — bus voltage under-limit
|
|
94
|
+
* - INA226Full.POL — power over-limit
|
|
95
|
+
* - INA226Full.CNVR — conversion ready
|
|
96
|
+
*/
|
|
97
|
+
class INA226Full extends INA226Minimal {
|
|
98
|
+
static SOL = 0x8000;
|
|
99
|
+
static SUL = 0x4000;
|
|
100
|
+
static BOL = 0x2000;
|
|
101
|
+
static BUL = 0x1000;
|
|
102
|
+
static POL = 0x0800;
|
|
103
|
+
static CNVR = 0x0400;
|
|
104
|
+
static AFF = 0x0010;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @param {object} transport - Configured I²C or SMBus transport.
|
|
108
|
+
* @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
109
|
+
* @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
|
|
110
|
+
*/
|
|
111
|
+
constructor(transport, rShunt = 0.1, maxCurrent = 2.0) {
|
|
112
|
+
super(transport, rShunt, maxCurrent);
|
|
113
|
+
this._mode = 0x07;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Write the Configuration Register.
|
|
118
|
+
* @param {number} [avg=0] - Averaging count selector 0–7 (0 = 1 sample … 7 = 1024 samples).
|
|
119
|
+
* @param {number} [vbusCt=4] - Bus voltage conversion time selector 0–7 (default 4 = 1.1 ms).
|
|
120
|
+
* @param {number} [vshCt=4] - Shunt voltage conversion time selector 0–7 (default 4 = 1.1 ms).
|
|
121
|
+
* @param {number} [mode=7] - Operating mode 0–7 (7 = shunt+bus continuous).
|
|
122
|
+
*/
|
|
123
|
+
configure(avg = 0, vbusCt = 4, vshCt = 4, mode = 7) {
|
|
124
|
+
const config = ((avg & 0x07) << 9) | ((vbusCt & 0x07) << 6) | ((vshCt & 0x07) << 3) | (mode & 0x07);
|
|
125
|
+
this._mode = mode & 0x07;
|
|
126
|
+
this._writeReg(_REG_CONFIG, config);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Read the Conversion Ready Flag (CVRF) from the Mask/Enable Register.
|
|
131
|
+
*
|
|
132
|
+
* Note: Reading Mask/Enable clears CVRF. Read it last if also checking other flags.
|
|
133
|
+
*
|
|
134
|
+
* @returns {boolean} True if a conversion completed since the last Mask/Enable read.
|
|
135
|
+
*/
|
|
136
|
+
conversionReady() { return !!(this._readReg(_REG_MASK) & 0x0008); }
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Read the Math Overflow Flag (OVF) from the Mask/Enable Register.
|
|
140
|
+
* @returns {boolean} True if an arithmetic overflow occurred in the power calculation.
|
|
141
|
+
*/
|
|
142
|
+
overflow() { return !!(this._readReg(_REG_MASK) & 0x0004); }
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Configure the alert pin function and threshold.
|
|
146
|
+
*
|
|
147
|
+
* Only one alert function can be active at a time.
|
|
148
|
+
*
|
|
149
|
+
* @param {number} fn - Alert function constant (SOL/SUL/BOL/BUL/POL/CNVR).
|
|
150
|
+
* @param {number} [limit=0] - Threshold in natural units (V for voltage, W for power).
|
|
151
|
+
* @param {number} [polarity=0] - 0 = active-low (default), 1 = active-high.
|
|
152
|
+
* @param {number} [latch=0] - 0 = transparent (default), 1 = latch until Mask/Enable is read.
|
|
153
|
+
*/
|
|
154
|
+
setAlert(fn, limit = 0, polarity = 0, latch = 0) {
|
|
155
|
+
let raw = 0;
|
|
156
|
+
if (fn === INA226Full.SOL || fn === INA226Full.SUL) raw = Math.floor(limit / 2.5e-6);
|
|
157
|
+
else if (fn === INA226Full.BOL || fn === INA226Full.BUL) raw = Math.floor(limit / 1.25e-3);
|
|
158
|
+
else if (fn === INA226Full.POL) raw = Math.floor(limit / (25 * this._currentLsb));
|
|
159
|
+
const mask = fn | ((polarity & 1) << 1) | (latch & 1);
|
|
160
|
+
this._writeReg(_REG_MASK, mask);
|
|
161
|
+
this._writeReg(_REG_ALERT, raw & 0xFFFF);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Read the Mask/Enable Register.
|
|
166
|
+
* @returns {number} Raw 16-bit value containing alert and status flags.
|
|
167
|
+
*/
|
|
168
|
+
alertFlags() { return this._readReg(_REG_MASK); }
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Reset all registers to power-on defaults, then re-write the Calibration Register.
|
|
172
|
+
*/
|
|
173
|
+
reset() {
|
|
174
|
+
this._writeReg(_REG_CONFIG, 0x8000);
|
|
175
|
+
this._writeReg(_REG_CAL, this._cal);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Enter power-down mode (MODE = 000) and save the current mode for wake().
|
|
180
|
+
*/
|
|
181
|
+
shutdown() {
|
|
182
|
+
const config = this._readReg(_REG_CONFIG);
|
|
183
|
+
this._mode = config & 0x07;
|
|
184
|
+
this._writeReg(_REG_CONFIG, config & 0xFFF8);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Restore the operating mode saved by shutdown().
|
|
189
|
+
*/
|
|
190
|
+
wake() {
|
|
191
|
+
const config = this._readReg(_REG_CONFIG);
|
|
192
|
+
this._writeReg(_REG_CONFIG, (config & 0xFFF8) | this._mode);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Read the Manufacturer ID register.
|
|
197
|
+
* @returns {number} Manufacturer ID; expect 0x5449 (Texas Instruments).
|
|
198
|
+
*/
|
|
199
|
+
manufacturerId() { return this._readReg(_REG_MFR_ID); }
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Read the Die ID register.
|
|
203
|
+
* @returns {number} Die revision ID; expect 0x2260.
|
|
204
|
+
*/
|
|
205
|
+
dieId() { return this._readReg(_REG_DIE_ID); }
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
module.exports = { INA226Minimal, INA226Full };
|