34
34
import touchio
35
35
36
36
try :
37
- from typing import Optional , Iterator
37
+ from typing import Optional , Iterator , List
38
38
from typing_extensions import Literal
39
39
from microcontroller import Pin
40
40
except ImportError :
@@ -82,22 +82,13 @@ def __init__(self) -> None:
82
82
self ._light = Photocell (board .LIGHT )
83
83
84
84
# Define touch:
85
- # Initially, self._touches stores the pin used for a particular touch. When that touch is
86
- # used for the first time, the pin is replaced with the corresponding TouchIn object.
87
- # This saves a little RAM over using a separate read-only pin tuple.
88
- # For example, after `cp.touch_A2`, self._touches is equivalent to:
89
- # [None, board.A1, touchio.TouchIn(board.A2), board.A3, ...]
90
- # Slot 0 is not used (A0 is not allowed as a touch pin).
91
- self ._touches = [
92
- None ,
93
- board .A1 ,
94
- board .A2 ,
95
- board .A3 ,
96
- board .A4 ,
97
- board .A5 ,
98
- board .A6 ,
99
- board .TX ,
100
- ]
85
+ # Initially, self._touches is an empty dictionary. When a touch is used
86
+ # for the first time, the pin is added as a key to the dictionary, with
87
+ # the corresponding TouchIn object added as the value. This saves a
88
+ # little RAM by only populating the dictionary as needed. For example,
89
+ # after `cp.touch_A2`, self._touches is equivalent to:
90
+ # { board.A2: TouchIn(board.A2) }
91
+ self ._touches = {}
101
92
self ._touch_threshold_adjustment = 0
102
93
103
94
# Define acceleration:
@@ -359,13 +350,14 @@ def shake(self, shake_threshold: int = 30) -> bool:
359
350
"""
360
351
return self ._lis3dh .shake (shake_threshold = shake_threshold )
361
352
362
- def _touch (self , i ) -> bool :
363
- if not isinstance (self ._touches [i ], touchio .TouchIn ):
364
- # First time referenced. Get the pin from the slot for this touch
365
- # and replace it with a TouchIn object for the pin.
366
- self ._touches [i ] = touchio .TouchIn (self ._touches [i ])
367
- self ._touches [i ].threshold += self ._touch_threshold_adjustment
368
- return self ._touches [i ].value
353
+ def _touch (self , pin : Pin ) -> bool :
354
+ touchin = self ._touches .get (pin )
355
+ if not touchin :
356
+ # First time referenced. Make TouchIn object for the pin
357
+ touchin = touchio .TouchIn (pin )
358
+ touchin .threshold += self ._touch_threshold_adjustment
359
+ self ._touches [pin ] = touchin
360
+ return touchin .value
369
361
370
362
# We chose these verbose touch_A# names so that beginners could use it without understanding
371
363
# lists and the capital A to match the pin name. The capitalization is not strictly Python
@@ -387,7 +379,7 @@ def touch_A1(self) -> bool: # pylint: disable=invalid-name
387
379
if cp.touch_A1:
388
380
print('Touched pad A1')
389
381
"""
390
- return self ._touch (1 )
382
+ return self ._touch (board . A1 )
391
383
392
384
@property
393
385
def touch_A2 (self ) -> bool : # pylint: disable=invalid-name
@@ -406,7 +398,7 @@ def touch_A2(self) -> bool: # pylint: disable=invalid-name
406
398
if cp.touch_A2:
407
399
print('Touched pad A2')
408
400
"""
409
- return self ._touch (2 )
401
+ return self ._touch (board . A2 )
410
402
411
403
@property
412
404
def touch_A3 (self ) -> bool : # pylint: disable=invalid-name
@@ -425,7 +417,7 @@ def touch_A3(self) -> bool: # pylint: disable=invalid-name
425
417
if cp.touch_A3:
426
418
print('Touched pad A3')
427
419
"""
428
- return self ._touch (3 )
420
+ return self ._touch (board . A3 )
429
421
430
422
@property
431
423
def touch_A4 (self ) -> bool : # pylint: disable=invalid-name
@@ -444,7 +436,7 @@ def touch_A4(self) -> bool: # pylint: disable=invalid-name
444
436
if cp.touch_A4:
445
437
print('Touched pad A4')
446
438
"""
447
- return self ._touch (4 )
439
+ return self ._touch (board . A4 )
448
440
449
441
@property
450
442
def touch_A5 (self ) -> bool : # pylint: disable=invalid-name
@@ -463,7 +455,7 @@ def touch_A5(self) -> bool: # pylint: disable=invalid-name
463
455
if cp.touch_A5:
464
456
print('Touched pad A5')
465
457
"""
466
- return self ._touch (5 )
458
+ return self ._touch (board . A5 )
467
459
468
460
@property
469
461
def touch_A6 (self ) -> bool : # pylint: disable=invalid-name
@@ -482,7 +474,7 @@ def touch_A6(self) -> bool: # pylint: disable=invalid-name
482
474
if cp.touch_A6:
483
475
print('Touched pad A6'
484
476
"""
485
- return self ._touch (6 )
477
+ return self ._touch (board . A6 )
486
478
487
479
@property
488
480
def touch_TX (self ) -> bool : # pylint: disable=invalid-name
@@ -502,7 +494,7 @@ def touch_TX(self) -> bool: # pylint: disable=invalid-name
502
494
if cp.touch_A7:
503
495
print('Touched pad A7')
504
496
"""
505
- return self ._touch (7 )
497
+ return self ._touch (board . TX )
506
498
507
499
def adjust_touch_threshold (self , adjustment : int ) -> None :
508
500
"""Adjust the threshold needed to activate the capacitive touch pads.
@@ -525,11 +517,20 @@ def adjust_touch_threshold(self, adjustment: int) -> None:
525
517
if cp.touch_A1:
526
518
print('Touched pad A1')
527
519
"""
528
- for touch_in in self ._touches :
529
- if isinstance (touch_in , touchio .TouchIn ):
530
- touch_in .threshold += adjustment
520
+ for touch_in in self ._touches .values ():
521
+ touch_in .threshold += adjustment
531
522
self ._touch_threshold_adjustment += adjustment
532
523
524
+ @property
525
+ def touch_pins (self ) -> List [Pin ]:
526
+ """A list of all the pins that are set up as touchpad inputs"""
527
+ return list (self ._touches .keys ())
528
+
529
+ @property
530
+ def touched (self ) -> List [Pin ]:
531
+ """A list of all the pins that are currently registering a touch"""
532
+ return [pin for pin , touchpad in self ._touches .items () if touchpad .value ]
533
+
533
534
@property
534
535
def pixels (self ) -> neopixel .NeoPixel :
535
536
"""Sequence-like object representing the ten NeoPixels around the outside
0 commit comments