@@ -160,48 +160,81 @@ def detect_taps(self):
160
160
"""
161
161
return self ._detect_taps
162
162
163
- @detect_taps .setter
164
- def detect_taps (self , value ):
165
- self ._detect_taps = value
163
+ @staticmethod
164
+ def _default_tap_threshold (tap ):
166
165
if (
167
166
"nRF52840" in os .uname ().machine
168
167
): # If we're on a CPB, use a higher tap threshold
169
- if value == 1 :
170
- self ._lis3dh .set_tap (
171
- value , 100 , time_limit = 4 , time_latency = 50 , time_window = 255
172
- )
173
- if value == 2 :
174
- self ._lis3dh .set_tap (
175
- value , 70 , time_limit = 10 , time_latency = 50 , time_window = 255
176
- )
177
- else : # If we're on a CPX
178
- if value == 1 :
179
- self ._lis3dh .set_tap (
180
- value , 90 , time_limit = 4 , time_latency = 50 , time_window = 255
181
- )
182
- if value == 2 :
183
- self ._lis3dh .set_tap (
184
- value , 60 , time_limit = 10 , time_latency = 50 , time_window = 255
185
- )
186
-
187
- # pylint: disable-msg=too-many-arguments
188
- # Many default arguments which will probably not need changing in most cases
189
- def configure_taps (
168
+ return 100 if tap == 1 else 70
169
+
170
+ # If we're on a CPX
171
+ return 90 if tap == 1 else 60
172
+
173
+ @detect_taps .setter
174
+ def detect_taps (self , value ):
175
+ self ._detect_taps = value
176
+ if value == 1 :
177
+ self ._lis3dh .set_tap (
178
+ value ,
179
+ self ._default_tap_threshold (value ),
180
+ time_limit = 4 ,
181
+ time_latency = 50 ,
182
+ time_window = 255 ,
183
+ )
184
+ if value == 2 :
185
+ self ._lis3dh .set_tap (
186
+ value ,
187
+ self ._default_tap_threshold (value ),
188
+ time_limit = 10 ,
189
+ time_latency = 50 ,
190
+ time_window = 255 ,
191
+ )
192
+
193
+ def configure_tap ( # pylint: disable-msg=too-many-arguments
190
194
self ,
191
- value ,
195
+ tap ,
192
196
accel_range = adafruit_lis3dh .RANGE_8_G ,
193
- theshold = None ,
197
+ threshold = None ,
194
198
time_limit = None ,
195
199
time_latency = 50 ,
196
200
time_window = 255 ,
197
201
):
198
- """ A way to customize the tap detection better. The default values don't work
199
- for all cases.
200
- Higher default thresholds for the CPB
202
+ """ Granular configuration of tap parameters. Expose the power of the
203
+ adafruit_lis3dh module.
204
+
205
+ :param int tap: 0 to disable tap detection, 1 to detect only single
206
+ taps, and 2 to detect only double taps.
207
+ :param int accel_range: Takes the defined values from the adafruit_lis3dh
208
+ module [ RANGE_2_G, RANGE_4_G, RANGE_8_G, RANGE_16_G ]
209
+ (default sets the same value as the *detect_taps* setter)
210
+ :param int threshold: A threshold for the tap detection. The higher the value
211
+ the less sensitive the detection. This changes based on the
212
+ accelerometer range. Good values are 5-10 for 16G, 10-20
213
+ for 8G, 20-40 for 4G, and 40-80 for 2G.
214
+ (default sets the same value as the *detect_taps* setter)
215
+ :param int time_limit: TIME_LIMIT register value
216
+ (default sets the same value as the *detect_taps* setter)
217
+ :param int time_latency: TIME_LATENCY register value (default 50).
218
+ :param int time_window: TIME_WINDOW register value (default 255).
219
+
220
+ To use with the Circuit Playground Express or Bluefruit:
221
+
222
+ .. code-block:: python
223
+
224
+ from adafruit_circuitplayground import cp
225
+ import adafruit_lis3dh
226
+
227
+ cp.configure_tap(1, accel_range=adafruit_lis3dh.RANGE_2_G, threshold=50)
228
+ while True:
229
+ if cp.tapped:
230
+ print("Single tap detected!")
231
+
201
232
"""
202
- if value < 0 or value > 2 :
233
+ if tap < 0 or tap > 2 :
203
234
return
204
235
236
+ self ._detect_taps = tap
237
+
205
238
if accel_range not in [
206
239
adafruit_lis3dh .RANGE_2_G ,
207
240
adafruit_lis3dh .RANGE_4_G ,
@@ -211,31 +244,29 @@ def configure_taps(
211
244
accel_range = adafruit_lis3dh .RANGE_8_G
212
245
self ._lis3dh .range = accel_range
213
246
214
- if value == 1 :
215
- if theshold is None or theshold < 0 or theshold > 127 :
216
- theshold = 100 if "nRF52840" in os . uname (). machine else 90
247
+ if tap == 1 :
248
+ if threshold is None or threshold < 0 or threshold > 127 :
249
+ threshold = self . _default_tap_threshold ( tap )
217
250
if time_limit is None :
218
251
time_limit = 4
219
- elif value == 2 :
220
- if theshold is None or theshold < 0 or theshold > 127 :
221
- theshold = 70 if "nRF52840" in os . uname (). machine else 60
252
+ elif tap == 2 :
253
+ if threshold is None or threshold < 0 or threshold > 127 :
254
+ threshold = self . _default_tap_threshold ( tap )
222
255
if time_limit is None :
223
256
time_limit = 10
224
257
else :
225
- # sane values for turning the tap detection off
226
- theshold = 100
258
+ # reasonable values for turning the tap detection off
259
+ threshold = 100
227
260
time_limit = 1
228
261
229
262
self ._lis3dh .set_tap (
230
- value ,
231
- theshold ,
263
+ tap ,
264
+ threshold ,
232
265
time_limit = time_limit ,
233
266
time_latency = time_latency ,
234
267
time_window = time_window ,
235
268
)
236
269
237
- # pylint: enable-msg=too-many-arguments
238
-
239
270
@property
240
271
def tapped (self ):
241
272
"""True once after a detecting a tap. Requires ``cp.detect_taps``.
0 commit comments