periph 1.3.2 → 1.4.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.
Files changed (114) hide show
  1. package/README.md +27 -0
  2. package/examples/accelerometer/bma180/complete.js +83 -0
  3. package/examples/accelerometer/bma180/demo.js +47 -0
  4. package/examples/accelerometer/bma180/minimal.js +21 -0
  5. package/examples/accelerometer/fxos8700/complete.js +111 -0
  6. package/examples/accelerometer/fxos8700/demo.js +80 -0
  7. package/examples/accelerometer/fxos8700/minimal.js +20 -0
  8. package/examples/accelerometer/lis3dh/complete.js +123 -0
  9. package/examples/accelerometer/lis3dh/demo.js +99 -0
  10. package/examples/accelerometer/lis3dh/minimal.js +19 -0
  11. package/examples/accelerometer/lsm303/complete.js +82 -0
  12. package/examples/accelerometer/lsm303/demo.js +96 -0
  13. package/examples/accelerometer/lsm303/minimal.js +23 -0
  14. package/examples/accelerometer/mma8451q/complete.js +96 -0
  15. package/examples/accelerometer/mma8451q/demo.js +120 -0
  16. package/examples/accelerometer/mma8451q/minimal.js +20 -0
  17. package/examples/adc_dac/ad5243/complete.js +52 -0
  18. package/examples/adc_dac/ad5243/demo.js +50 -0
  19. package/examples/adc_dac/ad5243/minimal.js +23 -0
  20. package/examples/adc_dac/ad5248/complete.js +49 -0
  21. package/examples/adc_dac/ad5248/demo.js +42 -0
  22. package/examples/adc_dac/ad5248/minimal.js +21 -0
  23. package/examples/adc_dac/ads1013/complete.js +51 -0
  24. package/examples/adc_dac/ads1013/demo.js +51 -0
  25. package/examples/adc_dac/ads1013/minimal.js +21 -0
  26. package/examples/adc_dac/ads1014/complete.js +88 -0
  27. package/examples/adc_dac/ads1014/demo.js +52 -0
  28. package/examples/adc_dac/ads1014/minimal.js +21 -0
  29. package/examples/adc_dac/ads1113/complete.js +47 -0
  30. package/examples/adc_dac/ads1113/demo.js +39 -0
  31. package/examples/adc_dac/ads1113/minimal.js +20 -0
  32. package/examples/adc_dac/ads1114/complete.js +77 -0
  33. package/examples/adc_dac/ads1114/demo.js +47 -0
  34. package/examples/adc_dac/ads1114/minimal.js +20 -0
  35. package/examples/adc_dac/ads1115/complete.js +82 -0
  36. package/examples/adc_dac/ads1115/demo.js +55 -0
  37. package/examples/adc_dac/ads1115/minimal.js +20 -0
  38. package/examples/adc_dac/es8311/complete.js +80 -0
  39. package/examples/adc_dac/es8311/demo.js +59 -0
  40. package/examples/adc_dac/es8311/minimal.js +18 -0
  41. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  42. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  43. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  44. package/examples/display/pcf8578/complete.js +58 -0
  45. package/examples/display/pcf8578/demo.js +62 -0
  46. package/examples/display/pcf8578/minimal.js +19 -0
  47. package/examples/display/pcf8579/complete.js +58 -0
  48. package/examples/display/pcf8579/demo.js +61 -0
  49. package/examples/display/pcf8579/minimal.js +19 -0
  50. package/examples/imu/icm20948/complete.js +114 -0
  51. package/examples/imu/icm20948/demo.js +79 -0
  52. package/examples/imu/icm20948/minimal.js +22 -0
  53. package/examples/imu/qmi8658a/complete.js +134 -0
  54. package/examples/imu/qmi8658a/demo.js +81 -0
  55. package/examples/imu/qmi8658a/minimal.js +22 -0
  56. package/examples/io_expander/mcp23008/complete.js +70 -0
  57. package/examples/io_expander/mcp23008/demo.js +58 -0
  58. package/examples/io_expander/mcp23008/minimal.js +20 -0
  59. package/examples/memory/mb85rc/complete.js +40 -0
  60. package/examples/memory/mb85rc/demo.js +58 -0
  61. package/examples/memory/mb85rc/minimal.js +15 -0
  62. package/examples/motor/drv2605/complete.js +100 -0
  63. package/examples/motor/drv2605/demo.js +55 -0
  64. package/examples/motor/drv2605/minimal.js +24 -0
  65. package/examples/motor/tmc2130/complete.js +138 -0
  66. package/examples/motor/tmc2130/demo.js +96 -0
  67. package/examples/motor/tmc2130/minimal.js +30 -0
  68. package/examples/motor/tmc2208/complete.js +92 -0
  69. package/examples/motor/tmc2208/demo.js +85 -0
  70. package/examples/motor/tmc2208/minimal.js +25 -0
  71. package/examples/motor/tmc2209/complete.js +106 -0
  72. package/examples/motor/tmc2209/demo.js +97 -0
  73. package/examples/motor/tmc2209/minimal.js +25 -0
  74. package/examples/motor/tmc5160/complete.js +205 -0
  75. package/examples/motor/tmc5160/demo.js +104 -0
  76. package/examples/motor/tmc5160/minimal.js +35 -0
  77. package/examples/power/axp2101/complete.js +135 -0
  78. package/examples/power/axp2101/demo.js +68 -0
  79. package/examples/power/axp2101/minimal.js +24 -0
  80. package/examples/rtc/pcf85063a/complete.js +68 -0
  81. package/examples/rtc/pcf85063a/demo.js +76 -0
  82. package/examples/rtc/pcf85063a/minimal.js +20 -0
  83. package/package.json +1 -1
  84. package/src/chips/accelerometer/bma180.js +573 -0
  85. package/src/chips/accelerometer/fxos8700.js +1600 -0
  86. package/src/chips/accelerometer/lis3dh.js +1341 -0
  87. package/src/chips/accelerometer/lsm303.js +1471 -0
  88. package/src/chips/accelerometer/mma8451q.js +1406 -0
  89. package/src/chips/adc_dac/_ads111x_base.js +272 -0
  90. package/src/chips/adc_dac/ad5243.js +231 -0
  91. package/src/chips/adc_dac/ad5248.js +194 -0
  92. package/src/chips/adc_dac/ads1013.js +219 -0
  93. package/src/chips/adc_dac/ads1014.js +509 -0
  94. package/src/chips/adc_dac/ads1113.js +112 -0
  95. package/src/chips/adc_dac/ads1114.js +201 -0
  96. package/src/chips/adc_dac/ads1115.js +232 -0
  97. package/src/chips/adc_dac/es8311.js +621 -0
  98. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  99. package/src/chips/display/pcf8578.js +452 -0
  100. package/src/chips/display/pcf8579.js +451 -0
  101. package/src/chips/imu/icm20948.js +849 -0
  102. package/src/chips/imu/qmi8658a.js +1011 -0
  103. package/src/chips/io_expander/mcp23008.js +558 -0
  104. package/src/chips/memory/mb85rc.js +166 -0
  105. package/src/chips/motor/drv2605.js +592 -0
  106. package/src/chips/motor/tmc2130.js +537 -0
  107. package/src/chips/motor/tmc2208.js +490 -0
  108. package/src/chips/motor/tmc2209.js +565 -0
  109. package/src/chips/motor/tmc5160.js +900 -0
  110. package/src/chips/power/axp2101.js +968 -0
  111. package/src/chips/rtc/pcf85063a.js +482 -0
  112. package/src/connection/uart.js +9 -0
  113. package/src/connection/uart_mock.js +65 -0
  114. package/src/discovery/registry.js +45 -19
@@ -0,0 +1,573 @@
1
+ 'use strict';
2
+
3
+ const _REG_CHIP_ID = 0x00;
4
+ const _REG_VERSION = 0x01;
5
+ const _REG_ACC_X_LSB = 0x02;
6
+ const _REG_ACC_X_MSB = 0x03;
7
+ const _REG_ACC_Y_LSB = 0x04;
8
+ const _REG_ACC_Y_MSB = 0x05;
9
+ const _REG_ACC_Z_LSB = 0x06;
10
+ const _REG_ACC_Z_MSB = 0x07;
11
+ const _REG_TEMP = 0x08;
12
+ const _REG_STATUS_REG1 = 0x09;
13
+ const _REG_STATUS_REG2 = 0x0A;
14
+ const _REG_STATUS_REG3 = 0x0B;
15
+ const _REG_STATUS_REG4 = 0x0C;
16
+ const _REG_CTRL_REG0 = 0x0D;
17
+ const _REG_CTRL_REG1 = 0x0E;
18
+ const _REG_CTRL_REG3 = 0x21;
19
+ const _REG_CTRL_REG4 = 0x22;
20
+ const _REG_HY = 0x23;
21
+ const _REG_SLOPE_TAPSENS = 0x24;
22
+ const _REG_HIGH_LOW_INFO = 0x25;
23
+ const _REG_LOW_DUR = 0x26;
24
+ const _REG_HIGH_DUR = 0x27;
25
+ const _REG_TAPSENS_TH = 0x28;
26
+ const _REG_LOW_TH = 0x29;
27
+ const _REG_HIGH_TH = 0x2A;
28
+ const _REG_SLOPE_TH = 0x2B;
29
+ const _REG_CD1 = 0x2C;
30
+ const _REG_CD2 = 0x2D;
31
+ const _REG_TCO_X = 0x2E;
32
+ const _REG_TCO_Y = 0x2F;
33
+ const _REG_TCO_Z = 0x30;
34
+ const _REG_GAIN_T = 0x31;
35
+ const _REG_GAIN_Y = 0x33;
36
+ const _REG_GAIN_Z = 0x34;
37
+ const _REG_OFFSET_LSB1 = 0x35;
38
+ const _REG_OFFSET_T = 0x37;
39
+ const _REG_RESET = 0x10;
40
+ const _REG_BW_TCS = 0x20;
41
+
42
+ const _CHIP_ID_VALUE = 0x03;
43
+ const _CHIP_ID_MASK = 0x07;
44
+
45
+ const _RANGE_BITS = { 1: 0x00, 1.5: 0x02, 2: 0x04, 3: 0x06, 4: 0x08, 8: 0x0A, 16: 0x0C };
46
+ const _BW_CODES = [10, 20, 40, 75, 150, 300, 600, 1200].map((hz, i) => [hz, i << 4]);
47
+ const _RANGE_SCALE = { 1: 8192, 1.5: 5460, 2: 4096, 3: 2730, 4: 2048, 8: 1024, 16: 512 };
48
+ const _TAP_DUR_CODES = { 50: 0x00, 75: 0x01, 100: 0x02, 150: 0x03, 250: 0x04, 500: 0x05, 750: 0x06, 1000: 0x07 };
49
+ const _WAKEUP_DUR_CODES = { 20: 0x00, 80: 0x01, 320: 0x02, 2560: 0x03 };
50
+ const _SLOPE_DUR_CODES = { 1: 0x00, 3: 0x01, 5: 0x02, 7: 0x03 };
51
+ const _DUR_LSB_MS = 2.085;
52
+
53
+ function _nearestBandwidth(bwHz) {
54
+ let best = _BW_CODES[0];
55
+ let bestDiff = Math.abs(_BW_CODES[0][0] - bwHz);
56
+ for (const entry of _BW_CODES) {
57
+ const diff = Math.abs(entry[0] - bwHz);
58
+ if (diff < bestDiff) {
59
+ best = entry;
60
+ bestDiff = diff;
61
+ }
62
+ }
63
+ return best[1];
64
+ }
65
+
66
+ function _nearestTapDur(windowMs) {
67
+ let bestMs = 50;
68
+ let bestCode = _TAP_DUR_CODES[50];
69
+ let bestAbsDiff = Infinity;
70
+ for (const [ms, code] of Object.entries(_TAP_DUR_CODES).map(([k, v]) => [Number(k), v])) {
71
+ const diff = Math.abs(ms - windowMs);
72
+ if (diff < bestAbsDiff) {
73
+ bestMs = ms;
74
+ bestCode = code;
75
+ bestAbsDiff = diff;
76
+ }
77
+ }
78
+ return bestCode;
79
+ }
80
+
81
+ function _delayMs(ms) {
82
+ const end = Date.now() + ms;
83
+ while (Date.now() < end) { /* spin */ }
84
+ }
85
+
86
+ /**
87
+ * BMA180 3-axis MEMS accelerometer (Bosch Sensortec) — minimal interface.
88
+ *
89
+ * Triaxial low-g accelerometer with 14-bit digital output and seven selectable
90
+ * full-scale ranges (±1 to ±16 *g*). Communicates over I²C at address
91
+ * 0x40 (SDO = GND) or 0x41 (SDO = VDDIO). The chip also supports 4-wire
92
+ * SPI; out of scope here.
93
+ *
94
+ * Default configuration (written at construction):
95
+ * - Range ±2 *g* (4096 LSB/g)
96
+ * - Bandwidth 150 Hz low-pass
97
+ * - mode_config = 00 (low-noise, factory-calibrated)
98
+ * - 14-bit readout, shadow_dis = 0
99
+ * - All interrupt enables left untouched
100
+ * - Calibration bits preserved everywhere
101
+ */
102
+ class BMA180Minimal {
103
+ /**
104
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - Configured I²C connection.
105
+ */
106
+ constructor(connection) {
107
+ this._conn = connection;
108
+ this._rangeG = 2;
109
+ this._init();
110
+ }
111
+ async _init() {
112
+ // First transaction must be something other than an acc LSB read
113
+ // (the chip returns MSB=0 for a first LSB read after power-up).
114
+ const chipId = await this._readReg(_REG_CHIP_ID);
115
+ if ((chipId & _CHIP_ID_MASK) !== _CHIP_ID_VALUE) {
116
+ throw new Error('BMA180 CHIP_ID: expected 0x' + _CHIP_ID_VALUE.toString(16) +
117
+ ', got 0x' + (chipId & _CHIP_ID_MASK).toString(16));
118
+ }
119
+ // Unlock image registers (0x20-0x3B) by setting ee_w = 1.
120
+ let ctrl0 = await this._readReg(_REG_CTRL_REG0);
121
+ ctrl0 |= 0x10;
122
+ await this._writeReg(_REG_CTRL_REG0, ctrl0 & 0xFF);
123
+ // Range = ±2 g (OFFSET_LSB1 bits 3:1 = 010), preserving cal/smp_skip.
124
+ let olsb1 = await this._readReg(_REG_OFFSET_LSB1);
125
+ olsb1 = (olsb1 & 0xF1) | _RANGE_BITS[2];
126
+ await this._writeReg(_REG_OFFSET_LSB1, olsb1 & 0xFF);
127
+ // bw = 150 Hz (BW_TCS bits 7:4 = 0100), preserving tcs.
128
+ const bwCode = _nearestBandwidth(150);
129
+ let bw = await this._readReg(_REG_BW_TCS);
130
+ bw = (bw & 0x0F) | bwCode;
131
+ await this._writeReg(_REG_BW_TCS, bw & 0xFF);
132
+ _delayMs(4);
133
+ }
134
+ async _writeReg(reg, value) {
135
+ await this._conn.writeReg(reg, value & 0xFF);
136
+ }
137
+ async _readReg(reg) {
138
+ return (await this._conn.readReg(reg, 1))[0];
139
+ }
140
+ /**
141
+ * Read 3-axis linear acceleration.
142
+ *
143
+ * Reads all six LSB-then-MSB data bytes (0x02..0x07) in one burst so the
144
+ * X, Y, Z samples are guaranteed to come from a single measurement with
145
+ * `shadow_dis=0` enforcing correct ordering.
146
+ *
147
+ * @returns {Promise<number[]>} [x, y, z] acceleration in *g*.
148
+ */
149
+ async read() {
150
+ const raw = await this._conn.readReg(_REG_ACC_X_LSB, 6);
151
+ // 14-bit two's complement; bit 1 of each LSB is 0, bit 0 is new_data.
152
+ const rx = (((raw[1] << 6) | (raw[0] >> 2)) & 0x3FFF);
153
+ const ry = (((raw[3] << 6) | (raw[2] >> 2)) & 0x3FFF);
154
+ const rz = (((raw[5] << 6) | (raw[4] >> 2)) & 0x3FFF);
155
+ const sx = (rx + 8192) % 16384 - 8192;
156
+ const sy = (ry + 8192) % 16384 - 8192;
157
+ const sz = (rz + 8192) % 16384 - 8192;
158
+ const scale = _RANGE_SCALE[this._rangeG];
159
+ return [sx / scale, sy / scale, sz / scale];
160
+ }
161
+ }
162
+
163
+ /**
164
+ * BMA180 full interface — extends BMA180Minimal with configuration,
165
+ * range/bandwidth/mode/filter selection, temperature, new-data, shadow,
166
+ * sample-skip, low-g/high-g/slope/alert/tap interrupts with per-axis
167
+ * enable and filter selection, latched or self-resetting interrupts,
168
+ * self-wake-up, sleep, soft reset, electrostatic self-test, offset
169
+ * regulation, version register, and the two CUSTOMER scratch bytes.
170
+ */
171
+ class BMA180Full extends BMA180Minimal {
172
+ /**
173
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - Configured I²C connection.
174
+ */
175
+ constructor(connection) {
176
+ super(connection);
177
+ this._enabledSources = 0;
178
+ this._sleeping = false;
179
+ }
180
+ async read() {
181
+ return super.read();
182
+ }
183
+ /** @param {number} rangeG - One of 1, 1.5, 2, 3, 4, 8, 16. */
184
+ async setRange(rangeG) {
185
+ if (!(rangeG in _RANGE_BITS)) {
186
+ throw new Error('rangeG must be one of 1, 1.5, 2, 3, 4, 8, 16');
187
+ }
188
+ this._rangeG = rangeG;
189
+ let olsb1 = await this._readReg(_REG_OFFSET_LSB1);
190
+ olsb1 = (olsb1 & 0xF1) | _RANGE_BITS[rangeG];
191
+ await this._writeReg(_REG_OFFSET_LSB1, olsb1 & 0xFF);
192
+ }
193
+ /** @param {number} bandwidthHz - 10..1200 Hz. */
194
+ async setBandwidth(bandwidthHz) {
195
+ const bwCode = _nearestBandwidth(bandwidthHz);
196
+ let bw = await this._readReg(_REG_BW_TCS);
197
+ bw = (bw & 0x0F) | bwCode;
198
+ await this._writeReg(_REG_BW_TCS, bw & 0xFF);
199
+ _delayMs(4);
200
+ }
201
+ /** @param {number} mode - 0=low-pass (use setBandwidth), 1=high-pass 1Hz, 2=band-pass. */
202
+ async setFilterMode(mode) {
203
+ if (mode === 0) return;
204
+ let code;
205
+ if (mode === 1) code = 0x80;
206
+ else if (mode === 2) code = 0x90;
207
+ else throw new Error('mode must be 0, 1, or 2');
208
+ let bw = await this._readReg(_REG_BW_TCS);
209
+ bw = (bw & 0x0F) | code;
210
+ await this._writeReg(_REG_BW_TCS, bw & 0xFF);
211
+ _delayMs(4);
212
+ }
213
+ /** @param {number} mode - 0..3 (mode_config). */
214
+ async setMode(mode) {
215
+ if (mode < 0 || mode > 3) throw new Error('mode must be 0..3');
216
+ let tcoz = await this._readReg(_REG_TCO_Z);
217
+ tcoz = (tcoz & 0xFC) | (mode & 0x03);
218
+ await this._writeReg(_REG_TCO_Z, tcoz & 0xFF);
219
+ }
220
+ /** @param {number} bits - 12 or 14. */
221
+ async setResolution(bits) {
222
+ if (bits !== 12 && bits !== 14) throw new Error('bits must be 12 or 14');
223
+ let ot = await this._readReg(_REG_OFFSET_T);
224
+ if (bits === 12) ot |= 0x01;
225
+ else ot &= ~0x01 & 0xFF;
226
+ await this._writeReg(_REG_OFFSET_T, ot & 0xFF);
227
+ }
228
+ /** @returns {Promise<number[]>} Signed 14-bit counts. */
229
+ async readRaw() {
230
+ const raw = await this._conn.readReg(_REG_ACC_X_LSB, 6);
231
+ const rx = (((raw[1] << 6) | (raw[0] >> 2)) & 0x3FFF);
232
+ const ry = (((raw[3] << 6) | (raw[2] >> 2)) & 0x3FFF);
233
+ const rz = (((raw[5] << 6) | (raw[4] >> 2)) & 0x3FFF);
234
+ return [(rx + 8192) % 16384 - 8192, (ry + 8192) % 16384 - 8192, (rz + 8192) % 16384 - 8192];
235
+ }
236
+ /** @returns {Promise<number>} Temperature in °C. */
237
+ async readTemperature() {
238
+ const raw = await this._readReg(_REG_TEMP);
239
+ const s = raw < 128 ? raw : raw - 256;
240
+ return 25.0 + ((s - 2) * 0.5);
241
+ }
242
+ /** @returns {Promise<boolean>} True if all three new_data_X/Y/Z bits are set. */
243
+ async newDataAvailable() {
244
+ const x = await this._readReg(_REG_ACC_X_LSB);
245
+ const y = await this._readReg(_REG_ACC_Y_LSB);
246
+ const z = await this._readReg(_REG_ACC_Z_LSB);
247
+ return (x & 0x01) && (y & 0x01) && (z & 0x01);
248
+ }
249
+ /** @param {boolean} enabled - True to allow MSB-only reads. */
250
+ async setShadow(enabled) {
251
+ let gy = await this._readReg(_REG_GAIN_Y);
252
+ if (enabled) gy &= ~0x01 & 0xFF;
253
+ else gy |= 0x01;
254
+ await this._writeReg(_REG_GAIN_Y, gy & 0xFF);
255
+ }
256
+ /** @param {boolean} enabled - True to enable smp_skip. */
257
+ async setSampleSkip(enabled) {
258
+ let olsb1 = await this._readReg(_REG_OFFSET_LSB1);
259
+ if (enabled) olsb1 |= 0x01;
260
+ else olsb1 &= ~0x01 & 0xFF;
261
+ await this._writeReg(_REG_OFFSET_LSB1, olsb1 & 0xFF);
262
+ }
263
+ async setLowG(thresholdG, durationMs, hysteresisG = 0, axes = 0x07, counter = 0, filtered = true) {
264
+ await this._writeThreshold(_REG_LOW_TH, thresholdG);
265
+ await this._writeLowDur(durationMs);
266
+ await this._writeLowHysteresis(hysteresisG);
267
+ await this._writeAxisEnablesLow(axes);
268
+ await this._writeFiltBit(_REG_HIGH_LOW_INFO, 0x01, filtered);
269
+ await this._writeDebounce('lg', counter);
270
+ await this._enableSource(BMA180Full.SOURCE_LOW_G);
271
+ }
272
+ async setHighG(thresholdG, durationMs, hysteresisG = 0, axes = 0x07, counter = 0, filtered = true) {
273
+ await this._writeThreshold(_REG_HIGH_TH, thresholdG);
274
+ await this._writeHighDur(durationMs);
275
+ await this._writeHighHysteresis(hysteresisG);
276
+ await this._writeAxisEnablesHigh(axes);
277
+ await this._writeFiltBit(_REG_HIGH_LOW_INFO, 0x10, filtered);
278
+ await this._writeDebounce('hg', counter);
279
+ await this._enableSource(BMA180Full.SOURCE_HIGH_G);
280
+ }
281
+ async setSlope(thresholdG, samples = 1, axes = 0x07, filtered = true) {
282
+ await this._writeSlopeThreshold(_REG_SLOPE_TH, thresholdG);
283
+ await this._writeSlopeDur(samples);
284
+ await this._writeAxisEnablesSlope(axes);
285
+ await this._writeFiltBit(_REG_SLOPE_TAPSENS, 0x10, filtered);
286
+ await this._writeCr3Bit(0x40, true); // slope_int
287
+ await this._writeCr3Bit(0x80, false); // slope_alert
288
+ await this._writeCr3Bit(0x04, true); // adv_int
289
+ await this._enableSource(BMA180Full.SOURCE_SLOPE);
290
+ }
291
+ async setAlert(enabled) {
292
+ if (enabled) {
293
+ this._enabledSources &= ~BMA180Full.SOURCE_SLOPE;
294
+ await this._writeCr3Bit(0x40, false);
295
+ await this._writeCr3Bit(0x80, true);
296
+ await this._writeCr3Bit(0x04, true);
297
+ await this._enableSource(BMA180Full.SOURCE_ALERT);
298
+ } else {
299
+ await this._disableSource(BMA180Full.SOURCE_ALERT);
300
+ await this._writeCr3Bit(0x80, false);
301
+ }
302
+ }
303
+ async setTap(thresholdG, windowMs = 250, axes = 0x07, filtered = true) {
304
+ await this._writeSlopeThreshold(_REG_TAPSENS_TH, thresholdG);
305
+ await this._writeTapDur(windowMs);
306
+ await this._writeAxisEnablesTap(axes);
307
+ await this._writeFiltBit(_REG_SLOPE_TAPSENS, 0x01, filtered);
308
+ await this._enableSource(BMA180Full.SOURCE_TAP);
309
+ }
310
+ async setLatch(enabled) {
311
+ await this._writeCr3Bit(0x01, enabled);
312
+ }
313
+ async clearInterrupt() {
314
+ if (this._sleeping) return;
315
+ let ctrl0 = await this._readReg(_REG_CTRL_REG0);
316
+ await this._writeReg(_REG_CTRL_REG0, (ctrl0 | 0x40) & 0xFF);
317
+ }
318
+ async enableInterrupt(source) {
319
+ if (source === BMA180Full.SOURCE_NEW_DATA) {
320
+ await this._writeCr3Bit(0x02, true);
321
+ } else {
322
+ await this._writeCr3Bit(0x02, false);
323
+ if (source === BMA180Full.SOURCE_SLOPE) {
324
+ await this._writeCr3Bit(0x80, false);
325
+ await this._writeCr3Bit(0x40, true);
326
+ await this._writeCr3Bit(0x04, true);
327
+ await this._disableSource(BMA180Full.SOURCE_ALERT);
328
+ } else if (source === BMA180Full.SOURCE_ALERT) {
329
+ await this._writeCr3Bit(0x40, false);
330
+ await this._writeCr3Bit(0x80, true);
331
+ await this._writeCr3Bit(0x04, true);
332
+ await this._disableSource(BMA180Full.SOURCE_SLOPE);
333
+ } else if (source === BMA180Full.SOURCE_HIGH_G) {
334
+ await this._writeCr3Bit(0x20, true);
335
+ } else if (source === BMA180Full.SOURCE_LOW_G) {
336
+ await this._writeCr3Bit(0x10, true);
337
+ } else if (source === BMA180Full.SOURCE_TAP) {
338
+ await this._writeCr3Bit(0x08, true);
339
+ }
340
+ }
341
+ await this._enableSource(source);
342
+ }
343
+ async disableInterrupt(source) {
344
+ if (source === BMA180Full.SOURCE_NEW_DATA) {
345
+ await this._writeCr3Bit(0x02, false);
346
+ } else if (source === BMA180Full.SOURCE_SLOPE) {
347
+ await this._writeCr3Bit(0x40, false);
348
+ } else if (source === BMA180Full.SOURCE_ALERT) {
349
+ await this._writeCr3Bit(0x80, false);
350
+ await this._writeCr3Bit(0x04, false);
351
+ } else if (source === BMA180Full.SOURCE_HIGH_G) {
352
+ await this._writeCr3Bit(0x20, false);
353
+ } else if (source === BMA180Full.SOURCE_LOW_G) {
354
+ await this._writeCr3Bit(0x10, false);
355
+ } else if (source === BMA180Full.SOURCE_TAP) {
356
+ await this._writeCr3Bit(0x08, false);
357
+ }
358
+ await this._disableSource(source);
359
+ }
360
+ /** @returns {Promise<number>} STATUS_REG3 byte. */
361
+ async pollInterrupt() {
362
+ return this._readReg(_REG_STATUS_REG3);
363
+ }
364
+ async readStatus() {
365
+ return [
366
+ await this._readReg(_REG_STATUS_REG1),
367
+ await this._readReg(_REG_STATUS_REG2),
368
+ await this._readReg(_REG_STATUS_REG3),
369
+ await this._readReg(_REG_STATUS_REG4),
370
+ ];
371
+ }
372
+ async setWakeUp(enabled, pauseMs = 20) {
373
+ if (!(pauseMs in _WAKEUP_DUR_CODES)) {
374
+ throw new Error('pauseMs must be 20, 80, 320, or 2560');
375
+ }
376
+ let tcoy = await this._readReg(_REG_TCO_Y);
377
+ tcoy = (tcoy & 0xFC) | _WAKEUP_DUR_CODES[pauseMs];
378
+ await this._writeReg(_REG_TCO_Y, tcoy & 0xFF);
379
+ let gz = await this._readReg(_REG_GAIN_Z);
380
+ if (enabled) gz |= 0x01;
381
+ else gz &= ~0x01 & 0xFF;
382
+ await this._writeReg(_REG_GAIN_Z, gz & 0xFF);
383
+ }
384
+ async sleep() {
385
+ if (this._sleeping) return;
386
+ let ctrl0 = await this._readReg(_REG_CTRL_REG0);
387
+ await this._writeReg(_REG_CTRL_REG0, (ctrl0 | 0x02) & 0xFF);
388
+ this._sleeping = true;
389
+ }
390
+ async wake() {
391
+ if (!this._sleeping) return;
392
+ let ctrl0 = await this._readReg(_REG_CTRL_REG0);
393
+ await this._writeReg(_REG_CTRL_REG0, ctrl0 & ~0x02 & 0xFF);
394
+ _delayMs(2);
395
+ this._sleeping = false;
396
+ }
397
+ async softReset() {
398
+ await this._writeReg(_REG_RESET, 0xB6);
399
+ _delayMs(30);
400
+ this._rangeG = 2;
401
+ const chipId = await this._readReg(_REG_CHIP_ID);
402
+ if ((chipId & _CHIP_ID_MASK) !== _CHIP_ID_VALUE) {
403
+ throw new Error('BMA180 CHIP_ID after reset: expected 0x' + _CHIP_ID_VALUE.toString(16));
404
+ }
405
+ let ctrl0 = await this._readReg(_REG_CTRL_REG0);
406
+ ctrl0 |= 0x10;
407
+ await this._writeReg(_REG_CTRL_REG0, ctrl0 & 0xFF);
408
+ let olsb1 = await this._readReg(_REG_OFFSET_LSB1);
409
+ olsb1 = (olsb1 & 0xF1) | _RANGE_BITS[2];
410
+ await this._writeReg(_REG_OFFSET_LSB1, olsb1 & 0xFF);
411
+ const bwCode = _nearestBandwidth(150);
412
+ let bw = await this._readReg(_REG_BW_TCS);
413
+ bw = (bw & 0x0F) | bwCode;
414
+ await this._writeReg(_REG_BW_TCS, bw & 0xFF);
415
+ _delayMs(4);
416
+ this._sleeping = false;
417
+ }
418
+ /** @returns {Promise<boolean>} True if self-test passed. */
419
+ async selfTest() {
420
+ const ctrl0 = await this._readReg(_REG_CTRL_REG0);
421
+ await this._writeReg(_REG_CTRL_REG0, (ctrl0 | 0x04) & 0xFF);
422
+ _delayMs(10);
423
+ const [x, y, z] = await this.readRaw();
424
+ await this._writeReg(_REG_CTRL_REG0, ctrl0 & 0xFF);
425
+ const passed = Math.abs(x) > 200 && Math.abs(y) > 200 && Math.abs(z) > 200;
426
+ await this.softReset();
427
+ return passed;
428
+ }
429
+ async calibrateOffset(axes = 0x07, mode = 1) {
430
+ if (mode < 0 || mode > 3) throw new Error('mode must be 0..3');
431
+ let cr4 = await this._readReg(_REG_CTRL_REG4);
432
+ cr4 = (cr4 & 0xFC) | (mode & 0x03);
433
+ await this._writeReg(_REG_CTRL_REG4, cr4 & 0xFF);
434
+ const bits = [
435
+ { mask: 0x01, ctrl1Bit: 0x80 },
436
+ { mask: 0x02, ctrl1Bit: 0x40 },
437
+ { mask: 0x04, ctrl1Bit: 0x20 },
438
+ ];
439
+ for (const { mask, ctrl1Bit } of bits) {
440
+ if (!(axes & mask)) continue;
441
+ let ctrl1 = await this._readReg(_REG_CTRL_REG1);
442
+ ctrl1 |= ctrl1Bit;
443
+ await this._writeReg(_REG_CTRL_REG1, ctrl1 & 0xFF);
444
+ for (let t = 0; t < 100; t++) {
445
+ const s1 = await this._readReg(_REG_STATUS_REG1);
446
+ if (s1 & 0x02) break;
447
+ _delayMs(100);
448
+ }
449
+ ctrl1 = await this._readReg(_REG_CTRL_REG1);
450
+ ctrl1 &= ~ctrl1Bit & 0xFF;
451
+ await this._writeReg(_REG_CTRL_REG1, ctrl1 & 0xFF);
452
+ }
453
+ cr4 = await this._readReg(_REG_CTRL_REG4);
454
+ cr4 &= 0xFC;
455
+ await this._writeReg(_REG_CTRL_REG4, cr4 & 0xFF);
456
+ }
457
+ /** @returns {Promise<[number, number]>} (al_version, ml_version). */
458
+ async readVersion() {
459
+ const raw = await this._readReg(_REG_VERSION);
460
+ return [(raw >> 4) & 0x0F, raw & 0x0F];
461
+ }
462
+ async readCustomer(index) {
463
+ return this._readReg(index === 0 ? _REG_CD1 : _REG_CD2);
464
+ }
465
+ async writeCustomer(index, value) {
466
+ await this._writeReg(index === 0 ? _REG_CD1 : _REG_CD2, value & 0xFF);
467
+ }
468
+ async _writeThreshold(reg, thresholdG) {
469
+ let code = Math.round(thresholdG / this._rangeG * 255.0);
470
+ if (code < 0) code = 0;
471
+ if (code > 255) code = 255;
472
+ await this._writeReg(reg, code & 0xFF);
473
+ }
474
+ async _writeSlopeThreshold(reg, thresholdG) {
475
+ let code = Math.round(thresholdG / (0.0156 * this._rangeG / 2.0));
476
+ if (code < 0) code = 0;
477
+ if (code > 255) code = 255;
478
+ await this._writeReg(reg, code & 0xFF);
479
+ }
480
+ async _writeLowDur(durationMs) {
481
+ let code = Math.round(durationMs / _DUR_LSB_MS);
482
+ if (code < 0) code = 0;
483
+ if (code > 127) code = 127;
484
+ const ld = await this._readReg(_REG_LOW_DUR);
485
+ await this._writeReg(_REG_LOW_DUR, ((ld & 0x01) | ((code & 0x7F) << 1)) & 0xFF);
486
+ }
487
+ async _writeHighDur(durationMs) {
488
+ let code = Math.round(durationMs / _DUR_LSB_MS);
489
+ if (code < 0) code = 0;
490
+ if (code > 127) code = 127;
491
+ const hd = await this._readReg(_REG_HIGH_DUR);
492
+ await this._writeReg(_REG_HIGH_DUR, ((hd & 0x01) | ((code & 0x7F) << 1)) & 0xFF);
493
+ }
494
+ async _writeLowHysteresis(hysteresisG) {
495
+ let code = Math.round(hysteresisG / this._rangeG * 255.0 / 32.0);
496
+ if (code < 0) code = 0;
497
+ if (code > 31) code = 31;
498
+ const hy = await this._readReg(_REG_HY);
499
+ await this._writeReg(_REG_HY, (hy & 0xF8) | (code & 0x07));
500
+ const cr4 = await this._readReg(_REG_CTRL_REG4);
501
+ await this._writeReg(_REG_CTRL_REG4, (cr4 & ~(0x03 << 6)) | (((code >> 3) & 0x03) << 6));
502
+ }
503
+ async _writeHighHysteresis(hysteresisG) {
504
+ let code = Math.round(hysteresisG / this._rangeG * 255.0 / 32.0);
505
+ if (code < 0) code = 0;
506
+ if (code > 31) code = 31;
507
+ const hy = await this._readReg(_REG_HY);
508
+ await this._writeReg(_REG_HY, ((hy & 0x07) | ((code & 0x1F) << 3)) & 0xFF);
509
+ }
510
+ async _writeAxisEnablesLow(axes) {
511
+ const hli = await this._readReg(_REG_HIGH_LOW_INFO);
512
+ await this._writeReg(_REG_HIGH_LOW_INFO, ((hli & 0xF1) | ((axes & 0x07) << 1)) & 0xFF);
513
+ }
514
+ async _writeAxisEnablesHigh(axes) {
515
+ const hli = await this._readReg(_REG_HIGH_LOW_INFO);
516
+ await this._writeReg(_REG_HIGH_LOW_INFO, ((hli & 0x0F) | ((axes & 0x07) << 5)) & 0xFF);
517
+ }
518
+ async _writeAxisEnablesSlope(axes) {
519
+ const st = await this._readReg(_REG_SLOPE_TAPSENS);
520
+ await this._writeReg(_REG_SLOPE_TAPSENS, ((st & 0x0F) | ((axes & 0x07) << 5)) & 0xFF);
521
+ }
522
+ async _writeAxisEnablesTap(axes) {
523
+ const st = await this._readReg(_REG_SLOPE_TAPSENS);
524
+ await this._writeReg(_REG_SLOPE_TAPSENS, ((st & 0xF1) | ((axes & 0x07) << 1)) & 0xFF);
525
+ }
526
+ async _writeFiltBit(reg, bit, enabled) {
527
+ const v = await this._readReg(reg);
528
+ if (enabled) await this._writeReg(reg, (v | bit) & 0xFF);
529
+ else await this._writeReg(reg, (v & ~bit) & 0xFF);
530
+ }
531
+ async _writeDebounce(kind, counter) {
532
+ if (counter < 0 || counter > 3) throw new Error('counter must be 0..3');
533
+ const code = (counter & 0x03) << 2;
534
+ const cr4 = await this._readReg(_REG_CTRL_REG4);
535
+ if (kind === 'lg') {
536
+ await this._writeReg(_REG_CTRL_REG4, (cr4 & ~(0x03 << 2)) | (code & (0x03 << 2)));
537
+ } else {
538
+ await this._writeReg(_REG_CTRL_REG4, (cr4 & ~(0x03 << 4)) | ((code << 2) & (0x03 << 4)));
539
+ }
540
+ }
541
+ async _writeSlopeDur(samples) {
542
+ if (!(samples in _SLOPE_DUR_CODES)) throw new Error('samples must be 1, 3, 5, or 7');
543
+ const tcox = await this._readReg(_REG_TCO_X);
544
+ await this._writeReg(_REG_TCO_X, ((tcox & 0xFC) | _SLOPE_DUR_CODES[samples]) & 0xFF);
545
+ }
546
+ async _writeTapDur(windowMs) {
547
+ const code = _nearestTapDur(windowMs);
548
+ const gt = await this._readReg(_REG_GAIN_T);
549
+ await this._writeReg(_REG_GAIN_T, ((gt & 0xF8) | code) & 0xFF);
550
+ }
551
+ async _writeCr3Bit(bit, enabled) {
552
+ if (this._sleeping) return;
553
+ const cr3 = await this._readReg(_REG_CTRL_REG3);
554
+ if (enabled) await this._writeReg(_REG_CTRL_REG3, (cr3 | bit) & 0xFF);
555
+ else await this._writeReg(_REG_CTRL_REG3, (cr3 & ~bit) & 0xFF);
556
+ }
557
+ async _enableSource(source) {
558
+ if (this._sleeping) return;
559
+ this._enabledSources |= source;
560
+ }
561
+ async _disableSource(source) {
562
+ this._enabledSources &= ~source & 0xFF;
563
+ }
564
+ }
565
+
566
+ BMA180Full.SOURCE_LOW_G = 0x01;
567
+ BMA180Full.SOURCE_HIGH_G = 0x02;
568
+ BMA180Full.SOURCE_SLOPE = 0x04;
569
+ BMA180Full.SOURCE_ALERT = 0x08;
570
+ BMA180Full.SOURCE_TAP = 0x10;
571
+ BMA180Full.SOURCE_NEW_DATA = 0x20;
572
+
573
+ module.exports = { BMA180Minimal, BMA180Full };