Skip to content

Commit 1cd92b4

Browse files
author
Pavel Petroshenko
authored
Merge pull request #19 from electricimp/develop
v2.0.2
2 parents 961fbf9 + 5637ce3 commit 1cd92b4

5 files changed

+43
-52
lines changed

Diff for: .impt.test

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"allowDisconnect": false,
66
"builderCache": false,
77
"testFiles": [
8-
"*.test.nut",
9-
"tests/**/*.test.nut"
8+
"LIS3DH.automated.device.test.nut",
9+
"tests/**/LIS3DH.automated.device.test.nut"
1010
],
1111
"deviceFile": "LIS3DH.device.lib.nut"
1212
}

Diff for: LIS3DH.device.lib.nut

+18-16
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424

2525

2626
// Registers
27-
const LIS3DH_TEMP_CFG_REG = 0x1F;
28-
const LIS3DH_CTRL_REG1 = 0x20;
29-
const LIS3DH_CTRL_REG2 = 0x21;
30-
const LIS3DH_CTRL_REG3 = 0x22;
31-
const LIS3DH_CTRL_REG4 = 0x23;
32-
const LIS3DH_CTRL_REG5 = 0x24;
33-
const LIS3DH_CTRL_REG6 = 0x25;
34-
const LIS3DH_OUT_X_L_INCR = 0xA8;
27+
const LIS3DH_TEMP_CFG_REG = 0x1F; // Enable temp/ADC
28+
const LIS3DH_CTRL_REG1 = 0x20; // Data rate, normal/low power mode, enable xyz axis
29+
const LIS3DH_CTRL_REG2 = 0x21; // HPF config
30+
const LIS3DH_CTRL_REG3 = 0x22; // Int1 interrupt type enable/disable
31+
const LIS3DH_CTRL_REG4 = 0x23; // BDU, endian data sel, range, high res mode, self test, SPI 3 or 4 wire
32+
const LIS3DH_CTRL_REG5 = 0x24; // boot, FIFO enable, latch int1 & int2, 4D enable int1 & int2 with 6D bit set
33+
const LIS3DH_CTRL_REG6 = 0x25; // int2 interrupt settings, set polarity of int1 and int2 pins
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;
@@ -98,7 +98,7 @@ const LIS3DH_ADC2 = 0x02;
9898
const LIS3DH_ADC3 = 0x03;
9999

100100
class LIS3DH {
101-
static VERSION = "2.0.1";
101+
static VERSION = "2.0.2";
102102

103103
// I2C information
104104
_i2c = null;
@@ -123,7 +123,7 @@ class LIS3DH {
123123
_setReg(LIS3DH_CTRL_REG1, 0x07);
124124
_setReg(LIS3DH_CTRL_REG2, 0x00);
125125
_setReg(LIS3DH_CTRL_REG3, 0x00);
126-
_setReg(LIS3DH_CTRL_REG4, 0x00);
126+
_setReg(LIS3DH_CTRL_REG4, 0x00); // Sets range to default
127127
_setReg(LIS3DH_CTRL_REG5, 0x00);
128128
_setReg(LIS3DH_CTRL_REG6, 0x00);
129129
_setReg(LIS3DH_INT1_CFG, 0x00);
@@ -136,8 +136,9 @@ class LIS3DH {
136136
_setReg(LIS3DH_TIME_LATENCY, 0x00);
137137
_setReg(LIS3DH_TIME_WINDOW, 0x00);
138138
_setReg(LIS3DH_FIFO_CTRL_REG, 0x00);
139+
_setReg(LIS3DH_TEMP_CFG_REG, 0x00);
139140

140-
// Read the range + set _range property
141+
// Reads the default range from register and + sets local _range property
141142
getRange();
142143
}
143144

@@ -360,11 +361,12 @@ class LIS3DH {
360361
_setReg(LIS3DH_CLICK_CFG, clickType);
361362

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

366368
threshold = (((threshold * 1.0) / (_range)) * 127).tointeger();
367-
_setReg(LIS3DH_CLICK_THS, threshold);
369+
_setReg(LIS3DH_CLICK_THS, latchedBit | (threshold & 0x7F));
368370

369371
// Set the LIS3DH_TIME_LIMIT register (max time for a click)
370372
_setReg(LIS3DH_TIME_LIMIT, timeLimit);
@@ -408,9 +410,9 @@ class LIS3DH {
408410
local stats = _getReg(LIS3DH_FIFO_SRC_REG);
409411
return {
410412
"watermark": (stats & 0x80) != 0,
411-
"overrun": (stats & 0x40) != 0,
412-
"empty": (stats & 0x20) != 0,
413-
"unread": (stats & 0x1F) + ((stats & 0x40) ? 1 : 0)
413+
"overrun" : (stats & 0x40) != 0,
414+
"empty" : (stats & 0x20) != 0,
415+
"unread" : (stats & 0x1F) + ((stats & 0x40) ? 1 : 0)
414416
}
415417
}
416418

Diff for: README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ The [LIS3DH](http://www.st.com/st-web-ui/static/active/en/resource/technical/doc
66

77
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).
88

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

1111
## Class Usage ##
1212

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

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

141141
#### Return Value ####
142142

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

201201
#### Return Value ####
202202

203-
Integer &mdash; the current measurement range.
203+
Integer &mdash; the current measurement range.
204204

205205
#### Example ####
206206

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

217217
#### Return Value ####
218218

219-
Integer &mdash; the current measurement range.
219+
Integer &mdash; the current measurement range.
220220

221221
```
222222
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
245245

246246
#### Return Value ####
247247

248-
Nothing.
248+
Nothing.
249249

250250
#### Example ####
251251

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

334334
#### Return Value ####
335335

336-
Nothing.
336+
Nothing.
337337

338338
#### Example ####
339339

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

358358
#### Return Value ####
359359

360-
Nothing.
360+
Nothing.
361361

362362
#### Example ####
363363

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

385385
#### Return Value ####
386386

387-
Nothing.
387+
Nothing.
388388

389389
#### Single Click Example ####
390390

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

413413
#### Return Value ####
414414

415-
Nothing.
415+
Nothing.
416416

417417
#### Example ####
418418

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

436436
#### Return Value ####
437437

438-
Nothing.
438+
Nothing.
439439

440440
#### Example ####
441441

@@ -524,7 +524,7 @@ function takeReading() {
524524
} else {
525525
// add timestamp to result table
526526
result.ts <- time();
527-
527+
528528
// log reading
529529
foreach(k, v in result) {
530530
server.log(k + ": " + v);
@@ -635,7 +635,7 @@ This method configures the high-pass filter.
635635

636636
#### Return Value ####
637637

638-
Nothing.
638+
Nothing.
639639

640640
#### Example ####
641641

Diff for: tests/LIS3HD.automated.device.test.nut renamed to tests/LIS3DH.automated.device.test.nut

+5-16
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,12 @@ class MyTestCase extends ImpTestCase {
5454
this.assertEqual(myVal, accel._getReg(LIS3DH_CTRL_REG3));
5555
}
5656

57-
function testInterruptLatching() {
58-
local accel = getLIS();
59-
accel.configureInterruptLatching(true);
60-
accel.configureClickInterrupt(true);
61-
accel.configureInertialInterrupt(true);
62-
63-
imp.sleep(DATA_WAIT); // hopefully something gets asserted in this time
64-
65-
this.assertTrue(accel.getInterruptTable() != 0);
66-
}
67-
6857
function testConstruction() {
6958
local accel = LIS3DH(_i2c, 0x32);
7059
this.assertTrue(accel._addr == 0x32);
7160
}
7261

73-
// test that calling reset correctly resets registers (in particular,
62+
// test that calling reset correctly resets registers (in particular,
7463
// data ready interrupt and range)
7564
function testInit() {
7665
local accel = LIS3DH(_i2c, 0x32);
@@ -114,7 +103,7 @@ class MyTestCase extends ImpTestCase {
114103
local accel = getLIS();
115104
local res = accel.getAccel();
116105
this.assertTrue(("x" in res ? typeof res.x == "float" : false) &&
117-
("y" in res ? typeof res.y == "float" : false) &&
106+
("y" in res ? typeof res.y == "float" : false) &&
118107
("z" in res ? typeof res.z == "float" : false));
119108
}
120109

@@ -123,7 +112,7 @@ class MyTestCase extends ImpTestCase {
123112
local accel = getLIS();
124113
accel.getAccel(function(res) {
125114
if (("x" in res ? typeof res.x == "float" : false) &&
126-
("y" in res ? typeof res.y == "float" : false) &&
115+
("y" in res ? typeof res.y == "float" : false) &&
127116
("z" in res ? typeof res.z == "float" : false)) {
128117
resolve("async resolved successfully");
129118
} else {
@@ -145,15 +134,15 @@ class MyTestCase extends ImpTestCase {
145134
accel.enable(true);
146135
imp.wakeup(DATA_WAIT, function() {
147136
res = accel.getAccel();
148-
// technically it's possible to have all axes at 0
137+
// technically it's possible to have all axes at 0
149138
// acceleration but it's unlikedly
150139
if (!(res.x || res.y || res.z)) {
151140
reject("failed to enable axes");
152141
} else {
153142
resolve("successfully disabled and enabled axes");
154143
}
155144
}.bindenv(this));
156-
}
145+
}
157146
}.bindenv(this));
158147
}.bindenv(this))
159148
}

Diff for: tests/LIS3HD.manual.device.test.nut renamed to tests/LIS3DH.manual.device.test.nut

+6-6
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class MyTestCase extends ImpTestCase {
8585
reject("ADC did not receive correct reading on channel 1");
8686
}
8787
}.bindenv(this));
88-
}.bindenv(this));
88+
}.bindenv(this));
8989
}
9090
*/
9191

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

108-
// test that calling reset correctly resets registers (in particular,
108+
// test that calling reset correctly resets registers (in particular,
109109
// data ready interrupt and range)
110110
function testInit() {
111111
local accel = LIS3DH(_i2c, 0x32);
@@ -149,7 +149,7 @@ class MyTestCase extends ImpTestCase {
149149
local accel = getLIS();
150150
local res = accel.getAccel();
151151
this.assertTrue(("x" in res ? typeof res.x == "float" : false) &&
152-
("y" in res ? typeof res.y == "float" : false) &&
152+
("y" in res ? typeof res.y == "float" : false) &&
153153
("z" in res ? typeof res.z == "float" : false));
154154
}
155155

@@ -158,7 +158,7 @@ class MyTestCase extends ImpTestCase {
158158
local accel = getLIS();
159159
accel.getAccel(function(res) {
160160
if (("x" in res ? typeof res.x == "float" : false) &&
161-
("y" in res ? typeof res.y == "float" : false) &&
161+
("y" in res ? typeof res.y == "float" : false) &&
162162
("z" in res ? typeof res.z == "float" : false)) {
163163
resolve("async resolved successfully");
164164
} else {
@@ -180,15 +180,15 @@ class MyTestCase extends ImpTestCase {
180180
accel.enable(true);
181181
imp.wakeup(DATA_WAIT, function() {
182182
res = accel.getAccel();
183-
// technically it's possible to have all axes at 0
183+
// technically it's possible to have all axes at 0
184184
// acceleration but it's unlikedly
185185
if (!(res.x || res.y || res.z)) {
186186
reject("failed to enable axes");
187187
} else {
188188
resolve("successfully disabled and enabled axes");
189189
}
190190
}.bindenv(this));
191-
}
191+
}
192192
}.bindenv(this));
193193
}.bindenv(this))
194194
}

0 commit comments

Comments
 (0)