Skip to content

Commit b3c63eb

Browse files
authored
Merge pull request #20 from kattni/touch-threshold
Added ability to change touch threshold
2 parents 0535d1c + c72c8d9 commit b3c63eb

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

_static/capacitive_touch_pads.jpg

742 KB
Loading

adafruit_circuitplayground/express.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ def light(self):
6666
"""Light level in SI Lux."""
6767
return self._photocell.value * 330 // (2 ** 16)
6868

69-
class Express:
69+
70+
class Express: # pylint: disable=too-many-public-methods
7071
"""Represents a single CircuitPlayground Express. Do not use more than one at
7172
a time."""
7273
def __init__(self):
@@ -110,6 +111,7 @@ def __init__(self):
110111
self._touch_A5 = None
111112
self._touch_A6 = None
112113
self._touch_A7 = None
114+
self._touch_threshold_adjustment = 0
113115

114116
# Define acceleration:
115117
self._i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
@@ -188,6 +190,7 @@ def touch_A1(self): # pylint: disable=invalid-name
188190
"""
189191
if self._touch_A1 is None:
190192
self._touch_A1 = touchio.TouchIn(board.A1)
193+
self._touch_A1.threshold += self._touch_threshold_adjustment
191194
return self._touch_A1.value
192195

193196
@property
@@ -207,6 +210,7 @@ def touch_A2(self): # pylint: disable=invalid-name
207210
"""
208211
if self._touch_A2 is None:
209212
self._touch_A2 = touchio.TouchIn(board.A2)
213+
self._touch_A2.threshold += self._touch_threshold_adjustment
210214
return self._touch_A2.value
211215

212216
@property
@@ -226,6 +230,7 @@ def touch_A3(self): # pylint: disable=invalid-name
226230
"""
227231
if self._touch_A3 is None:
228232
self._touch_A3 = touchio.TouchIn(board.A3)
233+
self._touch_A3.threshold += self._touch_threshold_adjustment
229234
return self._touch_A3.value
230235

231236
@property
@@ -245,6 +250,7 @@ def touch_A4(self): # pylint: disable=invalid-name
245250
"""
246251
if self._touch_A4 is None:
247252
self._touch_A4 = touchio.TouchIn(board.A4)
253+
self._touch_A4.threshold += self._touch_threshold_adjustment
248254
return self._touch_A4.value
249255

250256
@property
@@ -264,6 +270,7 @@ def touch_A5(self): # pylint: disable=invalid-name
264270
"""
265271
if self._touch_A5 is None:
266272
self._touch_A5 = touchio.TouchIn(board.A5)
273+
self._touch_A5.threshold += self._touch_threshold_adjustment
267274
return self._touch_A5.value
268275

269276
@property
@@ -283,6 +290,7 @@ def touch_A6(self): # pylint: disable=invalid-name
283290
"""
284291
if self._touch_A6 is None:
285292
self._touch_A6 = touchio.TouchIn(board.A6)
293+
self._touch_A6.threshold += self._touch_threshold_adjustment
286294
return self._touch_A6.value
287295

288296
@property
@@ -302,8 +310,34 @@ def touch_A7(self): # pylint: disable=invalid-name
302310
"""
303311
if self._touch_A7 is None:
304312
self._touch_A7 = touchio.TouchIn(board.A7)
313+
self._touch_A7.threshold += self._touch_threshold_adjustment
305314
return self._touch_A7.value
306315

316+
def adjust_touch_threshold(self, adjustment):
317+
"""Adjust the threshold needed to activate the capacitive touch pads.
318+
Higher numbers make the touch pads less sensitive.
319+
320+
:param int adjustment: The desired threshold increase
321+
322+
.. image :: /_static/capacitive_touch_pads.jpg
323+
:alt: Capacitive touch pads
324+
325+
.. code-block:: python
326+
327+
from adafruit_circuitplayground.express import cpx
328+
329+
cpx.adjust_touch_threshold(200)
330+
331+
while True:
332+
if cpx.touch_A1:
333+
print('Touched pad A1')
334+
"""
335+
for pad_name in ["_touch_A" + str(x) for x in range(1, 8)]:
336+
touch_in = getattr(self, pad_name)
337+
if touch_in:
338+
touch_in.threshold += adjustment
339+
self._touch_threshold_adjustment += adjustment
340+
307341
@property
308342
def pixels(self):
309343
"""Sequence like object representing the ten NeoPixels around the outside

0 commit comments

Comments
 (0)