@@ -101,17 +101,13 @@ def __init__(self):
101
101
self ._sine_wave_sample = None
102
102
103
103
# 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 ]
115
111
self ._touch_threshold_adjustment = 0
116
112
117
113
# Define acceleration:
@@ -268,6 +264,17 @@ def shake(self, shake_threshold=30):
268
264
raise RuntimeError ("Oops! You need a newer version of CircuitPython "
269
265
"(2.2.0 or greater) to use this feature." )
270
266
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:
271
278
@property
272
279
def touch_A1 (self ): # pylint: disable=invalid-name
273
280
"""Detect touch on capacitive touch pad A1.
@@ -283,10 +290,7 @@ def touch_A1(self): # pylint: disable=invalid-name
283
290
if cpx.touch_A1:
284
291
print('Touched pad A1')
285
292
"""
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 )
290
294
291
295
@property
292
296
def touch_A2 (self ): # pylint: disable=invalid-name
@@ -303,10 +307,7 @@ def touch_A2(self): # pylint: disable=invalid-name
303
307
if cpx.touch_A2:
304
308
print('Touched pad A2')
305
309
"""
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 )
310
311
311
312
@property
312
313
def touch_A3 (self ): # pylint: disable=invalid-name
@@ -323,10 +324,7 @@ def touch_A3(self): # pylint: disable=invalid-name
323
324
if cpx.touch_A3:
324
325
print('Touched pad A3')
325
326
"""
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 )
330
328
331
329
@property
332
330
def touch_A4 (self ): # pylint: disable=invalid-name
@@ -343,10 +341,7 @@ def touch_A4(self): # pylint: disable=invalid-name
343
341
if cpx.touch_A4:
344
342
print('Touched pad A4')
345
343
"""
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 )
350
345
351
346
@property
352
347
def touch_A5 (self ): # pylint: disable=invalid-name
@@ -363,10 +358,7 @@ def touch_A5(self): # pylint: disable=invalid-name
363
358
if cpx.touch_A5:
364
359
print('Touched pad A5')
365
360
"""
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 )
370
362
371
363
@property
372
364
def touch_A6 (self ): # pylint: disable=invalid-name
@@ -383,10 +375,7 @@ def touch_A6(self): # pylint: disable=invalid-name
383
375
if cpx.touch_A6:
384
376
print('Touched pad A6')
385
377
"""
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 )
390
379
391
380
@property
392
381
def touch_A7 (self ): # pylint: disable=invalid-name
@@ -403,10 +392,7 @@ def touch_A7(self): # pylint: disable=invalid-name
403
392
if cpx.touch_A7:
404
393
print('Touched pad A7')
405
394
"""
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 )
410
396
411
397
def adjust_touch_threshold (self , adjustment ):
412
398
"""Adjust the threshold needed to activate the capacitive touch pads.
@@ -427,9 +413,8 @@ def adjust_touch_threshold(self, adjustment):
427
413
if cpx.touch_A1:
428
414
print('Touched pad A1')
429
415
"""
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 ):
433
418
touch_in .threshold += adjustment
434
419
self ._touch_threshold_adjustment += adjustment
435
420
@@ -595,12 +580,11 @@ def _generate_sample(self):
595
580
if self ._sample is not None :
596
581
return
597
582
length = 100
583
+ self ._sine_wave = array .array ("H" , Express ._sine_sample (length ))
598
584
if sys .implementation .version [0 ] >= 3 :
599
- self ._sine_wave = array .array ("H" , Express ._sine_sample (length ))
600
585
self ._sample = audioio .AudioOut (board .SPEAKER )
601
586
self ._sine_wave_sample = audioio .RawSample (self ._sine_wave )
602
587
else :
603
- self ._sine_wave = array .array ("H" , Express ._sine_sample (length ))
604
588
self ._sample = audioio .AudioOut (board .SPEAKER , self ._sine_wave )
605
589
606
590
0 commit comments