Skip to content

Commit e0852f3

Browse files
Rewrite to use Macropad library
1 parent 09b1eaf commit e0852f3

1 file changed

Lines changed: 62 additions & 56 deletions

File tree

Macropad_Hotkeys/code.py

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@
66
key sequences.
77
"""
88

9-
# pylint: disable=import-error, unused-import, too-few-public-methods
9+
# pylint: disable=import-error, unused-import, too-few-public-methods, protected-access
1010

1111
import os
12-
import board
13-
import digitalio
1412
import displayio
15-
import neopixel
16-
import rotaryio
17-
import keypad
1813
import terminalio
19-
import usb_hid
2014
from adafruit_display_shapes.rect import Rect
2115
from adafruit_display_text import label
22-
from adafruit_hid.keyboard import Keyboard
23-
from adafruit_hid.keycode import Keycode
24-
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
16+
from adafruit_macropad import MacroPad
2517

2618

2719
# CONFIGURABLES ------------------------
@@ -44,43 +36,36 @@ def switch(self):
4436
GROUP[13].text = self.name # Application name
4537
for i in range(12):
4638
if i < len(self.macros): # Key in use, set label + LED color
47-
PIXELS[i] = self.macros[i][0]
39+
MACROPAD.pixels[i] = self.macros[i][0]
4840
GROUP[i].text = self.macros[i][1]
49-
else: # Key not in use, no label or LED
50-
PIXELS[i] = 0
41+
else: # Key not in use, no label or LED
42+
MACROPAD.pixels[i] = 0
5143
GROUP[i].text = ''
52-
PIXELS.show()
53-
DISPLAY.refresh()
44+
MACROPAD.pixels.show()
45+
MACROPAD.display.refresh()
5446

5547

5648
# INITIALIZATION -----------------------
5749

58-
DISPLAY = board.DISPLAY
59-
DISPLAY.auto_refresh = False
60-
ENCODER = rotaryio.IncrementalEncoder(board.ENCODER_B, board.ENCODER_A)
61-
PIXELS = neopixel.NeoPixel(board.NEOPIXEL, 12, auto_write=False)
62-
KEYBOARD = Keyboard(usb_hid.devices)
63-
LAYOUT = KeyboardLayoutUS(KEYBOARD)
64-
KEYS = keypad.Keys((board.KEY1, board.KEY2, board.KEY3, board.KEY4, board.KEY5,
65-
board.KEY6, board.KEY7, board.KEY8, board.KEY9, board.KEY10,
66-
board.KEY11, board.KEY12, board.ENCODER_SWITCH),
67-
value_when_pressed=False, pull=True)
50+
MACROPAD = MacroPad()
51+
MACROPAD.display.auto_refresh = False
52+
MACROPAD._pixels.auto_write = False
6853

6954
# Set up displayio group with all labels
70-
GROUP = displayio.Group(max_size=14)
55+
GROUP = displayio.Group(max_size=14) # 12 keys + rect + app name
7156
for KEY_INDEX in range(12):
7257
x = KEY_INDEX % 3
7358
y = KEY_INDEX // 3
7459
GROUP.append(label.Label(terminalio.FONT, text='', color=0xFFFFFF,
75-
anchored_position=((DISPLAY.width - 1) * x / 2,
76-
DISPLAY.height - 1 -
60+
anchored_position=((MACROPAD.display.width - 1) * x / 2,
61+
MACROPAD.display.height - 1 -
7762
(3 - y) * 12),
7863
anchor_point=(x / 2, 1.0), max_glyphs=15))
79-
GROUP.append(Rect(0, 0, DISPLAY.width, 12, fill=0xFFFFFF))
64+
GROUP.append(Rect(0, 0, MACROPAD.display.width, 12, fill=0xFFFFFF))
8065
GROUP.append(label.Label(terminalio.FONT, text='', color=0x000000,
81-
anchored_position=(DISPLAY.width//2, -2),
66+
anchored_position=(MACROPAD.display.width//2, -2),
8267
anchor_point=(0.5, 0.0), max_glyphs=30))
83-
DISPLAY.show(GROUP)
68+
MACROPAD.display.show(GROUP)
8469

8570
# Load all the macro key setups from .py files in MACRO_FOLDER
8671
APPS = []
@@ -97,45 +82,66 @@ def switch(self):
9782

9883
if not APPS:
9984
GROUP[13].text = 'NO MACRO FILES FOUND'
100-
DISPLAY.refresh()
85+
MACROPAD.display.refresh()
10186
while True:
10287
pass
10388

10489
LAST_POSITION = None
90+
LAST_ENCODER_SWITCH = MACROPAD.encoder_switch_debounced.pressed
10591
APP_INDEX = 0
10692
APPS[APP_INDEX].switch()
10793

10894

10995
# MAIN LOOP ----------------------------
11096

11197
while True:
112-
POSITION = ENCODER.position
98+
# Read encoder position. If it's changed, switch apps.
99+
POSITION = MACROPAD.encoder
113100
if POSITION != LAST_POSITION:
114101
APP_INDEX = POSITION % len(APPS)
115102
APPS[APP_INDEX].switch()
116103
LAST_POSITION = POSITION
117104

118-
EVENT = KEYS.events.get()
119-
if EVENT and EVENT.key_number < len(APPS[APP_INDEX].macros):
120-
SEQUENCE = APPS[APP_INDEX].macros[EVENT.key_number][2]
121-
if EVENT.pressed:
122-
if EVENT.key_number < 12:
123-
PIXELS[EVENT.key_number] = 0xFFFFFF
124-
PIXELS.show()
125-
for item in SEQUENCE:
126-
if isinstance(item, int):
127-
if item >= 0:
128-
KEYBOARD.press(item)
129-
else:
130-
KEYBOARD.release(item)
105+
# Handle encoder button. If state has changed, and if there's a
106+
# corresponding macro, set up variables to act on this just like
107+
# the keypad keys, as if it were a 13th key/macro.
108+
MACROPAD.encoder_switch_debounced.update()
109+
ENCODER_SWITCH = MACROPAD.encoder_switch_debounced.pressed
110+
if ENCODER_SWITCH != LAST_ENCODER_SWITCH:
111+
LAST_ENCODER_SWITCH = ENCODER_SWITCH
112+
if len(APPS[APP_INDEX].macros) < 13:
113+
continue # No 13th macro, just resume main loop
114+
KEY_NUMBER = 12 # else process below as 13th macro
115+
PRESSED = ENCODER_SWITCH
116+
else:
117+
EVENT = MACROPAD.keys.events.get()
118+
if not EVENT or EVENT.key_number >= len(APPS[APP_INDEX].macros):
119+
continue # No key events, or no corresponding macro, resume loop
120+
KEY_NUMBER = EVENT.key_number
121+
PRESSED = EVENT.pressed
122+
123+
# If code reaches here, a key or the encoder button WAS pressed/released
124+
# and there IS a corresponding macro available for it...other situations
125+
# are avoided by 'continue' statements above which resume the loop.
126+
127+
SEQUENCE = APPS[APP_INDEX].macros[KEY_NUMBER][2]
128+
if PRESSED:
129+
if KEY_NUMBER < 12: # No pixel for encoder button
130+
MACROPAD.pixels[KEY_NUMBER] = 0xFFFFFF
131+
MACROPAD.pixels.show()
132+
for item in SEQUENCE:
133+
if isinstance(item, int):
134+
if item >= 0:
135+
MACROPAD.keyboard.press(item)
131136
else:
132-
LAYOUT.write(item)
133-
else:
134-
# Release any still-pressed modifier keys
135-
for item in SEQUENCE:
136-
if isinstance(item, int) and item >= 0:
137-
KEYBOARD.release(item)
138-
if EVENT.key_number < 12:
139-
PIXELS[EVENT.key_number] = APPS[APP_INDEX].macros[
140-
EVENT.key_number][0]
141-
PIXELS.show()
137+
MACROPAD.keyboard.release(item)
138+
else:
139+
MACROPAD._keyboard_layout.write(item)
140+
else:
141+
# Release any still-pressed modifier keys
142+
for item in SEQUENCE:
143+
if isinstance(item, int) and item >= 0:
144+
MACROPAD.keyboard.release(item)
145+
if KEY_NUMBER < 12: # No pixel for encoder button
146+
MACROPAD.pixels[KEY_NUMBER] = APPS[APP_INDEX].macros[KEY_NUMBER][0]
147+
MACROPAD.pixels.show()

0 commit comments

Comments
 (0)