From c71fa2cd2f50666c89ba3361ecb49b768bacca8c Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 18 Feb 2022 15:15:55 -0500 Subject: [PATCH 01/22] Add iterable return touchpad properties --- adafruit_circuitplayground/circuit_playground_base.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 69b5188..0d52ba5 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -521,6 +521,14 @@ def adjust_touch_threshold(self, adjustment): touch_in.threshold += adjustment self._touch_threshold_adjustment += adjustment + @property + def touch_pins(self): + return [touch for touch in self._touches if isinstance(touch, touchio.TouchIn)] + + @property + def touched(self): + return [touch for touch in self.touch_pins if touch.value] + @property def pixels(self): """Sequence-like object representing the ten NeoPixels around the outside From d8c128cefc0f2c5c68e599362d64c1175654221c Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 18 Feb 2022 16:37:53 -0500 Subject: [PATCH 02/22] Fix touch_pins, touched properties Now return board.XX! --- .../circuit_playground_base.py | 52 ++++++++++--------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 0d52ba5..9af1521 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -83,16 +83,15 @@ def __init__(self): # 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, - ] + self._touches = { + "A1": board.A1, + "A2": board.A2, + "A3": board.A3, + "A4": board.A4, + "A5": board.A5, + "A6": board.A6, + "TX": board.TX, + } self._touch_threshold_adjustment = 0 # Define acceleration: @@ -350,13 +349,13 @@ 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): + def _touch(self, pinname): + if not isinstance(self._touches[pinname], 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 + self._touches[pinname] = touchio.TouchIn(self._touches[pinname]) + self._touches[pinname].threshold += self._touch_threshold_adjustment + return self._touches[pinname].value # 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 @@ -378,7 +377,7 @@ def touch_A1(self): # pylint: disable=invalid-name if cp.touch_A1: print('Touched pad A1') """ - return self._touch(1) + return self._touch("A1") @property def touch_A2(self): # pylint: disable=invalid-name @@ -397,7 +396,7 @@ def touch_A2(self): # pylint: disable=invalid-name if cp.touch_A2: print('Touched pad A2') """ - return self._touch(2) + return self._touch("A2") @property def touch_A3(self): # pylint: disable=invalid-name @@ -416,7 +415,7 @@ def touch_A3(self): # pylint: disable=invalid-name if cp.touch_A3: print('Touched pad A3') """ - return self._touch(3) + return self._touch("A3") @property def touch_A4(self): # pylint: disable=invalid-name @@ -435,7 +434,7 @@ def touch_A4(self): # pylint: disable=invalid-name if cp.touch_A4: print('Touched pad A4') """ - return self._touch(4) + return self._touch("A4") @property def touch_A5(self): # pylint: disable=invalid-name @@ -454,7 +453,7 @@ def touch_A5(self): # pylint: disable=invalid-name if cp.touch_A5: print('Touched pad A5') """ - return self._touch(5) + return self._touch("A5") @property def touch_A6(self): # pylint: disable=invalid-name @@ -473,7 +472,7 @@ def touch_A6(self): # pylint: disable=invalid-name if cp.touch_A6: print('Touched pad A6' """ - return self._touch(6) + return self._touch("A6") @property def touch_TX(self): # pylint: disable=invalid-name @@ -493,7 +492,7 @@ def touch_TX(self): # pylint: disable=invalid-name if cp.touch_A7: print('Touched pad A7') """ - return self._touch(7) + return self._touch("TX") def adjust_touch_threshold(self, adjustment): """Adjust the threshold needed to activate the capacitive touch pads. @@ -516,18 +515,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 self._touch_threshold_adjustment += adjustment + def _get_active_touchpad_items(self): + return [(pinname, touchpad) for pinname, touchpad in self._touches.items() if isinstance(touchpad, touchio.TouchIn)] + @property def touch_pins(self): - return [touch for touch in self._touches if isinstance(touch, touchio.TouchIn)] + return [getattr(board, pinname) for pinname, _ in self._get_active_touchpad_items()] @property def touched(self): - return [touch for touch in self.touch_pins if touch.value] + return [getattr(board, pinname) for pinname, touchpad in self._get_active_touchpad_items() if touchpad.value] @property def pixels(self): From 4d7b49248b12d1a3f2898c3cedffa2cf37535c83 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 18 Feb 2022 16:37:59 -0500 Subject: [PATCH 03/22] Changed example for touching all pads to updated usage --- examples/circuitplayground_touch_all.py | 35 +++++++++++++++---------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/examples/circuitplayground_touch_all.py b/examples/circuitplayground_touch_all.py index a8a3b3f..6fb24cc 100644 --- a/examples/circuitplayground_touch_all.py +++ b/examples/circuitplayground_touch_all.py @@ -2,20 +2,27 @@ # SPDX-License-Identifier: MIT """This example prints to the serial console when you touch the capacitive touch pads.""" +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 keep the result though +is_a1_touched = cp.touch_A1 # This result can be saved if you want to use it like below +if is_a1_touched: + print("A1 was touched upon startup!") +cp.touch_A2 # These work exactly the same as above, but we're not storing the result +cp.touch_A3 +cp.touch_A4 + + 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") + + print("Pads that are currently setup as touchpads:") + print(cp.touch_pins) + + print("Touchpads currently registering a touch:") + print(cp.touched) + + if all(pad in cp.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!") From b68b3d2cf44cbe1cf242a7fdb65ca832d8485f37 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 18 Feb 2022 16:38:51 -0500 Subject: [PATCH 04/22] Reformatted per pre-commit --- .../circuit_playground_base.py | 16 +++++++++++++--- examples/circuitplayground_touch_all.py | 4 ++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 9af1521..3ee316a 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -521,15 +521,25 @@ def adjust_touch_threshold(self, adjustment): self._touch_threshold_adjustment += adjustment def _get_active_touchpad_items(self): - return [(pinname, touchpad) for pinname, touchpad in self._touches.items() if isinstance(touchpad, touchio.TouchIn)] + return [ + (pinname, touchpad) + for pinname, touchpad in self._touches.items() + if isinstance(touchpad, touchio.TouchIn) + ] @property def touch_pins(self): - return [getattr(board, pinname) for pinname, _ in self._get_active_touchpad_items()] + return [ + getattr(board, pinname) for pinname, _ in self._get_active_touchpad_items() + ] @property def touched(self): - return [getattr(board, pinname) for pinname, touchpad in self._get_active_touchpad_items() if touchpad.value] + return [ + getattr(board, pinname) + for pinname, touchpad in self._get_active_touchpad_items() + if touchpad.value + ] @property def pixels(self): diff --git a/examples/circuitplayground_touch_all.py b/examples/circuitplayground_touch_all.py index 6fb24cc..dc667d2 100644 --- a/examples/circuitplayground_touch_all.py +++ b/examples/circuitplayground_touch_all.py @@ -8,10 +8,10 @@ # You'll need to first use the touchpads individually to register them as active touchpads # You don't have to keep the result though -is_a1_touched = cp.touch_A1 # This result can be saved if you want to use it like below +is_a1_touched = cp.touch_A1 # This result can be saved if you want to use it like below if is_a1_touched: print("A1 was touched upon startup!") -cp.touch_A2 # These work exactly the same as above, but we're not storing the result +cp.touch_A2 # These work exactly the same as above, but we're not storing the result cp.touch_A3 cp.touch_A4 From af87b00168a544921764f03b7b5320562b4eef98 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 18 Feb 2022 16:59:25 -0500 Subject: [PATCH 05/22] Prevent pointless statements --- examples/circuitplayground_touch_all.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/examples/circuitplayground_touch_all.py b/examples/circuitplayground_touch_all.py index dc667d2..ce07a57 100644 --- a/examples/circuitplayground_touch_all.py +++ b/examples/circuitplayground_touch_all.py @@ -7,13 +7,13 @@ # You'll need to first use the touchpads individually to register them as active touchpads -# You don't have to keep the result though -is_a1_touched = cp.touch_A1 # This result can be saved if you want to use it like below +# 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!") -cp.touch_A2 # These work exactly the same as above, but we're not storing the result -cp.touch_A3 -cp.touch_A4 +is_a2_touched = cp.touch_A2 +is_a3_touched = cp.touch_A3 +is_a4_touched = cp.touch_A4 while True: From aaa7582adfde0ac26d93a3d84206624b2c2e0e71 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Fri, 18 Feb 2022 16:59:38 -0500 Subject: [PATCH 06/22] Add new method docstrings --- adafruit_circuitplayground/circuit_playground_base.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 3ee316a..8cca054 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -529,12 +529,14 @@ def _get_active_touchpad_items(self): @property def touch_pins(self): + """Return a list of all the pins that are set up as touchpad inputs""" return [ getattr(board, pinname) for pinname, _ in self._get_active_touchpad_items() ] @property def touched(self): + """Return a list of all the pins that are currently registering a touch""" return [ getattr(board, pinname) for pinname, touchpad in self._get_active_touchpad_items() From 25c97ca6e06d4cc97620f3fe7da3a89455024a2b Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 21 Mar 2022 13:23:09 -0400 Subject: [PATCH 07/22] Refactor example for touch all --- examples/circuitplayground_touch_all.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/examples/circuitplayground_touch_all.py b/examples/circuitplayground_touch_all.py index ce07a57..11fe86c 100644 --- a/examples/circuitplayground_touch_all.py +++ b/examples/circuitplayground_touch_all.py @@ -15,14 +15,16 @@ 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: - print("Pads that are currently setup as touchpads:") - print(cp.touch_pins) + current_touched = cp.touched - print("Touchpads currently registering a touch:") - print(cp.touched) + if current_touched: + print("Touchpads currently registering a touch:") + print(current_touched) - if all(pad in cp.touched for pad in (board.A2, board.A3, board.A4)): + 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!") From 01ceb462348b64490f35b28119c586edea176f08 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 5 Jul 2022 19:50:30 -0400 Subject: [PATCH 08/22] Account for pins being hashable --- .../circuit_playground_base.py | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 8cca054..e410f63 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -84,13 +84,13 @@ def __init__(self): # [None, board.A1, touchio.TouchIn(board.A2), board.A3, ...] # Slot 0 is not used (A0 is not allowed as a touch pin). self._touches = { - "A1": board.A1, - "A2": board.A2, - "A3": board.A3, - "A4": board.A4, - "A5": board.A5, - "A6": board.A6, - "TX": board.TX, + board.A1: board.A1, + board.A2: board.A2, + board.A3: board.A3, + board.A4: board.A4, + board.A5: board.A5, + board.A6: board.A6, + board.TX: board.TX, } self._touch_threshold_adjustment = 0 @@ -349,13 +349,13 @@ def shake(self, shake_threshold=30): """ return self._lis3dh.shake(shake_threshold=shake_threshold) - def _touch(self, pinname): - if not isinstance(self._touches[pinname], touchio.TouchIn): + 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[pinname] = touchio.TouchIn(self._touches[pinname]) - self._touches[pinname].threshold += self._touch_threshold_adjustment - return self._touches[pinname].value + self._touches[i] = touchio.TouchIn(self._touches[i]) + self._touches[i].threshold += self._touch_threshold_adjustment + return self._touches[i].value # 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 @@ -377,7 +377,7 @@ def touch_A1(self): # pylint: disable=invalid-name if cp.touch_A1: print('Touched pad A1') """ - return self._touch("A1") + return self._touch(1) @property def touch_A2(self): # pylint: disable=invalid-name @@ -396,7 +396,7 @@ def touch_A2(self): # pylint: disable=invalid-name if cp.touch_A2: print('Touched pad A2') """ - return self._touch("A2") + return self._touch(2) @property def touch_A3(self): # pylint: disable=invalid-name @@ -415,7 +415,7 @@ def touch_A3(self): # pylint: disable=invalid-name if cp.touch_A3: print('Touched pad A3') """ - return self._touch("A3") + return self._touch(3) @property def touch_A4(self): # pylint: disable=invalid-name @@ -434,7 +434,7 @@ def touch_A4(self): # pylint: disable=invalid-name if cp.touch_A4: print('Touched pad A4') """ - return self._touch("A4") + return self._touch(4) @property def touch_A5(self): # pylint: disable=invalid-name @@ -453,7 +453,7 @@ def touch_A5(self): # pylint: disable=invalid-name if cp.touch_A5: print('Touched pad A5') """ - return self._touch("A5") + return self._touch(5) @property def touch_A6(self): # pylint: disable=invalid-name @@ -472,7 +472,7 @@ def touch_A6(self): # pylint: disable=invalid-name if cp.touch_A6: print('Touched pad A6' """ - return self._touch("A6") + return self._touch(6) @property def touch_TX(self): # pylint: disable=invalid-name @@ -492,7 +492,7 @@ def touch_TX(self): # pylint: disable=invalid-name if cp.touch_A7: print('Touched pad A7') """ - return self._touch("TX") + return self._touch(7) def adjust_touch_threshold(self, adjustment): """Adjust the threshold needed to activate the capacitive touch pads. @@ -531,15 +531,15 @@ def _get_active_touchpad_items(self): def touch_pins(self): """Return a list of all the pins that are set up as touchpad inputs""" return [ - getattr(board, pinname) for pinname, _ in self._get_active_touchpad_items() + pin for pin, _ in self._get_active_touchpad_items() ] @property def touched(self): """Return a list of all the pins that are currently registering a touch""" return [ - getattr(board, pinname) - for pinname, touchpad in self._get_active_touchpad_items() + pin + for pin, touchpad in self._get_active_touchpad_items() if touchpad.value ] From 39e4751a95ca697264fbb91803eb0d7625b55bac Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 5 Jul 2022 19:51:38 -0400 Subject: [PATCH 09/22] Reformatted per pre-commit --- adafruit_circuitplayground/circuit_playground_base.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 1cacb6f..13e5b5b 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -532,17 +532,13 @@ def _get_active_touchpad_items(self): @property def touch_pins(self): """Return a list of all the pins that are set up as touchpad inputs""" - return [ - pin for pin, _ in self._get_active_touchpad_items() - ] + return [pin for pin, _ in self._get_active_touchpad_items()] @property def touched(self): """Return a list of all the pins that are currently registering a touch""" return [ - pin - for pin, touchpad in self._get_active_touchpad_items() - if touchpad.value + pin for pin, touchpad in self._get_active_touchpad_items() if touchpad.value ] @property From 97d1a078e5cd53f06331cbb7514f6510443975a3 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 5 Jul 2022 21:07:14 -0400 Subject: [PATCH 10/22] Fix use of _touch() --- .../circuit_playground_base.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 13e5b5b..cfae204 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -379,7 +379,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) @property def touch_A2(self): # pylint: disable=invalid-name @@ -398,7 +398,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 @@ -417,7 +417,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 @@ -436,7 +436,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 @@ -455,7 +455,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 @@ -474,7 +474,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 @@ -494,7 +494,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. From f03a085674c81e6e1ac6fe65b2f4c8ae71bfbe41 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Tue, 5 Jul 2022 21:07:58 -0400 Subject: [PATCH 11/22] Slow down example printing and show statement if none touched --- examples/circuitplayground_touch_all.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/examples/circuitplayground_touch_all.py b/examples/circuitplayground_touch_all.py index 11fe86c..3e281d6 100644 --- a/examples/circuitplayground_touch_all.py +++ b/examples/circuitplayground_touch_all.py @@ -2,6 +2,7 @@ # 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 @@ -25,6 +26,10 @@ if current_touched: print("Touchpads currently registering a touch:") print(current_touched) + else: + print("No touchpads are currently regustering a touch.") 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) From 3e942fc316cc6615840aa473dc0553b5b634cde7 Mon Sep 17 00:00:00 2001 From: Alec Delaney <89490472+tekktrik@users.noreply.github.com> Date: Sat, 9 Jul 2022 12:45:59 -0400 Subject: [PATCH 12/22] Use list of keys instead of list comprehension Co-authored-by: Dan Halbert --- adafruit_circuitplayground/circuit_playground_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index cfae204..5653bd3 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -532,7 +532,7 @@ def _get_active_touchpad_items(self): @property def touch_pins(self): """Return a list of all the pins that are set up as touchpad inputs""" - return [pin for pin, _ in self._get_active_touchpad_items()] + return list(self._touches.keys()) @property def touched(self): From 266ef588658b9de520dcb7c8296c944a862623f3 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 9 Jul 2022 12:51:21 -0400 Subject: [PATCH 13/22] Update comment regard self._touches --- .../circuit_playground_base.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index cfae204..7b6d145 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -75,12 +75,12 @@ 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). + # 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 = { board.A1: board.A1, board.A2: board.A2, From 04776961b15604fb9774dc47a9e42eb284157803 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 9 Jul 2022 12:52:22 -0400 Subject: [PATCH 14/22] Initialize self._touches as empty dictionary --- adafruit_circuitplayground/circuit_playground_base.py | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 7b6d145..5ed5a2b 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -81,15 +81,7 @@ def __init__(self): # 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 = { - board.A1: board.A1, - board.A2: board.A2, - board.A3: board.A3, - board.A4: board.A4, - board.A5: board.A5, - board.A6: board.A6, - board.TX: board.TX, - } + self._touches = {} self._touch_threshold_adjustment = 0 # Define acceleration: From 9142472ec27648b682a78f6c7454796ebf969407 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 9 Jul 2022 12:53:41 -0400 Subject: [PATCH 15/22] Change argument name --- adafruit_circuitplayground/circuit_playground_base.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 89cb1f7..fc0be21 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -343,13 +343,13 @@ 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): + def _touch(self, pin): + if not isinstance(self._touches[pin], 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 + self._touches[pin] = touchio.TouchIn(self._touches[pin]) + self._touches[pin].threshold += self._touch_threshold_adjustment + return self._touches[pin].value # 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 From 937a4a54f7135af5686737cdd1c08545ece0a3ea Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 9 Jul 2022 12:56:17 -0400 Subject: [PATCH 16/22] Update _touch() to populate _tocuhes as needed --- adafruit_circuitplayground/circuit_playground_base.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index fc0be21..edd8ef4 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -344,11 +344,12 @@ def shake(self, shake_threshold=30): return self._lis3dh.shake(shake_threshold=shake_threshold) def _touch(self, pin): - if not isinstance(self._touches[pin], 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[pin] = touchio.TouchIn(self._touches[pin]) - self._touches[pin].threshold += self._touch_threshold_adjustment + 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 # We chose these verbose touch_A# names so that beginners could use it without understanding From 96be262209850d47fd347841bd336d13567447fe Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 9 Jul 2022 12:58:16 -0400 Subject: [PATCH 17/22] Update list comprehension --- adafruit_circuitplayground/circuit_playground_base.py | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index edd8ef4..650382c 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -515,13 +515,6 @@ def adjust_touch_threshold(self, adjustment): touch_in.threshold += adjustment self._touch_threshold_adjustment += adjustment - def _get_active_touchpad_items(self): - return [ - (pinname, touchpad) - for pinname, touchpad in self._touches.items() - if isinstance(touchpad, touchio.TouchIn) - ] - @property def touch_pins(self): """Return a list of all the pins that are set up as touchpad inputs""" @@ -531,7 +524,7 @@ def touch_pins(self): def touched(self): """Return a list of all the pins that are currently registering a touch""" return [ - pin for pin, touchpad in self._get_active_touchpad_items() if touchpad.value + pin for pin, touchpad in self._touches.items() if touchpad.value ] @property From f4f69427ceecd70cdef7ea3f27722bfec7fa63e1 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 9 Jul 2022 13:01:57 -0400 Subject: [PATCH 18/22] Reformatted per pre-commit --- adafruit_circuitplayground/circuit_playground_base.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 650382c..a4cf10b 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -523,9 +523,7 @@ def touch_pins(self): @property def touched(self): """Return a list of all the pins that are currently registering a touch""" - return [ - pin for pin, touchpad in self._touches.items() if touchpad.value - ] + return [pin for pin, touchpad in self._touches.items() if touchpad.value] @property def pixels(self): From 3c707f6982431590b8f50c8e9b5e9c200c5eeeb3 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Sat, 9 Jul 2022 18:18:01 -0400 Subject: [PATCH 19/22] Simplifications to code --- adafruit_circuitplayground/circuit_playground_base.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index a4cf10b..29c42d8 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -350,7 +350,7 @@ def _touch(self, pin): touchin = touchio.TouchIn(pin) touchin.threshold += self._touch_threshold_adjustment self._touches[pin] = touchin - return self._touches[pin].value + return touchin.value # 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 @@ -511,8 +511,7 @@ def adjust_touch_threshold(self, adjustment): print('Touched pad A1') """ for touch_in in self._touches.values(): - if isinstance(touch_in, touchio.TouchIn): - touch_in.threshold += adjustment + touch_in.threshold += adjustment self._touch_threshold_adjustment += adjustment @property From 4bdba83ac7008cdb6c80b7708bbf4837e405792d Mon Sep 17 00:00:00 2001 From: Alec Delaney <89490472+tekktrik@users.noreply.github.com> Date: Wed, 24 Aug 2022 10:22:10 -0400 Subject: [PATCH 20/22] Update adafruit_circuitplayground/circuit_playground_base.py Co-authored-by: Dan Halbert --- adafruit_circuitplayground/circuit_playground_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index acc2633..b999ade 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -523,7 +523,7 @@ def adjust_touch_threshold(self, adjustment: int) -> None: @property def touch_pins(self) -> List[Pin]: - """Return a list of all the pins that are set up as touchpad inputs""" + """A list of all the pins that are set up as touchpad inputs""" return list(self._touches.keys()) @property From 5eaaac85511c5f69c963bb6bfeb18167452335cd Mon Sep 17 00:00:00 2001 From: Alec Delaney <89490472+tekktrik@users.noreply.github.com> Date: Wed, 24 Aug 2022 10:22:17 -0400 Subject: [PATCH 21/22] Update adafruit_circuitplayground/circuit_playground_base.py Co-authored-by: Dan Halbert --- adafruit_circuitplayground/circuit_playground_base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index b999ade..5806ebf 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -528,7 +528,7 @@ def touch_pins(self) -> List[Pin]: @property def touched(self) -> List[Pin]: - """Return a list of all the pins that are currently registering a touch""" + """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 From dbfa322bc7001592f0901adef3de247b963d75f3 Mon Sep 17 00:00:00 2001 From: Alec Delaney <89490472+tekktrik@users.noreply.github.com> Date: Wed, 24 Aug 2022 19:07:48 -0400 Subject: [PATCH 22/22] Correct typo in example --- examples/circuitplayground_touch_all.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/circuitplayground_touch_all.py b/examples/circuitplayground_touch_all.py index 3e281d6..8608de7 100644 --- a/examples/circuitplayground_touch_all.py +++ b/examples/circuitplayground_touch_all.py @@ -27,7 +27,7 @@ print("Touchpads currently registering a touch:") print(current_touched) else: - print("No touchpads are currently regustering a touch.") + print("No touchpads are currently registering a touch.") 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!")