16
16
17
17
import time
18
18
import math
19
+ import digitalio
19
20
try :
20
21
import struct
21
22
except ImportError :
36
37
REG_CTRL4 = const (0x23 )
37
38
REG_CTRL5 = const (0x24 )
38
39
REG_OUT_X_L = const (0x28 )
40
+ REG_INT1SRC = const (0x31 )
39
41
REG_CLICKCFG = const (0x38 )
40
42
REG_CLICKSRC = const (0x39 )
41
43
REG_CLICKTHS = const (0x3A )
48
50
RANGE_8_G = const (0b10 ) # +/- 8g
49
51
RANGE_4_G = const (0b01 ) # +/- 4g
50
52
RANGE_2_G = const (0b00 ) # +/- 2g (default value)
53
+ DATARATE_1344_HZ = const (0b1001 ) # 1.344 KHz
51
54
DATARATE_400_HZ = const (0b0111 ) # 400Hz
52
55
DATARATE_200_HZ = const (0b0110 ) # 200Hz
53
56
DATARATE_100_HZ = const (0b0101 ) # 100Hz
63
66
64
67
class LIS3DH :
65
68
"""Driver base for the LIS3DH accelerometer."""
66
- def __init__ (self ):
69
+ def __init__ (self , int1 = None , int2 = None ):
67
70
# Check device ID.
68
71
device_id = self ._read_register_byte (REG_WHOAMI )
69
72
if device_id != 0x33 :
70
73
raise RuntimeError ('Failed to find LIS3DH!' )
74
+ # Reboot
75
+ self ._write_register_byte (REG_CTRL5 , 0x80 )
76
+ time .sleep (0.01 ) # takes 5ms
71
77
# Enable all axes, normal mode.
72
78
self ._write_register_byte (REG_CTRL1 , 0x07 )
73
79
# Set 400Hz data rate.
74
80
self .data_rate = DATARATE_400_HZ
75
81
# High res & BDU enabled.
76
82
self ._write_register_byte (REG_CTRL4 , 0x88 )
77
- # DRDY on INT1.
78
- self ._write_register_byte (REG_CTRL3 , 0x10 )
79
83
# Enable ADCs.
80
84
self ._write_register_byte (REG_TEMPCFG , 0x80 )
85
+ # Latch interrupt for INT1
86
+ self ._write_register_byte (REG_CTRL5 , 0x08 )
87
+
88
+ # Initialise interrupt pins
89
+ self ._int1 = int1
90
+ self ._int2 = int2
91
+ if self ._int1 :
92
+ self ._int1 .direction = digitalio .Direction .INPUT
93
+ self ._int1 .pull = digitalio .Pull .UP
81
94
82
95
@property
83
96
def data_rate (self ):
@@ -105,7 +118,7 @@ def range(self):
105
118
@range .setter
106
119
def range (self , range_value ):
107
120
ctl4 = self ._read_register_byte (REG_CTRL4 )
108
- ctl4 &= ~ ( 0x30 )
121
+ ctl4 &= ~ 0x30
109
122
ctl4 |= range_value << 4
110
123
self ._write_register_byte (REG_CTRL4 , ctl4 )
111
124
@@ -125,7 +138,7 @@ def acceleration(self):
125
138
126
139
x , y , z = struct .unpack ('<hhh' , self ._read_register (REG_OUT_X_L | 0x80 , 6 ))
127
140
128
- return ( x / divider * 9.806 , y / divider * 9.806 , z / divider * 9.806 )
141
+ return x / divider * 9.806 , y / divider * 9.806 , z / divider * 9.806
129
142
130
143
def shake (self , shake_threshold = 30 , avg_count = 10 , total_delay = 0.1 ):
131
144
"""Detect when the accelerometer is shaken. Optional parameters:
@@ -184,8 +197,25 @@ def read_adc_mV(self, adc): # pylint: disable=invalid-name
184
197
@property
185
198
def tapped (self ):
186
199
"""True if a tap was detected recently. Whether its a single tap or double tap is
187
- determined by the tap param on `set_tap`. This may be True over multiple reads
188
- even if only a single tap or single double tap occurred."""
200
+ determined by the tap param on ``set_tap``. ``tapped`` may be True over
201
+ multiple reads even if only a single tap or single double tap occurred if the
202
+ interrupt (int) pin is not specified.
203
+
204
+ The following example uses ``i2c`` and specifies the interrupt pin:
205
+
206
+ .. code-block:: python
207
+
208
+ import adafruit_lis3dh
209
+ import digitalio
210
+
211
+ i2c = busio.I2C(board.SCL, board.SDA)
212
+ int1 = digitalio.DigitalInOut(board.D11) # pin connected to interrupt
213
+ lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
214
+ lis3dh.range = adafruit_lis3dh.RANGE_8_G
215
+
216
+ """
217
+ if self ._int1 and not self ._int1 .value :
218
+ return False
189
219
raw = self ._read_register_byte (REG_CLICKSRC )
190
220
return raw & 0x40 > 0
191
221
@@ -209,24 +239,24 @@ def set_tap(self, tap, threshold, *,
209
239
raise ValueError ('Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!' )
210
240
if threshold > 127 or threshold < 0 :
211
241
raise ValueError ('Threshold out of range (0-127)' )
242
+
243
+ ctrl3 = self ._read_register_byte (REG_CTRL3 )
212
244
if tap == 0 and click_cfg is None :
213
245
# Disable click interrupt.
214
- r = self ._read_register_byte (REG_CTRL3 )
215
- r &= ~ (0x80 ) # Turn off I1_CLICK.
216
- self ._write_register_byte (REG_CTRL3 , r )
246
+ self ._write_register_byte (REG_CTRL3 , ctrl3 & ~ (0x80 )) # Turn off I1_CLICK.
217
247
self ._write_register_byte (REG_CLICKCFG , 0 )
218
248
return
219
- # Else enable click with specified parameters.
220
- self ._write_register_byte (REG_CTRL3 , 0x80 ) # Turn on int1 click.
221
- self . _write_register_byte ( REG_CTRL5 , 0x08 ) # Latch interrupt on int1.
222
- if click_cfg is not None :
223
- # Custom click configuration register value specified, use it.
224
- self . _write_register_byte ( REG_CLICKCFG , click_cfg )
225
- elif tap == 1 :
226
- self . _write_register_byte ( REG_CLICKCFG , 0x15 ) # Turn on all axes & singletap .
227
- elif tap == 2 :
228
- self ._write_register_byte (REG_CLICKCFG , 0x2A ) # Turn on all axes & doubletap.
229
- self ._write_register_byte (REG_CLICKTHS , threshold | 0x80 )
249
+ else :
250
+ self ._write_register_byte (REG_CTRL3 , ctrl3 | 0x80 ) # Turn on int1 click output
251
+
252
+ if click_cfg is None :
253
+ if tap == 1 :
254
+ click_cfg = 0x15 # Turn on all axes & singletap.
255
+ if tap == 2 :
256
+ click_cfg = 0x2A # Turn on all axes & doubletap .
257
+ # Or, if a custom click configuration register value specified, use it.
258
+ self ._write_register_byte (REG_CLICKCFG , click_cfg )
259
+ self ._write_register_byte (REG_CLICKTHS , 0x80 | threshold )
230
260
self ._write_register_byte (REG_TIMELIMIT , time_limit )
231
261
self ._write_register_byte (REG_TIMELATENCY , time_latency )
232
262
self ._write_register_byte (REG_TIMEWINDOW , time_window )
@@ -250,11 +280,11 @@ def _write_register_byte(self, register, value):
250
280
class LIS3DH_I2C (LIS3DH ):
251
281
"""Driver for the LIS3DH accelerometer connected over I2C."""
252
282
253
- def __init__ (self , i2c , address = 0x18 ):
283
+ def __init__ (self , i2c , * , address = 0x18 , int1 = None , int2 = None ):
254
284
import adafruit_bus_device .i2c_device as i2c_device
255
285
self ._i2c = i2c_device .I2CDevice (i2c , address )
256
286
self ._buffer = bytearray (6 )
257
- super ().__init__ ()
287
+ super ().__init__ (int1 = int1 , int2 = int2 )
258
288
259
289
def _read_register (self , register , length ):
260
290
self ._buffer [0 ] = register & 0xFF
@@ -273,11 +303,11 @@ def _write_register_byte(self, register, value):
273
303
class LIS3DH_SPI (LIS3DH ):
274
304
"""Driver for the LIS3DH accelerometer connected over SPI."""
275
305
276
- def __init__ (self , spi , cs , baudrate = 100000 ):
306
+ def __init__ (self , spi , cs , * , baudrate = 100000 , int1 = None , int2 = None ):
277
307
import adafruit_bus_device .spi_device as spi_device
278
308
self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate )
279
309
self ._buffer = bytearray (6 )
280
- super ().__init__ ()
310
+ super ().__init__ (int1 = int1 , int2 = int2 )
281
311
282
312
def _read_register (self , register , length ):
283
313
if length == 1 :
0 commit comments