Skip to content

Commit aba4f1d

Browse files
committed
Updated to work with lis3dh int change
Improved documentation, included example of using single-tap and double-tap detection to clarify the need for a delay when using both together.
1 parent 6fc77d3 commit aba4f1d

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

adafruit_circuitplayground/express.py

+42-14
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,18 @@ 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+
self._lis3dh = adafruit_lis3dh.LIS3DH_I2C(self._i2c, address=0x19, int1=self._int1)
119120
self._lis3dh.range = adafruit_lis3dh.RANGE_8_G
120121

121122
# Initialise tap:
122-
self._last_tap = False
123123
self._detect_taps = 1
124124
self.detect_taps = 1
125125

126126
@property
127127
def detect_taps(self):
128-
"""Configure how many taps are used to set off the 'tapped' property!
128+
"""Configure what type of tap is detected by ``cpx.tapped``. Use ``1`` for single-tap
129+
detection and ``2`` for double-tap detection. This does nothing without ``cpx.tapped``.
129130
130131
.. image :: /_static/accelerometer.jpg
131132
:alt: Accelerometer
@@ -137,42 +138,69 @@ def detect_taps(self):
137138
cpx.detect_taps = 1
138139
while True:
139140
if cpx.tapped:
140-
print("Single Tap detected!")
141+
print("Single tap detected!")
141142
"""
142143
return self._detect_taps
143144

144145
@detect_taps.setter
145146
def detect_taps(self, value):
146147
self._detect_taps = value
147148
try:
148-
self._lis3dh.set_tap(value, 80, time_limit=4, time_latency=17, time_window=110)
149+
self._lis3dh.set_tap(value, 90, time_limit=2, time_latency=50, time_window=255)
149150
except AttributeError:
150151
pass
151-
self._last_tap = False
152152

153153
@property
154154
def tapped(self):
155-
"""True once after a tap detection. use cpx.detect_taps to assign single (1) or
156-
double (2) tap
155+
"""True once after a detecting a tap. Requires ``cpx.detect_taps``.
157156
158157
.. image :: /_static/accelerometer.jpg
159158
:alt: Accelerometer
160159
161-
Quickly tap the CPX twice to double-tap, or tap once for single-tap
160+
Tap the CPX once for a single-tap, or quickly tap twice for a double-tap.
162161
163162
.. code-block:: python
164163
165164
from adafruit_circuitplayground.express import cpx
166165
166+
cpx.detect_taps = 1
167+
167168
while True:
168169
if cpx.tapped:
169-
print("Tapped!")
170+
print("Single tap detected!")
171+
172+
To use single and double tap together, you must have a delay between them. It
173+
will not function properly without it. This example uses both by counting a
174+
specified number of each type of tap before moving on in the code.
175+
176+
.. code-block:: python
177+
178+
from adafruit_circuitplayground.express import cpx
179+
180+
# Set to check for single-taps.
181+
cpx.detect_taps = 1
182+
tap_count = 0
183+
184+
# We're looking for 2 single-taps before moving on.
185+
while tap_count < 2:
186+
if cpx.tapped:
187+
tap_count += 1
188+
print("Reached 2 single-taps!")
189+
190+
# Now switch to checking for double-taps
191+
tap_count = 0
192+
cpx.detect_taps = 2
193+
194+
# We're looking for 2 double-taps before moving on.
195+
while tap_count < 2:
196+
if cpx.tapped:
197+
tap_count += 1
198+
print("Reached 2 double-taps!")
199+
print("Done.")
200+
170201
"""
171202
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
203+
return self._lis3dh.tapped
176204
except AttributeError:
177205
raise RuntimeError("Oops! You need a newer version of CircuitPython "
178206
"(2.2.0 or greater) to use this feature.")

0 commit comments

Comments
 (0)