Skip to content

Commit a784863

Browse files
committed
refactor touch code to save space
1 parent d0022de commit a784863

File tree

2 files changed

+28
-44
lines changed

2 files changed

+28
-44
lines changed

adafruit_circuitplayground/__init__.py

Whitespace-only changes.

adafruit_circuitplayground/express.py

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -101,17 +101,13 @@ def __init__(self):
101101
self._sine_wave_sample = None
102102

103103
# Define touch:
104-
# We chose these verbose touch_A# names so that beginners could use it without understanding
105-
# lists and the capital A to match the pin name. The capitalization is not strictly Python
106-
# style, so everywhere we use these names, we whitelist the errors using:
107-
# pylint: disable=invalid-name
108-
self._touch_A1 = None
109-
self._touch_A2 = None
110-
self._touch_A3 = None
111-
self._touch_A4 = None
112-
self._touch_A5 = None
113-
self._touch_A6 = None
114-
self._touch_A7 = None
104+
# Initially, self._touches stores the pin used for a particular touch. When that touch is
105+
# used for the first time, the pin is replaced with the corresponding TouchIn object.
106+
# This saves a little RAM over using a separate read-only pin tuple.
107+
# For example, after `cpx.touch_A2`, self._touches is equivalent to:
108+
# [None, board.A1, touchio.TouchIn(board.A2), board.A3, ...]
109+
# Slot 0 is not used (A0 is not allowed as a touch pin).
110+
self._touches = [None, board.A1, board.A2, board.A3, board.A4, board.A5, board.A6, board.A7]
115111
self._touch_threshold_adjustment = 0
116112

117113
# Define acceleration:
@@ -268,6 +264,17 @@ def shake(self, shake_threshold=30):
268264
raise RuntimeError("Oops! You need a newer version of CircuitPython "
269265
"(2.2.0 or greater) to use this feature.")
270266

267+
def _touch(self, i):
268+
if not isinstance(self._touches[i], touchio.TouchIn):
269+
# First time referenced. Get the pin from the slot for this touch
270+
# and replace it with a TouchIn object for the pin.
271+
self._touches[i] = touchio.TouchIn(self._touches[i])
272+
self._touches[i].threshold += self._touch_threshold_adjustment
273+
return self._touches[i].value
274+
275+
# We chose these verbose touch_A# names so that beginners could use it without understanding
276+
# lists and the capital A to match the pin name. The capitalization is not strictly Python
277+
# style, so everywhere we use these names, we whitelist the errors using:
271278
@property
272279
def touch_A1(self): # pylint: disable=invalid-name
273280
"""Detect touch on capacitive touch pad A1.
@@ -283,10 +290,7 @@ def touch_A1(self): # pylint: disable=invalid-name
283290
if cpx.touch_A1:
284291
print('Touched pad A1')
285292
"""
286-
if self._touch_A1 is None:
287-
self._touch_A1 = touchio.TouchIn(board.A1)
288-
self._touch_A1.threshold += self._touch_threshold_adjustment
289-
return self._touch_A1.value
293+
return self._touch(1)
290294

291295
@property
292296
def touch_A2(self): # pylint: disable=invalid-name
@@ -303,10 +307,7 @@ def touch_A2(self): # pylint: disable=invalid-name
303307
if cpx.touch_A2:
304308
print('Touched pad A2')
305309
"""
306-
if self._touch_A2 is None:
307-
self._touch_A2 = touchio.TouchIn(board.A2)
308-
self._touch_A2.threshold += self._touch_threshold_adjustment
309-
return self._touch_A2.value
310+
return self._touch(2)
310311

311312
@property
312313
def touch_A3(self): # pylint: disable=invalid-name
@@ -323,10 +324,7 @@ def touch_A3(self): # pylint: disable=invalid-name
323324
if cpx.touch_A3:
324325
print('Touched pad A3')
325326
"""
326-
if self._touch_A3 is None:
327-
self._touch_A3 = touchio.TouchIn(board.A3)
328-
self._touch_A3.threshold += self._touch_threshold_adjustment
329-
return self._touch_A3.value
327+
return self._touch(3)
330328

331329
@property
332330
def touch_A4(self): # pylint: disable=invalid-name
@@ -343,10 +341,7 @@ def touch_A4(self): # pylint: disable=invalid-name
343341
if cpx.touch_A4:
344342
print('Touched pad A4')
345343
"""
346-
if self._touch_A4 is None:
347-
self._touch_A4 = touchio.TouchIn(board.A4)
348-
self._touch_A4.threshold += self._touch_threshold_adjustment
349-
return self._touch_A4.value
344+
return self._touch(4)
350345

351346
@property
352347
def touch_A5(self): # pylint: disable=invalid-name
@@ -363,10 +358,7 @@ def touch_A5(self): # pylint: disable=invalid-name
363358
if cpx.touch_A5:
364359
print('Touched pad A5')
365360
"""
366-
if self._touch_A5 is None:
367-
self._touch_A5 = touchio.TouchIn(board.A5)
368-
self._touch_A5.threshold += self._touch_threshold_adjustment
369-
return self._touch_A5.value
361+
return self._touch(5)
370362

371363
@property
372364
def touch_A6(self): # pylint: disable=invalid-name
@@ -383,10 +375,7 @@ def touch_A6(self): # pylint: disable=invalid-name
383375
if cpx.touch_A6:
384376
print('Touched pad A6')
385377
"""
386-
if self._touch_A6 is None:
387-
self._touch_A6 = touchio.TouchIn(board.A6)
388-
self._touch_A6.threshold += self._touch_threshold_adjustment
389-
return self._touch_A6.value
378+
return self._touch(6)
390379

391380
@property
392381
def touch_A7(self): # pylint: disable=invalid-name
@@ -403,10 +392,7 @@ def touch_A7(self): # pylint: disable=invalid-name
403392
if cpx.touch_A7:
404393
print('Touched pad A7')
405394
"""
406-
if self._touch_A7 is None:
407-
self._touch_A7 = touchio.TouchIn(board.A7)
408-
self._touch_A7.threshold += self._touch_threshold_adjustment
409-
return self._touch_A7.value
395+
return self._touch(7)
410396

411397
def adjust_touch_threshold(self, adjustment):
412398
"""Adjust the threshold needed to activate the capacitive touch pads.
@@ -427,9 +413,8 @@ def adjust_touch_threshold(self, adjustment):
427413
if cpx.touch_A1:
428414
print('Touched pad A1')
429415
"""
430-
for pad_name in ["_touch_A" + str(x) for x in range(1, 8)]:
431-
touch_in = getattr(self, pad_name)
432-
if touch_in:
416+
for touch_in in self._touches:
417+
if isinstance(touch_in, touchio.TouchIn):
433418
touch_in.threshold += adjustment
434419
self._touch_threshold_adjustment += adjustment
435420

@@ -595,12 +580,11 @@ def _generate_sample(self):
595580
if self._sample is not None:
596581
return
597582
length = 100
583+
self._sine_wave = array.array("H", Express._sine_sample(length))
598584
if sys.implementation.version[0] >= 3:
599-
self._sine_wave = array.array("H", Express._sine_sample(length))
600585
self._sample = audioio.AudioOut(board.SPEAKER)
601586
self._sine_wave_sample = audioio.RawSample(self._sine_wave)
602587
else:
603-
self._sine_wave = array.array("H", Express._sine_sample(length))
604588
self._sample = audioio.AudioOut(board.SPEAKER, self._sine_wave)
605589

606590

0 commit comments

Comments
 (0)