Skip to content

Commit 862a379

Browse files
authored
Merge pull request #34 from dhalbert/touch_refactor
refactor touch code to save space
2 parents d0022de + 53a8fe1 commit 862a379

File tree

2 files changed

+36
-66
lines changed

2 files changed

+36
-66
lines changed

adafruit_circuitplayground/__init__.py

Whitespace-only changes.

adafruit_circuitplayground/express.py

Lines changed: 36 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,19 @@ 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:
118114
self._i2c = busio.I2C(board.ACCELEROMETER_SCL, board.ACCELEROMETER_SDA)
119115
self._int1 = digitalio.DigitalInOut(board.ACCELEROMETER_INTERRUPT)
120-
try:
121-
self._lis3dh = adafruit_lis3dh.LIS3DH_I2C(self._i2c, address=0x19, int1=self._int1)
122-
except TypeError:
123-
self._lis3dh = adafruit_lis3dh.LIS3DH_I2C(self._i2c, address=0x19)
116+
self._lis3dh = adafruit_lis3dh.LIS3DH_I2C(self._i2c, address=0x19, int1=self._int1)
124117
self._lis3dh.range = adafruit_lis3dh.RANGE_8_G
125118

126119
# Initialise tap:
@@ -149,13 +142,10 @@ def detect_taps(self):
149142
@detect_taps.setter
150143
def detect_taps(self, value):
151144
self._detect_taps = value
152-
try:
153-
if value == 1:
154-
self._lis3dh.set_tap(value, 90, time_limit=4, time_latency=50, time_window=255)
155-
if value == 2:
156-
self._lis3dh.set_tap(value, 60, time_limit=10, time_latency=50, time_window=255)
157-
except AttributeError:
158-
pass
145+
if value == 1:
146+
self._lis3dh.set_tap(value, 90, time_limit=4, time_latency=50, time_window=255)
147+
if value == 2:
148+
self._lis3dh.set_tap(value, 60, time_limit=10, time_latency=50, time_window=255)
159149

160150
@property
161151
def tapped(self):
@@ -206,11 +196,7 @@ def tapped(self):
206196
print("Done.")
207197
208198
"""
209-
try:
210-
return self._lis3dh.tapped
211-
except AttributeError:
212-
raise RuntimeError("Oops! You need a newer version of CircuitPython "
213-
"(2.2.0 or greater) to use this feature.")
199+
return self._lis3dh.tapped
214200

215201
@property
216202
def acceleration(self):
@@ -262,12 +248,19 @@ def shake(self, shake_threshold=30):
262248
if cpx.shake(shake_threshold=20):
263249
print("Shake detected more easily than before!")
264250
"""
265-
try:
266-
return self._lis3dh.shake(shake_threshold=shake_threshold)
267-
except AttributeError:
268-
raise RuntimeError("Oops! You need a newer version of CircuitPython "
269-
"(2.2.0 or greater) to use this feature.")
270-
251+
return self._lis3dh.shake(shake_threshold=shake_threshold)
252+
253+
def _touch(self, i):
254+
if not isinstance(self._touches[i], touchio.TouchIn):
255+
# First time referenced. Get the pin from the slot for this touch
256+
# and replace it with a TouchIn object for the pin.
257+
self._touches[i] = touchio.TouchIn(self._touches[i])
258+
self._touches[i].threshold += self._touch_threshold_adjustment
259+
return self._touches[i].value
260+
261+
# We chose these verbose touch_A# names so that beginners could use it without understanding
262+
# lists and the capital A to match the pin name. The capitalization is not strictly Python
263+
# style, so everywhere we use these names, we whitelist the errors using:
271264
@property
272265
def touch_A1(self): # pylint: disable=invalid-name
273266
"""Detect touch on capacitive touch pad A1.
@@ -283,10 +276,7 @@ def touch_A1(self): # pylint: disable=invalid-name
283276
if cpx.touch_A1:
284277
print('Touched pad A1')
285278
"""
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
279+
return self._touch(1)
290280

291281
@property
292282
def touch_A2(self): # pylint: disable=invalid-name
@@ -303,10 +293,7 @@ def touch_A2(self): # pylint: disable=invalid-name
303293
if cpx.touch_A2:
304294
print('Touched pad A2')
305295
"""
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
296+
return self._touch(2)
310297

311298
@property
312299
def touch_A3(self): # pylint: disable=invalid-name
@@ -323,10 +310,7 @@ def touch_A3(self): # pylint: disable=invalid-name
323310
if cpx.touch_A3:
324311
print('Touched pad A3')
325312
"""
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
313+
return self._touch(3)
330314

331315
@property
332316
def touch_A4(self): # pylint: disable=invalid-name
@@ -343,10 +327,7 @@ def touch_A4(self): # pylint: disable=invalid-name
343327
if cpx.touch_A4:
344328
print('Touched pad A4')
345329
"""
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
330+
return self._touch(4)
350331

351332
@property
352333
def touch_A5(self): # pylint: disable=invalid-name
@@ -363,10 +344,7 @@ def touch_A5(self): # pylint: disable=invalid-name
363344
if cpx.touch_A5:
364345
print('Touched pad A5')
365346
"""
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
347+
return self._touch(5)
370348

371349
@property
372350
def touch_A6(self): # pylint: disable=invalid-name
@@ -383,10 +361,7 @@ def touch_A6(self): # pylint: disable=invalid-name
383361
if cpx.touch_A6:
384362
print('Touched pad A6')
385363
"""
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
364+
return self._touch(6)
390365

391366
@property
392367
def touch_A7(self): # pylint: disable=invalid-name
@@ -403,10 +378,7 @@ def touch_A7(self): # pylint: disable=invalid-name
403378
if cpx.touch_A7:
404379
print('Touched pad A7')
405380
"""
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
381+
return self._touch(7)
410382

411383
def adjust_touch_threshold(self, adjustment):
412384
"""Adjust the threshold needed to activate the capacitive touch pads.
@@ -427,9 +399,8 @@ def adjust_touch_threshold(self, adjustment):
427399
if cpx.touch_A1:
428400
print('Touched pad A1')
429401
"""
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:
402+
for touch_in in self._touches:
403+
if isinstance(touch_in, touchio.TouchIn):
433404
touch_in.threshold += adjustment
434405
self._touch_threshold_adjustment += adjustment
435406

@@ -595,12 +566,11 @@ def _generate_sample(self):
595566
if self._sample is not None:
596567
return
597568
length = 100
569+
self._sine_wave = array.array("H", Express._sine_sample(length))
598570
if sys.implementation.version[0] >= 3:
599-
self._sine_wave = array.array("H", Express._sine_sample(length))
600571
self._sample = audioio.AudioOut(board.SPEAKER)
601572
self._sine_wave_sample = audioio.RawSample(self._sine_wave)
602573
else:
603-
self._sine_wave = array.array("H", Express._sine_sample(length))
604574
self._sample = audioio.AudioOut(board.SPEAKER, self._sine_wave)
605575

606576

0 commit comments

Comments
 (0)