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,341 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_CONFIG = 0x00;
|
|
4
|
+
const _REG_SHUNT1 = 0x01;
|
|
5
|
+
const _REG_BUS1 = 0x02;
|
|
6
|
+
const _REG_SHUNT2 = 0x03;
|
|
7
|
+
const _REG_BUS2 = 0x04;
|
|
8
|
+
const _REG_SHUNT3 = 0x05;
|
|
9
|
+
const _REG_BUS3 = 0x06;
|
|
10
|
+
const _REG_CH1_CRIT = 0x07;
|
|
11
|
+
const _REG_CH1_WARN = 0x08;
|
|
12
|
+
const _REG_CH2_CRIT = 0x09;
|
|
13
|
+
const _REG_CH2_WARN = 0x0A;
|
|
14
|
+
const _REG_CH3_CRIT = 0x0B;
|
|
15
|
+
const _REG_CH3_WARN = 0x0C;
|
|
16
|
+
const _REG_SUM = 0x0D;
|
|
17
|
+
const _REG_SUM_LIMIT = 0x0E;
|
|
18
|
+
const _REG_MASK_EN = 0x0F;
|
|
19
|
+
const _REG_PV_UPPER = 0x10;
|
|
20
|
+
const _REG_PV_LOWER = 0x11;
|
|
21
|
+
const _REG_MFR_ID = 0xFE;
|
|
22
|
+
const _REG_DIE_ID = 0xFF;
|
|
23
|
+
|
|
24
|
+
const _SHUNT_REGS = [_REG_SHUNT1, _REG_SHUNT2, _REG_SHUNT3];
|
|
25
|
+
const _BUS_REGS = [_REG_BUS1, _REG_BUS2, _REG_BUS3 ];
|
|
26
|
+
const _CRIT_REGS = [_REG_CH1_CRIT, _REG_CH2_CRIT, _REG_CH3_CRIT];
|
|
27
|
+
const _WARN_REGS = [_REG_CH1_WARN, _REG_CH2_WARN, _REG_CH3_WARN];
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* INA3221 three-channel 26V current/voltage/power monitor — minimal interface.
|
|
31
|
+
*
|
|
32
|
+
* Reads bus voltage, shunt voltage, current, and power for each of the three
|
|
33
|
+
* channels with no configuration beyond the transport and shunt resistors.
|
|
34
|
+
* The chip's power-on default (all three channels on, continuous shunt+bus)
|
|
35
|
+
* is used without modification.
|
|
36
|
+
*
|
|
37
|
+
* @param {object} transport - Configured I2C or SMBus transport (writeRead, write).
|
|
38
|
+
* @param {number|number[]} [rShunt=0.1] - Shunt resistor value in ohms. Pass a
|
|
39
|
+
* single number to apply the same value to all three channels, or an
|
|
40
|
+
* array of 3 numbers for per-channel values.
|
|
41
|
+
*/
|
|
42
|
+
class INA3221Minimal {
|
|
43
|
+
constructor(transport, rShunt = 0.1) {
|
|
44
|
+
this._transport = transport;
|
|
45
|
+
if (Array.isArray(rShunt)) {
|
|
46
|
+
this._rShunt = rShunt.map(v => Number(v));
|
|
47
|
+
} else {
|
|
48
|
+
this._rShunt = [Number(rShunt), Number(rShunt), Number(rShunt)];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
_writeReg(reg, value) {
|
|
53
|
+
const buf = Buffer.alloc(3);
|
|
54
|
+
buf[0] = reg;
|
|
55
|
+
buf.writeUInt16BE(value, 1);
|
|
56
|
+
this._transport.write(buf);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
_readReg(reg) {
|
|
60
|
+
return this._transport.writeRead(Buffer.from([reg]), 2).readUInt16BE(0);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_readRegSigned(reg) {
|
|
64
|
+
return this._transport.writeRead(Buffer.from([reg]), 2).readInt16BE(0);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
_channelValid(channel) {
|
|
68
|
+
const ch = Number(channel);
|
|
69
|
+
if (ch < 1 || ch > 3) throw new Error('channel must be 1, 2, or 3');
|
|
70
|
+
return ch;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Read bus voltage for a channel.
|
|
75
|
+
* @param {number} channel - Channel number 1, 2, or 3.
|
|
76
|
+
* @returns {number} Bus voltage in volts.
|
|
77
|
+
*/
|
|
78
|
+
voltage(channel) {
|
|
79
|
+
const ch = this._channelValid(channel);
|
|
80
|
+
const raw = this._readReg(_BUS_REGS[ch - 1]);
|
|
81
|
+
return (raw >> 3) * 8e-3;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Read differential shunt voltage for a channel.
|
|
86
|
+
* @param {number} channel - Channel number 1, 2, or 3.
|
|
87
|
+
* @returns {number} Shunt voltage in volts, signed.
|
|
88
|
+
*/
|
|
89
|
+
shuntVoltage(channel) {
|
|
90
|
+
const ch = this._channelValid(channel);
|
|
91
|
+
const raw = this._readRegSigned(_SHUNT_REGS[ch - 1]);
|
|
92
|
+
return raw * 5e-6;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Read calculated current through the shunt for a channel.
|
|
97
|
+
* @param {number} channel - Channel number 1, 2, or 3.
|
|
98
|
+
* @returns {number} Current in amperes.
|
|
99
|
+
*/
|
|
100
|
+
current(channel) {
|
|
101
|
+
const ch = this._channelValid(channel);
|
|
102
|
+
return this.shuntVoltage(ch) / this._rShunt[ch - 1];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Read calculated power for a channel.
|
|
107
|
+
* @param {number} channel - Channel number 1, 2, or 3.
|
|
108
|
+
* @returns {number} Power in watts.
|
|
109
|
+
*/
|
|
110
|
+
power(channel) {
|
|
111
|
+
const ch = this._channelValid(channel);
|
|
112
|
+
return this.voltage(ch) * this.current(ch);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* INA3221 full interface — extends INA3221Minimal with configuration and alert support.
|
|
118
|
+
*
|
|
119
|
+
* Adds Configuration Register programming, channel enables, conversion-ready,
|
|
120
|
+
* per-channel critical and warning alerts, shunt-voltage summation, power-valid
|
|
121
|
+
* monitoring, reset, and shutdown/wake.
|
|
122
|
+
*
|
|
123
|
+
* Alert flag constants (from Mask/Enable register):
|
|
124
|
+
* - INA3221Full.CF1, CF2, CF3 — Channel 1/2/3 critical-alert flag
|
|
125
|
+
* - INA3221Full.WF1, WF2, WF3 — Channel 1/2/3 warning-alert flag
|
|
126
|
+
* - INA3221Full.SF — Summation-alert flag
|
|
127
|
+
* - INA3221Full.PVF — Power-valid flag
|
|
128
|
+
* - INA3221Full.TCF — Timing-control flag
|
|
129
|
+
* - INA3221Full.CVRF — Conversion-ready flag
|
|
130
|
+
*
|
|
131
|
+
* Mode constants:
|
|
132
|
+
* - INA3221Full.MODE_POWERDOWN = 0
|
|
133
|
+
* - INA3221Full.MODE_SHUNT_TRIG = 1
|
|
134
|
+
* - INA3221Full.MODE_BUS_TRIG = 2
|
|
135
|
+
* - INA3221Full.MODE_SHUNT_BUS_TRIG = 3
|
|
136
|
+
* - INA3221Full.MODE_SHUNT_CONT = 5
|
|
137
|
+
* - INA3221Full.MODE_BUS_CONT = 6
|
|
138
|
+
* - INA3221Full.MODE_SHUNT_BUS_CONT = 7
|
|
139
|
+
*
|
|
140
|
+
* @param {object} transport - Configured I2C or SMBus transport.
|
|
141
|
+
* @param {number|number[]} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
142
|
+
*/
|
|
143
|
+
class INA3221Full extends INA3221Minimal {
|
|
144
|
+
static CF1 = 0x0200;
|
|
145
|
+
static CF2 = 0x0100;
|
|
146
|
+
static CF3 = 0x0080;
|
|
147
|
+
static SF = 0x0040;
|
|
148
|
+
static WF1 = 0x0020;
|
|
149
|
+
static WF2 = 0x0010;
|
|
150
|
+
static WF3 = 0x0008;
|
|
151
|
+
static PVF = 0x0004;
|
|
152
|
+
static TCF = 0x0002;
|
|
153
|
+
static CVRF = 0x0001;
|
|
154
|
+
|
|
155
|
+
static MODE_POWERDOWN = 0;
|
|
156
|
+
static MODE_SHUNT_TRIG = 1;
|
|
157
|
+
static MODE_BUS_TRIG = 2;
|
|
158
|
+
static MODE_SHUNT_BUS_TRIG = 3;
|
|
159
|
+
static MODE_SHUNT_CONT = 5;
|
|
160
|
+
static MODE_BUS_CONT = 6;
|
|
161
|
+
static MODE_SHUNT_BUS_CONT = 7;
|
|
162
|
+
|
|
163
|
+
constructor(transport, rShunt = 0.1) {
|
|
164
|
+
super(transport, rShunt);
|
|
165
|
+
this._mode = 0x07;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Write the Configuration Register.
|
|
170
|
+
* @param {number} [avg=0] - Averaging count selector 0–7 (0=1 sample, 7=1024 samples).
|
|
171
|
+
* @param {number} [vbusCt=4] - Bus voltage conversion time selector 0–7 (default 4=1.1 ms).
|
|
172
|
+
* @param {number} [vshCt=4] - Shunt voltage conversion time selector 0–7 (default 4=1.1 ms).
|
|
173
|
+
* @param {number} [mode=7] - Operating mode (default 7=shunt+bus continuous).
|
|
174
|
+
*/
|
|
175
|
+
configure(avg = 0, vbusCt = 4, vshCt = 4, mode = 7) {
|
|
176
|
+
const cfg = this._readReg(_REG_CONFIG);
|
|
177
|
+
const config = ((avg & 0x07) << 9) | ((vbusCt & 0x07) << 6) | ((vshCt & 0x07) << 3) | (mode & 0x07);
|
|
178
|
+
this._mode = mode & 0x07;
|
|
179
|
+
this._writeReg(_REG_CONFIG, config | (cfg & 0x7000));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Enable or disable a channel.
|
|
184
|
+
* @param {number} channel - Channel number 1, 2, or 3.
|
|
185
|
+
* @param {boolean} enabled - true to enable, false to disable.
|
|
186
|
+
*/
|
|
187
|
+
enableChannel(channel, enabled) {
|
|
188
|
+
const ch = this._channelValid(channel);
|
|
189
|
+
const cfg = this._readReg(_REG_CONFIG);
|
|
190
|
+
const bit = 14 - (ch - 1);
|
|
191
|
+
if (enabled) this._writeReg(_REG_CONFIG, cfg | (1 << bit));
|
|
192
|
+
else this._writeReg(_REG_CONFIG, cfg & ~(1 << bit));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Read whether a channel is enabled.
|
|
197
|
+
* @param {number} channel - Channel number 1, 2, or 3.
|
|
198
|
+
* @returns {boolean} true if the channel is enabled.
|
|
199
|
+
*/
|
|
200
|
+
channelEnabled(channel) {
|
|
201
|
+
const ch = this._channelValid(channel);
|
|
202
|
+
const cfg = this._readReg(_REG_CONFIG);
|
|
203
|
+
const bit = 14 - (ch - 1);
|
|
204
|
+
return (cfg & (1 << bit)) !== 0;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Read the Conversion Ready Flag (CVRF).
|
|
209
|
+
* @returns {boolean} true if a conversion completed.
|
|
210
|
+
*/
|
|
211
|
+
conversionReady() {
|
|
212
|
+
return (this._readReg(_REG_MASK_EN) & INA3221Full.CVRF) !== 0;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Set the critical-alert limit for a channel.
|
|
217
|
+
* @param {number} channel - Channel number 1, 2, or 3.
|
|
218
|
+
* @param {number} limitV - Voltage limit in volts.
|
|
219
|
+
* @param {boolean} [latch=false] - If true, use latched mode.
|
|
220
|
+
*/
|
|
221
|
+
setCriticalAlert(channel, limitV, latch = false) {
|
|
222
|
+
const ch = this._channelValid(channel);
|
|
223
|
+
const raw = (Math.floor(limitV / 40e-6) << 3) & 0xFFF8;
|
|
224
|
+
this._writeReg(_CRIT_REGS[ch - 1], raw);
|
|
225
|
+
const cfg = this._readReg(_REG_MASK_EN);
|
|
226
|
+
this._writeReg(_REG_MASK_EN, latch ? (cfg | 0x0400) : (cfg & ~0x0400));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Set the warning-alert limit for a channel.
|
|
231
|
+
* @param {number} channel - Channel number 1, 2, or 3.
|
|
232
|
+
* @param {number} limitV - Voltage limit in volts.
|
|
233
|
+
* @param {boolean} [latch=false] - If true, use latched mode.
|
|
234
|
+
*/
|
|
235
|
+
setWarningAlert(channel, limitV, latch = false) {
|
|
236
|
+
const ch = this._channelValid(channel);
|
|
237
|
+
const raw = (Math.floor(limitV / 40e-6) << 3) & 0xFFF8;
|
|
238
|
+
this._writeReg(_WARN_REGS[ch - 1], raw);
|
|
239
|
+
const cfg = this._readReg(_REG_MASK_EN);
|
|
240
|
+
this._writeReg(_REG_MASK_EN, latch ? (cfg | 0x0800) : (cfg & ~0x0800));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Read the Mask/Enable Register.
|
|
245
|
+
*
|
|
246
|
+
* Reading this register clears the latched alert flags (CF1/CF2/CF3,
|
|
247
|
+
* WF1/WF2/WF3, SF) when latch mode is enabled.
|
|
248
|
+
*
|
|
249
|
+
* @returns {number} Raw 16-bit Mask/Enable register value.
|
|
250
|
+
*/
|
|
251
|
+
alertFlags() {
|
|
252
|
+
return this._readReg(_REG_MASK_EN);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Configure the shunt-voltage summation function.
|
|
257
|
+
* @param {number[]} channels - Array of channel numbers to sum (e.g. [1, 2, 3]).
|
|
258
|
+
* @param {number} limitV - Shunt-voltage sum limit in volts.
|
|
259
|
+
*/
|
|
260
|
+
setSummationChannels(channels, limitV) {
|
|
261
|
+
let cfg = this._readReg(_REG_MASK_EN) & ~0xE000;
|
|
262
|
+
for (const ch of channels) {
|
|
263
|
+
this._channelValid(ch);
|
|
264
|
+
cfg |= 1 << (15 - (Number(ch) - 1));
|
|
265
|
+
}
|
|
266
|
+
this._writeReg(_REG_MASK_EN, cfg);
|
|
267
|
+
const raw = (Math.floor(limitV / 40e-6) << 1) & 0xFFFE;
|
|
268
|
+
this._writeReg(_REG_SUM_LIMIT, raw);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Read the shunt-voltage sum.
|
|
273
|
+
* @returns {number} Sum of selected channels' shunt voltages in volts.
|
|
274
|
+
*/
|
|
275
|
+
summationValue() {
|
|
276
|
+
const raw = this._readRegSigned(_REG_SUM);
|
|
277
|
+
return raw * 5e-6;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Set the Power-Valid upper and lower voltage limits.
|
|
282
|
+
* @param {number} upperV - Upper bus voltage limit in volts.
|
|
283
|
+
* @param {number} lowerV - Lower bus voltage limit in volts.
|
|
284
|
+
*/
|
|
285
|
+
setPowerValidLimits(upperV, lowerV) {
|
|
286
|
+
const rawUpper = (Math.floor(upperV / 8e-3) << 3) & 0xFFF8;
|
|
287
|
+
const rawLower = (Math.floor(lowerV / 8e-3) << 3) & 0xFFF8;
|
|
288
|
+
this._writeReg(_REG_PV_UPPER, rawUpper);
|
|
289
|
+
this._writeReg(_REG_PV_LOWER, rawLower);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Read the Power-Valid flag (PVF).
|
|
294
|
+
* @returns {boolean} true if all enabled bus voltages are within the PV limits.
|
|
295
|
+
*/
|
|
296
|
+
powerValid() {
|
|
297
|
+
return (this._readReg(_REG_MASK_EN) & INA3221Full.PVF) !== 0;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Enter power-down mode and save the current mode for wake().
|
|
302
|
+
*/
|
|
303
|
+
shutdown() {
|
|
304
|
+
const cfg = this._readReg(_REG_CONFIG);
|
|
305
|
+
this._mode = cfg & 0x07;
|
|
306
|
+
this._writeReg(_REG_CONFIG, cfg & 0xFFF8);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Restore the operating mode saved by shutdown().
|
|
311
|
+
*/
|
|
312
|
+
wake() {
|
|
313
|
+
const cfg = this._readReg(_REG_CONFIG);
|
|
314
|
+
this._writeReg(_REG_CONFIG, (cfg & 0xFFF8) | this._mode);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Reset all registers to power-on defaults.
|
|
319
|
+
*/
|
|
320
|
+
reset() {
|
|
321
|
+
this._writeReg(_REG_CONFIG, 0x8000);
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Read the Manufacturer ID register.
|
|
326
|
+
* @returns {number} Manufacturer ID; expect 0x5449 (Texas Instruments).
|
|
327
|
+
*/
|
|
328
|
+
manufacturerId() {
|
|
329
|
+
return this._readReg(_REG_MFR_ID);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Read the Die ID register.
|
|
334
|
+
* @returns {number} Die revision ID; expect 0x3220.
|
|
335
|
+
*/
|
|
336
|
+
dieId() {
|
|
337
|
+
return this._readReg(_REG_DIE_ID);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
module.exports = { INA3221Minimal, INA3221Full };
|
|
File without changes
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_ID = 0xD0;
|
|
4
|
+
const _REG_CAL_START = 0xAA;
|
|
5
|
+
const _REG_CTRL_MEAS = 0xF4;
|
|
6
|
+
const _REG_OUT_MSB = 0xF6;
|
|
7
|
+
const _REG_OUT_LSB = 0xF7;
|
|
8
|
+
const _REG_OUT_XLSB = 0xF8;
|
|
9
|
+
const _REG_SOFT_RESET = 0xE0;
|
|
10
|
+
|
|
11
|
+
const _CMD_TEMP = 0x2E;
|
|
12
|
+
const _CMD_PRESSURE = [0x34, 0x74, 0xB4, 0xF4];
|
|
13
|
+
|
|
14
|
+
const _CHIP_ID = 0x55;
|
|
15
|
+
const _SOFT_RESET_CMD = 0xB6;
|
|
16
|
+
|
|
17
|
+
const _CONV_TIME = [0.0045, 0.0075, 0.0135, 0.0255];
|
|
18
|
+
const _CONV_TIME_TEMP = 0.0045;
|
|
19
|
+
|
|
20
|
+
function _delay(ms) {
|
|
21
|
+
const start = Date.now();
|
|
22
|
+
while (Date.now() - start < ms) { /* spin */ }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* BMP180 piezo-resistive pressure + temperature sensor — minimal interface.
|
|
27
|
+
*
|
|
28
|
+
* Provides calibrated temperature (°C) and pressure (hPa) with no configuration
|
|
29
|
+
* beyond the transport. Fixed I²C address 0x77.
|
|
30
|
+
*
|
|
31
|
+
* Default OSS = 0 (Ultra Low Power, 4.5 ms conversion).
|
|
32
|
+
*
|
|
33
|
+
* @param {object} transport - Configured I²C transport.
|
|
34
|
+
*/
|
|
35
|
+
class BMP180Minimal {
|
|
36
|
+
constructor(transport) {
|
|
37
|
+
this._transport = transport;
|
|
38
|
+
this._oss = 0;
|
|
39
|
+
this._readCalibration();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
_readCalibration() {
|
|
43
|
+
const data = this._transport.writeRead(Buffer.from([_REG_CAL_START]), 22);
|
|
44
|
+
this._ac1 = data.readInt16BE(0);
|
|
45
|
+
this._ac2 = data.readInt16BE(2);
|
|
46
|
+
this._ac3 = data.readInt16BE(4);
|
|
47
|
+
this._ac4 = data.readUInt16BE(6);
|
|
48
|
+
this._ac5 = data.readUInt16BE(8);
|
|
49
|
+
this._ac6 = data.readUInt16BE(10);
|
|
50
|
+
this._b1 = data.readInt16BE(12);
|
|
51
|
+
this._b2 = data.readInt16BE(14);
|
|
52
|
+
this._mb = data.readInt16BE(16);
|
|
53
|
+
this._mc = data.readInt16BE(18);
|
|
54
|
+
this._md = data.readInt16BE(20);
|
|
55
|
+
|
|
56
|
+
const coefficients = [this._ac1, this._ac2, this._ac3, this._ac4,
|
|
57
|
+
this._ac5, this._ac6, this._b1, this._b2, this._mb, this._mc, this._md];
|
|
58
|
+
if (coefficients.some(c => c === 0 || c === 0xFFFF)) {
|
|
59
|
+
throw new Error('BMP180 calibration data invalid');
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_writeReg(reg, value) {
|
|
64
|
+
this._transport.write(Buffer.from([reg, value]));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
_readRawTemp() {
|
|
68
|
+
this._writeReg(_REG_CTRL_MEAS, _CMD_TEMP);
|
|
69
|
+
_delay(_CONV_TIME_TEMP * 1000);
|
|
70
|
+
const data = this._transport.writeRead(Buffer.from([_REG_OUT_MSB]), 2);
|
|
71
|
+
return data.readUInt16BE(0);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
_readRawPressure() {
|
|
75
|
+
const cmd = _CMD_PRESSURE[this._oss];
|
|
76
|
+
this._writeReg(_REG_CTRL_MEAS, cmd);
|
|
77
|
+
_delay(_CONV_TIME[this._oss] * 1000);
|
|
78
|
+
const data = this._transport.writeRead(Buffer.from([_REG_OUT_MSB]), 3);
|
|
79
|
+
let up = ((data.readUInt16BE(0) << 8) | data[2]) >> (8 - this._oss);
|
|
80
|
+
return up;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
_compensateTemp(ut) {
|
|
84
|
+
const ac1 = this._ac1, ac2 = this._ac2, ac3 = this._ac3;
|
|
85
|
+
const ac4 = this._ac4, ac5 = this._ac5, ac6 = this._ac6;
|
|
86
|
+
const b1 = this._b1, b2 = this._b2;
|
|
87
|
+
const mc = this._mc, md = this._md;
|
|
88
|
+
|
|
89
|
+
let x1 = Math.floor(((ut - ac6) * ac5) / 32768);
|
|
90
|
+
let x2 = Math.floor((mc << 11) / (x1 + md));
|
|
91
|
+
let b5 = x1 + x2;
|
|
92
|
+
this._b5 = b5;
|
|
93
|
+
return b5;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
_compensatePressure(up) {
|
|
97
|
+
const oss = this._oss;
|
|
98
|
+
const ac1 = this._ac1, ac2 = this._ac2, ac3 = this._ac3;
|
|
99
|
+
const ac4 = this._ac4;
|
|
100
|
+
const b1 = this._b1, b2 = this._b2;
|
|
101
|
+
const b5 = this._b5;
|
|
102
|
+
|
|
103
|
+
let b6 = b5 - 4000;
|
|
104
|
+
let x1 = Math.floor((b2 * Math.floor((b6 * b6) / 4096)) / 2048);
|
|
105
|
+
let x2 = Math.floor((ac2 * b6) / 2048);
|
|
106
|
+
let x3 = x1 + x2;
|
|
107
|
+
let b3 = Math.floor((((ac1 * 4) + x3) << oss) / 4);
|
|
108
|
+
x1 = Math.floor((ac3 * b6) / 8192);
|
|
109
|
+
x2 = Math.floor((b1 * Math.floor((b6 * b6) / 4096)) / 65536);
|
|
110
|
+
x3 = Math.floor((x1 + x2 + 2) / 4);
|
|
111
|
+
let b4 = Math.floor((ac4 * (x3 + 32768)) / 32768);
|
|
112
|
+
let b7 = Math.floor((up - b3) * (50000 >>> oss));
|
|
113
|
+
|
|
114
|
+
let p;
|
|
115
|
+
if (b7 < 0x80000000) {
|
|
116
|
+
p = Math.floor((b7 * 2) / b4);
|
|
117
|
+
} else {
|
|
118
|
+
p = Math.floor((b7 / b4) * 2);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
x1 = Math.floor((p >> 8) * (p >> 8));
|
|
122
|
+
x1 = Math.floor((x1 * 3038) / 65536);
|
|
123
|
+
x2 = Math.floor((-7357 * p) / 65536);
|
|
124
|
+
p = p + Math.floor((x1 + x2 + 3791) / 16);
|
|
125
|
+
|
|
126
|
+
return p;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Read calibrated temperature.
|
|
131
|
+
* @returns {number} Temperature in degrees Celsius.
|
|
132
|
+
*/
|
|
133
|
+
temperature() {
|
|
134
|
+
const ut = this._readRawTemp();
|
|
135
|
+
const b5 = this._compensateTemp(ut);
|
|
136
|
+
return ((b5 + 8) / 160.0);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Read calibrated pressure.
|
|
141
|
+
*
|
|
142
|
+
* Reads temperature first to refresh B5, then reads pressure.
|
|
143
|
+
* Self-contained — may be called without a prior temperature() call.
|
|
144
|
+
*
|
|
145
|
+
* @returns {number} Pressure in hPa.
|
|
146
|
+
*/
|
|
147
|
+
pressure() {
|
|
148
|
+
const ut = this._readRawTemp();
|
|
149
|
+
this._compensateTemp(ut);
|
|
150
|
+
const up = this._readRawPressure();
|
|
151
|
+
const p_pa = this._compensatePressure(up);
|
|
152
|
+
return p_pa / 100.0;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* BMP180 full interface — extends BMP180Minimal with OSS control and altitude helpers.
|
|
158
|
+
*
|
|
159
|
+
* OSS constants:
|
|
160
|
+
* - BMP180Full.OSS_ULP — Ultra Low Power (oss=0, 4.5 ms)
|
|
161
|
+
* - BMP180Full.OSS_STANDARD — Standard (oss=1, 7.5 ms)
|
|
162
|
+
* - BMP180Full.OSS_HIGH_RES — High Resolution (oss=2, 13.5 ms)
|
|
163
|
+
* - BMP180Full.OSS_ULTRA_HIGH_RES — Ultra High Resolution (oss=3, 25.5 ms)
|
|
164
|
+
*
|
|
165
|
+
* @param {object} transport - Configured I²C transport.
|
|
166
|
+
* @param {number} [oss=0] - Oversampling mode 0–3.
|
|
167
|
+
*/
|
|
168
|
+
class BMP180Full extends BMP180Minimal {
|
|
169
|
+
static OSS_ULP = 0;
|
|
170
|
+
static OSS_STANDARD = 1;
|
|
171
|
+
static OSS_HIGH_RES = 2;
|
|
172
|
+
static OSS_ULTRA_HIGH_RES = 3;
|
|
173
|
+
|
|
174
|
+
constructor(transport, oss = 0) {
|
|
175
|
+
super(transport);
|
|
176
|
+
this._oss = oss & 0x03;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Read the current oversampling mode.
|
|
181
|
+
* @returns {number} OSS value 0–3.
|
|
182
|
+
*/
|
|
183
|
+
oversampling() {
|
|
184
|
+
return this._oss;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Change the oversampling mode for subsequent pressure() calls.
|
|
189
|
+
* @param {number} oss - New OSS value 0–3.
|
|
190
|
+
*/
|
|
191
|
+
setOversampling(oss) {
|
|
192
|
+
this._oss = oss & 0x03;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Compute altitude above sea level from the current pressure.
|
|
197
|
+
* @param {number} [seaLevelHpa=1013.25] - Reference sea-level pressure in hPa.
|
|
198
|
+
* @returns {number} Altitude in metres.
|
|
199
|
+
*/
|
|
200
|
+
altitude(seaLevelHpa = 1013.25) {
|
|
201
|
+
const p = this.pressure();
|
|
202
|
+
return 44330 * (1 - Math.pow(p / seaLevelHpa, 1 / 5.255));
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Compute sea-level pressure for a known altitude.
|
|
207
|
+
* @param {number} altitudeM - Altitude in metres.
|
|
208
|
+
* @returns {number} Sea-level pressure in hPa.
|
|
209
|
+
*/
|
|
210
|
+
seaLevelPressure(altitudeM) {
|
|
211
|
+
const p = this.pressure();
|
|
212
|
+
return p / Math.pow(1 - altitudeM / 44330, 5.255);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Read the chip ID register.
|
|
217
|
+
* @returns {number} Chip ID; expect 0x55.
|
|
218
|
+
*/
|
|
219
|
+
chipId() {
|
|
220
|
+
const data = this._transport.writeRead(Buffer.from([_REG_ID]), 1);
|
|
221
|
+
return data[0];
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Perform a soft reset and re-read calibration coefficients.
|
|
226
|
+
*/
|
|
227
|
+
reset() {
|
|
228
|
+
this._writeReg(_REG_SOFT_RESET, _SOFT_RESET_CMD);
|
|
229
|
+
_delay(10);
|
|
230
|
+
this._readCalibration();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
module.exports = { BMP180Minimal, BMP180Full };
|