diff --git a/.impt.test b/.impt.test index 5857368..cab6d68 100644 --- a/.impt.test +++ b/.impt.test @@ -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" } diff --git a/LIS3DH.device.lib.nut b/LIS3DH.device.lib.nut index 21804c6..f1323c4 100644 --- a/LIS3DH.device.lib.nut +++ b/LIS3DH.device.lib.nut @@ -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; @@ -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; @@ -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); @@ -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(); } @@ -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); @@ -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) } } diff --git a/README.md b/README.md index 8a620ce..38149e6 100644 --- a/README.md +++ b/README.md @@ -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™ 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 ## @@ -200,7 +200,7 @@ This method sets the measurement range of the sensor in Gs. Supported ranges are #### Return Value #### -Integer — the current measurement range. +Integer — the current measurement range. #### Example #### @@ -216,7 +216,7 @@ This method returns the currently-set measurement range of the sensor in Gs. #### Return Value #### -Integer — the current measurement range. +Integer — the current measurement range. ``` server.log(format("Current Sensor Range is +/- %dG", accel.getRange())); @@ -245,7 +245,7 @@ This method configures an interrupt when the FIFO buffer reaches the set waterma #### Return Value #### -Nothing. +Nothing. #### Example #### @@ -333,7 +333,7 @@ The following is taken from the from [LIS3DH Datasheet](http://www.st.com/st-web #### Return Value #### -Nothing. +Nothing. #### Example #### @@ -357,7 +357,7 @@ This method configures the intertial interrupt generator to generate interrupts #### Return Value #### -Nothing. +Nothing. #### Example #### @@ -384,7 +384,7 @@ This method configures the click interrupt generator. #### Return Value #### -Nothing. +Nothing. #### Single Click Example #### @@ -412,7 +412,7 @@ This method enables (*state* is `true`) or disables (*state* is `false`) data-re #### Return Value #### -Nothing. +Nothing. #### Example #### @@ -435,7 +435,7 @@ Interrupt latching is disabled by default. #### Return Value #### -Nothing. +Nothing. #### Example #### @@ -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); @@ -635,7 +635,7 @@ This method configures the high-pass filter. #### Return Value #### -Nothing. +Nothing. #### Example #### diff --git a/tests/LIS3HD.automated.device.test.nut b/tests/LIS3DH.automated.device.test.nut similarity index 93% rename from tests/LIS3HD.automated.device.test.nut rename to tests/LIS3DH.automated.device.test.nut index 269e570..dbdb8ca 100644 --- a/tests/LIS3HD.automated.device.test.nut +++ b/tests/LIS3DH.automated.device.test.nut @@ -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); @@ -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)); } @@ -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 { @@ -145,7 +134,7 @@ 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"); @@ -153,7 +142,7 @@ class MyTestCase extends ImpTestCase { resolve("successfully disabled and enabled axes"); } }.bindenv(this)); - } + } }.bindenv(this)); }.bindenv(this)) } diff --git a/tests/LIS3HD.manual.device.test.nut b/tests/LIS3DH.manual.device.test.nut similarity index 99% rename from tests/LIS3HD.manual.device.test.nut rename to tests/LIS3DH.manual.device.test.nut index fcc5e38..cc6cd95 100644 --- a/tests/LIS3HD.manual.device.test.nut +++ b/tests/LIS3DH.manual.device.test.nut @@ -85,7 +85,7 @@ class MyTestCase extends ImpTestCase { reject("ADC did not receive correct reading on channel 1"); } }.bindenv(this)); - }.bindenv(this)); + }.bindenv(this)); } */ @@ -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); @@ -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)); } @@ -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 { @@ -180,7 +180,7 @@ 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"); @@ -188,7 +188,7 @@ class MyTestCase extends ImpTestCase { resolve("successfully disabled and enabled axes"); } }.bindenv(this)); - } + } }.bindenv(this)); }.bindenv(this)) }