Skip to content

Commit

Permalink
Merge pull request #19 from electricimp/develop
Browse files Browse the repository at this point in the history
v2.0.2
  • Loading branch information
Pavel Petroshenko authored Jul 6, 2018
2 parents 961fbf9 + 5637ce3 commit 1cd92b4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 52 deletions.
4 changes: 2 additions & 2 deletions .impt.test
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"allowDisconnect": false,
"builderCache": false,
"testFiles": [
"*.test.nut",
"tests/**/*.test.nut"
"LIS3DH.automated.device.test.nut",
"tests/**/LIS3DH.automated.device.test.nut"
],
"deviceFile": "LIS3DH.device.lib.nut"
}
34 changes: 18 additions & 16 deletions LIS3DH.device.lib.nut
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@


// Registers
const LIS3DH_TEMP_CFG_REG = 0x1F;
const LIS3DH_CTRL_REG1 = 0x20;
const LIS3DH_CTRL_REG2 = 0x21;
const LIS3DH_CTRL_REG3 = 0x22;
const LIS3DH_CTRL_REG4 = 0x23;
const LIS3DH_CTRL_REG5 = 0x24;
const LIS3DH_CTRL_REG6 = 0x25;
const LIS3DH_OUT_X_L_INCR = 0xA8;
const LIS3DH_TEMP_CFG_REG = 0x1F; // Enable temp/ADC
const LIS3DH_CTRL_REG1 = 0x20; // Data rate, normal/low power mode, enable xyz axis
const LIS3DH_CTRL_REG2 = 0x21; // HPF config
const LIS3DH_CTRL_REG3 = 0x22; // Int1 interrupt type enable/disable
const LIS3DH_CTRL_REG4 = 0x23; // BDU, endian data sel, range, high res mode, self test, SPI 3 or 4 wire
const LIS3DH_CTRL_REG5 = 0x24; // boot, FIFO enable, latch int1 & int2, 4D enable int1 & int2 with 6D bit set
const LIS3DH_CTRL_REG6 = 0x25; // int2 interrupt settings, set polarity of int1 and int2 pins
const LIS3DH_OUT_X_L_INCR = 0xA8; //
const LIS3DH_OUT_X_L = 0x28;
const LIS3DH_OUT_X_H = 0x29;
const LIS3DH_OUT_Y_L = 0x2A;
Expand Down Expand Up @@ -98,7 +98,7 @@ const LIS3DH_ADC2 = 0x02;
const LIS3DH_ADC3 = 0x03;

class LIS3DH {
static VERSION = "2.0.1";
static VERSION = "2.0.2";

// I2C information
_i2c = null;
Expand All @@ -123,7 +123,7 @@ class LIS3DH {
_setReg(LIS3DH_CTRL_REG1, 0x07);
_setReg(LIS3DH_CTRL_REG2, 0x00);
_setReg(LIS3DH_CTRL_REG3, 0x00);
_setReg(LIS3DH_CTRL_REG4, 0x00);
_setReg(LIS3DH_CTRL_REG4, 0x00); // Sets range to default
_setReg(LIS3DH_CTRL_REG5, 0x00);
_setReg(LIS3DH_CTRL_REG6, 0x00);
_setReg(LIS3DH_INT1_CFG, 0x00);
Expand All @@ -136,8 +136,9 @@ class LIS3DH {
_setReg(LIS3DH_TIME_LATENCY, 0x00);
_setReg(LIS3DH_TIME_WINDOW, 0x00);
_setReg(LIS3DH_FIFO_CTRL_REG, 0x00);
_setReg(LIS3DH_TEMP_CFG_REG, 0x00);

// Read the range + set _range property
// Reads the default range from register and + sets local _range property
getRange();
}

Expand Down Expand Up @@ -360,11 +361,12 @@ class LIS3DH {
_setReg(LIS3DH_CLICK_CFG, clickType);

// Set the LIS3DH_CLICK_THS register
local latchedBit = _getReg(LIS3DH_CLICK_THS) & 0x80; // Get LIR_Click bit
if (threshold < 0) { threshold = threshold * -1.0; } // Make sure we have a positive value
if (threshold > _range) { threshold = _range; } // Make sure it doesn't exceed the _range
if (threshold > _range) { threshold = _range; } // Make sure it doesn't exceed the _range

threshold = (((threshold * 1.0) / (_range)) * 127).tointeger();
_setReg(LIS3DH_CLICK_THS, threshold);
_setReg(LIS3DH_CLICK_THS, latchedBit | (threshold & 0x7F));

// Set the LIS3DH_TIME_LIMIT register (max time for a click)
_setReg(LIS3DH_TIME_LIMIT, timeLimit);
Expand Down Expand Up @@ -408,9 +410,9 @@ class LIS3DH {
local stats = _getReg(LIS3DH_FIFO_SRC_REG);
return {
"watermark": (stats & 0x80) != 0,
"overrun": (stats & 0x40) != 0,
"empty": (stats & 0x20) != 0,
"unread": (stats & 0x1F) + ((stats & 0x40) ? 1 : 0)
"overrun" : (stats & 0x40) != 0,
"empty" : (stats & 0x20) != 0,
"unread" : (stats & 0x1F) + ((stats & 0x40) ? 1 : 0)
}
}

Expand Down
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The [LIS3DH](http://www.st.com/st-web-ui/static/active/en/resource/technical/doc

This library also supports the LIS2DH12, another widely used three-axis MEMS accelerometer and which can be found on [Electric Imp’s impExplorer&trade; Kit](https://developer.electricimp.com/gettingstarted/devkits).

**To add this library to your project, add** `#require "LIS3DH.device.lib.nut:2.0.1"` **to the top of your device code**
**To add this library to your project, add** `#require "LIS3DH.device.lib.nut:2.0.2"` **to the top of your device code**

## Class Usage ##

Expand Down Expand Up @@ -136,7 +136,7 @@ This method enables or disables all three axes on the accelerometer. Calling the

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| *state* | Boolean | No | Activate (`true`) or disable (`false`) the acceleromater is active (Default: `true`) |
| *state* | Boolean | No | Activate (`true`) or disable (`false`) the accelerometer (Default: `true`) |

#### Return Value ####

Expand Down Expand Up @@ -200,7 +200,7 @@ This method sets the measurement range of the sensor in Gs. Supported ranges are

#### Return Value ####

Integer &mdash; the current measurement range.
Integer &mdash; the current measurement range.

#### Example ####

Expand All @@ -216,7 +216,7 @@ This method returns the currently-set measurement range of the sensor in Gs.

#### Return Value ####

Integer &mdash; the current measurement range.
Integer &mdash; the current measurement range.

```
server.log(format("Current Sensor Range is +/- %dG", accel.getRange()));
Expand Down Expand Up @@ -245,7 +245,7 @@ This method configures an interrupt when the FIFO buffer reaches the set waterma

#### Return Value ####

Nothing.
Nothing.

#### Example ####

Expand Down Expand Up @@ -333,7 +333,7 @@ The following is taken from the from [LIS3DH Datasheet](http://www.st.com/st-web

#### Return Value ####

Nothing.
Nothing.

#### Example ####

Expand All @@ -357,7 +357,7 @@ This method configures the intertial interrupt generator to generate interrupts

#### Return Value ####

Nothing.
Nothing.

#### Example ####

Expand All @@ -384,7 +384,7 @@ This method configures the click interrupt generator.

#### Return Value ####

Nothing.
Nothing.

#### Single Click Example ####

Expand Down Expand Up @@ -412,7 +412,7 @@ This method enables (*state* is `true`) or disables (*state* is `false`) data-re

#### Return Value ####

Nothing.
Nothing.

#### Example ####

Expand All @@ -435,7 +435,7 @@ Interrupt latching is disabled by default.

#### Return Value ####

Nothing.
Nothing.

#### Example ####

Expand Down Expand Up @@ -524,7 +524,7 @@ function takeReading() {
} else {
// add timestamp to result table
result.ts <- time();
// log reading
foreach(k, v in result) {
server.log(k + ": " + v);
Expand Down Expand Up @@ -635,7 +635,7 @@ This method configures the high-pass filter.

#### Return Value ####

Nothing.
Nothing.

#### Example ####

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,12 @@ class MyTestCase extends ImpTestCase {
this.assertEqual(myVal, accel._getReg(LIS3DH_CTRL_REG3));
}

function testInterruptLatching() {
local accel = getLIS();
accel.configureInterruptLatching(true);
accel.configureClickInterrupt(true);
accel.configureInertialInterrupt(true);

imp.sleep(DATA_WAIT); // hopefully something gets asserted in this time

this.assertTrue(accel.getInterruptTable() != 0);
}

function testConstruction() {
local accel = LIS3DH(_i2c, 0x32);
this.assertTrue(accel._addr == 0x32);
}

// test that calling reset correctly resets registers (in particular,
// test that calling reset correctly resets registers (in particular,
// data ready interrupt and range)
function testInit() {
local accel = LIS3DH(_i2c, 0x32);
Expand Down Expand Up @@ -114,7 +103,7 @@ class MyTestCase extends ImpTestCase {
local accel = getLIS();
local res = accel.getAccel();
this.assertTrue(("x" in res ? typeof res.x == "float" : false) &&
("y" in res ? typeof res.y == "float" : false) &&
("y" in res ? typeof res.y == "float" : false) &&
("z" in res ? typeof res.z == "float" : false));
}

Expand All @@ -123,7 +112,7 @@ class MyTestCase extends ImpTestCase {
local accel = getLIS();
accel.getAccel(function(res) {
if (("x" in res ? typeof res.x == "float" : false) &&
("y" in res ? typeof res.y == "float" : false) &&
("y" in res ? typeof res.y == "float" : false) &&
("z" in res ? typeof res.z == "float" : false)) {
resolve("async resolved successfully");
} else {
Expand All @@ -145,15 +134,15 @@ class MyTestCase extends ImpTestCase {
accel.enable(true);
imp.wakeup(DATA_WAIT, function() {
res = accel.getAccel();
// technically it's possible to have all axes at 0
// technically it's possible to have all axes at 0
// acceleration but it's unlikedly
if (!(res.x || res.y || res.z)) {
reject("failed to enable axes");
} else {
resolve("successfully disabled and enabled axes");
}
}.bindenv(this));
}
}
}.bindenv(this));
}.bindenv(this))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class MyTestCase extends ImpTestCase {
reject("ADC did not receive correct reading on channel 1");
}
}.bindenv(this));
}.bindenv(this));
}.bindenv(this));
}
*/

Expand All @@ -105,7 +105,7 @@ class MyTestCase extends ImpTestCase {
this.assertTrue(accel._addr == 0x32);
}

// test that calling reset correctly resets registers (in particular,
// test that calling reset correctly resets registers (in particular,
// data ready interrupt and range)
function testInit() {
local accel = LIS3DH(_i2c, 0x32);
Expand Down Expand Up @@ -149,7 +149,7 @@ class MyTestCase extends ImpTestCase {
local accel = getLIS();
local res = accel.getAccel();
this.assertTrue(("x" in res ? typeof res.x == "float" : false) &&
("y" in res ? typeof res.y == "float" : false) &&
("y" in res ? typeof res.y == "float" : false) &&
("z" in res ? typeof res.z == "float" : false));
}

Expand All @@ -158,7 +158,7 @@ class MyTestCase extends ImpTestCase {
local accel = getLIS();
accel.getAccel(function(res) {
if (("x" in res ? typeof res.x == "float" : false) &&
("y" in res ? typeof res.y == "float" : false) &&
("y" in res ? typeof res.y == "float" : false) &&
("z" in res ? typeof res.z == "float" : false)) {
resolve("async resolved successfully");
} else {
Expand All @@ -180,15 +180,15 @@ class MyTestCase extends ImpTestCase {
accel.enable(true);
imp.wakeup(DATA_WAIT, function() {
res = accel.getAccel();
// technically it's possible to have all axes at 0
// technically it's possible to have all axes at 0
// acceleration but it's unlikedly
if (!(res.x || res.y || res.z)) {
reject("failed to enable axes");
} else {
resolve("successfully disabled and enabled axes");
}
}.bindenv(this));
}
}
}.bindenv(this));
}.bindenv(this))
}
Expand Down

0 comments on commit 1cd92b4

Please sign in to comment.