Created
August 22, 2021 18:05
-
-
Save tapickell/1fcb1ded43b3acd89da4a0b5ebbfa989 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: Copyright (c) 2021 Kattni Rembor for Adafruit Industries | |
# | |
# SPDX-License-Identifier: Unlicense | |
""" | |
Rainbow LED grid layout demo for MacroPad. Displays the key pressed in a grid matching the key | |
layout on the built-in display, and animates a rainbow the first time you press a key and turns it | |
off on the next press. | |
""" | |
import displayio | |
import terminalio | |
from rainbowio import colorwheel | |
from adafruit_display_text import bitmap_label as label | |
from adafruit_displayio_layout.layouts.grid_layout import GridLayout | |
from adafruit_macropad import MacroPad | |
from adafruit_hid.keycode import Keycode | |
from adafruit_hid.consumer_control_code import ConsumerControlCode | |
macropad = MacroPad() | |
keys = [ | |
[ | |
Keycode.SEVEN, | |
Keycode.EIGHT, | |
Keycode.NINE, | |
Keycode.FOUR, | |
Keycode.FIVE, | |
Keycode.SIX, | |
Keycode.ONE, | |
Keycode.TWO, | |
Keycode.THREE, | |
Keycode.ZERO, | |
Keycode.PERIOD, | |
Keycode.RETURN | |
], | |
[ | |
Keycode.F7, | |
Keycode.F8, | |
Keycode.F9, | |
Keycode.F4, | |
Keycode.F5, | |
Keycode.F6, | |
Keycode.F1, | |
Keycode.F2, | |
Keycode.F3, | |
Keycode.F10, | |
Keycode.F11, | |
Keycode.F12 | |
], | |
[ | |
Keycode.HOME, | |
Keycode.UP_ARROW, | |
Keycode.PAGE_UP, | |
Keycode.LEFT_ARROW, | |
[Keycode.ESCAPE, Keycode.BACKSPACE], | |
Keycode.RIGHT_ARROW, | |
Keycode.END, | |
Keycode.DOWN_ARROW, | |
Keycode.PAGE_DOWN, | |
[Keycode.ESCAPE, Keycode.B], | |
[Keycode.ESCAPE, Keycode.D], | |
[Keycode.ESCAPE, Keycode.F], | |
], | |
[ | |
[Keycode.COMMAND, Keycode.OPTION, Keycode.CONTROL, Keycode.H], | |
ConsumerControlCode.VOLUME_INCREMENT, | |
[Keycode.COMMAND, Keycode.SHIFT, Keycode.F], | |
[Keycode.CONTROL, Keycode.P], | |
[Keycode.COMMAND, Keycode.SHIFT, Keycode.H], | |
[Keycode.CONTROL, Keycode.N], | |
[Keycode.COMMAND, Keycode.W], | |
ConsumerControlCode.VOLUME_DECREMENT, | |
[Keycode.COMMAND, Keycode.SHIFT, Keycode.M], | |
[Keycode.COMMAND, Keycode.SHIFT, Keycode.V], | |
[Keycode.COMMAND, Keycode.SHIFT, Keycode.A], | |
[Keycode.COMMAND, Keycode.SHIFT, Keycode.S] | |
] | |
] | |
mode = 0 | |
mode_text = [ | |
("NUMPAD", 0), | |
("FN", 1), | |
("EMACS", 2), | |
("ZOOM", 3) | |
] | |
def send_code(code): | |
if isinstance(code, Keycode): | |
macropad.keyboard.send(code) | |
elif isinstance(code, ConsumerControlCode): | |
macropad.consumer_control.send(code) | |
# --- Display text setup --- | |
text_lines = macropad.display_text("Macropad") | |
text_lines[0].text = "Mode: {}".format(mode_text[mode][0]) | |
text_lines[1].text = "" | |
text_lines[2].text = "" | |
text_lines.show() | |
lit_keys = [True] * 12 | |
wheel_offset = 0 | |
last_knob_pos = macropad.encoder # store knob position state | |
while True: | |
key_event = macropad.keys.events.get() | |
if key_event: | |
if key_event.pressed: | |
text_lines[1].text = "KEY{}".format(key_event.key_number) | |
if len(keys[mode]) >= key_event.key_number + 1: | |
if isinstance(keys[mode][key_event.key_number], list): | |
for keycode in keys[mode][key_event.key_number]: | |
macropad.keyboard.press(keycode) | |
macropad.keyboard.release_all() | |
else: | |
send_code(keys[mode][key_event.key_number]) | |
macropad.encoder_switch_debounced.update() # check the knob switch for press or release | |
if macropad.encoder_switch_debounced.pressed: | |
mode = (mode+1) % len(mode_text) | |
text_lines[0].text = "Mode: {}".format(mode_text[mode][0]) # Patch display offset by 1 | |
macropad.red_led = macropad.encoder_switch | |
text_lines[1].text = "" # clear the note line | |
if macropad.encoder_switch_debounced.released: | |
macropad.red_led = macropad.encoder_switch | |
if last_knob_pos is not macropad.encoder: # knob has been turned | |
knob_pos = macropad.encoder # read encoder | |
knob_delta = knob_pos - last_knob_pos # compute knob_delta since last read | |
last_knob_pos = knob_pos # save new reading | |
text_lines[2].text = "Encoder: {}".format(last_knob_pos) # Patch display offset by 1 | |
wheel_offset += 1 # Glow thru the colorwheel. | |
for pixel in range(12): | |
if lit_keys[pixel]: # Animate the specific LED. | |
macropad.pixels[pixel] = colorwheel((pixel / 12 * 256) + wheel_offset) | |
else: | |
macropad.pixels[pixel] = 0 # Otherwise, turn the LED off. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment