forked from ladyada/Adafruit_CircuitPython_Display_Button
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from caternuson/iss14
Update examples
- Loading branch information
Showing
2 changed files
with
133 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import os | ||
import board | ||
import displayio | ||
from adafruit_bitmap_font import bitmap_font | ||
from adafruit_button import Button | ||
import adafruit_touchscreen | ||
|
||
# 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-12.bdf" | ||
DISPLAY_STRING = "Button Text" | ||
|
||
# Make the display context | ||
splash = displayio.Group(max_size=20) | ||
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="button0", 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, | ||
fill_color=None, 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="button2", 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="button3", 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="button4", 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="button5", 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="button6", label_font=font, style=Button.SHADOWROUNDRECT) | ||
buttons.append(button_6) | ||
|
||
for b in buttons: | ||
splash.append(b.group) | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,106 +1,45 @@ | ||
import os | ||
import board | ||
import displayio | ||
from adafruit_bitmap_font import bitmap_font | ||
import terminalio | ||
from adafruit_button import Button | ||
import adafruit_touchscreen | ||
|
||
# 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 | ||
#--| Button Config |------------------------------------------------- | ||
BUTTON_X = 110 | ||
BUTTON_Y = 95 | ||
BUTTON_WIDTH = 100 | ||
BUTTON_HEIGHT = 50 | ||
BUTTON_STYLE = Button.ROUNDRECT | ||
BUTTON_FILL_COLOR = 0x00FFFF | ||
BUTTON_OUTLINE_COLOR = 0xFF00FF | ||
BUTTON_LABEL = "HELLO WORLD" | ||
BUTTON_LABEL_COLOR = 0x000000 | ||
#--| Button Config |------------------------------------------------- | ||
|
||
# Setup touchscreen (PyPortal) | ||
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-12.bdf" | ||
DISPLAY_STRING = "Button Text" | ||
|
||
# Make the display context | ||
splash = displayio.Group(max_size=20) | ||
splash = displayio.Group() | ||
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="button0", 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, | ||
fill_color=None, 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="button2", 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="button3", 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="button4", 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="button5", 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="button6", label_font=font, style=Button.SHADOWROUNDRECT) | ||
buttons.append(button_6) | ||
# Make the button | ||
button = Button(x=BUTTON_X, y=BUTTON_Y, | ||
width=BUTTON_WIDTH, height=BUTTON_HEIGHT, style=BUTTON_STYLE, | ||
fill_color=BUTTON_FILL_COLOR, outline_color=BUTTON_OUTLINE_COLOR, | ||
label="HELLO WORLD", label_font=terminalio.FONT, label_color=BUTTON_LABEL_COLOR) | ||
|
||
for b in buttons: | ||
splash.append(b.group) | ||
# Add button to the display context | ||
splash.append(button.group) | ||
|
||
# Loop and look for touches | ||
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 | ||
if button.contains(p): | ||
button.selected = True | ||
else: | ||
button.selected = False |