Skip to content

Commit 5be1eef

Browse files
Merge pull request #7 from electricimp/v1.0.4
V1.0.4
2 parents 0f6e789 + a9cd745 commit 5be1eef

File tree

2 files changed

+92
-100
lines changed

2 files changed

+92
-100
lines changed

Diff for: LIS3DH.class.nut

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class LIS3DH {
2-
static version = [1,0,3];
2+
static version = [1,0,4];
33

44
// Registers
55
static TEMP_CFG_REG = 0x1F;
@@ -123,9 +123,11 @@ class LIS3DH {
123123
// Set Accelerometer Data Rate in Hz
124124
function setDataRate(rate) {
125125
local val = _getReg(CTRL_REG1) & 0x0F;
126+
local normal_mode = (val < 8);
126127
if (rate == 0) {
127128
// 0b0000 -> power-down mode
128129
// we've already ANDed-out the top 4 bits; just write back
130+
rate = 0;
129131
} else if (rate <= 1) {
130132
val = val | 0x10;
131133
rate = 1;
@@ -147,10 +149,13 @@ class LIS3DH {
147149
} else if (rate <= 400) {
148150
val = val | 0x70;
149151
rate = 400;
152+
} else if (normal_mode) {
153+
val = val | 0x90;
154+
rate = 1250;
150155
} else if (rate <= 1600) {
151156
val = val | 0x80;
152157
rate = 1600;
153-
} else if (rate <= 5000) {
158+
} else {
154159
val = val | 0x90;
155160
rate = 5000;
156161
}

Diff for: README.md

+85-98
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,160 @@
11
# LIS3DH 3-Axis Accelerometer
22

3-
The [LIS3DH](http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/CD00274221.pdf) is a 3-Axis MEMS accelerometer. This sensor has extensive functionality and this class has not yet implemented all of it.
3+
The [LIS3DH](http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/CD00274221.pdf) is a 3-Axis MEMS accelerometer. The LIS3DH application note can be found [here](http://www.st.com/web/en/resource/technical/document/application_note/CD00290365.pdf). This sensor has extensive functionality and this class has not yet implemented all of it.
44

55
The LPS25H can interface over I&sup2;C or SPI. This class addresses only I&sup2;C for the time being.
66

7-
**To add this library to your project, add** `#require "LIS3DH.class.nut:1.0.3"` **to the top of your device code**
7+
**To add this library to your project, add** `#require "LIS3DH.class.nut:1.0.4"` **to the top of your device code**
88

99
## Class Usage
1010

11-
### constructor(*i2c[, addr]*)
12-
13-
The class’ constructor takes one required parameter (a configured imp I&sup2;C bus) and an optional parameter (the I&sup2;C address of the accelerometer):
11+
### Constructor: LIS3DH(*i2cBus[, i2cAddress]*)
1412

13+
The class’ constructor takes one required parameter (a configured imp I&sup2;C bus) and an optional parameter (the I&sup2;C address of the accelerometer). The I&sup2;C address must be the address of your sensor or an I&sup2;C error will be thrown.
1514

1615
| Parameter | Type | Default | Description |
1716
| ------------- | ------------ | ------- | ----------- |
18-
| *i2c* | hardware.i2c | N/A | A pre-configured I&sup2;C bus |
19-
| *addr* | byte | 0x30 | The I&sup2;C address of the accelerometer |
17+
| *i2cBus* | hardware.i2c | N/A | A pre-configured I&sup2;C bus |
18+
| *i2cAddress* | byte | 0x30 | The I&sup2;C address of the accelerometer |
2019

2120
&nbsp;<br>
2221

2322
```squirrel
24-
#require "LIS3DH.class.nut:1.0.3"
23+
#require "LIS3DH.class.nut:1.0.4"
2524
2625
i2c <- hardware.i2c89;
2726
i2c.configure(CLOCK_SPEED_400_KHZ);
2827
29-
accel <- LIS3DH(i2c, 0x32); // using a non-default I2C address (SA0 pulled high)
28+
// Use a non-default I2C address (SA0 pulled high)
29+
accel <- LIS3DH(i2c, 0x32);
3030
```
3131

3232
## Class Methods
3333

3434
### init()
3535

36-
The *init()* method resets all registers to datasheet default values. This can be very useful during active development as clicking ‘Build and Run’ will not reset the chip.
36+
The *init()* method resets all control and interrupt registers to datasheet default values.
3737

3838
```squirrel
39-
i2c <- hardware.i2c89;
40-
i2c.configure(CLOCK_SPEED_400_KHZ);
41-
4239
accel <- LIS3DH(i2c, 0x32);
43-
44-
// ***REMOVE BEFORE GOING TO PRODUCTION***
45-
accel.reset();
46-
// ***************************************
40+
accel.init();
4741
```
4842

4943
### setDataRate(*rateHz*)
5044

51-
The *setDataRate()* method sets the Output Data Rate (ODR) of the accelerometer in Hz. The nearest supported data rate less than or equal to the requested rate will be used and returned. Supported datarates are 0 (Shutdown), 1, 10, 25, 50, 100, 200, 400, 1600 and 5000Hz.
45+
The *setDataRate()* method sets the Output Data Rate (ODR) of the accelerometer in Hz. Supported datarates are 0 (Shutdown), 1, 10, 25, 50, 100, 200, 400, 1250 (Normal Mode only), 1600 (Low-Power Mode only), and 5000 (Low-Power Mode only) Hz. The requested data rate will be rounded up to the closest supported rate and the actual data rate will be returned.
46+
47+
The default data rate is 0 (Shutdown). To take a reading with *getAccel()* you must set a data rate greater than 0.
5248

5349
```squirrel
54-
local rate = accel.setDataRate(100);
55-
server.log(format("Accelerometer running at %d Hz",rate));
50+
local rate = accel.setDataRate(90);
51+
server.log(format("Accelerometer running at %dHz", rate));
52+
// Displays 'Accelerometer running at 100Hz'
5653
```
5754

58-
**Note** The datarate must be set before reading the accelerometer with *getAccel()*.
55+
### setLowPower(*state*)
5956

60-
### getAccel(*[callback]*)
57+
The *setLowPower()* method configures the device to run in low-power or normal mode. The method takes one boolean parameter, *state*. When *state* is `true`, low-power mode is enabled. When *state* is `false`, normal mode is enabled. Normal mode guarantees high resolution; low-power mode reduces the current consumption. Higher data rates only support specific modes. See *setDataRate()* for details.
6158

62-
The *getAccel()* method reads and returns the latest measurement from the accelerometer as a table (in Gs):
59+
Normal mode is enabled by default.
60+
61+
```Squirrel
62+
// Enable low-power mode
63+
accel.setLowPower(true);
64+
```
65+
66+
### enable([*state*])
67+
68+
The *enable()* method enables or disables all three axes on the accelerometer. The method takes an optional boolean parameter, *state*. By default *state* is set to `true` and the accelerometer is enabled. When *state* is `false`, the accelerometer will be disabled.
6369

6470
```squirrel
65-
{ x: <xData>, y: <yData>, z: <zData> }
71+
function goToSleep() {
72+
imp.onidle(function() {
73+
// Set data rate to 0 and disable the accelerometer to save power
74+
accel.setDataRate(0);
75+
accel.enable(false);
76+
77+
// Sleep for 1 hour
78+
server.sleepfor(3600);
79+
});
80+
}
6681
```
6782

83+
### getAccel(*[callback]*)
84+
85+
The *getAccel()* method reads the latest measurement from the accelerometer. The method takes an optional callback for asynchronous operation &mdash; it will block otherwise. The callback should take one parameter: a results table *(see below)*. If the callback is null or omitted, the method will return the results table.
86+
87+
```
88+
{ "x": <xData>,
89+
"y": <yData>,
90+
"z": <zData> }
91+
```
92+
93+
#### Synchronous example
94+
6895
```squirrel
69-
// Create and enable the sensor
70-
i2c <- hardware.i2c89;
71-
i2c.configure(CLOCK_SPEED_400_KHZ);
72-
accel <- LIS3DH(i2c, 0x32);
7396
accel.setDataRate(100);
74-
75-
local val = accel.getAccel()
97+
local val = accel.getAccel();
7698
server.log(format("Acceleration (G): (%0.2f, %0.2f, %0.2f)", val.x, val.y, val.z));
7799
```
78100

79-
An optional callback (with a single parameter) can be passed to *getAccel()*. If a callback is included, the class will read the sensor data and pass the result to the callback method as the first parameter:
101+
#### Asynchronous Example
80102

81103
```squirrel
82-
// Create and enable the sensor
83-
i2c <- hardware.i2c89;
84-
i2c.configure(CLOCK_SPEED_400_KHZ);
85-
accel <- LIS3DH(i2c, 0x32);
86104
accel.setDataRate(100);
87-
88105
accel.getAccel(function(val) {
89106
server.log(format("Acceleration (G): (%0.2f, %0.2f, %0.2f)", val.x, val.y, val.z));
90107
});
91108
```
92109

93-
### setRange(*range_g*)
110+
### setRange(*range*)
111+
112+
The *setRange()* method sets the measurement range of the sensor in Gs. Supported ranges are (&plusmn;) 2, 4, 8 and 16G. The data rate will be rounded up to the closest supported range and the actual range will be returned.
94113

95-
The *setRange()* method sets the measurement range of the sensor in Gs. The default measurement range is &plusmn;2G. The nearest supported range less than or equal to the requested range will be used and returned. Supported ranges are (&plusmn;) 2, 4, 6, 8 and 16G.
114+
The default measurement range is &plusmn;2G.
96115

97116
```squirrel
98-
// Set sensor range to +/- 6 G
99-
local range = accel.setRange(6);
100-
server.log(format("Range set to +/- %d G", range));
117+
// Set sensor range to +/- 8G
118+
local range = accel.setRange(8);
119+
server.log(format("Range set to +/- %dG", range));
101120
```
102121

103-
**Note** If you are not using the default &plusmn;2G range, you must set the range with *setRange()* before setting interrupt thresholds with *setInertialIntThreshold()* or *setClickIntThreshold()*.
104-
105122
### getRange()
106123

107124
The *getRange()* method returns the currently-set measurement range of the sensor in Gs.
108125

109126
```squirrel
110-
server.log(format("Current Sensor Range is +/- %d G", accel.getRange()));
127+
server.log(format("Current Sensor Range is +/- %dG", accel.getRange()));
111128
```
112129

113130
### configureInertialInterrupt(*state[, threshold][, duration][, options]*)
114131

115-
Configures the Inertial Interrupt generator:
132+
This method configures the inertial interrupt generator:
116133

117-
| parameter | type | default | description |
118-
| --------- | -------- | -------------------------- | ----------- |
119-
| state | bool | n/a | `true` to enable, `false` to disable |
120-
| threshold | float | 2.0 | Inertial interrupts threshold in Gs |
121-
| duration | int | 5 | Number of samples exceeding threshold required to generate interrupt |
122-
| options | bitfield | *X_HIGH* \| *Y_HIGH* \| *Z_HIGH* | See table below |
134+
| Parameter | Type | Default Value | Description |
135+
| --------- | ---- | ------------- | ----------- |
136+
| *state* | Boolean | N/A | `true` to enable, `false` to disable |
137+
| *threshold* | Float | 2.0 | Inertial interrupts threshold in Gs |
138+
| *duration* | Integer | 5 | Number of samples exceeding threshold required to generate interrupt |
139+
| *options* | bitfield | *X_HIGH* | *Y_HIGH* | *Z_HIGH* | See table below |
123140

124141
```squirrel
125142
// Configure the Inertial interrupt generator to generate an interrupt
126143
// when acceleration on all three exceeds 1G.
127144
accel.configureInertialInterrupt(true, 1.0, 10, LIS3DH.X_LOW | LIS3DH.Y_LOW | LIS3DH.Z_LOW | LIS3DH.AOI)
128145
```
129146

130-
The default configuration for the Intertial Interrupt generator is to generate an interrupt when the acceleration on *any* axis exceeds 1G. This behavior can be changed by OR-ing together any of the following flags:
147+
The default configuration for the Intertial Interrupt generator is to generate an interrupt when the acceleration on *any* axis exceeds 2G. This behavior can be changed by OR'ing together any of the following flags:
131148

132-
| flag | description |
149+
| Flag | Description |
133150
| ------ | ----------- |
134151
| *X_LOW* | Generates an interrupt when the x-axis acceleration goes below the threshold |
135152
| *X_HIGH* | Generates an interrupt when the x-axis acceleration goes above the threshold |
136153
| *Y_LOW* | Generates an interrupt when the y-axis acceleration goes below the threshold |
137154
| *Y_HIGH* | Generates an interrupt when the y-axis acceleration goes above the threshold |
138155
| *Z_LOW* | Generates an interrupt when the z-axis acceleration goes below the threshold |
139156
| *Z_HIGH* | Generates an interrupt when the z-axis acceleration goes above the threshold |
140-
| *AOI* | Sets the AOI flag *(see ‘Inertial Interrupt Modes’ below)* |
157+
| *AOI * | Sets the AOI flag *(see ‘Inertial Interrupt Modes’ below)* |
141158
| *SIX_D* | Sets the 6D flag *(see ‘Inertial Interrupt Modes’ below)* |
142159

143160
#### Inertial Interrupt Modes
@@ -157,7 +174,7 @@ The following is taken from the from [LIS3DH Datasheet](http://www.st.com/st-web
157174

158175
### configureFreeFallInterrupt(*state[, threshold][, duration]*)
159176

160-
The *configureFreeFallInterrupt()* method configures the intertial interrupt generator to generate interrupts when the device is in free fall (acceleration on all axis appraoches 0). The default *threshold* is 0.5 Gs.The default *duration* is five samples.
177+
The *configureFreeFallInterrupt()* method configures the intertial interrupt generator to generate interrupts when the device is in free fall (acceleration on all axis appraoches 0). The default *threshold* is 0.5G.The default *duration* is five samples.
161178

162179
```squirrel
163180
accel.configureFreeFallInterrupt(true);
@@ -167,18 +184,18 @@ accel.configureFreeFallInterrupt(true);
167184

168185
### configureClickInterrupt(*state[, clickType][, threshold][, timeLimit][, latency][, window]*)
169186

170-
Configures the Click Interrupt Generator:
187+
Configures the click interrupt generator:
171188

172-
| parameter | type | default | description |
173-
| --------- | ---------- | -------------------------- | -------------------------------------------------------- |
174-
| *state* | bool | n/a | `true` to enable, `false` to disable |
175-
| *clickType* | CONST | *LIS3DH.SINGLE_CLICK* | *LIS3DH.SINGLE_CLICK* or *LIS3DH.DOUBLE_CLICK* |
176-
| *threshold* | float | 1.1 | Threshold that must be exceeded to be considered a click |
177-
| *timeLimit* | float | 5 | Max time in *ms* the acceleration can spend above the threshold to be considered a click |
178-
| *latency* | float | 10 | Min time in *ms* between the end of one click event, and the start of another to be considred a *LIS3DH.DOUBLE_CLICK* |
179-
| *window* | float | 50 | Max time in *ms* between the start of one click event, and end of another to be considered a *LIS3DH.DOUBLE_CLICK* |
189+
| Parameter | Type | Default Value | Description |
190+
| --- | --- | --- | --- |
191+
| *state* | Boolean | N/A | `true` to enable, `false` to disable |
192+
| *clickType* | Constant | *LIS3DH.SINGLE_CLICK* | *LIS3DH.SINGLE_CLICK* or *LIS3DH.DOUBLE_CLICK* |
193+
| *threshold* | Float | 1.1 | Threshold that must be exceeded to be considered a click |
194+
| *timeLimit* | Float | 5 | Max time in *ms* the acceleration can spend above the threshold to be considered a click |
195+
| *latency* | Float | 10 | Min time in *ms* between the end of one click event and the start of another to be considered a *LIS3DH.DOUBLE_CLICK* |
196+
| *window* | Float | 50 | Max time in *ms* between the start of one click event and end of another to be considered a *LIS3DH.DOUBLE_CLICK* |
180197

181-
#### Single Click example
198+
#### Single Click Example
182199

183200
```squirrel
184201
// Configure a single click interrupt
@@ -194,7 +211,7 @@ accel.configureClickInterrupt(true, LIS3DH.DOUBLE_CLICK);
194211

195212
### configureDataReadyInterrupt(*state*)
196213

197-
Enables (*state* is `true`) or disables (*state* is `false`) Data Ready interrupts on the INT1 line.
214+
Enables (*state* is `true`) or disables (*state* is `false`) data-ready interrupts on the INT1 line. The data-ready signal rises to 1 when a new set of acceleration data has been generated and it is available for reading. The interrupt is reset when the higher part of the data of all the enabled channels has been read.
198215

199216
```squirrel
200217
accel.setDataRate(1); // 1 Hz
@@ -205,15 +222,13 @@ accel.configureDataReadyInterrupt(true);
205222

206223
Enables (*state* is `true`) or disables (*state* is `false`) interrupt latching. If interrupt latching is enabled, the interrupt signal will remain asserted until the interrupt source register is read by calling *getInterruptTable()*. If latching is disabled, the interrupt signal will remain asserted as long as the interrupt-generating condition persists.
207224

208-
Inertial and free fall are the only interrupts compatible with latching mode.
209-
210225
Interrupt latching is disabled by default.
211226

212227
*See sample code in getInterruptTable()*
213228

214229
### getInterruptTable()
215230

216-
The *getInterruptTable()* method reads the *INT1_SRC* and *CLICK_SRC* registers, and returns the result as a table with the following fields:
231+
The *getInterruptTable()* method reads the LIS3DH’s *INT1_SRC* and *CLICK_SRC* registers, and returns the result as a table with the following fields:
217232

218233
```squirrel
219234
{
@@ -230,7 +245,7 @@ The *getInterruptTable()* method reads the *INT1_SRC* and *CLICK_SRC* registers,
230245
}
231246
```
232247

233-
In the following example we setup an interrupt for double click detection:
248+
In the following example we setup an interrupt for double-click detection:
234249

235250
```squirrel
236251
function interruptHandler() {
@@ -348,34 +363,6 @@ Returns the one-byte device ID of the sensor (from the *WHO_AM_I* register). The
348363
server.log(format("Device ID: 0x%02X", accel.getDeviceId()));
349364
```
350365

351-
### enable(*state*)
352-
353-
The *enable()* methods enables (*state* is `true`) or disabled (*state* is `false`) the accelerometer. The accelerometer is enabled by default.
354-
355-
```squirrel
356-
function goToSleep() {
357-
imp.onidle(function() {
358-
// Set data rate to 0 and disable the accelerometer to save power
359-
accel.setDataRate(0);
360-
accel.enable(false);
361-
362-
// Sleep for 1 hour
363-
server.sleepfor(3600);
364-
});
365-
}
366-
```
367-
368-
### setLowPower(*state*)
369-
370-
The *setLowPower()* method enables (*state* is `true`) or disables (*state* is `false`) low-power mode.
371-
372-
```squirrel
373-
// Enable low-power mode
374-
accel.setLowPower(true);
375-
```
376-
377-
**Note** *setLowPower()* will change the data rate.
378-
379366
## License
380367

381368
The LIS3DH class is licensed under [MIT License](https://github.com/electricimp/lis3dh/blob/master/LICENSE).

0 commit comments

Comments
 (0)