Skip to content

Instantly share code, notes, and snippets.

@FoamyGuy
Created September 26, 2020 14:34
Show Gist options
  • Save FoamyGuy/d4e41f0158a0389222f395707c3c01fd to your computer and use it in GitHub Desktop.
Save FoamyGuy/d4e41f0158a0389222f395707c3c01fd to your computer and use it in GitHub Desktop.
import os
import board
import displayio
from adafruit_bitmap_font import bitmap_font
from adafruit_button import Button
import adafruit_touchscreen
from grid_layout import GridLayout
# These pins are used as both analog and digital! XL, XR and YU must be analog
# and digital capable. YD just need to be digital
ts = adafruit_touchscreen.Touchscreen(
board.TOUCH_XL,
board.TOUCH_XR,
board.TOUCH_YD,
board.TOUCH_YU,
calibration=((5200, 59000), (5800, 57000)),
size=(320, 240),
)
# the current working directory (where this file is)
cwd = ("/" + __file__).rsplit("/", 1)[0]
fonts = [
file
for file in os.listdir(cwd + "/fonts/")
if (file.endswith(".bdf") and not file.startswith("._"))
]
for i, filename in enumerate(fonts):
fonts[i] = cwd + "/fonts/" + filename
print(fonts)
THE_FONT = "/fonts/Arial-16.bdf"
DISPLAY_STRING = "Button Text"
# Make the display context
splash = displayio.Group(max_size=20)
board.DISPLAY.auto_refresh=False
board.DISPLAY.show(splash)
BUTTON_WIDTH = 80
BUTTON_HEIGHT = 40
BUTTON_MARGIN = 20
##########################################################################
# Make a background color fill
color_bitmap = displayio.Bitmap(320, 240, 1)
color_palette = displayio.Palette(1)
color_palette[0] = 0x404040
bg_sprite = displayio.TileGrid(color_bitmap, pixel_shader=color_palette, x=0, y=0)
print(bg_sprite.x, bg_sprite.y)
splash.append(bg_sprite)
##########################################################################
# Load the font
font = bitmap_font.load_font(THE_FONT)
buttons = []
# Default button styling:
button_0 = Button(
x=BUTTON_MARGIN,
y=BUTTON_MARGIN,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
label="butn0",
label_font=font,
)
buttons.append(button_0)
# a button with no indicators at all
button_1 = Button(
x=BUTTON_MARGIN * 2 + BUTTON_WIDTH,
y=BUTTON_MARGIN,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
label_font=font,
label="butn1",
label_color=0x00FF00,
fill_color=0x0,
outline_color=None,
)
buttons.append(button_1)
# various colorings
button_2 = Button(
x=BUTTON_MARGIN * 3 + 2 * BUTTON_WIDTH,
y=BUTTON_MARGIN,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
label="butn2",
label_font=font,
label_color=0x0000FF,
fill_color=0x00FF00,
outline_color=0xFF0000,
)
buttons.append(button_2)
# Transparent button with text
button_3 = Button(
x=BUTTON_MARGIN,
y=BUTTON_MARGIN * 2 + BUTTON_HEIGHT,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
label="butn3",
label_font=font,
label_color=0x0,
fill_color=None,
outline_color=None,
)
buttons.append(button_3)
# a roundrect
button_4 = Button(
x=BUTTON_MARGIN * 2 + BUTTON_WIDTH,
y=BUTTON_MARGIN * 2 + BUTTON_HEIGHT,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
label="butn4",
label_font=font,
style=Button.ROUNDRECT,
)
buttons.append(button_4)
# a shadowrect
button_5 = Button(
x=BUTTON_MARGIN * 3 + BUTTON_WIDTH * 2,
y=BUTTON_MARGIN * 2 + BUTTON_HEIGHT,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
label="butn5",
label_font=font,
style=Button.SHADOWRECT,
)
buttons.append(button_5)
# a shadowroundrect
button_6 = Button(
x=BUTTON_MARGIN,
y=BUTTON_MARGIN * 3 + BUTTON_HEIGHT * 2,
width=BUTTON_WIDTH,
height=BUTTON_HEIGHT,
label="butn6",
label_font=font,
style=Button.SHADOWROUNDRECT,
selected_label=0x0,
selected_fill=0x707070,
selected_outline=0xa0a0a0,
)
buttons.append(button_6)
#for b in buttons:
# splash.append(b)
print("width: {}".format(board.DISPLAY.width))
layout = GridLayout(
x=320-240, y=240-200,
width=240, height=200,
grid_size=(3,4), child_padding=8,
max_children=10
)
layout.add_sub_view(button_0, grid_position=(0,0), view_grid_size=(1,1))
layout.add_sub_view(button_1, grid_position=(1,0), view_grid_size=(1,1))
layout.add_sub_view(button_2, grid_position=(2,0), view_grid_size=(1,1))
layout.add_sub_view(button_3, grid_position=(0,1), view_grid_size=(1,1))
layout.add_sub_view(button_4, grid_position=(1,1), view_grid_size=(2,1))
layout.add_sub_view(button_5, grid_position=(0,2), view_grid_size=(1,2))
layout.add_sub_view(button_6, grid_position=(1,2), view_grid_size=(2,2))
splash.append(layout)
board.DISPLAY.refresh()
board.DISPLAY.auto_refresh=True
while True:
p = ts.touch_point
if p:
print(p)
for i, b in enumerate(buttons):
if b.contains(p):
print("Button %d pressed" % i)
b.selected = True
else:
b.selected = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment