Skip to content

Commit 7539464

Browse files
brentrubrentru
brentru
authored and
brentru
committed
add advanced cursor+btns+txt demo example
1 parent cfb1c6f commit 7539464

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

examples/cursor_buttons_text.py

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import adafruit_cursor
2+
import board
3+
import digitalio
4+
import displayio
5+
from adafruit_bitmap_font import bitmap_font
6+
from adafruit_button import Button
7+
from adafruit_display_text import label
8+
from gamepadshift import GamePadShift
9+
from micropython import const
10+
11+
# PyBadge Button Masks
12+
BUTTON_LEFT = const(128)
13+
BUTTON_UP = const(64)
14+
BUTTON_DOWN = const(32)
15+
BUTTON_RIGHT = const(16)
16+
BUTTON_A = const(2)
17+
BUTTON_B = const(1)
18+
19+
# Initialize PyBadge Gamepad
20+
pad = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
21+
digitalio.DigitalInOut(board.BUTTON_OUT),
22+
digitalio.DigitalInOut(board.BUTTON_LATCH))
23+
24+
# Load the font
25+
THE_FONT = "/fonts/Arial-12.bdf"
26+
font = bitmap_font.load_font(THE_FONT)
27+
28+
# Create the display
29+
display = board.DISPLAY
30+
31+
# Create the display context
32+
splash = displayio.Group(max_size=22)
33+
34+
##########################################################################
35+
# Make a background color fill
36+
37+
color_bitmap = displayio.Bitmap(320, 240, 1)
38+
color_palette = displayio.Palette(1)
39+
color_palette[0] = 0x404040
40+
bg_sprite = displayio.TileGrid(color_bitmap,
41+
pixel_shader=color_palette,
42+
x=0, y=0)
43+
splash.append(bg_sprite)
44+
45+
##########################################################################
46+
47+
# Set up button size info
48+
BUTTON_WIDTH = 80
49+
BUTTON_HEIGHT = 40
50+
BUTTON_MARGIN = 20
51+
52+
# Create the buttons
53+
buttons = []
54+
55+
button_speed_inc = Button(x=BUTTON_MARGIN, y=BUTTON_MARGIN+BUTTON_HEIGHT,
56+
width=BUTTON_WIDTH, height=BUTTON_HEIGHT,
57+
label="+ Speed", label_font=font)
58+
buttons.append(button_speed_inc)
59+
60+
button_speed_dec = Button(x=BUTTON_MARGIN, y=BUTTON_MARGIN*4+BUTTON_HEIGHT,
61+
width=BUTTON_WIDTH, height=BUTTON_HEIGHT,
62+
label="- Speed", label_font=font)
63+
buttons.append(button_speed_dec)
64+
65+
button_scale_pos = Button(x=BUTTON_MARGIN*3+2*BUTTON_WIDTH, y=BUTTON_MARGIN+BUTTON_HEIGHT,
66+
width=BUTTON_WIDTH, height=BUTTON_HEIGHT,
67+
label="+ Scale", label_font=font, style=Button.SHADOWRECT)
68+
buttons.append(button_scale_pos)
69+
70+
button_scale_neg = Button(x=BUTTON_MARGIN*3+2*BUTTON_WIDTH, y=BUTTON_MARGIN*4+BUTTON_HEIGHT,
71+
width=BUTTON_WIDTH, height=BUTTON_HEIGHT,
72+
label="- Scale", label_font=font, style=Button.SHADOWRECT)
73+
buttons.append(button_scale_neg)
74+
75+
# Show the button
76+
for b in buttons:
77+
splash.append(b.group)
78+
79+
# Create a text label
80+
text_label = label.Label(font, text="CircuitPython Cursor!", color=0x00FF00,
81+
x = 100, y = 20)
82+
splash.append(text_label)
83+
84+
text_speed = label.Label(font, max_glyphs = 15, color=0x00FF00,
85+
x = 120, y = 40)
86+
splash.append(text_speed)
87+
88+
text_scale = label.Label(font, max_glyphs = 15, color=0x00FF00,
89+
x = 120, y = 60)
90+
splash.append(text_scale)
91+
92+
# initialize the mouse cursor object
93+
mouse_cursor = adafruit_cursor.Cursor(display, display_group=splash)
94+
95+
# show displayio group
96+
display.show(splash)
97+
98+
def check_dpad(d_pad_buttons):
99+
"""Checks the directional pad for button presses."""
100+
if d_pad_buttons & BUTTON_RIGHT:
101+
mouse_cursor.x += mouse_cursor.speed
102+
elif d_pad_buttons & BUTTON_LEFT:
103+
mouse_cursor.x -= mouse_cursor.speed
104+
if d_pad_buttons & BUTTON_DOWN:
105+
mouse_cursor.y += mouse_cursor.speed
106+
elif d_pad_buttons & BUTTON_UP:
107+
mouse_cursor.y -= mouse_cursor.speed
108+
109+
is_pressed = False
110+
while True:
111+
display.wait_for_frame()
112+
text_speed.text = 'Speed: {0}px'.format(mouse_cursor.speed)
113+
text_scale.text = 'Scale: {0}px'.format(mouse_cursor.scale)
114+
pressed = pad.get_pressed()
115+
check_dpad(pressed)
116+
if is_pressed:
117+
if not pressed & (BUTTON_A | BUTTON_B):
118+
# buttons de-pressed
119+
is_pressed = False
120+
for i, b in enumerate(buttons):
121+
b.selected=False
122+
# otherwise, continue holding
123+
continue
124+
if pressed & BUTTON_B:
125+
is_pressed = True
126+
if mouse_cursor.hide:
127+
mouse_cursor.hide = False
128+
else:
129+
mouse_cursor.hide = True
130+
if pressed & BUTTON_A:
131+
is_pressed = True
132+
for i, b in enumerate(buttons):
133+
if b.contains((mouse_cursor.x, mouse_cursor.y)):
134+
print("Button %d pressed"%i)
135+
if i == 0: # Increase the cursor speed
136+
mouse_cursor.speed += 1
137+
elif i == 1: # Decrease the cursor speed
138+
mouse_cursor.speed -= 1
139+
if i == 2: # Increase the cursor scale
140+
mouse_cursor.scale += 1
141+
elif i == 3: # Decrease the cursor scale
142+
mouse_cursor.scale -= 1
143+
b.selected=True

0 commit comments

Comments
 (0)