Skip to content

Commit d5491cd

Browse files
authored
Merge pull request #32 from kattni/update-tap-example
Update tap example to use interrupt. See comments.
2 parents f8f1915 + 00dfed9 commit d5491cd

File tree

1 file changed

+18
-20
lines changed

1 file changed

+18
-20
lines changed

examples/tap.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,36 @@
44
# Author: Tony DiCola
55
import board
66
import adafruit_lis3dh
7+
import busio
8+
import digitalio
79

810

911
# Uncomment _one_ of the hardware setups below depending on your wiring:
1012

1113
# Hardware I2C setup. Use the CircuitPlayground built-in accelerometer if available;
1214
# otherwise check I2C pins.
13-
import busio
1415
if hasattr(board, 'ACCELEROMETER_SCL'):
1516
i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
16-
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19)
17+
int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
18+
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, address=0x19, int1=int1)
1719
else:
1820
i2c = busio.I2C(board.SCL, board.SDA)
19-
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
21+
int1 = digitalio.DigitalInOut(board.D10) # Set this to the correct pin for the interrupt!
22+
lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c, int1=int1)
2023

2124
# Software I2C setup:
22-
#import bitbangio
23-
#i2c = bitbangio.I2C(board.SCL, board.SDA)
24-
#lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
25+
# import bitbangio
26+
# i2c = bitbangio.I2C(board.SCL, board.SDA)
27+
# lis3dh = adafruit_lis3dh.LIS3DH_I2C(i2c)
2528

2629
# Hardware SPI setup:
27-
#import busio
28-
#spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
29-
#cs = busio.DigitalInOut(board.D6) # Set to appropriate CS pin!
30-
#lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs)
31-
30+
# import busio
31+
# spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
32+
# cs = busio.DigitalInOut(board.D6) # Set to appropriate CS pin!
33+
# lis3dh = adafruit_lis3dh.LIS3DH_SPI(spi, cs)
3234

3335
# Set range of accelerometer (can be RANGE_2_G, RANGE_4_G, RANGE_8_G or RANGE_16_G).
34-
lis3dh.range = adafruit_lis3dh.RANGE_2_G
36+
lis3dh.range = adafruit_lis3dh.RANGE_8_G
3537

3638
# Set tap detection to double taps. The first parameter is a value:
3739
# - 0 = Disable tap detection.
@@ -43,13 +45,9 @@
4345
# - 4G = 20-40 threshold
4446
# - 8G = 10-20 threshold
4547
# - 16G = 5-10 threshold
46-
lis3dh.set_tap(2, 80)
48+
lis3dh.set_tap(2, 60)
4749

48-
# Loop forever printing if a double tap is detected. A single double tap may cause multiple
49-
# `tapped` reads to return true so we make sure the last once was False.
50-
last_tap = False
50+
# Loop forever printing if a double tap is detected.
5151
while True:
52-
tap = lis3dh.tapped
53-
if tap and not last_tap:
54-
print('Double tap!')
55-
last_tap = tap
52+
if lis3dh.tapped:
53+
print('Tapped!')

0 commit comments

Comments
 (0)