Skip to content

Commit 8ff5a30

Browse files
author
betzrhodes
authored
Merge pull request #24 from electricimp/develop
v3.0.0
2 parents 1decb59 + 9880a75 commit 8ff5a30

7 files changed

+671
-346
lines changed

Diff for: LIS3DH.device.lib.nut

+75-39
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const LIS3DH_CTRL_REG3 = 0x22; // Int1 interrupt type enable/disable
3131
const LIS3DH_CTRL_REG4 = 0x23; // BDU, endian data sel, range, high res mode, self test, SPI 3 or 4 wire
3232
const LIS3DH_CTRL_REG5 = 0x24; // boot, FIFO enable, latch int1 & int2, 4D enable int1 & int2 with 6D bit set
3333
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;
3535
const LIS3DH_OUT_X_L = 0x28;
3636
const LIS3DH_OUT_X_H = 0x29;
3737
const LIS3DH_OUT_Y_L = 0x2A;
@@ -52,7 +52,6 @@ const LIS3DH_TIME_LATENCY = 0x3C;
5252
const LIS3DH_TIME_WINDOW = 0x3D;
5353
const LIS3DH_WHO_AM_I = 0x0F;
5454

55-
5655
// Bitfield values
5756
const LIS3DH_X_LOW = 0x01;
5857
const LIS3DH_X_HIGH = 0x02;
@@ -98,19 +97,21 @@ const LIS3DH_ADC2 = 0x02;
9897
const LIS3DH_ADC3 = 0x03;
9998

10099
class LIS3DH {
101-
static VERSION = "2.0.3";
100+
101+
static VERSION = "3.0.0";
102102

103103
// I2C information
104-
_i2c = null;
104+
_i2c = null;
105105
_addr = null;
106-
_mode = LIS3DH_MODE_NORMAL;
107106

108107
// The full-scale range (+/- _range G)
109108
_range = null;
109+
_mode = null;
110110

111111
constructor(i2c, addr = 0x30) {
112-
_i2c = i2c;
112+
_i2c = i2c;
113113
_addr = addr;
114+
_mode = LIS3DH_MODE_NORMAL;
114115

115116
// Read the range + set _range property
116117
getRange();
@@ -166,6 +167,18 @@ class LIS3DH {
166167
return (val * 0.4) + 1.2;
167168
}
168169

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+
169182
// Read data from the Accelerometer
170183
// Returns a table {x: <data>, y: <data>, z: <data>}
171184
function getAccel(cb = null) {
@@ -194,6 +207,26 @@ class LIS3DH {
194207
imp.wakeup(0, function() { cb(result); });
195208
}
196209

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+
197230
// Set Accelerometer Data Rate in Hz
198231
function setDataRate(rate) {
199232
local val = _getReg(LIS3DH_CTRL_REG1) & 0x0F;
@@ -258,21 +291,6 @@ class LIS3DH {
258291
return _range;
259292
}
260293

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-
276294
// Set the mode of the accelerometer by passing a constant (LIS3DH_MODE_NORMAL,
277295
// LIS3DH_MODE_LOW_POWER, LIS3DH_MODE_HIGH_RESOLUTION)
278296
function setMode(mode) {
@@ -281,11 +299,6 @@ class LIS3DH {
281299
_mode = mode;
282300
}
283301

284-
// Returns the deviceID (should be 51)
285-
function getDeviceId() {
286-
return _getReg(LIS3DH_WHO_AM_I);
287-
}
288-
289302
function configureHighPassFilter(filters, cutoff = LIS3DH_HPF_CUTOFF1, mode = LIS3DH_HPF_DEFAULT_MODE) {
290303
// clear and set filters
291304
filters = LIS3DH_HPF_DISABLED | filters;
@@ -294,29 +307,52 @@ class LIS3DH {
294307
_setReg(LIS3DH_CTRL_REG2, filters | cutoff | mode);
295308
}
296309

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) {
301312

302313
// 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
304319

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));
308324
} else {
309-
_setReg(LIS3DH_FIFO_CTRL_REG, 0x00);
325+
// Set mode to bypass
326+
_setReg(LIS3DH_FIFO_CTRL_REG, val);
310327
}
328+
}
311329

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+
}
314339

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);
315351
}
316352

317353
// 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) {
320356

321357
// Set the enable flag
322358
_setRegBit(LIS3DH_CTRL_REG3, 6, enable ? 1 : 0);

0 commit comments

Comments
 (0)