Skip to content

Commit 9679cd8

Browse files
authored
Merge pull request #27 from kattni/tap-detect-update
Updated to work with lis3dh int change
2 parents 6fc77d3 + bef9447 commit 9679cd8

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

adafruit_circuitplayground/express.py

+48-14
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,21 @@ def __init__(self):
115115

116116
# Define acceleration:
117117
self._i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
118-
self._lis3dh = adafruit_lis3dh.LIS3DH_I2C(self._i2c, address=0x19)
118+
self._int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
119+
try:
120+
self._lis3dh = adafruit_lis3dh.LIS3DH_I2C(self._i2c, address=0x19, int1=self._int1)
121+
except TypeError:
122+
self._lis3dh = adafruit_lis3dh.LIS3DH_I2C(self._i2c, address=0x19)
119123
self._lis3dh.range = adafruit_lis3dh.RANGE_8_G
120124

121125
# Initialise tap:
122-
self._last_tap = False
123126
self._detect_taps = 1
124127
self.detect_taps = 1
125128

126129
@property
127130
def detect_taps(self):
128-
"""Configure how many taps are used to set off the 'tapped' property!
131+
"""Configure what type of tap is detected by ``cpx.tapped``. Use ``1`` for single-tap
132+
detection and ``2`` for double-tap detection. This does nothing without ``cpx.tapped``.
129133
130134
.. image :: /_static/accelerometer.jpg
131135
:alt: Accelerometer
@@ -137,42 +141,72 @@ def detect_taps(self):
137141
cpx.detect_taps = 1
138142
while True:
139143
if cpx.tapped:
140-
print("Single Tap detected!")
144+
print("Single tap detected!")
141145
"""
142146
return self._detect_taps
143147

144148
@detect_taps.setter
145149
def detect_taps(self, value):
146150
self._detect_taps = value
147151
try:
148-
self._lis3dh.set_tap(value, 80, time_limit=4, time_latency=17, time_window=110)
152+
if value == 1:
153+
self._lis3dh.set_tap(value, 90, time_limit=4, time_latency=50, time_window=255)
154+
if value == 2:
155+
self._lis3dh.set_tap(value, 60, time_limit=10, time_latency=50, time_window=255)
149156
except AttributeError:
150157
pass
151-
self._last_tap = False
152158

153159
@property
154160
def tapped(self):
155-
"""True once after a tap detection. use cpx.detect_taps to assign single (1) or
156-
double (2) tap
161+
"""True once after a detecting a tap. Requires ``cpx.detect_taps``.
157162
158163
.. image :: /_static/accelerometer.jpg
159164
:alt: Accelerometer
160165
161-
Quickly tap the CPX twice to double-tap, or tap once for single-tap
166+
Tap the CPX once for a single-tap, or quickly tap twice for a double-tap.
162167
163168
.. code-block:: python
164169
165170
from adafruit_circuitplayground.express import cpx
166171
172+
cpx.detect_taps = 1
173+
167174
while True:
168175
if cpx.tapped:
169-
print("Tapped!")
176+
print("Single tap detected!")
177+
178+
To use single and double tap together, you must have a delay between them. It
179+
will not function properly without it. This example uses both by counting a
180+
specified number of each type of tap before moving on in the code.
181+
182+
.. code-block:: python
183+
184+
from adafruit_circuitplayground.express import cpx
185+
186+
# Set to check for single-taps.
187+
cpx.detect_taps = 1
188+
tap_count = 0
189+
190+
# We're looking for 2 single-taps before moving on.
191+
while tap_count < 2:
192+
if cpx.tapped:
193+
tap_count += 1
194+
print("Reached 2 single-taps!")
195+
196+
# Now switch to checking for double-taps
197+
tap_count = 0
198+
cpx.detect_taps = 2
199+
200+
# We're looking for 2 double-taps before moving on.
201+
while tap_count < 2:
202+
if cpx.tapped:
203+
tap_count += 1
204+
print("Reached 2 double-taps!")
205+
print("Done.")
206+
170207
"""
171208
try:
172-
tapped = self._lis3dh.tapped
173-
first_double_tap = tapped and not self._last_tap
174-
self._last_tap = tapped
175-
return first_double_tap
209+
return self._lis3dh.tapped
176210
except AttributeError:
177211
raise RuntimeError("Oops! You need a newer version of CircuitPython "
178212
"(2.2.0 or greater) to use this feature.")

0 commit comments

Comments
 (0)