|
| 1 | +# SPDX-FileCopyrightText: 2024 Liz Clark for Adafruit Industries |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +""" |
| 5 | +Touch buttons example for HX83570 + FT5336 TFT Breakout |
| 6 | +""" |
| 7 | + |
| 8 | +import time |
| 9 | +import board |
| 10 | +import displayio |
| 11 | +import terminalio |
| 12 | +from adafruit_hx8357 import HX8357 |
| 13 | +from adafruit_button import Button |
| 14 | +import adafruit_ft5336 |
| 15 | + |
| 16 | +displayio.release_displays() |
| 17 | + |
| 18 | +spi = board.SPI() |
| 19 | +# for eyespi bff |
| 20 | +# tft_cs = board.TX |
| 21 | +# tft_dc = board.RX |
| 22 | +# else: |
| 23 | +tft_cs = board.D9 |
| 24 | +tft_dc = board.D10 |
| 25 | + |
| 26 | +display_bus = displayio.FourWire(spi, command=tft_dc, chip_select=tft_cs) |
| 27 | +# display is rotated to align x, y with touch screen x, y |
| 28 | +display = HX8357(display_bus, width=320, height=480, rotation=270) |
| 29 | + |
| 30 | +i2c = board.I2C() # uses board.SCL and board.SDA |
| 31 | +touch = adafruit_ft5336.Adafruit_FT5336(i2c) |
| 32 | + |
| 33 | +splash = displayio.Group() |
| 34 | +display.root_group = splash |
| 35 | + |
| 36 | +RED = (255, 0, 0) |
| 37 | +YELLOW = (255, 255, 0) |
| 38 | +GREEN = (0, 255, 0) |
| 39 | +BLUE = (0, 0, 255) |
| 40 | + |
| 41 | +spots = [ |
| 42 | + {"label": "1", "pos": (10, 10), "color": RED}, |
| 43 | + {"label": "2", "pos": (165, 10), "color": YELLOW}, |
| 44 | + {"label": "3", "pos": (10, 245), "color": GREEN}, |
| 45 | + {"label": "4", "pos": (165, 245), "color": BLUE}, |
| 46 | +] |
| 47 | + |
| 48 | +buttons = [] |
| 49 | +for spot in spots: |
| 50 | + button = Button( |
| 51 | + x=spot["pos"][0], |
| 52 | + y=spot["pos"][1], |
| 53 | + width=145, |
| 54 | + height=225, |
| 55 | + style=Button.ROUNDRECT, |
| 56 | + fill_color=spot["color"], |
| 57 | + outline_color=0xFFFFFF, |
| 58 | + label=spot["label"], |
| 59 | + label_font=terminalio.FONT, |
| 60 | + label_color=0x000000, |
| 61 | + ) |
| 62 | + splash.append(button) |
| 63 | + buttons.append(button) |
| 64 | + |
| 65 | +display.root_group = splash |
| 66 | + |
| 67 | +button_states = [False for _ in buttons] |
| 68 | + |
| 69 | +while True: |
| 70 | + if touch.touched: |
| 71 | + t = touch.points |
| 72 | + print(t) |
| 73 | + # reset state |
| 74 | + button_states = [False for _ in buttons] |
| 75 | + for point in t: |
| 76 | + for button_index, button in enumerate(buttons): |
| 77 | + if button.contains(point[0:2]): |
| 78 | + # if button contains point, set state to True |
| 79 | + button_states[button_index] = True |
| 80 | + break |
| 81 | + # selected state == button state |
| 82 | + for button_index, button in enumerate(buttons): |
| 83 | + button.selected = button_states[button_index] |
| 84 | + else: |
| 85 | + # if no touch points, then no buttons are selected |
| 86 | + for button in buttons: |
| 87 | + button.selected = False |
| 88 | + |
| 89 | + time.sleep(0.1) |
0 commit comments