@@ -31,7 +31,7 @@ const LIS3DH_CTRL_REG3 = 0x22; // Int1 interrupt type enable/disable
31
31
const LIS3DH_CTRL_REG4 = 0x23 ; // BDU, endian data sel, range, high res mode, self test, SPI 3 or 4 wire
32
32
const LIS3DH_CTRL_REG5 = 0x24 ; // boot, FIFO enable, latch int1 & int2, 4D enable int1 & int2 with 6D bit set
33
33
const LIS3DH_CTRL_REG6 = 0x25 ; // int2 interrupt settings, set polarity of int1 and int2 pins
34
- const LIS3DH_OUT_X_L_INCR = 0xA8 ; //
34
+ const LIS3DH_OUT_X_L_INCR = 0xA8 ;
35
35
const LIS3DH_OUT_X_L = 0x28 ;
36
36
const LIS3DH_OUT_X_H = 0x29 ;
37
37
const LIS3DH_OUT_Y_L = 0x2A ;
@@ -52,7 +52,6 @@ const LIS3DH_TIME_LATENCY = 0x3C;
52
52
const LIS3DH_TIME_WINDOW = 0x3D ;
53
53
const LIS3DH_WHO_AM_I = 0x0F ;
54
54
55
-
56
55
// Bitfield values
57
56
const LIS3DH_X_LOW = 0x01 ;
58
57
const LIS3DH_X_HIGH = 0x02 ;
@@ -98,19 +97,21 @@ const LIS3DH_ADC2 = 0x02;
98
97
const LIS3DH_ADC3 = 0x03 ;
99
98
100
99
class LIS3DH {
101
- static VERSION = " 2.0.3" ;
100
+
101
+ static VERSION = " 3.0.0" ;
102
102
103
103
// I2C information
104
- _i2c = null ;
104
+ _i2c = null ;
105
105
_addr = null ;
106
- _mode = LIS3DH_MODE_NORMAL;
107
106
108
107
// The full-scale range (+/- _range G)
109
108
_range = null ;
109
+ _mode = null ;
110
110
111
111
constructor (i2c, addr = 0x30 ) {
112
- _i2c = i2c;
112
+ _i2c = i2c;
113
113
_addr = addr;
114
+ _mode = LIS3DH_MODE_NORMAL;
114
115
115
116
// Read the range + set _range property
116
117
getRange ();
@@ -166,6 +167,18 @@ class LIS3DH {
166
167
return (val * 0.4 ) + 1.2 ;
167
168
}
168
169
170
+ // Set the state of the accelerometer axes
171
+ function enable (state = true ) {
172
+ // LIS3DH_CTRL_REG1 enables/disables accelerometer axes
173
+ // bit 0 = X axis
174
+ // bit 1 = Y axis
175
+ // bit 2 = Z axis
176
+ local val = _getReg (LIS3DH_CTRL_REG1);
177
+ if (state) { val = val | 0x07 ; }
178
+ else { val = val & 0xF8 ; }
179
+ _setReg (LIS3DH_CTRL_REG1, val);
180
+ }
181
+
169
182
// Read data from the Accelerometer
170
183
// Returns a table {x: <data>, y: <data>, z: <data>}
171
184
function getAccel (cb = null ) {
@@ -194,6 +207,26 @@ class LIS3DH {
194
207
imp. wakeup (0 , function () { cb (result); });
195
208
}
196
209
210
+ // get the currently-set full-scale range of the accelerometer
211
+ function getRange () {
212
+ local range_bits = (_getReg (LIS3DH_CTRL_REG4) & 0x30 ) >> 4 ;
213
+ if (range_bits == 0x00 ) {
214
+ _range = 2 ;
215
+ } else if (range_bits == 0x01 ) {
216
+ _range = 4 ;
217
+ } else if (range_bits == 0x02 ) {
218
+ _range = 8 ;
219
+ } else {
220
+ _range = 16 ;
221
+ }
222
+ return _range;
223
+ }
224
+
225
+ // Returns the deviceID (should be 51)
226
+ function getDeviceId () {
227
+ return _getReg (LIS3DH_WHO_AM_I);
228
+ }
229
+
197
230
// Set Accelerometer Data Rate in Hz
198
231
function setDataRate (rate) {
199
232
local val = _getReg (LIS3DH_CTRL_REG1) & 0x0F ;
@@ -258,21 +291,6 @@ class LIS3DH {
258
291
return _range;
259
292
}
260
293
261
- // get the currently-set full-scale range of the accelerometer
262
- function getRange () {
263
- local range_bits = (_getReg (LIS3DH_CTRL_REG4) & 0x30 ) >> 4 ;
264
- if (range_bits == 0x00 ) {
265
- _range = 2 ;
266
- } else if (range_bits == 0x01 ) {
267
- _range = 4 ;
268
- } else if (range_bits == 0x02 ) {
269
- _range = 8 ;
270
- } else {
271
- _range = 16 ;
272
- }
273
- return _range;
274
- }
275
-
276
294
// Set the mode of the accelerometer by passing a constant (LIS3DH_MODE_NORMAL,
277
295
// LIS3DH_MODE_LOW_POWER, LIS3DH_MODE_HIGH_RESOLUTION)
278
296
function setMode (mode) {
@@ -281,11 +299,6 @@ class LIS3DH {
281
299
_mode = mode;
282
300
}
283
301
284
- // Returns the deviceID (should be 51)
285
- function getDeviceId () {
286
- return _getReg (LIS3DH_WHO_AM_I);
287
- }
288
-
289
302
function configureHighPassFilter (filters, cutoff = LIS3DH_HPF_CUTOFF1, mode = LIS3DH_HPF_DEFAULT_MODE) {
290
303
// clear and set filters
291
304
filters = LIS3DH_HPF_DISABLED | filters;
@@ -294,29 +307,52 @@ class LIS3DH {
294
307
_setReg (LIS3DH_CTRL_REG2, filters | cutoff | mode);
295
308
}
296
309
297
- // -------------------- INTERRUPTS --------------------//
298
-
299
- // Enable/disable and configure FIFO buffer watermark interrupts
300
- function configureFifoInterrupt (enable, fifomode = 0x80 , watermark = 28 ) {
310
+ // Enable/disable and configure FIFO buffer
311
+ function configureFifo (enBuffer, fifomode = LIS3DH_FIFO_STREAM_MODE) {
301
312
302
313
// Enable/disable the FIFO buffer
303
- _setRegBit (LIS3DH_CTRL_REG5, 6 , enable ? 1 : 0 );
314
+ _setRegBit (LIS3DH_CTRL_REG5, 6 , enBuffer ? 1 : 0 );
315
+
316
+ // NOTE: FIFO Trigger selection (LIS3DH_FIFO_CTRL_REG bit 5) defaults to int1
317
+ // this library currently doesn't change this bit, so trigger selection is
318
+ // alwasys set to trigger on int1
304
319
305
- if (enable) {
306
- // Stream-to-FIFO mode, watermark of [28].
307
- _setReg (LIS3DH_FIFO_CTRL_REG, (fifomode & 0xc0 ) | (watermark & 0x1F ));
320
+ local val = _getReg (LIS3DH_FIFO_CTRL_REG) & 0x3F ;
321
+
322
+ if (enBuffer) {
323
+ _setReg (LIS3DH_FIFO_CTRL_REG, (val | fifomode));
308
324
} else {
309
- _setReg (LIS3DH_FIFO_CTRL_REG, 0x00 );
325
+ // Set mode to bypass
326
+ _setReg (LIS3DH_FIFO_CTRL_REG, val);
310
327
}
328
+ }
311
329
312
- // Enable/disable watermark interrupt
313
- _setRegBit (LIS3DH_CTRL_REG3, 2 , enable ? 1 : 0 );
330
+ // -------------------- INTERRUPTS --------------------//
331
+
332
+ // Enable/Disable FIFO watermark and/or FIFO overrun interrupts on Int1 pin
333
+ function configureFifoInterrupts (enWatermark, enOverrun = false , watermark = 29 ) {
334
+ // Adjust if optional parameters are not expected types
335
+ if (typeof enOverrun == " integer" ) {
336
+ watermark = enOverrun;
337
+ enOverrun = false ;
338
+ }
314
339
340
+ local fcVal = _getReg (LIS3DH_FIFO_CTRL_REG) & 0xE0 ;
341
+ // NOTE: Watermark range in register is 0-31, so adjust given watermark value
342
+ // down by one
343
+ fcVal = fcVal | (-- watermark & 0x1F );
344
+ // Set watermark
345
+ _setReg (LIS3DH_FIFO_CTRL_REG, fcVal);
346
+
347
+ local r3val = _getReg (LIS3DH_CTRL_REG3) & 0xF9 ;
348
+ if (enWatermark) r3val = r3val | 0x04 ;
349
+ if (enOverrun) r3val = r3val | 0x02 ;
350
+ _setReg (LIS3DH_CTRL_REG3, r3val);
315
351
}
316
352
317
353
// Enable/disable and configure inertial interrupts
318
- function configureInertialInterrupt (enable, threshold = 2.0 , duration = 5 ,
319
- options = LIS3DH_X_HIGH | LIS3DH_Y_HIGH | LIS3DH_Z_HIGH) {
354
+ function configureInertialInterrupt (enable, threshold = 2.0 , duration = 5 ,
355
+ options = LIS3DH_X_HIGH | LIS3DH_Y_HIGH | LIS3DH_Z_HIGH) {
320
356
321
357
// Set the enable flag
322
358
_setRegBit (LIS3DH_CTRL_REG3, 6 , enable ? 1 : 0 );
0 commit comments