Skip to content

Instantly share code, notes, and snippets.

@brentru
Created June 18, 2019 17:05
Show Gist options
  • Save brentru/60eced3b3e2b33c0ea84f0abadfce0cf to your computer and use it in GitHub Desktop.
Save brentru/60eced3b3e2b33c0ea84f0abadfce0cf to your computer and use it in GitHub Desktop.
# simpletest - cursor control
from micropython import const
import digitalio
import board
import time
from gamepadshift import GamePadShift
from adafruit_bitmap_font import bitmap_font
from adafruit_button import Button
import os
import displayio
import adafruit_cursor
# PyBadge Button Masks
BUTTON_LEFT = const(128)
BUTTON_UP = const(64)
BUTTON_DOWN = const(32)
BUTTON_RIGHT = const(16)
BUTTON_SEL = const(8)
BUTTON_START = const(4)
BUTTON_A = const(2)
BUTTON_B = const(1)
pad = GamePadShift(digitalio.DigitalInOut(board.BUTTON_CLOCK),
digitalio.DigitalInOut(board.BUTTON_OUT),
digitalio.DigitalInOut(board.BUTTON_LATCH))
display = board.DISPLAY
splash_grp = displayio.Group(max_size=20)
###### BUTTON CONFIG ######
# 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
THE_FONT = "/fonts/Arial-12.bdf"
DISPLAY_STRING = "Button Text"
# Make the display context
BUTTON_WIDTH = 80
BUTTON_HEIGHT = 40
BUTTON_MARGIN = 20
# 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="btn0", label_font=font)
buttons.append(button_0)
for b in buttons:
splash_grp.append(b.group)
############
# Cursor Configuration
cursor_data = {
'cursor_path': '/cursor_delta.bmp',
'cursor_scale': 2,
'cursor_width' : 1,
'cursor_height' : 1,
'cursor_tile_width' : 16,
'cursor_tile_height' : 16,
}
display = board.DISPLAY
mouse_cursor = adafruit_cursor.Cursor(display, display_group = splash_grp,
cursor = cursor_data)
mouse_cursor.speed = 15
##############
# show displayio group
display.show(splash_grp)
def check_buttons(pad_btns):
if (pad_btns & BUTTON_RIGHT) > 0:
mouse_cursor.x += mouse_cursor.speed
elif (pad_btns & BUTTON_LEFT) > 0:
mouse_cursor.x -= mouse_cursor.speed
elif (pad_btns & BUTTON_UP) > 0:
mouse_cursor.y -= mouse_cursor.speed
elif (pad_btns & BUTTON_DOWN) > 0:
mouse_cursor.y += mouse_cursor.speed
elif (pad_btns & BUTTON_B) > 0:
if mouse_cursor.is_hidden == True:
print('showing cursor!')
mouse_cursor.show()
else:
print('hiding cursor!')
mouse_cursor.hide()
current_btns = pad.get_pressed()
last_read = 0
while True:
# check the current location of the cursor
# p = mouse_cursor.x, mouse_cursor.y
if (last_read + 0.00001) < time.monotonic():
btns = pad.get_pressed()
last_read = time.monotonic()
if current_btns != btns:
check_buttons(btns)
current_btns = btns
"""
# handle button presses
if p:
for i, b in enumerate(buttons):
if b.contains(p) and (pad_btns&BUTTON_A > 0):
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