1616
1717import time
1818import math
19+ import digitalio
1920try :
2021 import struct
2122except ImportError :
3637REG_CTRL4 = const (0x23 )
3738REG_CTRL5 = const (0x24 )
3839REG_OUT_X_L = const (0x28 )
40+ REG_INT1SRC = const (0x31 )
3941REG_CLICKCFG = const (0x38 )
4042REG_CLICKSRC = const (0x39 )
4143REG_CLICKTHS = const (0x3A )
4850RANGE_8_G = const (0b10 ) # +/- 8g
4951RANGE_4_G = const (0b01 ) # +/- 4g
5052RANGE_2_G = const (0b00 ) # +/- 2g (default value)
53+ DATARATE_1344_HZ = const (0b1001 ) # 1.344 KHz
5154DATARATE_400_HZ = const (0b0111 ) # 400Hz
5255DATARATE_200_HZ = const (0b0110 ) # 200Hz
5356DATARATE_100_HZ = const (0b0101 ) # 100Hz
6366
6467class LIS3DH :
6568 """Driver base for the LIS3DH accelerometer."""
66- def __init__ (self ):
69+ def __init__ (self , int1 = None , int2 = None ):
6770 # Check device ID.
6871 device_id = self ._read_register_byte (REG_WHOAMI )
6972 if device_id != 0x33 :
7073 raise RuntimeError ('Failed to find LIS3DH!' )
74+ # Reboot
75+ self ._write_register_byte (REG_CTRL5 , 0x80 )
76+ time .sleep (0.01 ) # takes 5ms
7177 # Enable all axes, normal mode.
7278 self ._write_register_byte (REG_CTRL1 , 0x07 )
7379 # Set 400Hz data rate.
7480 self .data_rate = DATARATE_400_HZ
7581 # High res & BDU enabled.
7682 self ._write_register_byte (REG_CTRL4 , 0x88 )
77- # DRDY on INT1.
78- self ._write_register_byte (REG_CTRL3 , 0x10 )
7983 # Enable ADCs.
8084 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
8194
8295 @property
8396 def data_rate (self ):
@@ -105,7 +118,7 @@ def range(self):
105118 @range .setter
106119 def range (self , range_value ):
107120 ctl4 = self ._read_register_byte (REG_CTRL4 )
108- ctl4 &= ~ ( 0x30 )
121+ ctl4 &= ~ 0x30
109122 ctl4 |= range_value << 4
110123 self ._write_register_byte (REG_CTRL4 , ctl4 )
111124
@@ -125,7 +138,7 @@ def acceleration(self):
125138
126139 x , y , z = struct .unpack ('<hhh' , self ._read_register (REG_OUT_X_L | 0x80 , 6 ))
127140
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
129142
130143 def shake (self , shake_threshold = 30 , avg_count = 10 , total_delay = 0.1 ):
131144 """Detect when the accelerometer is shaken. Optional parameters:
@@ -184,8 +197,25 @@ def read_adc_mV(self, adc): # pylint: disable=invalid-name
184197 @property
185198 def tapped (self ):
186199 """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
189219 raw = self ._read_register_byte (REG_CLICKSRC )
190220 return raw & 0x40 > 0
191221
@@ -209,24 +239,24 @@ def set_tap(self, tap, threshold, *,
209239 raise ValueError ('Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!' )
210240 if threshold > 127 or threshold < 0 :
211241 raise ValueError ('Threshold out of range (0-127)' )
242+
243+ ctrl3 = self ._read_register_byte (REG_CTRL3 )
212244 if tap == 0 and click_cfg is None :
213245 # 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.
217247 self ._write_register_byte (REG_CLICKCFG , 0 )
218248 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 )
230260 self ._write_register_byte (REG_TIMELIMIT , time_limit )
231261 self ._write_register_byte (REG_TIMELATENCY , time_latency )
232262 self ._write_register_byte (REG_TIMEWINDOW , time_window )
@@ -250,11 +280,11 @@ def _write_register_byte(self, register, value):
250280class LIS3DH_I2C (LIS3DH ):
251281 """Driver for the LIS3DH accelerometer connected over I2C."""
252282
253- def __init__ (self , i2c , address = 0x18 ):
283+ def __init__ (self , i2c , * , address = 0x18 , int1 = None , int2 = None ):
254284 import adafruit_bus_device .i2c_device as i2c_device
255285 self ._i2c = i2c_device .I2CDevice (i2c , address )
256286 self ._buffer = bytearray (6 )
257- super ().__init__ ()
287+ super ().__init__ (int1 = int1 , int2 = int2 )
258288
259289 def _read_register (self , register , length ):
260290 self ._buffer [0 ] = register & 0xFF
@@ -273,11 +303,11 @@ def _write_register_byte(self, register, value):
273303class LIS3DH_SPI (LIS3DH ):
274304 """Driver for the LIS3DH accelerometer connected over SPI."""
275305
276- def __init__ (self , spi , cs , baudrate = 100000 ):
306+ def __init__ (self , spi , cs , * , baudrate = 100000 , int1 = None , int2 = None ):
277307 import adafruit_bus_device .spi_device as spi_device
278308 self ._spi = spi_device .SPIDevice (spi , cs , baudrate = baudrate )
279309 self ._buffer = bytearray (6 )
280- super ().__init__ ()
310+ super ().__init__ (int1 = int1 , int2 = int2 )
281311
282312 def _read_register (self , register , length ):
283313 if length == 1 :
0 commit comments