Skip to content

Commit afa7683

Browse files
authored
Merge pull request #1621 from kattni/braille-macropad
Adding code and shortcuts files.
2 parents 5a1f488 + 9e71b47 commit afa7683

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

MacroPad_Braille_Keycaps/code.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries
2+
# SPDX-License-Identifier: MIT
3+
"""
4+
Adafruit MacroPad shortcut macropad with light up keys that play a tone or wav file when a key is
5+
pressed. Displays the associated key command being sent on key press in a grid matching the key
6+
layout for easily viewing what command is associated with what key.
7+
8+
REQUIRES associated shortcuts.py file containing a dictionary with all the key info.
9+
"""
10+
import displayio
11+
import terminalio
12+
from rainbowio import colorwheel
13+
from adafruit_displayio_layout.layouts.grid_layout import GridLayout
14+
from adafruit_display_text import bitmap_label as label
15+
from adafruit_macropad import MacroPad
16+
from shortcuts import shortcut_keys
17+
18+
# Initialise MacroPad
19+
macropad = MacroPad()
20+
21+
# Setup title and grid
22+
main_group = displayio.Group()
23+
macropad.display.show(main_group)
24+
title = label.Label(
25+
y=4,
26+
font=terminalio.FONT,
27+
color=0x0,
28+
text=" SHORTCUTS ",
29+
background_color=0xFFFFFF,
30+
)
31+
layout = GridLayout(x=0, y=10, width=128, height=54, grid_size=(3, 4), cell_padding=5)
32+
33+
# Extract data from shortcuts
34+
key_sounds = [sound[0] for sound in shortcut_keys["macros"]]
35+
label_names = [names[1] for names in shortcut_keys["macros"]]
36+
keys = [keys[3] for keys in shortcut_keys["macros"]]
37+
38+
# Generate the labels based on the label names and add them to the appropriate grid cell
39+
labels = []
40+
for index in range(12):
41+
x = index % 3
42+
y = index // 3
43+
labels.append(label.Label(terminalio.FONT, text=label_names[index], max_glyphs=10))
44+
layout.add_content(labels[index], grid_position=(x, y), cell_size=(1, 1))
45+
46+
# Display the text
47+
main_group.append(title)
48+
main_group.append(layout)
49+
50+
while True:
51+
key_event = macropad.keys.events.get() # Begin checking for key events.
52+
53+
if key_event: # If there is a key event, e.g. a key has been pressed...
54+
if key_event.pressed: # And a key is currently being pressed...
55+
56+
# ... light up the pressed key with a color from the rainbow.
57+
macropad.pixels[key_event.key_number] = colorwheel(
58+
int(255 / 12) * key_event.key_number
59+
)
60+
61+
# If it's a Keycode...
62+
if "KC" in shortcut_keys["macros"][key_event.key_number][2]:
63+
# ... send the associated key command or sequence of key commands.
64+
for key in keys[key_event.key_number]:
65+
macropad.keyboard.press(key)
66+
macropad.keyboard.release_all()
67+
68+
# If it's a ConsumerControlCode...
69+
if "CC" in shortcut_keys["macros"][key_event.key_number][2]:
70+
# ... send the associated consumer control code.
71+
for key in keys[key_event.key_number]:
72+
macropad.consumer_control.send(key)
73+
74+
sounds = key_sounds[key_event.key_number] # Assign the tones/wavs to the keys.
75+
if isinstance(sounds, int): # If the sound is a tone in Hz...
76+
macropad.start_tone(sounds) # ... play the tone while the key is pressed.
77+
if isinstance(sounds, str): # If the sound is a wav file name as a string...
78+
macropad.play_file(sounds) # ... play the wav file.
79+
else:
80+
# Otherwise, turn off the NeoPixels and stop the tone.
81+
macropad.pixels.fill(0)
82+
macropad.stop_tone()

MacroPad_Braille_Keycaps/shortcuts.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
A Python dictionary containing information to be associated with the twelve keys on a
3+
MacroPad.
4+
"""
5+
from adafruit_macropad import MacroPad
6+
macropad = MacroPad
7+
"""
8+
** Understanding the Dictionary **
9+
The following explains how to configure each entry below.
10+
11+
Sound:
12+
Can be an integer for a tone in Hz, e.g.196, OR, a string for a wav file name, e.g. "cat.wav".
13+
14+
Label:
15+
The label you would like to appear on the display. Should be limited to 6 characters to fit.
16+
17+
Keycode type:
18+
You must update this to match the type of key sequence you're sending.
19+
KC = Keycode
20+
CC = ConsumerControlCode
21+
22+
Key sequence:
23+
The Keycode, sequence of Keycodes, or ConsumerControlCode to send.
24+
"""
25+
shortcut_keys = {
26+
'macros': [
27+
# (Sound, Label, Keycode type, Key sequence)
28+
# 1st row ----------
29+
(196, 'Esc', 'KC', [macropad.Keycode.ESCAPE]),
30+
(220, 'Tab', 'KC', [macropad.Keycode.TAB]),
31+
(246, 'Vol+', 'CC', [macropad.ConsumerControlCode.VOLUME_INCREMENT]),
32+
# 2nd row ----------
33+
(262, 'Play', 'CC', [macropad.ConsumerControlCode.PLAY_PAUSE]),
34+
(294, 'Home', 'KC', [macropad.Keycode.HOME]),
35+
(330, 'Vol-', 'CC', [macropad.ConsumerControlCode.VOLUME_DECREMENT]),
36+
# 3rd row ----------
37+
(349, 'End', 'KC', [macropad.Keycode.END]),
38+
(392, 'Copy', 'KC', [macropad.Keycode.COMMAND, macropad.Keycode.C]),
39+
(440, 'Pg Up', 'KC', [macropad.Keycode.PAGE_UP]),
40+
# 4th row ----------
41+
(494, 'Quit', 'KC', [macropad.Keycode.COMMAND, macropad.Keycode.Q]),
42+
(523, 'Paste', 'KC', [macropad.Keycode.COMMAND, macropad.Keycode.V]),
43+
(587, 'Pg Dn', 'KC', [macropad.Keycode.PAGE_DOWN]),
44+
]
45+
}

0 commit comments

Comments
 (0)