|
| 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() |
0 commit comments