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