Skip to content

Commit

Permalink
Merge pull request #64 from FoamyGuy/iterable_touch
Browse files Browse the repository at this point in the history
iterable touch API
  • Loading branch information
FoamyGuy authored Nov 13, 2024
2 parents 0f8c432 + 0c12936 commit 32a608d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 16 deletions.
45 changes: 29 additions & 16 deletions adafruit_clue.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,15 @@
"""

try:
from typing import Union, Tuple, Optional
from typing import Union, Tuple, Optional, List
except ImportError:
pass

import time
import array
import math
import board
from microcontroller import Pin
import digitalio
import neopixel
import adafruit_apds9960.apds9960
Expand Down Expand Up @@ -188,12 +189,13 @@ def __init__(self):
self._i2c = board.I2C()

# Define touch:
# Initially, self._touches stores the pin used for a particular touch. When that touch is
# used for the first time, the pin is replaced with the corresponding TouchIn object.
# This saves a little RAM over using a separate read-only pin tuple.
# Initially, self._touches is an empty dictionary. When a touch is used
# for the first time, the pin is added as a key to the dictionary, with
# the corresponding TouchIn object added as the value. This saves a
# little RAM by only populating the dictionary as needed.
# For example, after `clue.touch_2`, self._touches is equivalent to:
# [board.D0, board.D1, touchio.TouchIn(board.D2)]
self._touches = [board.D0, board.D1, board.D2]
# { board.P2, touchio.TouchIn(board.P2) }
self._touches = {}
self._touch_threshold_adjustment = 0

# Define buttons:
Expand Down Expand Up @@ -240,13 +242,14 @@ def __init__(self):
# Create displayio object for passing.
self.display = board.DISPLAY

def _touch(self, i: int) -> bool:
if not isinstance(self._touches[i], touchio.TouchIn):
# First time referenced. Get the pin from the slot for this touch
# and replace it with a TouchIn object for the pin.
self._touches[i] = touchio.TouchIn(self._touches[i])
self._touches[i].threshold += self._touch_threshold_adjustment
return self._touches[i].value
def _touch(self, pin: Pin) -> bool:
touchin = self._touches.get(pin)
if not touchin:
# First time referenced. Make TouchIn object for the pin
touchin = touchio.TouchIn(pin)
touchin.threshold += self._touch_threshold_adjustment
self._touches[pin] = touchin
return touchin.value

@property
def touch_0(self) -> bool:
Expand All @@ -267,7 +270,7 @@ def touch_0(self) -> bool:
if clue.touch_0:
print("Touched pad 0")
"""
return self._touch(0)
return self._touch(board.P0)

@property
def touch_1(self) -> bool:
Expand All @@ -288,7 +291,7 @@ def touch_1(self) -> bool:
if clue.touch_1:
print("Touched pad 1")
"""
return self._touch(1)
return self._touch(board.P1)

@property
def touch_2(self) -> bool:
Expand All @@ -309,7 +312,17 @@ def touch_2(self) -> bool:
if clue.touch_2:
print("Touched pad 2")
"""
return self._touch(2)
return self._touch(board.P2)

@property
def touch_pins(self) -> List[Pin]:
"""A list of all the pins that are set up as touchpad inputs"""
return list(self._touches.keys())

@property
def touched(self):
"""A list of all the pins that are currently registering a touch"""
return [pin for pin, touchpad in self._touches.items() if touchpad.value]

@property
def button_a(self) -> bool:
Expand Down
34 changes: 34 additions & 0 deletions examples/clue_touch_all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""This example prints to the serial console when you touch the capacitive touch pads."""
import time
import board
from adafruit_clue import clue


# You'll need to first use the touchpads individually to register them as active touchpads
# You don't have to use the result though
is_p0_touched = clue.touch_0 # This result can be used if you want
if is_p0_touched:
print("P0/D0 was touched upon startup!")
is_p1_touched = clue.touch_1
is_p2_touched = clue.touch_2


print("Pads that are currently setup as touchpads:")
print(clue.touch_pins)

while True:
current_touched = clue.touched

if current_touched:
print("Touchpads currently registering a touch:")
print(current_touched)
else:
print("No touchpads are currently registering a touch.")

if all(pad in current_touched for pad in (board.P0, board.P1, board.P2)):
print("This only prints when P0, P1, and P2 are being held at the same time!")

time.sleep(0.25)

0 comments on commit 32a608d

Please sign in to comment.