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