Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iterable touchpad functionality #114

Merged
merged 25 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c71fa2c
Add iterable return touchpad properties
tekktrik Feb 18, 2022
d8c128c
Fix touch_pins, touched properties
tekktrik Feb 18, 2022
4d7b492
Changed example for touching all pads to updated usage
tekktrik Feb 18, 2022
b68b3d2
Reformatted per pre-commit
tekktrik Feb 18, 2022
af87b00
Prevent pointless statements
tekktrik Feb 18, 2022
aaa7582
Add new method docstrings
tekktrik Feb 18, 2022
25c97ca
Refactor example for touch all
tekktrik Mar 21, 2022
01ceb46
Account for pins being hashable
tekktrik Jul 5, 2022
511d45d
Merge branch 'main' of https://github.com/adafruit/Adafruit_CircuitPy…
tekktrik Jul 5, 2022
39e4751
Reformatted per pre-commit
tekktrik Jul 5, 2022
97d1a07
Fix use of _touch()
tekktrik Jul 6, 2022
f03a085
Slow down example printing and show statement if none touched
tekktrik Jul 6, 2022
3e942fc
Use list of keys instead of list comprehension
tekktrik Jul 9, 2022
266ef58
Update comment regard self._touches
tekktrik Jul 9, 2022
0477696
Initialize self._touches as empty dictionary
tekktrik Jul 9, 2022
2213f02
Merge branch 'dev/iterable-io-rework' of https://github.com/tekktrik/…
tekktrik Jul 9, 2022
9142472
Change argument name
tekktrik Jul 9, 2022
937a4a5
Update _touch() to populate _tocuhes as needed
tekktrik Jul 9, 2022
96be262
Update list comprehension
tekktrik Jul 9, 2022
f4f6942
Reformatted per pre-commit
tekktrik Jul 9, 2022
3c707f6
Simplifications to code
tekktrik Jul 9, 2022
108a35d
Merge branch 'main' of https://github.com/adafruit/Adafruit_CircuitPy…
Aug 19, 2022
4bdba83
Update adafruit_circuitplayground/circuit_playground_base.py
tekktrik Aug 24, 2022
5eaaac8
Update adafruit_circuitplayground/circuit_playground_base.py
tekktrik Aug 24, 2022
dbfa322
Correct typo in example
tekktrik Aug 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 33 additions & 31 deletions adafruit_circuitplayground/circuit_playground_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,13 @@ def __init__(self):
self._light = Photocell(board.LIGHT)

# 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.
# For example, after `cp.touch_A2`, self._touches is equivalent to:
# [None, board.A1, touchio.TouchIn(board.A2), board.A3, ...]
# Slot 0 is not used (A0 is not allowed as a touch pin).
self._touches = [
None,
board.A1,
board.A2,
board.A3,
board.A4,
board.A5,
board.A6,
board.TX,
]
# 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 `cp.touch_A2`, self._touches is equivalent to:
# { board.A2: TouchIn(board.A2) }
self._touches = {}
self._touch_threshold_adjustment = 0

# Define acceleration:
Expand Down Expand Up @@ -352,13 +343,14 @@ def shake(self, shake_threshold=30):
"""
return self._lis3dh.shake(shake_threshold=shake_threshold)

def _touch(self, i):
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):
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 self._touches[pin].value
tekktrik marked this conversation as resolved.
Show resolved Hide resolved

# We chose these verbose touch_A# names so that beginners could use it without understanding
# lists and the capital A to match the pin name. The capitalization is not strictly Python
Expand All @@ -380,7 +372,7 @@ def touch_A1(self): # pylint: disable=invalid-name
if cp.touch_A1:
print('Touched pad A1')
"""
return self._touch(1)
return self._touch(board.A1)
tekktrik marked this conversation as resolved.
Show resolved Hide resolved

@property
def touch_A2(self): # pylint: disable=invalid-name
Expand All @@ -399,7 +391,7 @@ def touch_A2(self): # pylint: disable=invalid-name
if cp.touch_A2:
print('Touched pad A2')
"""
return self._touch(2)
return self._touch(board.A2)

@property
def touch_A3(self): # pylint: disable=invalid-name
Expand All @@ -418,7 +410,7 @@ def touch_A3(self): # pylint: disable=invalid-name
if cp.touch_A3:
print('Touched pad A3')
"""
return self._touch(3)
return self._touch(board.A3)

@property
def touch_A4(self): # pylint: disable=invalid-name
Expand All @@ -437,7 +429,7 @@ def touch_A4(self): # pylint: disable=invalid-name
if cp.touch_A4:
print('Touched pad A4')
"""
return self._touch(4)
return self._touch(board.A4)

@property
def touch_A5(self): # pylint: disable=invalid-name
Expand All @@ -456,7 +448,7 @@ def touch_A5(self): # pylint: disable=invalid-name
if cp.touch_A5:
print('Touched pad A5')
"""
return self._touch(5)
return self._touch(board.A5)

@property
def touch_A6(self): # pylint: disable=invalid-name
Expand All @@ -475,7 +467,7 @@ def touch_A6(self): # pylint: disable=invalid-name
if cp.touch_A6:
print('Touched pad A6'
"""
return self._touch(6)
return self._touch(board.A6)

@property
def touch_TX(self): # pylint: disable=invalid-name
Expand All @@ -495,7 +487,7 @@ def touch_TX(self): # pylint: disable=invalid-name
if cp.touch_A7:
print('Touched pad A7')
"""
return self._touch(7)
return self._touch(board.TX)

def adjust_touch_threshold(self, adjustment):
"""Adjust the threshold needed to activate the capacitive touch pads.
Expand All @@ -518,11 +510,21 @@ def adjust_touch_threshold(self, adjustment):
if cp.touch_A1:
print('Touched pad A1')
"""
for touch_in in self._touches:
for touch_in in self._touches.values():
if isinstance(touch_in, touchio.TouchIn):
touch_in.threshold += adjustment
tekktrik marked this conversation as resolved.
Show resolved Hide resolved
self._touch_threshold_adjustment += adjustment

@property
def touch_pins(self):
"""Return a list of all the pins that are set up as touchpad inputs"""
tekktrik marked this conversation as resolved.
Show resolved Hide resolved
return list(self._touches.keys())

@property
def touched(self):
"""Return a list of all the pins that are currently registering a touch"""
tekktrik marked this conversation as resolved.
Show resolved Hide resolved
return [pin for pin, touchpad in self._touches.items() if touchpad.value]

@property
def pixels(self):
"""Sequence-like object representing the ten NeoPixels around the outside
Expand Down
42 changes: 28 additions & 14 deletions examples/circuitplayground_touch_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,34 @@
# SPDX-License-Identifier: MIT

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


# 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_a1_touched = cp.touch_A1 # This result can be used if you want
if is_a1_touched:
print("A1 was touched upon startup!")
is_a2_touched = cp.touch_A2
is_a3_touched = cp.touch_A3
is_a4_touched = cp.touch_A4

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

while True:
if cp.touch_A1:
print("Touched pad A1")
if cp.touch_A2:
print("Touched pad A2")
if cp.touch_A3:
print("Touched pad A3")
if cp.touch_A4:
print("Touched pad A4")
if cp.touch_A5:
print("Touched pad A5")
if cp.touch_A6:
print("Touched pad A6")
if cp.touch_TX:
print("Touched pad TX")

current_touched = cp.touched

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going back to test this again. noticed small typo here, regustering instead of registering

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ha! Good catch.


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

time.sleep(0.25)