Skip to content

Commit 80cd34a

Browse files
Refactor to use keypad module, update pylint rules, remove unused import time
1 parent aca2763 commit 80cd34a

File tree

1 file changed

+31
-56
lines changed

1 file changed

+31
-56
lines changed

Macropad_Hotkeys/code.py

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

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

1111
import os
12-
import time
1312
import board
1413
import digitalio
1514
import displayio
1615
import neopixel
1716
import rotaryio
17+
import keypad
1818
import terminalio
1919
import usb_hid
2020
from adafruit_display_shapes.rect import Rect
@@ -31,31 +31,6 @@
3131

3232
# CLASSES AND FUNCTIONS ----------------
3333

34-
class Key:
35-
""" Class representing the physical hardware of each MACROPAD key. """
36-
DEBOUNCE_TIME = 1 / 50
37-
38-
def __init__(self, keyname):
39-
self.pin = digitalio.DigitalInOut(keyname)
40-
self.pin.direction = digitalio.Direction.INPUT
41-
self.pin.pull = digitalio.Pull.UP
42-
self.last_value = self.pin.value # Initial state
43-
self.last_time = time.monotonic()
44-
45-
def debounce(self):
46-
""" Read a key's current state (hardware pin value), filtering out
47-
any "bounce" noise. This function needs to be called frequently,
48-
once for each key on pad, plus encoder switch. """
49-
value = self.pin.value
50-
if value != self.last_value:
51-
now = time.monotonic()
52-
elapsed = now - self.last_time
53-
if elapsed >= self.DEBOUNCE_TIME:
54-
self.last_value = value
55-
self.last_time = now
56-
return value
57-
return None
58-
5934
class App:
6035
""" Class representing a host-side application, for which we have a set
6136
of macro sequences. """
@@ -86,7 +61,12 @@ def switch(self):
8661
PIXELS = neopixel.NeoPixel(board.NEOPIXEL, 12, auto_write=False)
8762
KEYBOARD = Keyboard(usb_hid.devices)
8863
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)
8968

69+
# Set up displayio group with all labels
9070
GROUP = displayio.Group(max_size=14)
9171
for KEY_INDEX in range(12):
9272
x = KEY_INDEX % 3
@@ -102,12 +82,6 @@ def switch(self):
10282
anchor_point=(0.5, 0.0), max_glyphs=30))
10383
DISPLAY.show(GROUP)
10484

105-
KEYS = []
106-
for pin in (board.KEY1, board.KEY2, board.KEY3, board.KEY4, board.KEY5,
107-
board.KEY6, board.KEY7, board.KEY8, board.KEY9, board.KEY10,
108-
board.KEY11, board.KEY12, board.ENCODER_SWITCH):
109-
KEYS.append(Key(pin))
110-
11185
# Load all the macro key setups from .py files in MACRO_FOLDER
11286
APPS = []
11387
FILES = os.listdir(MACRO_FOLDER)
@@ -118,7 +92,8 @@ def switch(self):
11892
APPS.append(App(module.app))
11993

12094
if not APPS:
121-
print('No valid macro files found')
95+
GROUP[13].text = 'NO MACRO FILES FOUND'
96+
DISPLAY.refresh()
12297
while True:
12398
pass
12499

@@ -136,27 +111,27 @@ def switch(self):
136111
APPS[APP_INDEX].switch()
137112
LAST_POSITION = POSITION
138113

139-
for KEY_INDEX, KEY in enumerate(KEYS[0: len(APPS[APP_INDEX].macros)]):
140-
action = KEY.debounce()
141-
if action is not None:
142-
sequence = APPS[APP_INDEX].macros[KEY_INDEX][2]
143-
if action is False: # Macro key pressed
144-
if KEY_INDEX < 12:
145-
PIXELS[KEY_INDEX] = 0xFFFFFF
146-
PIXELS.show()
147-
for item in sequence:
148-
if isinstance(item, int):
149-
if item >= 0:
150-
KEYBOARD.press(item)
151-
else:
152-
KEYBOARD.release(item)
114+
EVENT = KEYS.events.get()
115+
if EVENT and EVENT.key_number < len(APPS[APP_INDEX].macros):
116+
SEQUENCE = APPS[APP_INDEX].macros[EVENT.key_number][2]
117+
if EVENT.pressed:
118+
if EVENT.key_number < 12:
119+
PIXELS[EVENT.key_number] = 0xFFFFFF
120+
PIXELS.show()
121+
for item in SEQUENCE:
122+
if isinstance(item, int):
123+
if item >= 0:
124+
KEYBOARD.press(item)
153125
else:
154-
LAYOUT.write(item)
155-
elif action is True: # Macro key released
156-
# Release any still-pressed modifier keys
157-
for item in sequence:
158-
if isinstance(item, int) and item >= 0:
159126
KEYBOARD.release(item)
160-
if KEY_INDEX < 12:
161-
PIXELS[KEY_INDEX] = APPS[APP_INDEX].macros[KEY_INDEX][0]
162-
PIXELS.show()
127+
else:
128+
LAYOUT.write(item)
129+
else:
130+
# Release any still-pressed modifier keys
131+
for item in SEQUENCE:
132+
if isinstance(item, int) and item >= 0:
133+
KEYBOARD.release(item)
134+
if EVENT.key_number < 12:
135+
PIXELS[EVENT.key_number] = APPS[APP_INDEX].macros[
136+
EVENT.key_number][0]
137+
PIXELS.show()

0 commit comments

Comments
 (0)