Created
June 19, 2022 18:50
-
-
Save DerBroader71/36be9a8eb3a6980e229393bec7be7d6a to your computer and use it in GitHub Desktop.
Adafruit Neokey 1x4 4-layer / 12 Macro Keypad
This file contains 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
# import all required libraries | |
import board | |
import neopixel | |
from adafruit_neokey.neokey1x4 import NeoKey1x4 | |
import usb_hid | |
from adafruit_hid.keyboard import Keyboard | |
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS | |
from adafruit_hid.keycode import Keycode | |
import time | |
# setup the onboard neopixel | |
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) | |
# setup the NeoKey1x4 | |
i2c_bus = board.I2C() | |
neokey = NeoKey1x4(i2c_bus, addr=0x30) | |
# create a keyboard object | |
keyboard = Keyboard(usb_hid.devices) | |
layout = KeyboardLayoutUS(keyboard) | |
# states for key presses | |
key_0_state = False | |
key_1_state = False | |
key_2_state = False | |
key_3_state = False | |
# set the default layer | |
layers = [0, 1, 2, 3] | |
layer = 0 | |
# setup the macros | |
macro = { | |
'A': [Keycode.A, -Keycode.A, 0.5, Keycode.B, -Keycode.B, 1.0, Keycode.C, -Keycode.C], | |
'B': ['Test B\n'], | |
'C': ['This is C\n'], | |
'D': ['Test D\n'], | |
'E': ['This is E\n'], | |
'F': ['Test F\n'], | |
'G': ['This is G\n'], | |
'H': ['Test H\n'], | |
'I': ['This is I\n'], | |
'J': ['Test J\n'], | |
'K': ['This is K\n'], | |
'L': ['Test L\n'] | |
} | |
def layer_rotate(layers, x): | |
neokey.pixels[layers[0]] = 0x000000 | |
layers[:] = layers[-x:] + layers[:-x] | |
neokey.pixels[layers[0]] = 0x00FF00 | |
return layers[0] | |
def runMacro(macro): | |
for item in macro: | |
if isinstance(item, int): | |
if item >= 0: | |
keyboard.press(item) | |
else: | |
keyboard.release(-item) | |
elif isinstance(item, float): | |
time.sleep(item) | |
elif isinstance(item, str): | |
layout.write(item) | |
elif isinstance(item, list): | |
for code in item: | |
if isinstance(code, int): | |
keyboard.release() | |
keyboard.press(code) | |
if isinstance(code, float): | |
time.sleep(code) | |
else: | |
keyboard.release_all() | |
while True: | |
# set the active layer neokey to active | |
neokey.pixels[layers[0]] = 0x00FF00 | |
# switch debouncing | |
# also turns off NeoPixel on release | |
if not neokey[0] and key_0_state: | |
key_0_state = False | |
if not neokey[1] and key_1_state: | |
key_1_state = False | |
neokey.pixels[1] = 0x000000 | |
if not neokey[2] and key_2_state: | |
key_2_state = False | |
neokey.pixels[2] = 0x000000 | |
if not neokey[3] and key_3_state: | |
key_3_state = False | |
neokey.pixels[3] = 0x000000 | |
if neokey[0] and not key_0_state: | |
# change the layer | |
key_0_state = True | |
layer = layer_rotate(layers, -1) | |
if neokey[1] and not key_1_state: | |
neokey.pixels[1] = 0x0000FF | |
if layer == 0: | |
runMacro(macro['A']) | |
if layer == 1: | |
runMacro(macro['D']) | |
if layer == 2: | |
runMacro(macro['G']) | |
if layer == 3: | |
runMacro(macro['J']) | |
key_1_state = True | |
if neokey[2] and not key_2_state: | |
neokey.pixels[2] = 0x0000FF | |
if layer == 0: | |
runMacro(macro['B']) | |
if layer == 1: | |
runMacro(macro['E']) | |
if layer == 2: | |
runMacro(macro['H']) | |
if layer == 3: | |
runMacro(macro['K']) | |
key_2_state = True | |
if neokey[3] and not key_3_state: | |
neokey.pixels[3] = 0x0000FF | |
if layer == 0: | |
runMacro(macro['C']) | |
if layer == 1: | |
runMacro(macro['F']) | |
if layer == 2: | |
runMacro(macro['I']) | |
if layer == 3: | |
runMacro(macro['L']) | |
key_3_state = True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment