Skip to content

Commit a7d25bd

Browse files
committed
add demo
1 parent 9555240 commit a7d25bd

File tree

2 files changed

+115
-2
lines changed

2 files changed

+115
-2
lines changed

adafruit_button.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Button():
5353
ROUNDRECT = const(1)
5454
SHADOWRECT = const(2)
5555
SHADOWROUNDRECT = const(3)
56-
def __init__(self, *, x, y, width, height, style=RECT,
56+
def __init__(self, *, x, y, width, height, name=None, style=RECT,
5757
fill_color=0xFFFFFF, outline_color=0x0,
5858
label=None, label_font=None, label_color=0x0,
5959
selected_fill=None, selected_outline=None,
@@ -65,6 +65,7 @@ def __init__(self, *, x, y, width, height, style=RECT,
6565
self._font = label_font
6666
self._selected = False
6767
self.group = displayio.Group()
68+
self.name = name
6869

6970
self.fill_color = fill_color
7071
self.outline_color = outline_color
@@ -101,7 +102,7 @@ def __init__(self, *, x, y, width, height, style=RECT,
101102
self.group.append(self.shadow)
102103
self.group.append(self.body)
103104

104-
if label: # button with text label
105+
if label and (label_color is not None): # button with text label
105106
if not label_font:
106107
raise RuntimeError("Please provide label font")
107108
dims = label_font.text_bounding_box(label)

examples/display_button_simpletest.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)