|
| 1 | +import time |
| 2 | +import board |
| 3 | +import displayio |
| 4 | +import os |
| 5 | +from adafruit_display_text.text_area import TextArea |
| 6 | +from adafruit_bitmap_font import bitmap_font |
| 7 | +from adafruit_display_shapes.rect import Rect |
| 8 | +from adafruit_button import Button |
| 9 | +import adafruit_touchscreen |
| 10 | + |
| 11 | +# These pins are used as both analog and digital! XL, XR and YU must be analog |
| 12 | +# and digital capable. YD just need to be digital |
| 13 | +ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR, |
| 14 | + board.TOUCH_YD, board.TOUCH_YU, |
| 15 | + calibration=((5200, 59000), (5800, 57000)), |
| 16 | + size=(320, 240)) |
| 17 | + |
| 18 | +# the current working directory (where this file is) |
| 19 | +cwd = ("/"+__file__).rsplit('/', 1)[0] |
| 20 | +fonts = [file for file in os.listdir(cwd+"/fonts/") |
| 21 | + if (file.endswith(".bdf") and not file.startswith("._"))] |
| 22 | +for i, filename in enumerate(fonts): |
| 23 | + fonts[i] = cwd+"/fonts/"+filename |
| 24 | +print(fonts) |
| 25 | +THE_FONT = "/fonts/Chicago-12.bdf" |
| 26 | +DISPLAY_STRING = "Button Text" |
| 27 | + |
| 28 | +# Make the display context |
| 29 | +splash = displayio.Group(max_size=20) |
| 30 | +board.DISPLAY.show(splash) |
| 31 | +BUTTON_WIDTH = 80 |
| 32 | +BUTTON_HEIGHT = 40 |
| 33 | +BUTTON_MARGIN = 20 |
| 34 | + |
| 35 | +########################################################################## |
| 36 | +# Make a background color fill |
| 37 | + |
| 38 | +color_bitmap = displayio.Bitmap(320, 240, 1) |
| 39 | +color_palette = displayio.Palette(1) |
| 40 | +color_palette[0] = 0x404040 |
| 41 | +bg_sprite = displayio.TileGrid(color_bitmap, |
| 42 | + pixel_shader=color_palette, |
| 43 | + position=(0, 0)) |
| 44 | +print(bg_sprite.position) |
| 45 | +splash.append(bg_sprite) |
| 46 | + |
| 47 | +########################################################################## |
| 48 | + |
| 49 | +# Load the font |
| 50 | +font = bitmap_font.load_font(THE_FONT) |
| 51 | + |
| 52 | +buttons = [] |
| 53 | +# Default button styling: |
| 54 | +button_0 = Button(x=BUTTON_MARGIN, y=BUTTON_MARGIN, |
| 55 | + width=BUTTON_WIDTH, height=BUTTON_HEIGHT, |
| 56 | + label="button0", label_font=font) |
| 57 | +buttons.append(button_0) |
| 58 | + |
| 59 | +# a button with no indicators at all |
| 60 | +button_1 = Button(x=BUTTON_MARGIN*2+BUTTON_WIDTH, y=BUTTON_MARGIN, |
| 61 | + width=BUTTON_WIDTH, height=BUTTON_HEIGHT, |
| 62 | + fill_color=None, outline_color=None) |
| 63 | +buttons.append(button_1) |
| 64 | + |
| 65 | +# various colorings |
| 66 | +button_2 = Button(x=BUTTON_MARGIN*3+2*BUTTON_WIDTH, y=BUTTON_MARGIN, |
| 67 | + width=BUTTON_WIDTH, height=BUTTON_HEIGHT, |
| 68 | + label="button2", label_font=font, label_color=0x0000FF, |
| 69 | + fill_color=0x00FF00, outline_color=0xFF0000) |
| 70 | +buttons.append(button_2) |
| 71 | + |
| 72 | +# Transparent button with text |
| 73 | +button_3 = Button(x=BUTTON_MARGIN, y=BUTTON_MARGIN*2+BUTTON_HEIGHT, |
| 74 | + width=BUTTON_WIDTH, height=BUTTON_HEIGHT, |
| 75 | + label="button3", label_font=font, label_color=0x0, |
| 76 | + fill_color=None, outline_color=None) |
| 77 | +buttons.append(button_3) |
| 78 | + |
| 79 | +# a roundrect |
| 80 | +button_4 = Button(x=BUTTON_MARGIN, y=BUTTON_MARGIN*2+BUTTON_HEIGHT, |
| 81 | + width=BUTTON_WIDTH, height=BUTTON_HEIGHT, |
| 82 | + label="button4", label_font=font, style=Button.ROUNDRECT) |
| 83 | +buttons.append(button_4) |
| 84 | + |
| 85 | +# a shadowrect |
| 86 | +button_5 = Button(x=BUTTON_MARGIN*2+BUTTON_WIDTH, y=BUTTON_MARGIN*2+BUTTON_HEIGHT, |
| 87 | + width=BUTTON_WIDTH, height=BUTTON_HEIGHT, |
| 88 | + label="button5", label_font=font, style=Button.SHADOWRECT) |
| 89 | +buttons.append(button_5) |
| 90 | + |
| 91 | +# a shadowroundrect |
| 92 | +button_6 = Button(x=BUTTON_MARGIN*3+2*BUTTON_WIDTH, y=BUTTON_MARGIN*2+BUTTON_HEIGHT, |
| 93 | + width=BUTTON_WIDTH, height=BUTTON_HEIGHT, |
| 94 | + label="button5", label_font=font, style=Button.SHADOWROUNDRECT) |
| 95 | +buttons.append(button_6) |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +for b in buttons: |
| 100 | + splash.append(b.group) |
| 101 | + |
| 102 | + |
| 103 | +while True: |
| 104 | + p = ts.touch_point |
| 105 | + if p: |
| 106 | + print(p) |
| 107 | + for i, b in enumerate(buttons): |
| 108 | + if b.contains(p): |
| 109 | + print("Button %d pressed" % i) |
| 110 | + b.selected = True |
| 111 | + else: |
| 112 | + b.selected = False |
0 commit comments