|
| 1 | +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +""" |
| 4 | +Basic debounced button example. |
| 5 | +""" |
| 6 | + |
| 7 | +import adafruit_touchscreen |
| 8 | +import board |
| 9 | +import displayio |
| 10 | +import terminalio |
| 11 | + |
| 12 | +from adafruit_button import Button |
| 13 | + |
| 14 | +# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.) |
| 15 | +# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.) |
| 16 | +# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus |
| 17 | +display = board.DISPLAY |
| 18 | + |
| 19 | +# --| Button Config |------------------------------------------------- |
| 20 | +BUTTON_X = 110 |
| 21 | +BUTTON_Y = 95 |
| 22 | +BUTTON_WIDTH = 100 |
| 23 | +BUTTON_HEIGHT = 50 |
| 24 | +BUTTON_STYLE = Button.ROUNDRECT |
| 25 | +BUTTON_FILL_COLOR = 0x00FFFF |
| 26 | +BUTTON_OUTLINE_COLOR = 0xFF00FF |
| 27 | +BUTTON_LABEL = "HELLO WORLD" |
| 28 | +BUTTON_LABEL_COLOR = 0x000000 |
| 29 | +# --| Button Config |------------------------------------------------- |
| 30 | + |
| 31 | +# Setup touchscreen (PyPortal) |
| 32 | +ts = adafruit_touchscreen.Touchscreen( |
| 33 | + board.TOUCH_XL, |
| 34 | + board.TOUCH_XR, |
| 35 | + board.TOUCH_YD, |
| 36 | + board.TOUCH_YU, |
| 37 | + calibration=((5200, 59000), (5800, 57000)), |
| 38 | + size=(display.width, display.height), |
| 39 | +) |
| 40 | + |
| 41 | +# Make the display context |
| 42 | +splash = displayio.Group() |
| 43 | +display.root_group = splash |
| 44 | + |
| 45 | +# Make the button |
| 46 | +button = Button( |
| 47 | + x=BUTTON_X, |
| 48 | + y=BUTTON_Y, |
| 49 | + width=BUTTON_WIDTH, |
| 50 | + height=BUTTON_HEIGHT, |
| 51 | + style=BUTTON_STYLE, |
| 52 | + fill_color=BUTTON_FILL_COLOR, |
| 53 | + outline_color=BUTTON_OUTLINE_COLOR, |
| 54 | + label=BUTTON_LABEL, |
| 55 | + label_font=terminalio.FONT, |
| 56 | + label_color=BUTTON_LABEL_COLOR, |
| 57 | +) |
| 58 | + |
| 59 | +# Add button to the display context |
| 60 | +splash.append(button) |
| 61 | + |
| 62 | +# Loop and look for touches |
| 63 | +while True: |
| 64 | + p = ts.touch_point |
| 65 | + if p: |
| 66 | + if button.contains(p): |
| 67 | + if not button.selected: |
| 68 | + button.selected = True |
| 69 | + print("pressed") |
| 70 | + else: |
| 71 | + button.selected = False # if touch is dragged outside of button |
| 72 | + else: |
| 73 | + button.selected = False # if touch is released |
0 commit comments