Skip to content

Commit 32a608d

Browse files
authored
Merge pull request #64 from FoamyGuy/iterable_touch
iterable touch API
2 parents 0f8c432 + 0c12936 commit 32a608d

File tree

2 files changed

+63
-16
lines changed

2 files changed

+63
-16
lines changed

adafruit_clue.py

+29-16
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,15 @@
4040
"""
4141

4242
try:
43-
from typing import Union, Tuple, Optional
43+
from typing import Union, Tuple, Optional, List
4444
except ImportError:
4545
pass
4646

4747
import time
4848
import array
4949
import math
5050
import board
51+
from microcontroller import Pin
5152
import digitalio
5253
import neopixel
5354
import 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:

examples/clue_touch_all.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
4+
"""This example prints to the serial console when you touch the capacitive touch pads."""
5+
import time
6+
import board
7+
from adafruit_clue import clue
8+
9+
10+
# You'll need to first use the touchpads individually to register them as active touchpads
11+
# You don't have to use the result though
12+
is_p0_touched = clue.touch_0 # This result can be used if you want
13+
if is_p0_touched:
14+
print("P0/D0 was touched upon startup!")
15+
is_p1_touched = clue.touch_1
16+
is_p2_touched = clue.touch_2
17+
18+
19+
print("Pads that are currently setup as touchpads:")
20+
print(clue.touch_pins)
21+
22+
while True:
23+
current_touched = clue.touched
24+
25+
if current_touched:
26+
print("Touchpads currently registering a touch:")
27+
print(current_touched)
28+
else:
29+
print("No touchpads are currently registering a touch.")
30+
31+
if all(pad in current_touched for pad in (board.P0, board.P1, board.P2)):
32+
print("This only prints when P0, P1, and P2 are being held at the same time!")
33+
34+
time.sleep(0.25)

0 commit comments

Comments
 (0)