40
40
"""
41
41
42
42
try :
43
- from typing import Union , Tuple , Optional
43
+ from typing import Union , Tuple , Optional , List
44
44
except ImportError :
45
45
pass
46
46
47
47
import time
48
48
import array
49
49
import math
50
50
import board
51
+ from microcontroller import Pin
51
52
import digitalio
52
53
import neopixel
53
54
import adafruit_apds9960 .apds9960
@@ -188,12 +189,13 @@ def __init__(self):
188
189
self ._i2c = board .I2C ()
189
190
190
191
# 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.
194
196
# 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 = {}
197
199
self ._touch_threshold_adjustment = 0
198
200
199
201
# Define buttons:
@@ -240,13 +242,14 @@ def __init__(self):
240
242
# Create displayio object for passing.
241
243
self .display = board .DISPLAY
242
244
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
250
253
251
254
@property
252
255
def touch_0 (self ) -> bool :
@@ -267,7 +270,7 @@ def touch_0(self) -> bool:
267
270
if clue.touch_0:
268
271
print("Touched pad 0")
269
272
"""
270
- return self ._touch (0 )
273
+ return self ._touch (board . P0 )
271
274
272
275
@property
273
276
def touch_1 (self ) -> bool :
@@ -288,7 +291,7 @@ def touch_1(self) -> bool:
288
291
if clue.touch_1:
289
292
print("Touched pad 1")
290
293
"""
291
- return self ._touch (1 )
294
+ return self ._touch (board . P1 )
292
295
293
296
@property
294
297
def touch_2 (self ) -> bool :
@@ -309,7 +312,17 @@ def touch_2(self) -> bool:
309
312
if clue.touch_2:
310
313
print("Touched pad 2")
311
314
"""
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 ]
313
326
314
327
@property
315
328
def button_a (self ) -> bool :
0 commit comments