Skip to content

Commit eaca0d7

Browse files
committed
Updated to move adjustment into each touchpad call
1 parent b19a3e2 commit eaca0d7

File tree

1 file changed

+34
-29
lines changed

1 file changed

+34
-29
lines changed

adafruit_circuitplayground/express.py

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def light(self):
6767
return self._photocell.value * 330 // (2 ** 16)
6868

6969

70-
class Express: #pylint: disable-msg=R0904
70+
class Express: # pylint: disable-msg=R0904
7171
"""Represents a single CircuitPlayground Express. Do not use more than one at
7272
a time."""
7373
def __init__(self):
@@ -111,6 +111,7 @@ def __init__(self):
111111
self._touch_A5 = None
112112
self._touch_A6 = None
113113
self._touch_A7 = None
114+
self._touch_threshold_adjustment = 0
114115

115116
# Define acceleration:
116117
self._i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
@@ -172,34 +173,6 @@ def shake(self):
172173
raise RuntimeError("Oops! You need a newer version of CircuitPython "
173174
"(2.2.0 or greater) to use cpx.shake.")
174175

175-
@classmethod
176-
def adjust_touch_threshold(cls, adjustment, pad_names):
177-
"""Adjust the threshold needed to activate the capacitive touch pads.
178-
Higher numbers make the touch pads less sensitive. Include the names
179-
of the touch pads for which you plan to change the threshold. They
180-
must be listed as in the example below.
181-
182-
:param int adjustment: The desired threshold increase
183-
:param str pad_names: The names, in a list, of the touch pads you intend to use
184-
185-
.. image :: /_static/capacitive_touch_pads.jpg
186-
:alt: Capacitive touch pads
187-
188-
.. code-block:: python
189-
190-
from adafruit_circuitplayground.express import cpx
191-
192-
cpx.adjust_touch_threshold(200, ["touch_A1", "touch_A2", "touch_A3", "touch_A4",
193-
"touch_A5", "touch_A6", "touch_A7"])
194-
195-
while True:
196-
if cpx.touch_A1:
197-
print('Touched pad A1')
198-
"""
199-
for pad_name in pad_names:
200-
getattr(cpx, pad_name)
201-
getattr(cpx, "_" + pad_name).threshold += adjustment
202-
203176
@property
204177
def touch_A1(self): # pylint: disable=invalid-name
205178
"""Detect touch on capacitive touch pad A1.
@@ -217,6 +190,7 @@ def touch_A1(self): # pylint: disable=invalid-name
217190
"""
218191
if self._touch_A1 is None:
219192
self._touch_A1 = touchio.TouchIn(board.A1)
193+
self._touch_A1.threshold += self._touch_threshold_adjustment
220194
return self._touch_A1.value
221195

222196
@property
@@ -236,6 +210,7 @@ def touch_A2(self): # pylint: disable=invalid-name
236210
"""
237211
if self._touch_A2 is None:
238212
self._touch_A2 = touchio.TouchIn(board.A2)
213+
self._touch_A2.threshold += self._touch_threshold_adjustment
239214
return self._touch_A2.value
240215

241216
@property
@@ -255,6 +230,7 @@ def touch_A3(self): # pylint: disable=invalid-name
255230
"""
256231
if self._touch_A3 is None:
257232
self._touch_A3 = touchio.TouchIn(board.A3)
233+
self._touch_A3.threshold += self._touch_threshold_adjustment
258234
return self._touch_A3.value
259235

260236
@property
@@ -274,6 +250,7 @@ def touch_A4(self): # pylint: disable=invalid-name
274250
"""
275251
if self._touch_A4 is None:
276252
self._touch_A4 = touchio.TouchIn(board.A4)
253+
self._touch_A4.threshold += self._touch_threshold_adjustment
277254
return self._touch_A4.value
278255

279256
@property
@@ -293,6 +270,7 @@ def touch_A5(self): # pylint: disable=invalid-name
293270
"""
294271
if self._touch_A5 is None:
295272
self._touch_A5 = touchio.TouchIn(board.A5)
273+
self._touch_A5.threshold += self._touch_threshold_adjustment
296274
return self._touch_A5.value
297275

298276
@property
@@ -312,6 +290,7 @@ def touch_A6(self): # pylint: disable=invalid-name
312290
"""
313291
if self._touch_A6 is None:
314292
self._touch_A6 = touchio.TouchIn(board.A6)
293+
self._touch_A6.threshold += self._touch_threshold_adjustment
315294
return self._touch_A6.value
316295

317296
@property
@@ -331,8 +310,34 @@ def touch_A7(self): # pylint: disable=invalid-name
331310
"""
332311
if self._touch_A7 is None:
333312
self._touch_A7 = touchio.TouchIn(board.A7)
313+
self._touch_A7.threshold += self._touch_threshold_adjustment
334314
return self._touch_A7.value
335315

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+
336341
@property
337342
def pixels(self):
338343
"""Sequence like object representing the ten NeoPixels around the outside

0 commit comments

Comments
 (0)