@@ -115,17 +115,18 @@ def __init__(self):
115
115
116
116
# Define acceleration:
117
117
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 )
119
120
self ._lis3dh .range = adafruit_lis3dh .RANGE_8_G
120
121
121
122
# Initialise tap:
122
- self ._last_tap = False
123
123
self ._detect_taps = 1
124
124
self .detect_taps = 1
125
125
126
126
@property
127
127
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``.
129
130
130
131
.. image :: /_static/accelerometer.jpg
131
132
:alt: Accelerometer
@@ -137,42 +138,69 @@ def detect_taps(self):
137
138
cpx.detect_taps = 1
138
139
while True:
139
140
if cpx.tapped:
140
- print("Single Tap detected!")
141
+ print("Single tap detected!")
141
142
"""
142
143
return self ._detect_taps
143
144
144
145
@detect_taps .setter
145
146
def detect_taps (self , value ):
146
147
self ._detect_taps = value
147
148
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 )
149
150
except AttributeError :
150
151
pass
151
- self ._last_tap = False
152
152
153
153
@property
154
154
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``.
157
156
158
157
.. image :: /_static/accelerometer.jpg
159
158
:alt: Accelerometer
160
159
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.
162
161
163
162
.. code-block:: python
164
163
165
164
from adafruit_circuitplayground.express import cpx
166
165
166
+ cpx.detect_taps = 1
167
+
167
168
while True:
168
169
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
+
170
201
"""
171
202
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
176
204
except AttributeError :
177
205
raise RuntimeError ("Oops! You need a newer version of CircuitPython "
178
206
"(2.2.0 or greater) to use this feature." )
0 commit comments