From 579cd86465a3eb4091db53327efad54582d35c2d Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 15:37:53 -0500 Subject: [PATCH 01/18] Add IterableInput functionality --- .../circuit_playground_base.py | 105 +++++++++++++++--- 1 file changed, 90 insertions(+), 15 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 69b5188..79b0723 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -32,6 +32,12 @@ import neopixel import touchio +try: + from typing import List + from microcontroller import Pin +except ImportError: + pass + __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground.git" @@ -50,6 +56,65 @@ def light(self): return self._photocell.value * 330 // (2 ** 16) +class InterableInput: + + def __init__(self, name_list: List[str]): + self._input_names = name_list + input_pins = [getattr(board, name) for name in name_list] + self._inputs = [touchio.TouchIn(pin) for pin in input_pins] + self._current_input = 0 + self._len_inputs = len(name_list) + + def __iter__(self): + return self + + def __next__(self): + for input_tio in self._inputs: + yield input_tio + + def __getitem__(self, index): + input_name = self._use_str_name(index) + if isinstance(index, str): + for name, tio in zip(self._input_names, self._inputs): + if name == input_name: + return tio + raise ValueError( + "The given padname is either not a valid touchpad or was deinitialized" + ) + raise TypeError("Pin must be access either by int index or analog string name") + + def __contains__(self, value): + input_name = self._use_str_name(value) + return input_name in self._input_names + + def _use_str_name(self, value): + if isinstance(value, int): + return "A" + str(value) + if isinstance(value, str): + if value.startswith("A"): + return value + raise TypeError("Iterable inputs can only be accessed by int index or analog string names") + + def deinit_input(self, input_name): + input_name = self._use_str_name(input_name) + if input_name in self._input_names: + input_index = self._input_names.index(input_name) + self._input_names.pop(input_index) + selected_tio = self._inputs.pop(input_index) + selected_tio.deinit() + + def init_input(self, input_name): + input_name = self._use_str_name(input_name) + if input_name not in self._input_names: + self._input_names.append(input_name) + input_pin = getattr(board, input_name) + self._inputs.append(touchio.TouchIn(input_pin)) + + @property + def names(self): + return self._input_names + + class CircuitPlaygroundBase: # pylint: disable=too-many-public-methods """Circuit Playground base class.""" @@ -83,16 +148,17 @@ 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 = InterableInput( + [ + "A1", + "A2", + "A3", + "A4", + "A5", + "A6", + "A7", + ] + ) self._touch_threshold_adjustment = 0 # Define acceleration: @@ -351,13 +417,22 @@ 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 deinit_touchpad(self, touchpad_pin): + self._touches.deinit_input(touchpad_pin) + + def init_touchpad(self, touchpad_pin): + self._touches.init_input(touchpad_pin) + + @property + def touched(self): + return [touch_pad for touch_pad in self._touches if touch_pad.value] + + @property + def touched_names(self): + list_touched = [touch_name for touch_pad, touch_name in zip(self._touches, self._touches.names) if touch_pad.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 # style, so everywhere we use these names, we whitelist the errors using: From 6613a9fd8ed1ff85231319bf2e59dde904d38119 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:06:49 -0500 Subject: [PATCH 02/18] Fix iteration --- 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 79b0723..4262e28 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -66,10 +66,8 @@ def __init__(self, name_list: List[str]): self._len_inputs = len(name_list) def __iter__(self): - return self - - def __next__(self): for input_tio in self._inputs: + print(input_tio) yield input_tio def __getitem__(self, index): From 729d1159c636e16987cb2ef5b01c8e66f999798d Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:07:18 -0500 Subject: [PATCH 03/18] Remove checking type argument for IterableInput.__getitem__() --- .../circuit_playground_base.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 4262e28..f2c786f 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -72,14 +72,12 @@ def __iter__(self): def __getitem__(self, index): input_name = self._use_str_name(index) - if isinstance(index, str): - for name, tio in zip(self._input_names, self._inputs): - if name == input_name: - return tio - raise ValueError( - "The given padname is either not a valid touchpad or was deinitialized" - ) - raise TypeError("Pin must be access either by int index or analog string name") + for name, tio in zip(self._input_names, self._inputs): + if name == input_name: + return tio + raise ValueError( + "The given padname is either not a valid touchpad or was deinitialized" + ) def __contains__(self, value): input_name = self._use_str_name(value) From 3e617ef7523eb60eff3cb7a07c2176b23827300a Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:08:14 -0500 Subject: [PATCH 04/18] Fix how converting to string name works for IterableInput index --- adafruit_circuitplayground/circuit_playground_base.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index f2c786f..5e17b49 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -84,11 +84,16 @@ def __contains__(self, value): return input_name in self._input_names def _use_str_name(self, value): + if value == 7 or value == "A7": + return "TX" if isinstance(value, int): + if value not in range(1, 7): + raise ValueError("Pins available as touchpads are 1-7") return "A" + str(value) if isinstance(value, str): - if value.startswith("A"): - return value + if not value.startswith("A") and int(value[1:]) not in range (1, 7): + raise ValueError("Pins available as touchpads are A1-A6 and A7/TX") + return value raise TypeError("Iterable inputs can only be accessed by int index or analog string names") def deinit_input(self, input_name): From 5681500bc2208fe99aa00a7a401f94a3300d9eb6 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:09:42 -0500 Subject: [PATCH 05/18] Only return list of pin names for IterableInput.touched --- adafruit_circuitplayground/circuit_playground_base.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 5e17b49..396e1bb 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -427,12 +427,10 @@ def init_touchpad(self, touchpad_pin): self._touches.init_input(touchpad_pin) @property - def touched(self): - return [touch_pad for touch_pad in self._touches if touch_pad.value] @property - def touched_names(self): - list_touched = [touch_name for touch_pad, touch_name in zip(self._touches, self._touches.names) if touch_pad.value] + def touched(self): + return [touch_name for touch_pad, touch_name in zip(self._touches, self._touches.names) if touch_pad.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 c55b1843c3e7e5036ce9e651f6d93e7920e1ecbe Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:09:59 -0500 Subject: [PATCH 06/18] Change name of board.A7 to board.TX --- 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 396e1bb..04857a1 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -157,7 +157,7 @@ def __init__(self): "A4", "A5", "A6", - "A7", + "TX", ] ) self._touch_threshold_adjustment = 0 From e63078e54bcd8e6784f2fe5e86bb96ec467abc60 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:10:30 -0500 Subject: [PATCH 07/18] Add IterableInput.touchpads to list all pins registered as touchpads --- 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 04857a1..ffd5ef9 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -427,6 +427,8 @@ def init_touchpad(self, touchpad_pin): self._touches.init_input(touchpad_pin) @property + def touchpads(self): + return self._touches.names @property def touched(self): From 7349f09de5eedb6a830ed3fd373ae07c43c22723 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:51:54 -0500 Subject: [PATCH 08/18] Add and update docstring documentation --- .../circuit_playground_base.py | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index ffd5ef9..7a4fa41 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -57,6 +57,10 @@ def light(self): class InterableInput: + """Wrapper class for iterable touchpad inputs + + :param name_list: A list of pin names to initialize as touchpad inputs + """ def __init__(self, name_list: List[str]): self._input_names = name_list @@ -84,6 +88,8 @@ def __contains__(self, value): return input_name in self._input_names def _use_str_name(self, value): + """Converts an index into the pin name if needed""" + if value == 7 or value == "A7": return "TX" if isinstance(value, int): @@ -97,6 +103,13 @@ def _use_str_name(self, value): raise TypeError("Iterable inputs can only be accessed by int index or analog string names") def deinit_input(self, input_name): + """Deinitialize a given pin as a touchpad input, freeing up the memory and allowing the + pin to be used as a different type of input + + :param input_name: The name or pad number to be deinitialized + :type input_name: str|int + """ + input_name = self._use_str_name(input_name) if input_name in self._input_names: input_index = self._input_names.index(input_name) @@ -105,6 +118,12 @@ def deinit_input(self, input_name): selected_tio.deinit() def init_input(self, input_name): + """Initializes a given pin as a touchpad input, if not already + + :param input_name: The name or pad number to be initialized + :type input_name: str|int + """ + input_name = self._use_str_name(input_name) if input_name not in self._input_names: self._input_names.append(input_name) @@ -113,6 +132,7 @@ def init_input(self, input_name): @property def names(self): + """Returns the names of all pins currently set up as touchpad inputs""" return self._input_names @@ -143,11 +163,9 @@ 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, ...] + # Initially, IterableInput stores the pin used for a particular touch. When that touch is + # used for the first time, the stored pin is replaced with the corresponding TouchIn object. + # This saves a little RAM over initializing all pins as inputs immediately. # Slot 0 is not used (A0 is not allowed as a touch pin). self._touches = InterableInput( [ @@ -421,17 +439,33 @@ def _touch(self, i): return self._touches[i].value def deinit_touchpad(self, touchpad_pin): + """Deinitializes an input as a touchpad to free it up for use elsewhere + + :param touchpad_pin: The touchpad name or number to deinitialize + :type touchpad_pin: str|int + """ + self._touches.deinit_input(touchpad_pin) def init_touchpad(self, touchpad_pin): + """Initializes a pin as a touchpad input + + :param touchpad_pin: The touchpad name or number to initialize + :type touchpad_pin: str|int + """ + self._touches.init_input(touchpad_pin) @property def touchpads(self): + """A list of all touchpad names currently set up as touchpad inputs""" + return self._touches.names @property def touched(self): + """A list of touchpad input names currently registering as being touched""" + return [touch_name for touch_pad, touch_name in zip(self._touches, self._touches.names) if touch_pad.value] # We chose these verbose touch_A# names so that beginners could use it without understanding From ced6010f38e0617072d47cefd2ef0660fa9e2895 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:52:37 -0500 Subject: [PATCH 09/18] Remove IterableInput.__contains__() --- adafruit_circuitplayground/circuit_playground_base.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 7a4fa41..f1bc822 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -83,9 +83,6 @@ def __getitem__(self, index): "The given padname is either not a valid touchpad or was deinitialized" ) - def __contains__(self, value): - input_name = self._use_str_name(value) - return input_name in self._input_names def _use_str_name(self, value): """Converts an index into the pin name if needed""" From 9ed4aa40ba91dbf9d4beabe23687a245d77edc7b Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:53:01 -0500 Subject: [PATCH 10/18] Remove debug print statement --- adafruit_circuitplayground/circuit_playground_base.py | 1 - 1 file changed, 1 deletion(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index f1bc822..6556fb4 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -71,7 +71,6 @@ def __init__(self, name_list: List[str]): def __iter__(self): for input_tio in self._inputs: - print(input_tio) yield input_tio def __getitem__(self, index): From 808c78a3258fe42a6edb12d4e2059c8ed44bb8ce Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:54:36 -0500 Subject: [PATCH 11/18] Use combination of board.Pin and touchio.TouchIn, only initializing and deinitialize as needed --- .../circuit_playground_base.py | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 6556fb4..44e7ea6 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -65,23 +65,33 @@ class InterableInput: def __init__(self, name_list: List[str]): self._input_names = name_list input_pins = [getattr(board, name) for name in name_list] - self._inputs = [touchio.TouchIn(pin) for pin in input_pins] + self._inputs = [pin for pin in input_pins] self._current_input = 0 self._len_inputs = len(name_list) def __iter__(self): for input_tio in self._inputs: - yield input_tio + yield self._auto_convert_tio(input_tio) def __getitem__(self, index): input_name = self._use_str_name(index) for name, tio in zip(self._input_names, self._inputs): if name == input_name: - return tio + return self._auto_convert_tio(tio) raise ValueError( "The given padname is either not a valid touchpad or was deinitialized" ) + def _auto_convert_tio(self, input_pad): + """Automagically turns an existing pin into a touchio.TouchIn as needed, and + also returns it + """ + + if not isinstance(input_pad, touchio.TouchIn): + name_index = self._inputs.index(input_pad) + self._inputs[name_index] = touchio.TouchIn(self._inputs[name_index]) + input_pad = self._inputs[name_index] + return input_pad def _use_str_name(self, value): """Converts an index into the pin name if needed""" @@ -111,7 +121,8 @@ def deinit_input(self, input_name): input_index = self._input_names.index(input_name) self._input_names.pop(input_index) selected_tio = self._inputs.pop(input_index) - selected_tio.deinit() + if isinstance(selected_tio, touchio.TouchIn): + selected_tio.deinit() def init_input(self, input_name): """Initializes a given pin as a touchpad input, if not already @@ -122,9 +133,8 @@ def init_input(self, input_name): input_name = self._use_str_name(input_name) if input_name not in self._input_names: + self._inputs.append(getattr(board, input_name)) self._input_names.append(input_name) - input_pin = getattr(board, input_name) - self._inputs.append(touchio.TouchIn(input_pin)) @property def names(self): From 19aaca1bc1db911dce3f1fa48231ab66d67d80d7 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:55:22 -0500 Subject: [PATCH 12/18] Remove typing imports and type annotations Can be added in a different PR --- adafruit_circuitplayground/circuit_playground_base.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 44e7ea6..68f3816 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -32,12 +32,6 @@ import neopixel import touchio -try: - from typing import List - from microcontroller import Pin -except ImportError: - pass - __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_CircuitPlayground.git" @@ -62,7 +56,7 @@ class InterableInput: :param name_list: A list of pin names to initialize as touchpad inputs """ - def __init__(self, name_list: List[str]): + def __init__(self, name_list): self._input_names = name_list input_pins = [getattr(board, name) for name in name_list] self._inputs = [pin for pin in input_pins] From 5b2cca9647ad11c74894c494c1118fd81b6705fe Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:55:35 -0500 Subject: [PATCH 13/18] Fix range() --- 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 68f3816..6d60dd7 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -97,7 +97,7 @@ def _use_str_name(self, value): raise ValueError("Pins available as touchpads are 1-7") return "A" + str(value) if isinstance(value, str): - if not value.startswith("A") and int(value[1:]) not in range (1, 7): + if not value.startswith("A") and int(value[1:]) not in range(1, 7): raise ValueError("Pins available as touchpads are A1-A6 and A7/TX") return value raise TypeError("Iterable inputs can only be accessed by int index or analog string names") From b965fdc3483f39a08aa1866aa9b59bab1ca196c3 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 20:55:44 -0500 Subject: [PATCH 14/18] Remove spaces in line --- 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 6d60dd7..5994c5a 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -459,7 +459,7 @@ def init_touchpad(self, touchpad_pin): @property def touchpads(self): """A list of all touchpad names currently set up as touchpad inputs""" - + return self._touches.names @property From db594248442310363b9a16927f9fc730cb699d36 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 21:16:43 -0500 Subject: [PATCH 15/18] Linted and reformatted per pre-commit --- .../circuit_playground_base.py | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/adafruit_circuitplayground/circuit_playground_base.py b/adafruit_circuitplayground/circuit_playground_base.py index 5994c5a..5bd525b 100755 --- a/adafruit_circuitplayground/circuit_playground_base.py +++ b/adafruit_circuitplayground/circuit_playground_base.py @@ -52,14 +52,14 @@ def light(self): class InterableInput: """Wrapper class for iterable touchpad inputs - + :param name_list: A list of pin names to initialize as touchpad inputs """ def __init__(self, name_list): self._input_names = name_list input_pins = [getattr(board, name) for name in name_list] - self._inputs = [pin for pin in input_pins] + self._inputs = list(input_pins) self._current_input = 0 self._len_inputs = len(name_list) @@ -74,7 +74,7 @@ def __getitem__(self, index): return self._auto_convert_tio(tio) raise ValueError( "The given padname is either not a valid touchpad or was deinitialized" - ) + ) def _auto_convert_tio(self, input_pad): """Automagically turns an existing pin into a touchio.TouchIn as needed, and @@ -87,10 +87,11 @@ def _auto_convert_tio(self, input_pad): input_pad = self._inputs[name_index] return input_pad - def _use_str_name(self, value): + @staticmethod + def _use_str_name(value): """Converts an index into the pin name if needed""" - if value == 7 or value == "A7": + if value in (7, "A7", "TX"): return "TX" if isinstance(value, int): if value not in range(1, 7): @@ -100,12 +101,14 @@ def _use_str_name(self, value): if not value.startswith("A") and int(value[1:]) not in range(1, 7): raise ValueError("Pins available as touchpads are A1-A6 and A7/TX") return value - raise TypeError("Iterable inputs can only be accessed by int index or analog string names") + raise TypeError( + "Iterable inputs can only be accessed by int index or analog string names" + ) def deinit_input(self, input_name): """Deinitialize a given pin as a touchpad input, freeing up the memory and allowing the pin to be used as a different type of input - + :param input_name: The name or pad number to be deinitialized :type input_name: str|int """ @@ -117,10 +120,10 @@ def deinit_input(self, input_name): selected_tio = self._inputs.pop(input_index) if isinstance(selected_tio, touchio.TouchIn): selected_tio.deinit() - + def init_input(self, input_name): """Initializes a given pin as a touchpad input, if not already - + :param input_name: The name or pad number to be initialized :type input_name: str|int """ @@ -223,7 +226,7 @@ def detect_taps(self): @staticmethod def _default_tap_threshold(tap): if ( - "nRF52840" in os.uname().machine + "nRF52840" in os.uname().machine # pylint: disable=no-member ): # If we're on a CPB, use a higher tap threshold return 100 if tap == 1 else 70 @@ -440,7 +443,7 @@ def _touch(self, i): def deinit_touchpad(self, touchpad_pin): """Deinitializes an input as a touchpad to free it up for use elsewhere - + :param touchpad_pin: The touchpad name or number to deinitialize :type touchpad_pin: str|int """ @@ -449,7 +452,7 @@ def deinit_touchpad(self, touchpad_pin): def init_touchpad(self, touchpad_pin): """Initializes a pin as a touchpad input - + :param touchpad_pin: The touchpad name or number to initialize :type touchpad_pin: str|int """ @@ -466,7 +469,11 @@ def touchpads(self): def touched(self): """A list of touchpad input names currently registering as being touched""" - return [touch_name for touch_pad, touch_name in zip(self._touches, self._touches.names) if touch_pad.value] + return [ + touch_name + for touch_pad, touch_name in zip(self._touches, self._touches.names) + if touch_pad.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 64869b519a86589843fe4562af36227cd2103160 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 21:19:21 -0500 Subject: [PATCH 16/18] Update touch all example to use new IterableInput features --- examples/circuitplayground_touch_all.py | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/examples/circuitplayground_touch_all.py b/examples/circuitplayground_touch_all.py index a8a3b3f..fc729b4 100644 --- a/examples/circuitplayground_touch_all.py +++ b/examples/circuitplayground_touch_all.py @@ -4,18 +4,9 @@ """This example prints to the serial console when you touch the capacitive touch pads.""" from adafruit_circuitplayground import cp +print("Here are all the possible touchpads:") +print(cp.touchpads) + 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("Touchpads currently registering a touch:") + print(cp.touched) From 8d63f8e4f3226fe6ae68dbeeaa8267216b462a28 Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 21:25:39 -0500 Subject: [PATCH 17/18] Adding example use of all() to touch all example --- examples/circuitplayground_touch_all.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/examples/circuitplayground_touch_all.py b/examples/circuitplayground_touch_all.py index fc729b4..21a5e8a 100644 --- a/examples/circuitplayground_touch_all.py +++ b/examples/circuitplayground_touch_all.py @@ -10,3 +10,6 @@ while True: print("Touchpads currently registering a touch:") print(cp.touched) + + if all(pad in cp.touched for pad in ("A2", "A3", "A4")): + print("This only prints when A2, A3, and A4 are being held at the same time!") From 78ff7fbcc9a8f8496d7e75440dab0996547144da Mon Sep 17 00:00:00 2001 From: Alec Delaney Date: Mon, 31 Jan 2022 21:27:22 -0500 Subject: [PATCH 18/18] Add example for initializing and deinitializing touchpad inputs --- examples/circuitplayground_touchpad_changing.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 examples/circuitplayground_touchpad_changing.py diff --git a/examples/circuitplayground_touchpad_changing.py b/examples/circuitplayground_touchpad_changing.py new file mode 100644 index 0000000..dfce19f --- /dev/null +++ b/examples/circuitplayground_touchpad_changing.py @@ -0,0 +1,17 @@ +# SPDX-FileCopyrightText: 2021 Alec Delaney +# SPDX-License-Identifier: MIT + +"""This example prints to the serial console when you touch the capacitive touch pads.""" +from adafruit_circuitplayground import cp + +print("Here are all the initially registered touchpads:") +print(cp.touchpads) + +print("You can remove a few if you need those pins:") +cp.deinit_touchpad("A2") +cp.deinit_touchpad("A5") +print(cp.touchpads) + +print("You can also readd them later!") +cp.init_touchpad("A2") +print(cp.touchpads)