Skip to content

Commit 3b88caa

Browse files
authored
Merge pull request #99 from mw46d/main
CP: more configuration options for the tap detection
2 parents d7dd9ea + 6100110 commit 3b88caa

File tree

3 files changed

+105
-20
lines changed

3 files changed

+105
-20
lines changed

adafruit_circuitplayground/circuit_playground_base.py

Lines changed: 103 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -142,29 +142,112 @@ def detect_taps(self):
142142
"""
143143
return self._detect_taps
144144

145-
@detect_taps.setter
146-
def detect_taps(self, value):
147-
self._detect_taps = value
145+
@staticmethod
146+
def _default_tap_threshold(tap):
148147
if (
149148
"nRF52840" in os.uname().machine
150149
): # If we're on a CPB, use a higher tap threshold
151-
if value == 1:
152-
self._lis3dh.set_tap(
153-
value, 100, time_limit=4, time_latency=50, time_window=255
154-
)
155-
if value == 2:
156-
self._lis3dh.set_tap(
157-
value, 70, time_limit=10, time_latency=50, time_window=255
158-
)
159-
else: # If we're on a CPX
160-
if value == 1:
161-
self._lis3dh.set_tap(
162-
value, 90, time_limit=4, time_latency=50, time_window=255
163-
)
164-
if value == 2:
165-
self._lis3dh.set_tap(
166-
value, 60, time_limit=10, time_latency=50, time_window=255
167-
)
150+
return 100 if tap == 1 else 70
151+
152+
# If we're on a CPX
153+
return 90 if tap == 1 else 60
154+
155+
@detect_taps.setter
156+
def detect_taps(self, value):
157+
self._detect_taps = value
158+
if value == 1:
159+
self._lis3dh.set_tap(
160+
value,
161+
self._default_tap_threshold(value),
162+
time_limit=4,
163+
time_latency=50,
164+
time_window=255,
165+
)
166+
if value == 2:
167+
self._lis3dh.set_tap(
168+
value,
169+
self._default_tap_threshold(value),
170+
time_limit=10,
171+
time_latency=50,
172+
time_window=255,
173+
)
174+
175+
def configure_tap( # pylint: disable-msg=too-many-arguments
176+
self,
177+
tap,
178+
accel_range=adafruit_lis3dh.RANGE_8_G,
179+
threshold=None,
180+
time_limit=None,
181+
time_latency=50,
182+
time_window=255,
183+
):
184+
"""Granular configuration of tap parameters. Expose the power of the
185+
adafruit_lis3dh module.
186+
187+
:param int tap: 0 to disable tap detection, 1 to detect only single
188+
taps, and 2 to detect only double taps.
189+
:param int accel_range: Takes the defined values from the adafruit_lis3dh
190+
module [ RANGE_2_G, RANGE_4_G, RANGE_8_G, RANGE_16_G ]
191+
(default sets the same value as the *detect_taps* setter)
192+
:param int threshold: A threshold for the tap detection. The higher the value
193+
the less sensitive the detection. This changes based on the
194+
accelerometer range. Good values are 5-10 for 16G, 10-20
195+
for 8G, 20-40 for 4G, and 40-80 for 2G.
196+
(default sets the same value as the *detect_taps* setter)
197+
:param int time_limit: TIME_LIMIT register value
198+
(default sets the same value as the *detect_taps* setter)
199+
:param int time_latency: TIME_LATENCY register value (default 50).
200+
:param int time_window: TIME_WINDOW register value (default 255).
201+
202+
To use with the Circuit Playground Express or Bluefruit:
203+
204+
.. code-block:: python
205+
206+
from adafruit_circuitplayground import cp
207+
import adafruit_lis3dh
208+
209+
cp.configure_tap(1, accel_range=adafruit_lis3dh.RANGE_2_G, threshold=50)
210+
while True:
211+
if cp.tapped:
212+
print("Single tap detected!")
213+
214+
"""
215+
if tap < 0 or tap > 2:
216+
return
217+
218+
self._detect_taps = tap
219+
220+
if accel_range not in [
221+
adafruit_lis3dh.RANGE_2_G,
222+
adafruit_lis3dh.RANGE_4_G,
223+
adafruit_lis3dh.RANGE_8_G,
224+
adafruit_lis3dh.RANGE_16_G,
225+
]:
226+
accel_range = adafruit_lis3dh.RANGE_8_G
227+
self._lis3dh.range = accel_range
228+
229+
if tap == 1:
230+
if threshold is None or threshold < 0 or threshold > 127:
231+
threshold = self._default_tap_threshold(tap)
232+
if time_limit is None:
233+
time_limit = 4
234+
elif tap == 2:
235+
if threshold is None or threshold < 0 or threshold > 127:
236+
threshold = self._default_tap_threshold(tap)
237+
if time_limit is None:
238+
time_limit = 10
239+
else:
240+
# reasonable values for turning the tap detection off
241+
threshold = 100
242+
time_limit = 1
243+
244+
self._lis3dh.set_tap(
245+
tap,
246+
threshold,
247+
time_limit=time_limit,
248+
time_latency=time_latency,
249+
time_window=time_window,
250+
)
168251

169252
@property
170253
def tapped(self):

examples/circuitplayground_ir_receive.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
raise NotImplementedError(
2222
"This example does not work with Circuit Playground Bluefruti!"
2323
) from err
24+
2425
# Create a decoder that will take pulses and turn them into numbers
2526
decoder = adafruit_irremote.GenericDecode()
2627

examples/circuitplayground_ir_transmit.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
raise NotImplementedError(
2323
"This example does not work with Circuit Playground Bluefruit!"
2424
) from err
25+
2526
pulseout = pulseio.PulseOut(pwm) # pylint: disable=no-member
2627
# Create an encoder that will take numbers and turn them into NEC IR pulses
2728
encoder = adafruit_irremote.GenericTransmit(

0 commit comments

Comments
 (0)