Skip to content

Instantly share code, notes, and snippets.

@kmatch98
Last active March 16, 2021 19:41
Show Gist options
  • Save kmatch98/aeae5c3b34754050037a294348416c52 to your computer and use it in GitHub Desktop.
Save kmatch98/aeae5c3b34754050037a294348416c52 to your computer and use it in GitHub Desktop.
Example touch_deck to exercise the icon_widget that is in development for adafruit_displayio_layout
# SPDX-FileCopyrightText: 2021 Tim C, written for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
"""
import storage
import gc
import time
import board
import displayio
import terminalio
from adafruit_display_text import label, bitmap_label
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
import adafruit_touchscreen
from touch_deck_layers import touch_deck_config, KEY
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_button import Button
from adafruit_displayio_layout.widgets.icon_widget import IconWidget
COOLDOWN_TIME = 0.25
LAST_PRESS_TIME = -1
current_layer = 0
# use built in display (PyPortal, PyGamer, PyBadge, CLUE, etc.)
# see guide for up external displays (TFT / OLED breakouts, RGB matrices, etc.)
# https://learn.adafruit.com/circuitpython-display-support-using-displayio/display-and-display-bus
display = board.DISPLAY
# Make the display context
main_group = displayio.Group(max_size=10)
display.show(main_group)
kbd = Keyboard(usb_hid.devices)
cc = ConsumerControl(usb_hid.devices)
ts = adafruit_touchscreen.Touchscreen(
board.TOUCH_XL,
board.TOUCH_XR,
board.TOUCH_YD,
board.TOUCH_YU,
calibration=((5200, 59000), (5800, 57000)),
size=(display.width, display.height),
)
layout = GridLayout(
x=0,
y=20,
width=300,
height=210,
grid_size=(4, 3),
cell_padding=6,
max_size=10,
)
_icons = []
# icon animation settings
max_scale = 1.4
max_angle = 8
animation_time = 0.15
_pressed_icons = []
"""
for i in range(12):
_new_icon = IconWidget("Shortcut {}".format(i), "images/test32_icon.bmp")
_icons.append(_new_icon)
layout.add_content(_new_icon, grid_position=(i%4, i//4), cell_size=(1, 1))
"""
layer_label = bitmap_label.Label(terminalio.FONT)
layer_label.anchor_point = (0.5, 0.0)
layer_label.anchored_position = (display.width // 2, 4)
main_group.append(layer_label)
next_layer_btn = Button(
x=layout.x + layout._width - 12,
y=display.height - 70,
width=50,
height=70,
style=Button.RECT,
fill_color=0x00ff99,
label="",
label_font=terminalio.FONT,
label_color=0x000000,
)
main_group.append(next_layer_btn)
home_layer_btn = Button(
x=layout.x + layout._width - 12,
y=0,
width=50,
height=70,
style=Button.RECT,
fill_color=0xFF9900,
label="",
label_font=terminalio.FONT,
label_color=0x000000,
)
main_group.append(home_layer_btn)
def load_layer(layer_index):
# remove everything from self
global _icons
_icons = []
layout._cell_content_list = []
while len(layout) > 0:
layout.pop()
layer_label.text = touch_deck_config["layers"][layer_index]["name"]
for i, shortcut in enumerate(touch_deck_config["layers"][layer_index]["shortcuts"]):
wheel_increment = 0
palette_skip_indices = []
###### setup the Pause button with a palette animation
if shortcut["label"] == 'Pause':
wheel_increment = 1
palette_skip_indices = [6] # skip the last color
_new_icon = IconWidget(display,
shortcut["label"],
shortcut["icon"],
max_scale=max_scale,
max_angle=max_angle,
animation_time=animation_time,
wheel_increment=wheel_increment,
palette_skip_indices = palette_skip_indices
)
_icons.append(_new_icon)
layout.add_content(_new_icon, grid_position=(i % 4, i // 4), cell_size=(1, 1))
load_layer(current_layer)
main_group.append(layout)
while True:
gc.collect()
p = ts.touch_point
if p:
_now = time.monotonic()
if _now - LAST_PRESS_TIME > COOLDOWN_TIME:
if next_layer_btn.contains(p):
#next_layer_btn.selected(p)
current_layer += 1
if current_layer >= len(touch_deck_config["layers"]):
current_layer = 0
load_layer(current_layer)
LAST_PRESS_TIME = time.monotonic()
if home_layer_btn.contains(p):
#home_layer_btn.selected(p)
current_layer = 0
load_layer(current_layer)
LAST_PRESS_TIME = time.monotonic()
for index, icon_shortcut in enumerate(_icons):
if icon_shortcut.contains(p):
if index not in _pressed_icons:
print("pressed {}".format(index))
icon_shortcut.selected(p)
print(touch_deck_config["layers"][current_layer]["shortcuts"][index]["actions"][1])
if touch_deck_config["layers"][current_layer]["shortcuts"][index]["actions"][0] == KEY:
kbd.press(*touch_deck_config["layers"][current_layer]["shortcuts"][index]["actions"][1])
kbd.release_all()
else:
cc.send(touch_deck_config["layers"][current_layer]["shortcuts"][index]["actions"][1])
LAST_PRESS_TIME = time.monotonic()
_pressed_icons.append(index)
#elif index in _pressed_icons:
# _pressed_icons.remove(index)
# _icons[index].released(p)
else:
for index in _pressed_icons:
_pressed_icons.remove(index)
_icons[index].released(p)
for icon in _icons:
icon.unselected() # call unselected() on all icons
from adafruit_hid.keycode import Keycode
from adafruit_hid.consumer_control_code import ConsumerControlCode
MEDIA = 1
KEY = 2
touch_deck_config = {
"layers":[
{
"name": "Youtube Controls",
"shortcuts": [
{
"label": "Play",
"icon": "/touch_deck_icons/Play_48x48_small.bmp",
"actions": (KEY, [Keycode.K])
},
{
"label": "Pause",
"icon": "/touch_deck_icons/Pause_48x48_small.bmp",
"actions": (KEY, [Keycode.K])
},
{
"label": "FastForward",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.RIGHT_ARROW])
},
{
"label": "Rewind",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.LEFT_ARROW])
},
{
"label": "Next",
"icon": "/touch_deck_icons/Next_48x48_small.bmp",
"actions": (KEY, [Keycode.RIGHT_SHIFT, Keycode.N])
},
{
"label": "Vol -",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (MEDIA, ConsumerControlCode.VOLUME_DECREMENT)
},
{
"label": "Vol +",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (MEDIA, ConsumerControlCode.VOLUME_INCREMENT)
},
{
"label": "Test (T)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.T])
},
{
"label": "Test (E)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.E])
},
{
"label": "Test (S)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.S])
},
{
"label": "Test (T)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.T])
},
{
"label": "Test [:)]",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.RIGHT_SHIFT, Keycode.SEMICOLON, Keycode.ZERO])
}
]
},
{
"name": "Test Second Layer",
"shortcuts": [
{
"label": "Test (T)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.T])
},
{
"label": "Test (E)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.E])
},
{
"label": "Test (S)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.S])
},
{
"label": "Test (T)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.T])
},
{
"label": "Test [:)]",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.RIGHT_SHIFT, Keycode.SEMICOLON, Keycode.ZERO])
}
]
},
{
"name": "Test Third Layer",
"shortcuts": [
{
"label": "Test (3)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.THREE])
},
{
"label": "Test (R)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.R])
},
{
"label": "Test (D)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.D])
},
{
"label": "Test (L)",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.L])
},
{
"label": "Test [:)]",
"icon": "/touch_deck_icons/test48_icon_small.bmp",
"actions": (KEY, [Keycode.RIGHT_SHIFT, Keycode.SEMICOLON, Keycode.ZERO])
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment