4040"""
4141
4242try :
43- from typing import Union , Tuple , Optional
43+ from typing import Union , Tuple , Optional , List
4444except ImportError :
4545 pass
4646
4747import time
4848import array
4949import math
5050import board
51+ from microcontroller import Pin
5152import digitalio
5253import neopixel
5354import adafruit_apds9960 .apds9960
@@ -188,12 +189,13 @@ def __init__(self):
188189 self ._i2c = board .I2C ()
189190
190191 # Define touch:
191- # Initially, self._touches stores the pin used for a particular touch. When that touch is
192- # used for the first time, the pin is replaced with the corresponding TouchIn object.
193- # This saves a little RAM over using a separate read-only pin tuple.
192+ # Initially, self._touches is an empty dictionary. When a touch is used
193+ # for the first time, the pin is added as a key to the dictionary, with
194+ # the corresponding TouchIn object added as the value. This saves a
195+ # little RAM by only populating the dictionary as needed.
194196 # For example, after `clue.touch_2`, self._touches is equivalent to:
195- # [board.D0, board.D1 , touchio.TouchIn(board.D2)]
196- self ._touches = [ board . D0 , board . D1 , board . D2 ]
197+ # { board.P2 , touchio.TouchIn(board.P2) }
198+ self ._touches = {}
197199 self ._touch_threshold_adjustment = 0
198200
199201 # Define buttons:
@@ -240,13 +242,14 @@ def __init__(self):
240242 # Create displayio object for passing.
241243 self .display = board .DISPLAY
242244
243- def _touch (self , i : int ) -> bool :
244- if not isinstance (self ._touches [i ], touchio .TouchIn ):
245- # First time referenced. Get the pin from the slot for this touch
246- # and replace it with a TouchIn object for the pin.
247- self ._touches [i ] = touchio .TouchIn (self ._touches [i ])
248- self ._touches [i ].threshold += self ._touch_threshold_adjustment
249- return self ._touches [i ].value
245+ def _touch (self , pin : Pin ) -> bool :
246+ touchin = self ._touches .get (pin )
247+ if not touchin :
248+ # First time referenced. Make TouchIn object for the pin
249+ touchin = touchio .TouchIn (pin )
250+ touchin .threshold += self ._touch_threshold_adjustment
251+ self ._touches [pin ] = touchin
252+ return touchin .value
250253
251254 @property
252255 def touch_0 (self ) -> bool :
@@ -267,7 +270,7 @@ def touch_0(self) -> bool:
267270 if clue.touch_0:
268271 print("Touched pad 0")
269272 """
270- return self ._touch (0 )
273+ return self ._touch (board . P0 )
271274
272275 @property
273276 def touch_1 (self ) -> bool :
@@ -288,7 +291,7 @@ def touch_1(self) -> bool:
288291 if clue.touch_1:
289292 print("Touched pad 1")
290293 """
291- return self ._touch (1 )
294+ return self ._touch (board . P1 )
292295
293296 @property
294297 def touch_2 (self ) -> bool :
@@ -309,7 +312,17 @@ def touch_2(self) -> bool:
309312 if clue.touch_2:
310313 print("Touched pad 2")
311314 """
312- return self ._touch (2 )
315+ return self ._touch (board .P2 )
316+
317+ @property
318+ def touch_pins (self ) -> List [Pin ]:
319+ """A list of all the pins that are set up as touchpad inputs"""
320+ return list (self ._touches .keys ())
321+
322+ @property
323+ def touched (self ):
324+ """A list of all the pins that are currently registering a touch"""
325+ return [pin for pin , touchpad in self ._touches .items () if touchpad .value ]
313326
314327 @property
315328 def button_a (self ) -> bool :
0 commit comments