|
| 1 | +# SPDX-FileCopyrightText: 2024 DJDevon3 |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +import time |
| 4 | +import displayio |
| 5 | +import terminalio |
| 6 | +import board |
| 7 | +import digitalio |
| 8 | +from adafruit_hx8357 import HX8357 # TFT Featherwing display driver |
| 9 | +import adafruit_stmpe610 # TFT Featherwing V1 touch driver |
| 10 | +from adafruit_button.sprite_button import SpriteButton |
| 11 | + |
| 12 | +# 3.5" TFT Featherwing is 480x320 |
| 13 | +displayio.release_displays() |
| 14 | +DISPLAY_WIDTH = 480 |
| 15 | +DISPLAY_HEIGHT = 320 |
| 16 | + |
| 17 | +# Initialize TFT Display |
| 18 | +spi = board.SPI() |
| 19 | +tft_cs = board.D9 |
| 20 | +tft_dc = board.D10 |
| 21 | +display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs) |
| 22 | +display = HX8357(display_bus, width=DISPLAY_WIDTH, height=DISPLAY_HEIGHT) |
| 23 | +display.rotation = 0 |
| 24 | +_touch_flip = (False, True) |
| 25 | + |
| 26 | +# Initialize 3.5" TFT Featherwing Touchscreen |
| 27 | +ts_cs_pin = digitalio.DigitalInOut(board.D6) |
| 28 | +touchscreen = adafruit_stmpe610.Adafruit_STMPE610_SPI( |
| 29 | + board.SPI(), |
| 30 | + ts_cs_pin, |
| 31 | + calibration=((231, 3703), (287, 3787)), |
| 32 | + size=(display.width, display.height), |
| 33 | + disp_rotation=display.rotation, |
| 34 | + touch_flip=_touch_flip, |
| 35 | +) |
| 36 | + |
| 37 | +TEXT_WHITE = 0xFFFFFF |
| 38 | + |
| 39 | +# --| Button Config |-- |
| 40 | +BUTTON_WIDTH = 7 * 16 |
| 41 | +BUTTON_HEIGHT = 2 * 16 |
| 42 | +BUTTON_MARGIN = 5 |
| 43 | + |
| 44 | +# Defiine the button |
| 45 | +button = SpriteButton( |
| 46 | + x=BUTTON_MARGIN, |
| 47 | + y=BUTTON_MARGIN, |
| 48 | + width=BUTTON_WIDTH, |
| 49 | + height=BUTTON_HEIGHT, |
| 50 | + label="MENU", |
| 51 | + label_font=terminalio.FONT, |
| 52 | + label_color=TEXT_WHITE, |
| 53 | + bmp_path="bmps/gradient_button_0.bmp", |
| 54 | + selected_bmp_path="bmps/gradient_button_1.bmp", |
| 55 | + transparent_index=0, |
| 56 | +) |
| 57 | + |
| 58 | +main_group = displayio.Group() |
| 59 | +main_group.append(button) |
| 60 | +display.root_group = main_group |
| 61 | + |
| 62 | +while True: |
| 63 | + p = touchscreen.touch_point |
| 64 | + if p: |
| 65 | + if button.contains(p): |
| 66 | + if not button.selected: |
| 67 | + button.selected = True |
| 68 | + time.sleep(0.25) # Wait a bit so we can see the button color change |
| 69 | + print("Button Pressed") |
| 70 | + else: |
| 71 | + button.selected = False # When touch moves outside of button |
| 72 | + else: |
| 73 | + button.selected = False # When button is released |
0 commit comments