diff --git a/examples/display_button_debounced.py b/examples/display_button_debounced.py new file mode 100644 index 0000000..8a0471b --- /dev/null +++ b/examples/display_button_debounced.py @@ -0,0 +1,73 @@ +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries +# SPDX-License-Identifier: MIT +""" +Basic debounced button example. +""" + +import adafruit_touchscreen +import board +import displayio +import terminalio + +from adafruit_button import Button + +# use built in display (MagTag, PyPortal, PyGamer, PyBadge, CLUE, etc.) +# see guide for setting up external displays (TFT / OLED breakouts, RGB matrices, etc.) +# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus +display = board.DISPLAY + +# --| Button Config |------------------------------------------------- +BUTTON_X = 110 +BUTTON_Y = 95 +BUTTON_WIDTH = 100 +BUTTON_HEIGHT = 50 +BUTTON_STYLE = Button.ROUNDRECT +BUTTON_FILL_COLOR = 0x00FFFF +BUTTON_OUTLINE_COLOR = 0xFF00FF +BUTTON_LABEL = "HELLO WORLD" +BUTTON_LABEL_COLOR = 0x000000 +# --| Button Config |------------------------------------------------- + +# Setup touchscreen (PyPortal) +ts = adafruit_touchscreen.Touchscreen( + board.TOUCH_XL, + board.TOUCH_XR, + board.TOUCH_YD, + board.TOUCH_YU, + calibration=((5200, 59000), (5800, 57000)), + size=(display.width, display.height), +) + +# Make the display context +splash = displayio.Group() +display.root_group = splash + +# Make the button +button = Button( + x=BUTTON_X, + y=BUTTON_Y, + width=BUTTON_WIDTH, + height=BUTTON_HEIGHT, + style=BUTTON_STYLE, + fill_color=BUTTON_FILL_COLOR, + outline_color=BUTTON_OUTLINE_COLOR, + label=BUTTON_LABEL, + label_font=terminalio.FONT, + label_color=BUTTON_LABEL_COLOR, +) + +# Add button to the display context +splash.append(button) + +# Loop and look for touches +while True: + p = ts.touch_point + if p: + if button.contains(p): + if not button.selected: + button.selected = True + print("pressed") + else: + button.selected = False # if touch is dragged outside of button + else: + button.selected = False # if touch is released diff --git a/examples/display_button_spritebutton_debounced.py b/examples/display_button_spritebutton_debounced.py new file mode 100644 index 0000000..6d6f65f --- /dev/null +++ b/examples/display_button_spritebutton_debounced.py @@ -0,0 +1,74 @@ +# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries +# SPDX-License-Identifier: MIT +import time + +import adafruit_touchscreen +import board +import displayio +import terminalio + +from adafruit_button.sprite_button import SpriteButton + +""" +Sprite button debounced example +""" + +# These pins are used as both analog and digital! XL, XR and YU must be analog +# and digital capable. YD just need to be digital +ts = adafruit_touchscreen.Touchscreen( + board.TOUCH_XL, + board.TOUCH_XR, + board.TOUCH_YD, + board.TOUCH_YU, + calibration=((5200, 59000), (5800, 57000)), + size=(board.DISPLAY.width, board.DISPLAY.height), +) + +# Make the display context +main_group = displayio.Group() +board.DISPLAY.root_group = main_group + +BUTTON_WIDTH = 10 * 16 +BUTTON_HEIGHT = 3 * 16 +BUTTON_MARGIN = 20 + +font = terminalio.FONT + +buttons = [] + + +button_0 = SpriteButton( + x=BUTTON_MARGIN, + y=BUTTON_MARGIN, + width=BUTTON_WIDTH, + height=BUTTON_HEIGHT, + label="button0", + label_font=font, + bmp_path="bmps/gradient_button_0.bmp", + selected_bmp_path="bmps/gradient_button_1.bmp", + transparent_index=0, +) + +buttons.append(button_0) + +for b in buttons: + main_group.append(b) +while True: + p = ts.touch_point + if p: + for i, b in enumerate(buttons): + if b.contains(p): + if not b.selected: + print("Button %d pressed" % i) + b.selected = True + b.label = "pressed" + else: + b.selected = False + b.label = f"button{i}" + + else: + for i, b in enumerate(buttons): + if b.selected: + b.selected = False + b.label = f"button{i}" + time.sleep(0.01)
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: