Adding code and shortcuts files.

This commit is contained in:
Kattni Rembor 2021-07-20 15:16:40 -04:00
parent 5a1f48839f
commit d718f1cb3f
2 changed files with 127 additions and 0 deletions

View file

@ -0,0 +1,82 @@
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
Adafruit MacroPad shortcut macropad with light up keys that play a tone or wav file when a key is
pressed. Displays the associated key command being sent on key press in a grid matching the key
layout for easily viewing what command is associated with what key.
REQUIRES associated shortcuts.py file containing a dictionary with all the key info.
"""
import displayio
import terminalio
from rainbowio import colorwheel
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
from adafruit_display_text import bitmap_label as label
from adafruit_macropad import MacroPad
from shortcuts import shortcut_keys
# Initialise MacroPad
macropad = MacroPad()
# Setup title and grid
main_group = displayio.Group()
macropad.display.show(main_group)
title = label.Label(
y=4,
font=terminalio.FONT,
color=0x0,
text=" SHORTCUTS ",
background_color=0xFFFFFF,
)
layout = GridLayout(x=0, y=10, width=128, height=54, grid_size=(3, 4), cell_padding=5)
# Extract data from shortcuts
key_sounds = [sound[0] for sound in shortcut_keys["macros"]]
label_names = [names[1] for names in shortcut_keys["macros"]]
keys = [keys[3] for keys in shortcut_keys["macros"]]
# Generate the labels based on the label names and add them to the appropriate grid cell
labels = []
for index in range(12):
x = index % 3
y = index // 3
labels.append(label.Label(terminalio.FONT, text=label_names[index], max_glyphs=10))
layout.add_content(labels[index], grid_position=(x, y), cell_size=(1, 1))
# Display the text
main_group.append(title)
main_group.append(layout)
while True:
key_event = macropad.keys.events.get() # Begin checking for key events.
if key_event: # If there is a key event, e.g. a key has been pressed...
if key_event.pressed: # And a key is currently being pressed...
# ... light up the pressed key with a color from the rainbow.
macropad.pixels[key_event.key_number] = colorwheel(
int(255 / 12) * key_event.key_number
)
# If it's a Keycode...
if "KC" in shortcut_keys["macros"][key_event.key_number][2]:
# ... send the associated key command or sequence of key commands.
for key in keys[key_event.key_number]:
macropad.keyboard.press(key)
macropad.keyboard.release_all()
# If it's a ConsumerControlCode...
if "CC" in shortcut_keys["macros"][key_event.key_number][2]:
# ... send the associated consumer control code.
for key in keys[key_event.key_number]:
macropad.consumer_control.send(key)
sounds = key_sounds[key_event.key_number] # Assign the tones/wavs to the keys.
if isinstance(sounds, int): # If the sound is a tone in Hz...
macropad.start_tone(sounds) # ... play the tone while the key is pressed.
if isinstance(sounds, str): # If the sound is a wav file name as a string...
macropad.play_file(sounds) # ... play the wav file.
else:
# Otherwise, turn off the NeoPixels and stop the tone.
macropad.pixels.fill(0)
macropad.stop_tone()

View file

@ -0,0 +1,45 @@
"""
A Python dictionary containing information to be associated with the twelve keys on a
MacroPad.
"""
from adafruit_macropad import MacroPad
macropad = MacroPad
"""
** Understanding the Dictionary **
The following explains how to configure each entry below.
Sound:
Can be an integer for a tone in Hz, e.g.196, OR, a string for a wav file name, e.g. "cat.wav".
Label:
The label you would like to appear on the display. Should be limited to 6 characters to fit.
Keycode type:
You must update this to match the type of key sequence you're sending.
KC = Keycode
CC = ConsumerControlCode
Key sequence:
The Keycode, sequence of Keycodes, or ConsumerControlCode to send.
"""
shortcut_keys = {
'macros': [
# (Sound, Label, Keycode type, Key sequence)
# 1st row ----------
(196, 'Esc', 'KC', [macropad.Keycode.ESCAPE]),
(220, 'Tab', 'KC', [macropad.Keycode.TAB]),
(246, 'Vol+', 'CC', [macropad.ConsumerControlCode.VOLUME_INCREMENT]),
# 2nd row ----------
(262, 'Play', 'CC', [macropad.ConsumerControlCode.PLAY_PAUSE]),
(294, 'Home', 'KC', [macropad.Keycode.HOME]),
(330, 'Vol-', 'CC', [macropad.ConsumerControlCode.VOLUME_DECREMENT]),
# 3rd row ----------
(349, 'End', 'KC', [macropad.Keycode.END]),
(392, 'Copy', 'KC', [macropad.Keycode.COMMAND, macropad.Keycode.C]),
(440, 'Pg Up', 'KC', [macropad.Keycode.PAGE_UP]),
# 4th row ----------
(494, 'Quit', 'KC', [macropad.Keycode.COMMAND, macropad.Keycode.Q]),
(523, 'Paste', 'KC', [macropad.Keycode.COMMAND, macropad.Keycode.V]),
(587, 'Pg Dn', 'KC', [macropad.Keycode.PAGE_DOWN]),
]
}