Skip to content

Commit e28fefc

Browse files
committed
update examples
1 parent d46ce2c commit e28fefc

File tree

2 files changed

+133
-88
lines changed

2 files changed

+133
-88
lines changed

examples/display_button_customfont.py

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import os
2+
import board
3+
import displayio
4+
from adafruit_bitmap_font import bitmap_font
5+
from adafruit_button import Button
6+
import adafruit_touchscreen
7+
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
10+
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
11+
board.TOUCH_YD, board.TOUCH_YU,
12+
calibration=((5200, 59000), (5800, 57000)),
13+
size=(320, 240))
14+
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+
# Make the display context
26+
splash = displayio.Group(max_size=20)
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+
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)
93+
94+
for b in buttons:
95+
splash.append(b.group)
96+
97+
while True:
98+
p = ts.touch_point
99+
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

examples/display_button_simpletest.py

+27-88
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,45 @@
1-
import os
21
import board
32
import displayio
4-
from adafruit_bitmap_font import bitmap_font
3+
import terminalio
54
from adafruit_button import Button
65
import adafruit_touchscreen
76

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)
1020
ts = adafruit_touchscreen.Touchscreen(board.TOUCH_XL, board.TOUCH_XR,
1121
board.TOUCH_YD, board.TOUCH_YU,
1222
calibration=((5200, 59000), (5800, 57000)),
1323
size=(320, 240))
1424

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-
2525
# Make the display context
26-
splash = displayio.Group(max_size=20)
26+
splash = displayio.Group()
2727
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)
8728

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)
9334

94-
for b in buttons:
95-
splash.append(b.group)
35+
# Add button to the display context
36+
splash.append(button.group)
9637

38+
# Loop and look for touches
9739
while True:
9840
p = ts.touch_point
9941
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

Comments
 (0)