From 9fdbdb1f90d495fb827f00eb46385d20674a445d Mon Sep 17 00:00:00 2001 From: Kattni Date: Thu, 28 Dec 2017 23:41:10 -0500 Subject: [PATCH 1/3] Update to use int1 for tap detection --- adafruit_lis3dh.py | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/adafruit_lis3dh.py b/adafruit_lis3dh.py index 5ca304b..ab4fe9a 100755 --- a/adafruit_lis3dh.py +++ b/adafruit_lis3dh.py @@ -16,6 +16,7 @@ import time import math +import digitalio try: import struct except ImportError: @@ -63,7 +64,7 @@ class LIS3DH: """Driver base for the LIS3DH accelerometer.""" - def __init__(self): + def __init__(self, int1=None, int2=None): # Check device ID. device_id = self._read_register_byte(REG_WHOAMI) if device_id != 0x33: @@ -74,10 +75,15 @@ def __init__(self): self.data_rate = DATARATE_400_HZ # High res & BDU enabled. self._write_register_byte(REG_CTRL4, 0x88) - # DRDY on INT1. - self._write_register_byte(REG_CTRL3, 0x10) # Enable ADCs. self._write_register_byte(REG_TEMPCFG, 0x80) + # Initialise interrupt pins + self._int1 = int1 + self._int2 = int2 + if self._int1: + self._int1.direction = digitalio.Direction.INPUT + self._int1.pull = digitalio.Pull.UP + self._write_register_byte(REG_CTRL3, 0x80) # Turn on int1 click. @property def data_rate(self): @@ -105,7 +111,7 @@ def range(self): @range.setter def range(self, range_value): ctl4 = self._read_register_byte(REG_CTRL4) - ctl4 &= ~(0x30) + ctl4 &= ~0x30 ctl4 |= range_value << 4 self._write_register_byte(REG_CTRL4, ctl4) @@ -125,7 +131,7 @@ def acceleration(self): x, y, z = struct.unpack(' 0 @@ -216,9 +225,6 @@ def set_tap(self, tap, threshold, *, self._write_register_byte(REG_CTRL3, r) self._write_register_byte(REG_CLICKCFG, 0) return - # Else enable click with specified parameters. - self._write_register_byte(REG_CTRL3, 0x80) # Turn on int1 click. - self._write_register_byte(REG_CTRL5, 0x08) # Latch interrupt on int1. if click_cfg is not None: # Custom click configuration register value specified, use it. self._write_register_byte(REG_CLICKCFG, click_cfg) @@ -226,7 +232,9 @@ def set_tap(self, tap, threshold, *, self._write_register_byte(REG_CLICKCFG, 0x15) # Turn on all axes & singletap. elif tap == 2: self._write_register_byte(REG_CLICKCFG, 0x2A) # Turn on all axes & doubletap. - self._write_register_byte(REG_CLICKTHS, threshold | 0x80) + if self._int1: + threshold |= 0x80 # latch click only if read with interrupt pin + self._write_register_byte(REG_CLICKTHS, threshold) self._write_register_byte(REG_TIMELIMIT, time_limit) self._write_register_byte(REG_TIMELATENCY, time_latency) self._write_register_byte(REG_TIMEWINDOW, time_window) @@ -250,11 +258,11 @@ def _write_register_byte(self, register, value): class LIS3DH_I2C(LIS3DH): """Driver for the LIS3DH accelerometer connected over I2C.""" - def __init__(self, i2c, address=0x18): + def __init__(self, i2c, *, address=0x18, int1=None, int2=None): import adafruit_bus_device.i2c_device as i2c_device self._i2c = i2c_device.I2CDevice(i2c, address) self._buffer = bytearray(6) - super().__init__() + super().__init__(int1=int1, int2=int2) def _read_register(self, register, length): self._buffer[0] = register & 0xFF @@ -273,11 +281,11 @@ def _write_register_byte(self, register, value): class LIS3DH_SPI(LIS3DH): """Driver for the LIS3DH accelerometer connected over SPI.""" - def __init__(self, spi, cs, baudrate=100000): + def __init__(self, spi, cs, *, baudrate=100000, int1=None, int2=None): import adafruit_bus_device.spi_device as spi_device self._spi = spi_device.SPIDevice(spi, cs, baudrate=baudrate) self._buffer = bytearray(6) - super().__init__() + super().__init__(int1=int1, int2=int2) def _read_register(self, register, length): if length == 1: From d8e170b98a86b60bc95eb50d9fbb57e6e16dc921 Mon Sep 17 00:00:00 2001 From: Kattni Date: Thu, 28 Dec 2017 23:52:22 -0500 Subject: [PATCH 2/3] Updates tapped with int pin example --- adafruit_lis3dh.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/adafruit_lis3dh.py b/adafruit_lis3dh.py index ab4fe9a..4066e18 100755 --- a/adafruit_lis3dh.py +++ b/adafruit_lis3dh.py @@ -190,9 +190,23 @@ def read_adc_mV(self, adc): # pylint: disable=invalid-name @property def tapped(self): """True if a tap was detected recently. Whether its a single tap or double tap is - determined by the tap param on `set_tap`. `tapped` may be True over multiple reads - even if only a single tap or single double tap occurred if the interrupt (int) - pin is not specified.""" + determined by the tap param on ``set_tap``. ``tapped`` may be True over + multiple reads even if only a single tap or single double tap occurred if the + interrupt (int) pin is not specified. + + The following example uses ``i2c`` and specifies the interrupt pin: + + .. code-block:: python + + import adafruit_lis3dh + import digitalio + + i2c = busio.I2C(board.SCL, board.SDA) + int1 = digitalio.DigitalInOut(board.D11) # pin connected to interrupt + lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1) + lis3dh.range = adafruit_lis3dh.RANGE_8_G + + """ if self._int1 and not self._int1.value: return False raw = self._read_register_byte(REG_CLICKSRC) From cadf4004f88da3a3b8b7aad569dbf02088cdf20d Mon Sep 17 00:00:00 2001 From: Kattni Date: Fri, 29 Dec 2017 20:17:44 -0500 Subject: [PATCH 3/3] Updates including latching interrupt --- adafruit_lis3dh.py | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/adafruit_lis3dh.py b/adafruit_lis3dh.py index 4066e18..b318866 100755 --- a/adafruit_lis3dh.py +++ b/adafruit_lis3dh.py @@ -37,6 +37,7 @@ REG_CTRL4 = const(0x23) REG_CTRL5 = const(0x24) REG_OUT_X_L = const(0x28) +REG_INT1SRC = const(0x31) REG_CLICKCFG = const(0x38) REG_CLICKSRC = const(0x39) REG_CLICKTHS = const(0x3A) @@ -49,6 +50,7 @@ RANGE_8_G = const(0b10) # +/- 8g RANGE_4_G = const(0b01) # +/- 4g RANGE_2_G = const(0b00) # +/- 2g (default value) +DATARATE_1344_HZ = const(0b1001) # 1.344 KHz DATARATE_400_HZ = const(0b0111) # 400Hz DATARATE_200_HZ = const(0b0110) # 200Hz DATARATE_100_HZ = const(0b0101) # 100Hz @@ -69,6 +71,9 @@ def __init__(self, int1=None, int2=None): device_id = self._read_register_byte(REG_WHOAMI) if device_id != 0x33: raise RuntimeError('Failed to find LIS3DH!') + # Reboot + self._write_register_byte(REG_CTRL5, 0x80) + time.sleep(0.01) # takes 5ms # Enable all axes, normal mode. self._write_register_byte(REG_CTRL1, 0x07) # Set 400Hz data rate. @@ -77,13 +82,15 @@ def __init__(self, int1=None, int2=None): self._write_register_byte(REG_CTRL4, 0x88) # Enable ADCs. self._write_register_byte(REG_TEMPCFG, 0x80) + # Latch interrupt for INT1 + self._write_register_byte(REG_CTRL5, 0x08) + # Initialise interrupt pins self._int1 = int1 self._int2 = int2 if self._int1: self._int1.direction = digitalio.Direction.INPUT self._int1.pull = digitalio.Pull.UP - self._write_register_byte(REG_CTRL3, 0x80) # Turn on int1 click. @property def data_rate(self): @@ -232,23 +239,24 @@ def set_tap(self, tap, threshold, *, raise ValueError('Tap must be 0 (disabled), 1 (single tap), or 2 (double tap)!') if threshold > 127 or threshold < 0: raise ValueError('Threshold out of range (0-127)') + + ctrl3 = self._read_register_byte(REG_CTRL3) if tap == 0 and click_cfg is None: # Disable click interrupt. - r = self._read_register_byte(REG_CTRL3) - r &= ~(0x80) # Turn off I1_CLICK. - self._write_register_byte(REG_CTRL3, r) + self._write_register_byte(REG_CTRL3, ctrl3 & ~(0x80)) # Turn off I1_CLICK. self._write_register_byte(REG_CLICKCFG, 0) return - if click_cfg is not None: - # Custom click configuration register value specified, use it. - self._write_register_byte(REG_CLICKCFG, click_cfg) - elif tap == 1: - self._write_register_byte(REG_CLICKCFG, 0x15) # Turn on all axes & singletap. - elif tap == 2: - self._write_register_byte(REG_CLICKCFG, 0x2A) # Turn on all axes & doubletap. - if self._int1: - threshold |= 0x80 # latch click only if read with interrupt pin - self._write_register_byte(REG_CLICKTHS, threshold) + else: + self._write_register_byte(REG_CTRL3, ctrl3 | 0x80) # Turn on int1 click output + + if click_cfg is None: + if tap == 1: + click_cfg = 0x15 # Turn on all axes & singletap. + if tap == 2: + click_cfg = 0x2A # Turn on all axes & doubletap. + # Or, if a custom click configuration register value specified, use it. + self._write_register_byte(REG_CLICKCFG, click_cfg) + self._write_register_byte(REG_CLICKTHS, 0x80 | threshold) self._write_register_byte(REG_TIMELIMIT, time_limit) self._write_register_byte(REG_TIMELATENCY, time_latency) self._write_register_byte(REG_TIMEWINDOW, time_window)