periph 1.4.0 → 1.5.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 +27 -0
- package/examples/adc_dac/ad5251/complete.js +66 -0
- package/examples/adc_dac/ad5251/demo.js +69 -0
- package/examples/adc_dac/ad5251/minimal.js +23 -0
- package/examples/adc_dac/ad5252/complete.js +66 -0
- package/examples/adc_dac/ad5252/demo.js +69 -0
- package/examples/adc_dac/ad5252/minimal.js +23 -0
- package/examples/adc_dac/ads7828/complete.js +46 -0
- package/examples/adc_dac/ads7828/demo.js +45 -0
- package/examples/adc_dac/ads7828/minimal.js +17 -0
- package/examples/adc_dac/cat5171/complete.js +52 -0
- package/examples/adc_dac/cat5171/demo.js +59 -0
- package/examples/adc_dac/cat5171/minimal.js +23 -0
- package/examples/adc_dac/cs43l22/complete.js +88 -0
- package/examples/adc_dac/cs43l22/demo.js +56 -0
- package/examples/adc_dac/cs43l22/minimal.js +20 -0
- package/examples/adc_dac/ds1881/complete.js +62 -0
- package/examples/adc_dac/ds1881/demo.js +61 -0
- package/examples/adc_dac/ds1881/minimal.js +25 -0
- package/examples/adc_dac/ds3502/complete.js +46 -0
- package/examples/adc_dac/ds3502/demo.js +54 -0
- package/examples/adc_dac/ds3502/minimal.js +28 -0
- package/examples/adc_dac/mcp3422/complete.js +48 -0
- package/examples/adc_dac/mcp3422/demo.js +49 -0
- package/examples/adc_dac/mcp3422/minimal.js +25 -0
- package/examples/display/sh1106/complete.js +101 -0
- package/examples/display/sh1106/demo.js +68 -0
- package/examples/display/sh1106/minimal.js +25 -0
- package/examples/display/ssd1305/complete.js +129 -0
- package/examples/display/ssd1305/demo.js +71 -0
- package/examples/display/ssd1305/minimal.js +25 -0
- package/examples/display/ssd1306/complete.js +115 -0
- package/examples/display/ssd1306/demo.js +70 -0
- package/examples/display/ssd1306/minimal.js +25 -0
- package/examples/environmental/am2315/complete.js +34 -0
- package/examples/environmental/am2315/demo.js +46 -0
- package/examples/environmental/am2315/minimal.js +21 -0
- package/examples/humidity/htu21df/complete.js +49 -0
- package/examples/humidity/htu21df/demo.js +50 -0
- package/examples/humidity/htu21df/minimal.js +22 -0
- package/examples/humidity/sht31/complete.js +53 -0
- package/examples/humidity/sht31/demo.js +53 -0
- package/examples/humidity/sht31/minimal.js +21 -0
- package/examples/humidity/si7021/complete.js +55 -0
- package/examples/humidity/si7021/demo.js +58 -0
- package/examples/humidity/si7021/minimal.js +21 -0
- package/examples/imu/bno055/complete.js +85 -0
- package/examples/imu/bno055/demo.js +70 -0
- package/examples/imu/bno055/minimal.js +20 -0
- package/examples/io_expander/pca6408a/complete.js +63 -0
- package/examples/io_expander/pca6408a/demo.js +58 -0
- package/examples/io_expander/pca6408a/minimal.js +20 -0
- package/examples/io_expander/pca6416a/complete.js +68 -0
- package/examples/io_expander/pca6416a/demo.js +60 -0
- package/examples/io_expander/pca6416a/minimal.js +20 -0
- package/examples/io_expander/pca9536/complete.js +63 -0
- package/examples/io_expander/pca9536/demo.js +57 -0
- package/examples/io_expander/pca9536/minimal.js +20 -0
- package/examples/io_expander/pca9539/complete.js +68 -0
- package/examples/io_expander/pca9539/demo.js +60 -0
- package/examples/io_expander/pca9539/minimal.js +20 -0
- package/examples/light/max44009/complete.js +56 -0
- package/examples/light/max44009/demo.js +49 -0
- package/examples/light/max44009/minimal.js +22 -0
- package/examples/memory/at24c02n/complete.js +39 -0
- package/examples/memory/at24c02n/demo.js +39 -0
- package/examples/memory/at24c02n/minimal.js +15 -0
- package/examples/memory/at24c64/complete.js +39 -0
- package/examples/memory/at24c64/demo.js +39 -0
- package/examples/memory/at24c64/minimal.js +15 -0
- package/examples/memory/cat24c512/complete.js +39 -0
- package/examples/memory/cat24c512/demo.js +39 -0
- package/examples/memory/cat24c512/minimal.js +15 -0
- package/examples/other/pca9541/complete.js +52 -0
- package/examples/other/pca9541/demo.js +61 -0
- package/examples/other/pca9541/minimal.js +23 -0
- package/examples/temperature/tmp102/complete.js +74 -0
- package/examples/temperature/tmp102/demo.js +71 -0
- package/examples/temperature/tmp102/minimal.js +20 -0
- package/examples/tof/vl6180x/complete.js +62 -0
- package/examples/tof/vl6180x/demo.js +77 -0
- package/examples/tof/vl6180x/minimal.js +23 -0
- package/package.json +1 -1
- package/src/chips/adc_dac/ad5251.js +345 -0
- package/src/chips/adc_dac/ad5252.js +345 -0
- package/src/chips/adc_dac/ads7828.js +225 -0
- package/src/chips/adc_dac/cat5171.js +213 -0
- package/src/chips/adc_dac/cs43l22.js +645 -0
- package/src/chips/adc_dac/ds1881.js +357 -0
- package/src/chips/adc_dac/ds3502.js +241 -0
- package/src/chips/adc_dac/mcp3422.js +312 -0
- package/src/chips/display/mono_oled.js +182 -0
- package/src/chips/display/sh1106.js +496 -0
- package/src/chips/display/ssd1305.js +655 -0
- package/src/chips/display/ssd1306.js +458 -0
- package/src/chips/environmental/am2315.js +237 -0
- package/src/chips/humidity/htu21df.js +222 -0
- package/src/chips/humidity/sht31.js +284 -0
- package/src/chips/humidity/si7021.js +282 -0
- package/src/chips/imu/bno055.js +496 -0
- package/src/chips/io_expander/pca6408a.js +407 -0
- package/src/chips/io_expander/pca6416a.js +455 -0
- package/src/chips/io_expander/pca9536.js +398 -0
- package/src/chips/io_expander/pca9539.js +457 -0
- package/src/chips/light/max44009.js +336 -0
- package/src/chips/memory/at24c02n.js +220 -0
- package/src/chips/memory/at24c64.js +220 -0
- package/src/chips/memory/cat24c512.js +221 -0
- package/src/chips/other/pca9541.js +315 -0
- package/src/chips/temperature/tmp102.js +361 -0
- package/src/chips/tof/vl6180x.js +580 -0
- package/src/discovery/registry.js +65 -13
|
@@ -0,0 +1,496 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_CHIP_ID = 0x00;
|
|
4
|
+
const _REG_PAGE_ID = 0x07;
|
|
5
|
+
const _REG_ACC_DATA = 0x08;
|
|
6
|
+
const _REG_MAG_DATA = 0x0E;
|
|
7
|
+
const _REG_GYR_DATA = 0x14;
|
|
8
|
+
const _REG_EUL_DATA = 0x1A;
|
|
9
|
+
const _REG_QUA_DATA = 0x20;
|
|
10
|
+
const _REG_LIA_DATA = 0x28;
|
|
11
|
+
const _REG_GRV_DATA = 0x2E;
|
|
12
|
+
const _REG_TEMP = 0x34;
|
|
13
|
+
const _REG_CALIB_STAT = 0x35;
|
|
14
|
+
const _REG_ST_RESULT = 0x36;
|
|
15
|
+
const _REG_INT_STA = 0x37;
|
|
16
|
+
const _REG_SYS_STATUS = 0x39;
|
|
17
|
+
const _REG_UNIT_SEL = 0x3B;
|
|
18
|
+
const _REG_OPR_MODE = 0x3D;
|
|
19
|
+
const _REG_PWR_MODE = 0x3E;
|
|
20
|
+
const _REG_SYS_TRIGGER = 0x3F;
|
|
21
|
+
const _REG_TEMP_SOURCE = 0x40;
|
|
22
|
+
const _REG_AXIS_MAP_CONFIG = 0x41;
|
|
23
|
+
const _REG_AXIS_MAP_SIGN = 0x42;
|
|
24
|
+
const _REG_ACC_OFFSET = 0x55;
|
|
25
|
+
const _REG_INT_MSK = 0x0F; // page 1
|
|
26
|
+
const _REG_INT_EN = 0x10; // page 1
|
|
27
|
+
const _REG_UNIQUE_ID = 0x50; // page 1
|
|
28
|
+
|
|
29
|
+
const _CHIP_ID_VALUE = 0xA0;
|
|
30
|
+
const _UNIT_SEL_SI = 0x86;
|
|
31
|
+
const _MODE_CONFIG = 0x00;
|
|
32
|
+
const _MODE_NDOF = 0x0C;
|
|
33
|
+
|
|
34
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Interrupt source bit masks used by `BNO055Full` (`pollInterrupt()` return value
|
|
38
|
+
* and the argument of `enableInterrupt()` / `disableInterrupt()`).
|
|
39
|
+
*/
|
|
40
|
+
const BNO055Source = {
|
|
41
|
+
ACC_BSX_DRDY: 0x01,
|
|
42
|
+
MAG_DRDY: 0x02,
|
|
43
|
+
GYRO_AM: 0x04,
|
|
44
|
+
GYR_HIGH_RATE: 0x08,
|
|
45
|
+
GYR_DRDY: 0x10,
|
|
46
|
+
ACC_HIGH_G: 0x20,
|
|
47
|
+
ACC_AM: 0x40,
|
|
48
|
+
ACC_NM: 0x80,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** Operation mode values for `BNO055Full.setMode()`. */
|
|
52
|
+
const BNO055Mode = {
|
|
53
|
+
CONFIG: 0x0, ACCONLY: 0x1, MAGONLY: 0x2, GYROONLY: 0x3, ACCMAG: 0x4, ACCGYRO: 0x5,
|
|
54
|
+
MAGGYRO: 0x6, AMG: 0x7, IMU: 0x8, COMPASS: 0x9, M4G: 0xA, NDOF_FMC_OFF: 0xB, NDOF: 0xC,
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const _s16 = (buf, offset) => buf.readInt16LE(offset);
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* BNO055 minimal interface — fused Euler angles.
|
|
61
|
+
*
|
|
62
|
+
* Waits for the chip to boot (up to 650 ms), checks `CHIP_ID` (0xA0), selects normal power
|
|
63
|
+
* mode and `UNIT_SEL` = 0x86 (Android orientation, radians, rad/s, m/s², °C), then enters
|
|
64
|
+
* NDOF fusion mode (100 Hz absolute orientation). I²C address 0x29 (COM3 high, datasheet
|
|
65
|
+
* default) or 0x28 (COM3 low); the chip stretches the I²C clock.
|
|
66
|
+
*
|
|
67
|
+
* The constructor starts the initialisation asynchronously; every public method waits for
|
|
68
|
+
* it. Call `await imu.init()` right after construction to surface a failure at a
|
|
69
|
+
* predictable point.
|
|
70
|
+
*/
|
|
71
|
+
class BNO055Minimal {
|
|
72
|
+
/**
|
|
73
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (0x28 or 0x29).
|
|
74
|
+
*/
|
|
75
|
+
constructor(connection) {
|
|
76
|
+
this._conn = connection;
|
|
77
|
+
this._mode = _MODE_NDOF;
|
|
78
|
+
this._ready = this._initialize();
|
|
79
|
+
// Re-raised by every public method; mark handled so it never surfaces on its own.
|
|
80
|
+
this._ready.catch(() => {});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Wait for the initialisation sequence started by the constructor.
|
|
85
|
+
* @returns {Promise<void>}
|
|
86
|
+
* @throws {Error} If `CHIP_ID` does not read 0xA0 within 650 ms.
|
|
87
|
+
*/
|
|
88
|
+
async init() {
|
|
89
|
+
await this._ready;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async _initialize() {
|
|
93
|
+
const deadline = Date.now() + 650;
|
|
94
|
+
let id = -1;
|
|
95
|
+
for (;;) {
|
|
96
|
+
try {
|
|
97
|
+
id = (await this._conn.readReg(_REG_CHIP_ID, 1))[0];
|
|
98
|
+
if (id === _CHIP_ID_VALUE) break;
|
|
99
|
+
} catch (e) {
|
|
100
|
+
// the chip does not ACK while booting
|
|
101
|
+
}
|
|
102
|
+
if (Date.now() >= deadline) {
|
|
103
|
+
throw new Error('BNO055 CHIP_ID: expected 0xA0, got 0x' + (id & 0xFF).toString(16).toUpperCase());
|
|
104
|
+
}
|
|
105
|
+
await _sleep(10);
|
|
106
|
+
}
|
|
107
|
+
await this._conn.writeReg(_REG_PWR_MODE, 0x00);
|
|
108
|
+
await this._conn.writeReg(_REG_PAGE_ID, 0x00);
|
|
109
|
+
await this._conn.writeReg(_REG_UNIT_SEL, _UNIT_SEL_SI);
|
|
110
|
+
await this._conn.writeReg(_REG_OPR_MODE, _MODE_NDOF);
|
|
111
|
+
this._mode = _MODE_NDOF;
|
|
112
|
+
await _sleep(7);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async _vector(reg, scale) {
|
|
116
|
+
await this._ready;
|
|
117
|
+
const raw = await this._conn.readReg(reg, 6);
|
|
118
|
+
return [_s16(raw, 0) / scale, _s16(raw, 2) / scale, _s16(raw, 4) / scale];
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Read the fused Euler angles (one burst of 0x1A–0x1F, raw / 900).
|
|
123
|
+
* @returns {Promise<[number, number, number]>} `[heading, roll, pitch]` in rad.
|
|
124
|
+
*/
|
|
125
|
+
async euler() {
|
|
126
|
+
return this._vector(_REG_EUL_DATA, 900);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* BNO055 full interface — all outputs, calibration, modes and interrupts.
|
|
132
|
+
*/
|
|
133
|
+
class BNO055Full extends BNO055Minimal {
|
|
134
|
+
/**
|
|
135
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (0x28 or 0x29).
|
|
136
|
+
*/
|
|
137
|
+
constructor(connection) {
|
|
138
|
+
super(connection);
|
|
139
|
+
this._clkSel = 0x00;
|
|
140
|
+
this._intCallback = null;
|
|
141
|
+
this._intHandler = null;
|
|
142
|
+
this._intPinUsed = null;
|
|
143
|
+
this._pollTimer = null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async _r1(reg) {
|
|
147
|
+
return (await this._conn.readReg(reg, 1))[0];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async _enterConfig() {
|
|
151
|
+
const prev = this._mode;
|
|
152
|
+
await this._conn.writeReg(_REG_OPR_MODE, _MODE_CONFIG);
|
|
153
|
+
await _sleep(19);
|
|
154
|
+
return prev;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async _leaveConfig(mode) {
|
|
158
|
+
if (mode !== _MODE_CONFIG) {
|
|
159
|
+
await this._conn.writeReg(_REG_OPR_MODE, mode);
|
|
160
|
+
await _sleep(7);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Select the operation mode (through CONFIGMODE, 19 ms + 7 ms waits).
|
|
166
|
+
* @param {number} mode - 0 (CONFIGMODE) … 12 (NDOF); see `BNO055Mode`.
|
|
167
|
+
* @returns {Promise<void>}
|
|
168
|
+
*/
|
|
169
|
+
async setMode(mode) {
|
|
170
|
+
await this._ready;
|
|
171
|
+
if (!(mode >= 0 && mode <= 12)) throw new RangeError('mode must be 0..12');
|
|
172
|
+
await this._conn.writeReg(_REG_OPR_MODE, _MODE_CONFIG);
|
|
173
|
+
await _sleep(19);
|
|
174
|
+
if (mode !== _MODE_CONFIG) {
|
|
175
|
+
await this._conn.writeReg(_REG_OPR_MODE, mode);
|
|
176
|
+
await _sleep(7);
|
|
177
|
+
}
|
|
178
|
+
this._mode = mode;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Read `OPR_MODE`.
|
|
183
|
+
* @returns {Promise<number>} Current operation mode, 0–12.
|
|
184
|
+
*/
|
|
185
|
+
async getMode() {
|
|
186
|
+
await this._ready;
|
|
187
|
+
return (await this._r1(_REG_OPR_MODE)) & 0x0F;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Read the fused orientation quaternion (raw / 16384).
|
|
192
|
+
* @returns {Promise<[number, number, number, number]>} `[w, x, y, z]`.
|
|
193
|
+
*/
|
|
194
|
+
async quaternion() {
|
|
195
|
+
await this._ready;
|
|
196
|
+
const raw = await this._conn.readReg(_REG_QUA_DATA, 8);
|
|
197
|
+
return [_s16(raw, 0) / 16384, _s16(raw, 2) / 16384, _s16(raw, 4) / 16384, _s16(raw, 6) / 16384];
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Read the gravity-free linear acceleration (raw / 100).
|
|
202
|
+
* @returns {Promise<[number, number, number]>} `[x, y, z]` in m/s².
|
|
203
|
+
*/
|
|
204
|
+
async linearAcceleration() { return this._vector(_REG_LIA_DATA, 100); }
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Read the gravity vector (raw / 100).
|
|
208
|
+
* @returns {Promise<[number, number, number]>} `[x, y, z]` in m/s².
|
|
209
|
+
*/
|
|
210
|
+
async gravity() { return this._vector(_REG_GRV_DATA, 100); }
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Read the raw accelerometer (raw / 100).
|
|
214
|
+
* @returns {Promise<[number, number, number]>} `[x, y, z]` in m/s².
|
|
215
|
+
*/
|
|
216
|
+
async acceleration() { return this._vector(_REG_ACC_DATA, 100); }
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Read the raw magnetometer (raw / 16).
|
|
220
|
+
* @returns {Promise<[number, number, number]>} `[x, y, z]` in µT.
|
|
221
|
+
*/
|
|
222
|
+
async magneticField() { return this._vector(_REG_MAG_DATA, 16); }
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Read the raw gyroscope (raw / 900).
|
|
226
|
+
* @returns {Promise<[number, number, number]>} `[x, y, z]` in rad/s.
|
|
227
|
+
*/
|
|
228
|
+
async angularRate() { return this._vector(_REG_GYR_DATA, 900); }
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Read the temperature (signed 8 bit, 1 LSB = 1 °C).
|
|
232
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
233
|
+
*/
|
|
234
|
+
async temperature() {
|
|
235
|
+
await this._ready;
|
|
236
|
+
const v = await this._r1(_REG_TEMP);
|
|
237
|
+
return v & 0x80 ? v - 256 : v;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Read `CALIB_STAT`.
|
|
242
|
+
* @returns {Promise<[number, number, number, number]>} `[sys, gyr, acc, mag]`, each 0 (uncalibrated) … 3 (calibrated).
|
|
243
|
+
*/
|
|
244
|
+
async calibrationStatus() {
|
|
245
|
+
await this._ready;
|
|
246
|
+
const v = await this._r1(_REG_CALIB_STAT);
|
|
247
|
+
return [(v >> 6) & 3, (v >> 4) & 3, (v >> 2) & 3, v & 3];
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Check whether sys, gyr, acc and mag are all fully calibrated.
|
|
252
|
+
* @returns {Promise<boolean>} True if every status value is 3.
|
|
253
|
+
*/
|
|
254
|
+
async isCalibrated() {
|
|
255
|
+
await this._ready;
|
|
256
|
+
return (await this._r1(_REG_CALIB_STAT)) === 0xFF;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Read the calibration offsets and radii in CONFIGMODE (previous mode restored).
|
|
261
|
+
* @returns {Promise<Buffer>} 22 bytes from `ACC_OFFSET_X_LSB` (0x55) to `MAG_RADIUS_MSB` (0x6A).
|
|
262
|
+
*/
|
|
263
|
+
async getCalibration() {
|
|
264
|
+
await this._ready;
|
|
265
|
+
const prev = await this._enterConfig();
|
|
266
|
+
const data = Buffer.from(await this._conn.readReg(_REG_ACC_OFFSET, 22));
|
|
267
|
+
await this._leaveConfig(prev);
|
|
268
|
+
return data;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Write the calibration offsets and radii in CONFIGMODE (previous mode restored).
|
|
273
|
+
* @param {Buffer|number[]} data - 22 bytes as returned by `getCalibration()`.
|
|
274
|
+
* @returns {Promise<void>}
|
|
275
|
+
*/
|
|
276
|
+
async setCalibration(data) {
|
|
277
|
+
await this._ready;
|
|
278
|
+
if (data.length !== 22) throw new RangeError('calibration data must be 22 bytes');
|
|
279
|
+
const prev = await this._enterConfig();
|
|
280
|
+
for (let i = 0; i < 22; i++) await this._conn.writeReg(_REG_ACC_OFFSET + i, data[i]);
|
|
281
|
+
await this._leaveConfig(prev);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Remap the sensor axes (CONFIGMODE, previous mode restored).
|
|
286
|
+
* @param {number} config - `AXIS_MAP_CONFIG` (bits 1:0 X, 3:2 Y, 5:4 Z source axis; default 0x24).
|
|
287
|
+
* @param {number} sign - `AXIS_MAP_SIGN` (bit 2 X, bit 1 Y, bit 0 Z inverted; default 0x00).
|
|
288
|
+
* @returns {Promise<void>}
|
|
289
|
+
*/
|
|
290
|
+
async setAxisRemap(config, sign) {
|
|
291
|
+
await this._ready;
|
|
292
|
+
const prev = await this._enterConfig();
|
|
293
|
+
await this._conn.writeReg(_REG_AXIS_MAP_CONFIG, config);
|
|
294
|
+
await this._conn.writeReg(_REG_AXIS_MAP_SIGN, sign);
|
|
295
|
+
await this._leaveConfig(prev);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Select the power mode (CONFIGMODE, previous mode restored).
|
|
300
|
+
* @param {'normal'|'low_power'|'suspend'} mode
|
|
301
|
+
* @returns {Promise<void>}
|
|
302
|
+
*/
|
|
303
|
+
async setPowerMode(mode) {
|
|
304
|
+
await this._ready;
|
|
305
|
+
const codes = { normal: 0, low_power: 1, suspend: 2 };
|
|
306
|
+
if (!(mode in codes)) throw new RangeError("mode must be 'normal', 'low_power' or 'suspend'");
|
|
307
|
+
const prev = await this._enterConfig();
|
|
308
|
+
await this._conn.writeReg(_REG_PWR_MODE, codes[mode]);
|
|
309
|
+
await this._leaveConfig(prev);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Select the temperature sensor (CONFIGMODE, previous mode restored).
|
|
314
|
+
* @param {'accelerometer'|'gyroscope'} source
|
|
315
|
+
* @returns {Promise<void>}
|
|
316
|
+
*/
|
|
317
|
+
async setTemperatureSource(source) {
|
|
318
|
+
await this._ready;
|
|
319
|
+
const codes = { accelerometer: 0, gyroscope: 1 };
|
|
320
|
+
if (!(source in codes)) throw new RangeError("source must be 'accelerometer' or 'gyroscope'");
|
|
321
|
+
const prev = await this._enterConfig();
|
|
322
|
+
await this._conn.writeReg(_REG_TEMP_SOURCE, codes[source]);
|
|
323
|
+
await this._leaveConfig(prev);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Select the external 32.768 kHz crystal (only if fitted) or the internal oscillator (CONFIGMODE).
|
|
328
|
+
* @param {boolean} on
|
|
329
|
+
* @returns {Promise<void>}
|
|
330
|
+
*/
|
|
331
|
+
async useExternalCrystal(on) {
|
|
332
|
+
await this._ready;
|
|
333
|
+
const prev = await this._enterConfig();
|
|
334
|
+
this._clkSel = on ? 0x80 : 0x00;
|
|
335
|
+
await this._conn.writeReg(_REG_SYS_TRIGGER, this._clkSel);
|
|
336
|
+
await this._leaveConfig(prev);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Run the power-on self-test (CONFIGMODE, previous mode restored).
|
|
341
|
+
* @returns {Promise<{acc: boolean, mag: boolean, gyr: boolean, mcu: boolean}>} true = passed.
|
|
342
|
+
*/
|
|
343
|
+
async selfTest() {
|
|
344
|
+
await this._ready;
|
|
345
|
+
const prev = await this._enterConfig();
|
|
346
|
+
await this._conn.writeReg(_REG_SYS_TRIGGER, this._clkSel | 0x01);
|
|
347
|
+
await _sleep(500);
|
|
348
|
+
const v = await this._r1(_REG_ST_RESULT);
|
|
349
|
+
await this._leaveConfig(prev);
|
|
350
|
+
return { acc: !!(v & 1), mag: !!(v & 2), gyr: !!(v & 4), mcu: !!(v & 8) };
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
/**
|
|
354
|
+
* Read `SYS_STATUS` and `SYS_ERR`.
|
|
355
|
+
* @returns {Promise<[number, number]>} `[status, error]` — status 0–6 (5 = fusion running), error 0–10.
|
|
356
|
+
*/
|
|
357
|
+
async systemStatus() {
|
|
358
|
+
await this._ready;
|
|
359
|
+
const raw = await this._conn.readReg(_REG_SYS_STATUS, 2);
|
|
360
|
+
return [raw[0], raw[1]];
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Reset the chip (`RST_SYS`), wait 650 ms and re-initialise into NDOF.
|
|
365
|
+
* @returns {Promise<void>}
|
|
366
|
+
*/
|
|
367
|
+
async reset() {
|
|
368
|
+
await this._ready;
|
|
369
|
+
await this._conn.writeReg(_REG_SYS_TRIGGER, this._clkSel | 0x20);
|
|
370
|
+
await _sleep(650);
|
|
371
|
+
this._clkSel = 0x00;
|
|
372
|
+
await this._initialize();
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Read the identification and revision registers 0x00–0x06.
|
|
377
|
+
* @returns {Promise<{chipId: number, accId: number, magId: number, gyrId: number, swRevision: number, bootloader: number}>}
|
|
378
|
+
*/
|
|
379
|
+
async revisions() {
|
|
380
|
+
await this._ready;
|
|
381
|
+
const raw = await this._conn.readReg(_REG_CHIP_ID, 7);
|
|
382
|
+
return { chipId: raw[0], accId: raw[1], magId: raw[2], gyrId: raw[3],
|
|
383
|
+
swRevision: raw[4] | (raw[5] << 8), bootloader: raw[6] };
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Read the 16-byte unique ID (page 1, page 0 restored).
|
|
388
|
+
* @returns {Promise<Buffer>} 16 bytes from `UNIQUE_ID` (0x50).
|
|
389
|
+
*/
|
|
390
|
+
async uniqueId() {
|
|
391
|
+
await this._ready;
|
|
392
|
+
await this._conn.writeReg(_REG_PAGE_ID, 0x01);
|
|
393
|
+
try {
|
|
394
|
+
return Buffer.from(await this._conn.readReg(_REG_UNIQUE_ID, 16));
|
|
395
|
+
} finally {
|
|
396
|
+
await this._conn.writeReg(_REG_PAGE_ID, 0x00);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
async _updateIntReg(source, enable) {
|
|
401
|
+
await this._ready;
|
|
402
|
+
await this._conn.writeReg(_REG_PAGE_ID, 0x01);
|
|
403
|
+
try {
|
|
404
|
+
for (const reg of [_REG_INT_EN, _REG_INT_MSK]) {
|
|
405
|
+
let v = await this._r1(reg);
|
|
406
|
+
v = enable ? (v | source) : (v & ~source & 0xFF);
|
|
407
|
+
await this._conn.writeReg(reg, v);
|
|
408
|
+
}
|
|
409
|
+
} finally {
|
|
410
|
+
await this._conn.writeReg(_REG_PAGE_ID, 0x00);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Enable interrupt source bits in `INT_EN` and `INT_MSK` (page 1, page 0 restored).
|
|
416
|
+
* @param {number} source - One or more `BNO055Source` bits.
|
|
417
|
+
* @returns {Promise<void>}
|
|
418
|
+
*/
|
|
419
|
+
async enableInterrupt(source) { await this._updateIntReg(source, true); }
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* Disable interrupt source bits in `INT_EN` and `INT_MSK`.
|
|
423
|
+
* @param {number} source - One or more `BNO055Source` bits.
|
|
424
|
+
* @returns {Promise<void>}
|
|
425
|
+
*/
|
|
426
|
+
async disableInterrupt(source) { await this._updateIntReg(source, false); }
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Read `INT_STA` (cleared on read).
|
|
430
|
+
* @returns {Promise<number>} Status bits using the `BNO055Source` layout.
|
|
431
|
+
*/
|
|
432
|
+
async pollInterrupt() {
|
|
433
|
+
await this._ready;
|
|
434
|
+
return this._r1(_REG_INT_STA);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Subscribe to INT assertions (rising edge). Uses `intPin` or the connection's `intPin`;
|
|
439
|
+
* without either, a 100 ms polling timer calls `pollInterrupt()`.
|
|
440
|
+
* @param {function(number): void} callback - Called with the `INT_STA` bits when non-zero.
|
|
441
|
+
* @param {object} [intPin] - Optional input pin overriding `connection.intPin`.
|
|
442
|
+
* @returns {Promise<void>}
|
|
443
|
+
*/
|
|
444
|
+
async onInterrupt(callback, intPin) {
|
|
445
|
+
await this._ready;
|
|
446
|
+
this._intCallback = callback;
|
|
447
|
+
const pin = intPin || (this._conn ? this._conn.intPin : null);
|
|
448
|
+
if (pin) {
|
|
449
|
+
this._intPinUsed = pin;
|
|
450
|
+
this._intHandler = () => { this._dispatchInt(); };
|
|
451
|
+
await pin.onEdge(this._intHandler, 'rising');
|
|
452
|
+
} else {
|
|
453
|
+
this._pollTimer = setInterval(() => { this._dispatchInt(); }, 100);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/**
|
|
458
|
+
* Unsubscribe from INT assertions.
|
|
459
|
+
* @returns {Promise<void>}
|
|
460
|
+
*/
|
|
461
|
+
async offInterrupt() {
|
|
462
|
+
this._intCallback = null;
|
|
463
|
+
if (this._intPinUsed && this._intHandler) await this._intPinUsed.offEdge(this._intHandler);
|
|
464
|
+
this._intPinUsed = null;
|
|
465
|
+
this._intHandler = null;
|
|
466
|
+
if (this._pollTimer !== null) {
|
|
467
|
+
clearInterval(this._pollTimer);
|
|
468
|
+
this._pollTimer = null;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
async _dispatchInt() {
|
|
473
|
+
const cb = this._intCallback;
|
|
474
|
+
if (!cb) return;
|
|
475
|
+
try {
|
|
476
|
+
const status = await this.pollInterrupt();
|
|
477
|
+
if (status) cb(status);
|
|
478
|
+
} catch (err) {
|
|
479
|
+
// ignore: the next edge or poll retries
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
Object.assign(BNO055Full, {
|
|
485
|
+
SOURCE_ACC_BSX_DRDY: BNO055Source.ACC_BSX_DRDY,
|
|
486
|
+
SOURCE_MAG_DRDY: BNO055Source.MAG_DRDY,
|
|
487
|
+
SOURCE_GYRO_AM: BNO055Source.GYRO_AM,
|
|
488
|
+
SOURCE_GYR_HIGH_RATE: BNO055Source.GYR_HIGH_RATE,
|
|
489
|
+
SOURCE_GYR_DRDY: BNO055Source.GYR_DRDY,
|
|
490
|
+
SOURCE_ACC_HIGH_G: BNO055Source.ACC_HIGH_G,
|
|
491
|
+
SOURCE_ACC_AM: BNO055Source.ACC_AM,
|
|
492
|
+
SOURCE_ACC_NM: BNO055Source.ACC_NM,
|
|
493
|
+
MODE: BNO055Mode,
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
module.exports = { BNO055Minimal, BNO055Full, BNO055Source, BNO055Mode };
|