|
| 1 | +"""CircuitPython Touch-Compatible Pin Identification Script""" |
| 2 | +import board |
| 3 | +import touchio |
| 4 | +from microcontroller import Pin |
| 5 | + |
| 6 | + |
| 7 | +def is_touch_capable(pin_name): |
| 8 | + """Attempts to create touchio.TouchIn() object on all available pins. Returns True if valid.""" |
| 9 | + try: |
| 10 | + _ = touchio.TouchIn(pin_name) |
| 11 | + # Print the touch-capable pins that do not need, or already have, an external pulldown. |
| 12 | + return True |
| 13 | + except ValueError as e: # A ValueError is raised when a pin is invalid or needs a pulldown. |
| 14 | + x = getattr(e, "message", str(e)) # Obtain the message associated with the ValueError. |
| 15 | + if "pulldown" in x: # If the ValueError is regarding needing a pulldown... |
| 16 | + return True # ...the pin is valid. |
| 17 | + else: |
| 18 | + return False # Otherwise, the pins are invalid. |
| 19 | + except TypeError: # Error returned when checking a non-pin object in dir(board). |
| 20 | + pass # Passes over non-pin objects in dir(board). |
| 21 | + |
| 22 | + |
| 23 | +def get_pin_names(): |
| 24 | + """Gets all unique pin names available in the board module, excluding a defined list.""" |
| 25 | + exclude = ["NEOPIXEL", "APA102_MOSI", "APA102_SCK", "LED", "NEOPIXEL_POWER", "BUTTON", |
| 26 | + "BUTTON_UP", "BUTTON_DOWN", "BUTTON_SELECT", "DOTSTAR_CLOCK", "DOTSTAR_DATA", |
| 27 | + "IR_PROXIMITY"] |
| 28 | + pins = [pin for pin in [getattr(board, p) for p in dir(board) if p not in exclude] |
| 29 | + if isinstance(pin, Pin)] |
| 30 | + pin_names = [] |
| 31 | + for p in pins: |
| 32 | + if p not in pin_names: |
| 33 | + pin_names.append(p) |
| 34 | + return pin_names |
| 35 | + |
| 36 | + |
| 37 | +for possible_touch_pin in get_pin_names(): # Get the pin name. |
| 38 | + if is_touch_capable(possible_touch_pin): # Check if the pin is touch-capable. |
| 39 | + print("Touch on:", str(possible_touch_pin).replace("board.", "")) # Print the valid list. |
0 commit comments