Skip to content

Updated to work with lis3dh int change #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 30, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 48 additions & 14 deletions adafruit_circuitplayground/express.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,21 @@ def __init__(self):

# Define acceleration:
self._i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
self._lis3dh = adafruit_lis3dh.LIS3DH_I2C(self._i2c, address=0x19)
self._int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
try:
self._lis3dh = adafruit_lis3dh.LIS3DH_I2C(self._i2c, address=0x19, int1=self._int1)
except TypeError:
self._lis3dh = adafruit_lis3dh.LIS3DH_I2C(self._i2c, address=0x19)
self._lis3dh.range = adafruit_lis3dh.RANGE_8_G

# Initialise tap:
self._last_tap = False
self._detect_taps = 1
self.detect_taps = 1

@property
def detect_taps(self):
"""Configure how many taps are used to set off the 'tapped' property!
"""Configure what type of tap is detected by ``cpx.tapped``. Use ``1`` for single-tap
detection and ``2`` for double-tap detection. This does nothing without ``cpx.tapped``.

.. image :: /_static/accelerometer.jpg
:alt: Accelerometer
Expand All @@ -137,42 +141,72 @@ def detect_taps(self):
cpx.detect_taps = 1
while True:
if cpx.tapped:
print("Single Tap detected!")
print("Single tap detected!")
"""
return self._detect_taps

@detect_taps.setter
def detect_taps(self, value):
self._detect_taps = value
try:
self._lis3dh.set_tap(value, 80, time_limit=4, time_latency=17, time_window=110)
if value == 1:
self._lis3dh.set_tap(value, 90, time_limit=4, time_latency=50, time_window=255)
if value == 2:
self._lis3dh.set_tap(value, 60, time_limit=10, time_latency=50, time_window=255)
except AttributeError:
pass
self._last_tap = False

@property
def tapped(self):
"""True once after a tap detection. use cpx.detect_taps to assign single (1) or
double (2) tap
"""True once after a detecting a tap. Requires ``cpx.detect_taps``.

.. image :: /_static/accelerometer.jpg
:alt: Accelerometer

Quickly tap the CPX twice to double-tap, or tap once for single-tap
Tap the CPX once for a single-tap, or quickly tap twice for a double-tap.

.. code-block:: python

from adafruit_circuitplayground.express import cpx

cpx.detect_taps = 1

while True:
if cpx.tapped:
print("Tapped!")
print("Single tap detected!")

To use single and double tap together, you must have a delay between them. It
will not function properly without it. This example uses both by counting a
specified number of each type of tap before moving on in the code.

.. code-block:: python

from adafruit_circuitplayground.express import cpx

# Set to check for single-taps.
cpx.detect_taps = 1
tap_count = 0

# We're looking for 2 single-taps before moving on.
while tap_count < 2:
if cpx.tapped:
tap_count += 1
print("Reached 2 single-taps!")

# Now switch to checking for double-taps
tap_count = 0
cpx.detect_taps = 2

# We're looking for 2 double-taps before moving on.
while tap_count < 2:
if cpx.tapped:
tap_count += 1
print("Reached 2 double-taps!")
print("Done.")

"""
try:
tapped = self._lis3dh.tapped
first_double_tap = tapped and not self._last_tap
self._last_tap = tapped
return first_double_tap
return self._lis3dh.tapped
except AttributeError:
raise RuntimeError("Oops! You need a newer version of CircuitPython "
"(2.2.0 or greater) to use this feature.")
Expand Down