periph 1.2.0 → 1.3.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/README.md +18 -0
- package/examples/accelerometer/bma150/complete.js +62 -0
- package/examples/accelerometer/bma150/demo.js +65 -0
- package/examples/accelerometer/bma150/minimal.js +18 -0
- package/examples/adc_dac/hy3131/complete.js +66 -0
- package/examples/adc_dac/hy3131/demo.js +37 -0
- package/examples/adc_dac/hy3131/minimal.js +18 -0
- package/examples/discovery/complete.js +25 -0
- package/examples/discovery/demo.js +30 -0
- package/examples/discovery/minimal.js +12 -0
- package/examples/display/il0373/complete.js +71 -0
- package/examples/display/il0373/demo.js +49 -0
- package/examples/display/il0373/minimal.js +26 -0
- package/examples/display/ssd1331/complete.js +83 -0
- package/examples/display/ssd1331/demo.js +49 -0
- package/examples/display/ssd1331/minimal.js +27 -0
- package/examples/display/st7036/complete.js +71 -0
- package/examples/display/st7036/demo.js +56 -0
- package/examples/display/st7036/minimal.js +21 -0
- package/examples/display/uc1611s/complete.js +135 -0
- package/examples/display/uc1611s/complete_spi.js +128 -0
- package/examples/display/uc1611s/demo.js +90 -0
- package/examples/display/uc1611s/demo_spi.js +91 -0
- package/examples/display/uc1611s/minimal.js +28 -0
- package/examples/display/uc1611s/minimal_spi.js +30 -0
- package/examples/display/uc8159/complete.js +64 -0
- package/examples/display/uc8159/demo.js +41 -0
- package/examples/display/uc8159/minimal.js +28 -0
- package/examples/display/uc8276c/complete.js +73 -0
- package/examples/display/uc8276c/demo.js +49 -0
- package/examples/display/uc8276c/minimal.js +26 -0
- package/examples/environmental/bme280/complete.js +1 -1
- package/examples/environmental/bme280/demo.js +1 -1
- package/examples/environmental/bme280/minimal.js +1 -1
- package/examples/gyroscope/l3g4200d/complete.js +1 -1
- package/examples/gyroscope/l3g4200d/demo.js +1 -1
- package/examples/gyroscope/l3g4200d/minimal.js +1 -1
- package/examples/memory/23x1024/complete.js +54 -0
- package/examples/memory/23x1024/demo.js +51 -0
- package/examples/memory/23x1024/minimal.js +22 -0
- package/examples/memory/mx25l12845e/complete.js +66 -0
- package/examples/memory/mx25l12845e/demo.js +66 -0
- package/examples/memory/mx25l12845e/minimal.js +27 -0
- package/examples/memory/sd_spi/complete.js +55 -0
- package/examples/memory/sd_spi/demo.js +67 -0
- package/examples/memory/sd_spi/minimal.js +32 -0
- package/examples/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -0
- package/examples/pressure/bmp280/complete.js +1 -1
- package/examples/pressure/bmp280/demo.js +1 -1
- package/examples/pressure/bmp280/minimal.js +1 -1
- package/examples/pressure/bmp581/complete.js +1 -1
- package/examples/pressure/bmp581/demo.js +1 -1
- package/examples/pressure/bmp581/minimal.js +1 -1
- package/examples/pressure/lps22df/complete.js +1 -1
- package/examples/pressure/lps22df/demo.js +1 -1
- package/examples/pressure/lps22df/minimal.js +1 -1
- package/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -0
- package/package.json +1 -1
- package/src/chips/accelerometer/adxl345.js +10 -31
- package/src/chips/accelerometer/adxl362.js +7 -7
- package/src/chips/accelerometer/bma150.js +419 -0
- package/src/chips/adc_dac/ad7705.js +36 -9
- package/src/chips/adc_dac/ad7706.js +39 -9
- package/src/chips/adc_dac/hy3131.js +311 -0
- package/src/chips/display/il0373.js +607 -0
- package/src/chips/display/ssd1331.js +412 -0
- package/src/chips/display/st7036.js +401 -0
- package/src/chips/display/uc1611s.js +648 -0
- package/src/chips/display/uc8159.js +394 -0
- package/src/chips/display/uc8276c.js +658 -0
- package/src/chips/environmental/bme280.js +31 -46
- package/src/chips/environmental/bme680.js +38 -43
- package/src/chips/gas/ens160.js +18 -26
- package/src/chips/gyroscope/l3g4200d.js +34 -50
- package/src/chips/gyroscope/l3gd20h.js +24 -40
- package/src/chips/imu/mpu6050.js +29 -41
- package/src/chips/imu/mpu9250.js +45 -69
- package/src/chips/imu/mpu9255.js +54 -78
- package/src/chips/io_expander/mcp23017.js +24 -33
- package/src/chips/light/apds-9930.js +52 -60
- package/src/chips/light/apds9960.js +69 -77
- package/src/chips/magnetometer/as5600.js +13 -27
- package/src/chips/magnetometer/hmc5883l.js +32 -29
- package/src/chips/memory/_23x1024.js +228 -0
- package/src/chips/memory/_24aa02uid.js +8 -11
- package/src/chips/memory/mx25l12845e.js +467 -0
- package/src/chips/memory/sd_spi.js +486 -0
- package/src/chips/motor/drv8830.js +239 -0
- package/src/chips/other/mpr121.js +7 -7
- package/src/chips/power/ina219.js +5 -8
- package/src/chips/power/ina226.js +5 -8
- package/src/chips/power/ina3221.js +5 -8
- package/src/chips/pressure/bmp085.js +17 -11
- package/src/chips/pressure/bmp180.js +7 -7
- package/src/chips/pressure/bmp280.js +21 -33
- package/src/chips/pressure/bmp384.js +28 -39
- package/src/chips/pressure/bmp581.js +59 -70
- package/src/chips/pressure/lps22df.js +50 -52
- package/src/chips/pressure/lps28dfw.js +15 -13
- package/src/chips/pressure/lps33hw.js +7 -5
- package/src/chips/rtc/ds3231.js +504 -0
- package/src/chips/rtc/pcf8523.js +533 -0
- package/src/chips/temperature/mcp9808.js +490 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -0
- package/src/connection/connection.js +6 -4
- package/src/connection/dhtxx.js +5 -3
- package/src/connection/display.js +278 -0
- package/src/connection/display_mock.js +77 -0
- package/src/connection/hx711.js +5 -3
- package/src/connection/i2c.js +6 -4
- package/src/connection/i2c_mock.js +47 -0
- package/src/connection/input_pin.js +36 -5
- package/src/connection/register.js +14 -0
- package/src/connection/register_connection.js +65 -0
- package/src/connection/sipo.js +4 -2
- package/src/connection/sipo_mock.js +49 -0
- package/src/connection/smbus.js +6 -4
- package/src/connection/softspi.js +73 -0
- package/src/connection/spi.js +110 -23
- package/src/connection/spi_mock.js +50 -0
- package/src/connection/uart.js +2 -1
- package/src/discovery/discovery.js +218 -0
- package/src/discovery/registry.js +85 -0
- package/src/storage/block_device.js +95 -0
- package/src/storage/ram_block_device.js +72 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_CONTROL = 0x00;
|
|
4
|
+
const _REG_FAULT = 0x01;
|
|
5
|
+
|
|
6
|
+
const _CTRL_IN1 = 0x01;
|
|
7
|
+
const _CTRL_IN2 = 0x02;
|
|
8
|
+
|
|
9
|
+
const _FAULT_FAULT = 0x01;
|
|
10
|
+
const _FAULT_OCP = 0x02;
|
|
11
|
+
const _FAULT_UVLO = 0x04;
|
|
12
|
+
const _FAULT_OTS = 0x08;
|
|
13
|
+
const _FAULT_ILIMIT = 0x10;
|
|
14
|
+
const _FAULT_CLEAR = 0x80;
|
|
15
|
+
|
|
16
|
+
const _VREF = 1.285;
|
|
17
|
+
const _VSET_MIN = 6;
|
|
18
|
+
const _VSET_MAX = 63;
|
|
19
|
+
|
|
20
|
+
const _DIRECTIONS = ['coast', 'forward', 'reverse', 'brake'];
|
|
21
|
+
|
|
22
|
+
/** Map |voltage| to a VSET code; 0 means coast (below the vset=6 floor). */
|
|
23
|
+
function _voltageToVset(voltage) {
|
|
24
|
+
const vset = Math.floor(Math.abs(voltage) * 16 / _VREF + 0.5);
|
|
25
|
+
if (vset < _VSET_MIN) return 0;
|
|
26
|
+
if (vset > _VSET_MAX) return _VSET_MAX;
|
|
27
|
+
return vset;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function _vsetToVoltage(vset) {
|
|
31
|
+
if (vset < _VSET_MIN) return 0;
|
|
32
|
+
return _VREF * vset / 16;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* DRV8830 low-voltage motor driver with I²C interface (Texas Instruments)
|
|
37
|
+
* — minimal interface.
|
|
38
|
+
*
|
|
39
|
+
* H-bridge driver for a single brushed DC motor, controlled entirely over
|
|
40
|
+
* I²C. The host commands a target output *voltage*; the chip PWM-regulates
|
|
41
|
+
* the bridge to hold that average voltage regardless of supply sag. Nine
|
|
42
|
+
* selectable addresses (0x60–0x68) via the tri-state A0/A1 strap pins.
|
|
43
|
+
*
|
|
44
|
+
* No register writes are needed at construction — the chip's POR default
|
|
45
|
+
* already leaves the motor in standby/coast.
|
|
46
|
+
*/
|
|
47
|
+
class DRV8830Minimal {
|
|
48
|
+
/**
|
|
49
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x60–0x68).
|
|
50
|
+
*/
|
|
51
|
+
constructor(connection) {
|
|
52
|
+
this._conn = connection;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async _writeReg(reg, value) {
|
|
56
|
+
await this._conn.writeReg(reg & 0xFF, value & 0xFF);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async _readReg(reg) {
|
|
60
|
+
const buf = await this._conn.readReg(reg & 0xFF, 1);
|
|
61
|
+
return buf[0];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Confirm the device answers on the bus. The DRV8830 has no
|
|
66
|
+
* WHO_AM_I/identity register, so this is a plain CONTROL read; it makes
|
|
67
|
+
* no register writes.
|
|
68
|
+
* @returns {Promise<void>}
|
|
69
|
+
*/
|
|
70
|
+
async init() {
|
|
71
|
+
await this._readReg(_REG_CONTROL);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Drive the motor at a regulated output voltage. Writes VSET and
|
|
76
|
+
* IN1/IN2 together in one CONTROL write. A magnitude below the ~0.48 V
|
|
77
|
+
* floor is treated as 0 V (coast); above ~5.06 V it is clamped.
|
|
78
|
+
* @param {number} voltage - Signed target voltage in V: positive = forward, negative = reverse, 0 = coast.
|
|
79
|
+
* @returns {Promise<void>}
|
|
80
|
+
*/
|
|
81
|
+
async drive(voltage) {
|
|
82
|
+
const vset = _voltageToVset(voltage);
|
|
83
|
+
if (vset === 0) {
|
|
84
|
+
await this._writeReg(_REG_CONTROL, 0x00);
|
|
85
|
+
} else if (voltage > 0) {
|
|
86
|
+
await this._writeReg(_REG_CONTROL, (vset << 2) | _CTRL_IN1);
|
|
87
|
+
} else {
|
|
88
|
+
await this._writeReg(_REG_CONTROL, (vset << 2) | _CTRL_IN2);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Short-brake the motor (IN1 = IN2 = 1, both outputs high).
|
|
94
|
+
* @returns {Promise<void>}
|
|
95
|
+
*/
|
|
96
|
+
async brake() {
|
|
97
|
+
await this._writeReg(_REG_CONTROL, _CTRL_IN1 | _CTRL_IN2);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Put the bridge in standby/coast (IN1 = IN2 = 0) — same as `drive(0)`.
|
|
102
|
+
* @returns {Promise<void>}
|
|
103
|
+
*/
|
|
104
|
+
async stop() {
|
|
105
|
+
await this._writeReg(_REG_CONTROL, 0x00);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* DRV8830 full interface — extends Minimal with raw CONTROL access, output
|
|
111
|
+
* read-back, fault reporting/clearing, and the FAULTn interrupt API.
|
|
112
|
+
*
|
|
113
|
+
* Faults are never cleared implicitly: a latched OCP/ILIMIT fault also
|
|
114
|
+
* disables the H-bridge, so clearing is always an explicit `clearFault()`.
|
|
115
|
+
*/
|
|
116
|
+
class DRV8830Full extends DRV8830Minimal {
|
|
117
|
+
/**
|
|
118
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x60–0x68).
|
|
119
|
+
*/
|
|
120
|
+
constructor(connection) {
|
|
121
|
+
super(connection);
|
|
122
|
+
this._callback = null;
|
|
123
|
+
this._edgeHandler = null;
|
|
124
|
+
this._pollTimer = null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Write the CONTROL register from raw fields.
|
|
129
|
+
* @param {number} vset - VSET DAC code, 6–63 (0–5 are reserved).
|
|
130
|
+
* @param {boolean} in1 - H-bridge input 1.
|
|
131
|
+
* @param {boolean} in2 - H-bridge input 2.
|
|
132
|
+
* @returns {Promise<void>}
|
|
133
|
+
* @throws {RangeError} If vset is outside 6–63.
|
|
134
|
+
*/
|
|
135
|
+
async setOutput(vset, in1, in2) {
|
|
136
|
+
if (!Number.isInteger(vset) || vset < _VSET_MIN || vset > _VSET_MAX) {
|
|
137
|
+
throw new RangeError('vset must be an integer 6-63');
|
|
138
|
+
}
|
|
139
|
+
await this._writeReg(_REG_CONTROL, (vset << 2) | (in1 ? _CTRL_IN1 : 0) | (in2 ? _CTRL_IN2 : 0));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Read back and decode the CONTROL register.
|
|
144
|
+
* @returns {Promise<{voltage: number, direction: ('forward'|'reverse'|'coast'|'brake')}>}
|
|
145
|
+
* voltage is the commanded magnitude in V (0 for a reserved VSET code).
|
|
146
|
+
*/
|
|
147
|
+
async readOutput() {
|
|
148
|
+
const ctrl = await this._readReg(_REG_CONTROL);
|
|
149
|
+
return { voltage: _vsetToVoltage(ctrl >> 2), direction: _DIRECTIONS[ctrl & 0x03] };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Read the FAULT register without clearing it.
|
|
154
|
+
* @returns {Promise<{fault: boolean, ocp: boolean, uvlo: boolean, ots: boolean, ilimit: boolean}>}
|
|
155
|
+
*/
|
|
156
|
+
async readFault() {
|
|
157
|
+
const f = await this._readReg(_REG_FAULT);
|
|
158
|
+
return {
|
|
159
|
+
fault: (f & _FAULT_FAULT) !== 0,
|
|
160
|
+
ocp: (f & _FAULT_OCP) !== 0,
|
|
161
|
+
uvlo: (f & _FAULT_UVLO) !== 0,
|
|
162
|
+
ots: (f & _FAULT_OTS) !== 0,
|
|
163
|
+
ilimit: (f & _FAULT_ILIMIT) !== 0,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Clear all fault status bits (CLEAR = 1); re-enables the H-bridge if an
|
|
169
|
+
* OCP/ILIMIT fault had latched it off.
|
|
170
|
+
* @returns {Promise<void>}
|
|
171
|
+
*/
|
|
172
|
+
async clearFault() {
|
|
173
|
+
await this._writeReg(_REG_FAULT, _FAULT_CLEAR);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// -- Interrupt API (Level 1: FAULTn) ------------------------------------
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Read the fault status — equivalent to `readFault()`, does not clear.
|
|
180
|
+
* @returns {Promise<{fault: boolean, ocp: boolean, uvlo: boolean, ots: boolean, ilimit: boolean}>}
|
|
181
|
+
*/
|
|
182
|
+
async pollInterrupt() {
|
|
183
|
+
return this.readFault();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Subscribe to fault interrupts. Uses `connection.intPin.onEdge()` (FAULTn
|
|
188
|
+
* falling edge) when an INT-line InputPin is wired; otherwise falls back
|
|
189
|
+
* to a 5 ms polling loop that fires on each new fault (FAULT bit rising).
|
|
190
|
+
* The fault is not cleared.
|
|
191
|
+
* @param {function({fault: boolean, ocp: boolean, uvlo: boolean, ots: boolean, ilimit: boolean}): void} callback
|
|
192
|
+
* @returns {Promise<void>}
|
|
193
|
+
*/
|
|
194
|
+
async onInterrupt(callback) {
|
|
195
|
+
this._callback = callback;
|
|
196
|
+
if (this._conn.intPin) {
|
|
197
|
+
this._edgeHandler = async () => {
|
|
198
|
+
const status = await this.pollInterrupt();
|
|
199
|
+
if (status.fault && this._callback) this._callback(status);
|
|
200
|
+
};
|
|
201
|
+
await this._conn.intPin.onEdge(this._edgeHandler, 'falling');
|
|
202
|
+
} else {
|
|
203
|
+
let wasFault = false;
|
|
204
|
+
let busy = false;
|
|
205
|
+
this._pollTimer = setInterval(async () => {
|
|
206
|
+
if (busy) return;
|
|
207
|
+
busy = true;
|
|
208
|
+
try {
|
|
209
|
+
const status = await this.pollInterrupt();
|
|
210
|
+
if (status.fault && !wasFault && this._callback) this._callback(status);
|
|
211
|
+
wasFault = status.fault;
|
|
212
|
+
} finally {
|
|
213
|
+
busy = false;
|
|
214
|
+
}
|
|
215
|
+
}, 5);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Unsubscribe from fault interrupts.
|
|
221
|
+
* @returns {Promise<void>}
|
|
222
|
+
*/
|
|
223
|
+
async offInterrupt() {
|
|
224
|
+
if (this._conn.intPin && this._edgeHandler) {
|
|
225
|
+
await this._conn.intPin.offEdge(this._edgeHandler);
|
|
226
|
+
this._edgeHandler = null;
|
|
227
|
+
}
|
|
228
|
+
if (this._pollTimer) {
|
|
229
|
+
clearInterval(this._pollTimer);
|
|
230
|
+
this._pollTimer = null;
|
|
231
|
+
}
|
|
232
|
+
this._callback = null;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Default 7-bit I²C address (A0 = A1 = GND). Valid range 0x60–0x68. */
|
|
237
|
+
DRV8830Minimal.I2C_ADDRESS = 0x60;
|
|
238
|
+
|
|
239
|
+
module.exports = { DRV8830Minimal, DRV8830Full };
|
|
@@ -57,7 +57,7 @@ function _sleep(ms) {
|
|
|
57
57
|
* - USL = 0xC9, TL = 0xB4, LSL = 0x82 (3.3 V VDD)
|
|
58
58
|
* - ECR = 0x8C (all 12 electrodes enabled)
|
|
59
59
|
*
|
|
60
|
-
* @param {import('../../connection/
|
|
60
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the device (address 0x5A by default).
|
|
61
61
|
*/
|
|
62
62
|
class MPR121Minimal {
|
|
63
63
|
constructor(connection) {
|
|
@@ -90,15 +90,15 @@ class MPR121Minimal {
|
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
async _writeReg(reg, value) {
|
|
93
|
-
await this._conn.
|
|
93
|
+
await this._conn.writeReg(reg & 0xFF, value & 0xFF);
|
|
94
94
|
}
|
|
95
95
|
|
|
96
96
|
async _readReg(reg) {
|
|
97
|
-
return (await this._conn.
|
|
97
|
+
return (await this._conn.readReg(reg & 0xFF, 1))[0];
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
async _readReg16(reg) {
|
|
101
|
-
const buf = await this._conn.
|
|
101
|
+
const buf = await this._conn.readReg(reg & 0xFF, 2);
|
|
102
102
|
return (buf[0]) | ((buf[1] & 0x03) << 8);
|
|
103
103
|
}
|
|
104
104
|
|
|
@@ -111,7 +111,7 @@ class MPR121Minimal {
|
|
|
111
111
|
* @returns {Promise<number>} 12-bit bitmask; bit n = 1 if ELEn is touched.
|
|
112
112
|
*/
|
|
113
113
|
async touched() {
|
|
114
|
-
const buf = await this._conn.
|
|
114
|
+
const buf = await this._conn.readReg(_REG_ELE0_7_TOUCH, 2);
|
|
115
115
|
return buf[0] | ((buf[1] & 0x0F) << 8);
|
|
116
116
|
}
|
|
117
117
|
|
|
@@ -140,7 +140,7 @@ class MPR121Full extends MPR121Minimal {
|
|
|
140
140
|
static get SOURCE_ACF() { return 0x01; }
|
|
141
141
|
|
|
142
142
|
/**
|
|
143
|
-
* @param {import('../../connection/
|
|
143
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the device.
|
|
144
144
|
*/
|
|
145
145
|
constructor(connection) {
|
|
146
146
|
super(connection);
|
|
@@ -267,7 +267,7 @@ class MPR121Full extends MPR121Minimal {
|
|
|
267
267
|
* @returns {Promise<number>} bits 0-11 = ELE0-ELE11 OOR, bit 12 = ELEPROX OOR.
|
|
268
268
|
*/
|
|
269
269
|
async oorStatus() {
|
|
270
|
-
const buf = await this._conn.
|
|
270
|
+
const buf = await this._conn.readReg(_REG_ELE0_7_OOR, 2);
|
|
271
271
|
return buf[0] | ((buf[1] & 0x1F) << 8);
|
|
272
272
|
}
|
|
273
273
|
|
|
@@ -23,7 +23,7 @@ const _REG_CAL = 0x05;
|
|
|
23
23
|
*/
|
|
24
24
|
class INA219Minimal {
|
|
25
25
|
/**
|
|
26
|
-
* @param {import('../../connection/
|
|
26
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (writeRead, write).
|
|
27
27
|
* @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
28
28
|
* @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
|
|
29
29
|
*/
|
|
@@ -35,18 +35,15 @@ class INA219Minimal {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
async _writeReg(reg, value) {
|
|
38
|
-
|
|
39
|
-
buf[0] = reg;
|
|
40
|
-
buf.writeUInt16BE(value, 1);
|
|
41
|
-
await this._conn.write(buf);
|
|
38
|
+
await this._conn.writeReg(reg, Buffer.from([(value >> 8) & 0xFF, value & 0xFF]));
|
|
42
39
|
}
|
|
43
40
|
|
|
44
41
|
async _readReg(reg) {
|
|
45
|
-
return (await this._conn.
|
|
42
|
+
return (await this._conn.readReg(reg, 2)).readUInt16BE(0);
|
|
46
43
|
}
|
|
47
44
|
|
|
48
45
|
async _readRegSigned(reg) {
|
|
49
|
-
return (await this._conn.
|
|
46
|
+
return (await this._conn.readReg(reg, 2)).readInt16BE(0);
|
|
50
47
|
}
|
|
51
48
|
|
|
52
49
|
/** @return {Promise<number>} Bus voltage in volts ((raw >> 3) × 4 mV LSB). */
|
|
@@ -99,7 +96,7 @@ class INA219Full extends INA219Minimal {
|
|
|
99
96
|
static MODE_SHUNT_BUS_CONT = 7;
|
|
100
97
|
|
|
101
98
|
/**
|
|
102
|
-
* @param {import('../../connection/
|
|
99
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
|
|
103
100
|
* @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
104
101
|
* @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
|
|
105
102
|
*/
|
|
@@ -28,7 +28,7 @@ const _CONFIG_DEFAULT = 0x4127;
|
|
|
28
28
|
*/
|
|
29
29
|
class INA226Minimal {
|
|
30
30
|
/**
|
|
31
|
-
* @param {import('../../connection/
|
|
31
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (writeRead, write).
|
|
32
32
|
* @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
33
33
|
* @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
|
|
34
34
|
*/
|
|
@@ -41,18 +41,15 @@ class INA226Minimal {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
async _writeReg(reg, value) {
|
|
44
|
-
|
|
45
|
-
buf[0] = reg;
|
|
46
|
-
buf.writeUInt16BE(value, 1);
|
|
47
|
-
await this._conn.write(buf);
|
|
44
|
+
await this._conn.writeReg(reg, Buffer.from([(value >> 8) & 0xFF, value & 0xFF]));
|
|
48
45
|
}
|
|
49
46
|
|
|
50
47
|
async _readReg(reg) {
|
|
51
|
-
return (await this._conn.
|
|
48
|
+
return (await this._conn.readReg(reg, 2)).readUInt16BE(0);
|
|
52
49
|
}
|
|
53
50
|
|
|
54
51
|
async _readRegSigned(reg) {
|
|
55
|
-
return (await this._conn.
|
|
52
|
+
return (await this._conn.readReg(reg, 2)).readInt16BE(0);
|
|
56
53
|
}
|
|
57
54
|
|
|
58
55
|
/**
|
|
@@ -104,7 +101,7 @@ class INA226Full extends INA226Minimal {
|
|
|
104
101
|
static AFF = 0x0010;
|
|
105
102
|
|
|
106
103
|
/**
|
|
107
|
-
* @param {import('../../connection/
|
|
104
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
|
|
108
105
|
* @param {number} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
109
106
|
* @param {number} [maxCurrent=2.0] - Maximum expected current in amperes.
|
|
110
107
|
*/
|
|
@@ -34,7 +34,7 @@ const _WARN_REGS = [_REG_CH1_WARN, _REG_CH2_WARN, _REG_CH3_WARN];
|
|
|
34
34
|
* The chip's power-on default (all three channels on, continuous shunt+bus)
|
|
35
35
|
* is used without modification.
|
|
36
36
|
*
|
|
37
|
-
* @param {import('../../connection/
|
|
37
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (writeRead, write).
|
|
38
38
|
* @param {number|number[]} [rShunt=0.1] - Shunt resistor value in ohms. Pass a
|
|
39
39
|
* single number to apply the same value to all three channels, or an
|
|
40
40
|
* array of 3 numbers for per-channel values.
|
|
@@ -50,18 +50,15 @@ class INA3221Minimal {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
async _writeReg(reg, value) {
|
|
53
|
-
|
|
54
|
-
buf[0] = reg;
|
|
55
|
-
buf.writeUInt16BE(value, 1);
|
|
56
|
-
await this._conn.write(buf);
|
|
53
|
+
await this._conn.writeReg(reg, Buffer.from([(value >> 8) & 0xFF, value & 0xFF]));
|
|
57
54
|
}
|
|
58
55
|
|
|
59
56
|
async _readReg(reg) {
|
|
60
|
-
return (await this._conn.
|
|
57
|
+
return (await this._conn.readReg(reg, 2)).readUInt16BE(0);
|
|
61
58
|
}
|
|
62
59
|
|
|
63
60
|
async _readRegSigned(reg) {
|
|
64
|
-
return (await this._conn.
|
|
61
|
+
return (await this._conn.readReg(reg, 2)).readInt16BE(0);
|
|
65
62
|
}
|
|
66
63
|
|
|
67
64
|
_channelValid(channel) {
|
|
@@ -137,7 +134,7 @@ class INA3221Minimal {
|
|
|
137
134
|
* - INA3221Full.MODE_BUS_CONT = 6
|
|
138
135
|
* - INA3221Full.MODE_SHUNT_BUS_CONT = 7
|
|
139
136
|
*
|
|
140
|
-
* @param {import('../../connection/
|
|
137
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
|
|
141
138
|
* @param {number|number[]} [rShunt=0.1] - Shunt resistor value in ohms.
|
|
142
139
|
*/
|
|
143
140
|
class INA3221Full extends INA3221Minimal {
|
|
@@ -41,7 +41,7 @@ function _delay(ms) {
|
|
|
41
41
|
* `await sensor.temperature()` right after construction and handle its
|
|
42
42
|
* rejection.
|
|
43
43
|
*
|
|
44
|
-
* @param {import('../../connection/
|
|
44
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
|
|
45
45
|
*/
|
|
46
46
|
class BMP085Minimal {
|
|
47
47
|
constructor(connection) {
|
|
@@ -51,7 +51,7 @@ class BMP085Minimal {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
async _readCalibration() {
|
|
54
|
-
const data = await this._conn.
|
|
54
|
+
const data = await this._conn.readReg(_REG_CAL_START, 22);
|
|
55
55
|
this._ac1 = data.readInt16BE(0);
|
|
56
56
|
this._ac2 = data.readInt16BE(2);
|
|
57
57
|
this._ac3 = data.readInt16BE(4);
|
|
@@ -64,21 +64,27 @@ class BMP085Minimal {
|
|
|
64
64
|
this._mc = data.readInt16BE(18);
|
|
65
65
|
this._md = data.readInt16BE(20);
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
// Sanity-check the RAW 16-bit words, not the signed-interpreted
|
|
68
|
+
// coefficients: 8 of the 11 are signed (readInt16BE, range
|
|
69
|
+
// -32768..32767), so a raw 0xFFFF word reads back as -1, which can
|
|
70
|
+
// never equal the literal 0xFFFF -- comparing the signed value
|
|
71
|
+
// would silently miss the "all-ones" sentinel on those 8 fields.
|
|
72
|
+
for (let i = 0; i < 22; i += 2) {
|
|
73
|
+
const raw16 = data.readUInt16BE(i);
|
|
74
|
+
if (raw16 === 0x0000 || raw16 === 0xFFFF) {
|
|
75
|
+
throw new Error('BMP085 calibration data invalid');
|
|
76
|
+
}
|
|
71
77
|
}
|
|
72
78
|
}
|
|
73
79
|
|
|
74
80
|
async _writeReg(reg, value) {
|
|
75
|
-
await this._conn.
|
|
81
|
+
await this._conn.writeReg(reg, value);
|
|
76
82
|
}
|
|
77
83
|
|
|
78
84
|
async _readRawTemp() {
|
|
79
85
|
await this._writeReg(_REG_CTRL_MEAS, _CMD_TEMP);
|
|
80
86
|
_delay(_CONV_TIME_TEMP * 1000);
|
|
81
|
-
const data = await this._conn.
|
|
87
|
+
const data = await this._conn.readReg(_REG_OUT_MSB, 2);
|
|
82
88
|
return data.readUInt16BE(0);
|
|
83
89
|
}
|
|
84
90
|
|
|
@@ -86,7 +92,7 @@ class BMP085Minimal {
|
|
|
86
92
|
const cmd = _CMD_PRESSURE[this._oss];
|
|
87
93
|
await this._writeReg(_REG_CTRL_MEAS, cmd);
|
|
88
94
|
_delay(_CONV_TIME[this._oss] * 1000);
|
|
89
|
-
const data = await this._conn.
|
|
95
|
+
const data = await this._conn.readReg(_REG_OUT_MSB, 3);
|
|
90
96
|
let up = ((data.readUInt16BE(0) << 8) | data[2]) >> (8 - this._oss);
|
|
91
97
|
return up;
|
|
92
98
|
}
|
|
@@ -178,7 +184,7 @@ class BMP085Minimal {
|
|
|
178
184
|
* - BMP085Full.OSS_HIGH_RES — High Resolution (oss=2, 13.5 ms)
|
|
179
185
|
* - BMP085Full.OSS_ULTRA_HIGH_RES — Ultra High Resolution (oss=3, 25.5 ms)
|
|
180
186
|
*
|
|
181
|
-
* @param {import('../../connection/
|
|
187
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
|
|
182
188
|
* @param {number} [oss=0] - Oversampling mode 0–3.
|
|
183
189
|
*/
|
|
184
190
|
class BMP085Full extends BMP085Minimal {
|
|
@@ -233,7 +239,7 @@ class BMP085Full extends BMP085Minimal {
|
|
|
233
239
|
* @returns {Promise<number>} Chip ID; expect 0x55.
|
|
234
240
|
*/
|
|
235
241
|
async chipId() {
|
|
236
|
-
const data = await this._conn.
|
|
242
|
+
const data = await this._conn.readReg(_REG_ID, 1);
|
|
237
243
|
return data[0];
|
|
238
244
|
}
|
|
239
245
|
|
|
@@ -41,7 +41,7 @@ function _delay(ms) {
|
|
|
41
41
|
* `await sensor.temperature()` right after construction and handle its
|
|
42
42
|
* rejection.
|
|
43
43
|
*
|
|
44
|
-
* @param {import('../../connection/
|
|
44
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
|
|
45
45
|
*/
|
|
46
46
|
class BMP180Minimal {
|
|
47
47
|
constructor(connection) {
|
|
@@ -51,7 +51,7 @@ class BMP180Minimal {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
async _readCalibration() {
|
|
54
|
-
const data = await this._conn.
|
|
54
|
+
const data = await this._conn.readReg(_REG_CAL_START, 22);
|
|
55
55
|
this._ac1 = data.readInt16BE(0);
|
|
56
56
|
this._ac2 = data.readInt16BE(2);
|
|
57
57
|
this._ac3 = data.readInt16BE(4);
|
|
@@ -72,13 +72,13 @@ class BMP180Minimal {
|
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
async _writeReg(reg, value) {
|
|
75
|
-
await this._conn.
|
|
75
|
+
await this._conn.writeReg(reg, value);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
async _readRawTemp() {
|
|
79
79
|
await this._writeReg(_REG_CTRL_MEAS, _CMD_TEMP);
|
|
80
80
|
_delay(_CONV_TIME_TEMP * 1000);
|
|
81
|
-
const data = await this._conn.
|
|
81
|
+
const data = await this._conn.readReg(_REG_OUT_MSB, 2);
|
|
82
82
|
return data.readUInt16BE(0);
|
|
83
83
|
}
|
|
84
84
|
|
|
@@ -86,7 +86,7 @@ class BMP180Minimal {
|
|
|
86
86
|
const cmd = _CMD_PRESSURE[this._oss];
|
|
87
87
|
await this._writeReg(_REG_CTRL_MEAS, cmd);
|
|
88
88
|
_delay(_CONV_TIME[this._oss] * 1000);
|
|
89
|
-
const data = await this._conn.
|
|
89
|
+
const data = await this._conn.readReg(_REG_OUT_MSB, 3);
|
|
90
90
|
let up = ((data.readUInt16BE(0) << 8) | data[2]) >> (8 - this._oss);
|
|
91
91
|
return up;
|
|
92
92
|
}
|
|
@@ -178,7 +178,7 @@ class BMP180Minimal {
|
|
|
178
178
|
* - BMP180Full.OSS_HIGH_RES — High Resolution (oss=2, 13.5 ms)
|
|
179
179
|
* - BMP180Full.OSS_ULTRA_HIGH_RES — Ultra High Resolution (oss=3, 25.5 ms)
|
|
180
180
|
*
|
|
181
|
-
* @param {import('../../connection/
|
|
181
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
|
|
182
182
|
* @param {number} [oss=0] - Oversampling mode 0–3.
|
|
183
183
|
*/
|
|
184
184
|
class BMP180Full extends BMP180Minimal {
|
|
@@ -233,7 +233,7 @@ class BMP180Full extends BMP180Minimal {
|
|
|
233
233
|
* @returns {Promise<number>} Chip ID; expect 0x55.
|
|
234
234
|
*/
|
|
235
235
|
async chipId() {
|
|
236
|
-
const data = await this._conn.
|
|
236
|
+
const data = await this._conn.readReg(_REG_ID, 1);
|
|
237
237
|
return data[0];
|
|
238
238
|
}
|
|
239
239
|
|