Skip to content

Commit 3aadb88

Browse files
Add docstrings, add LED blink to keys
1 parent e3b50fc commit 3aadb88

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

Macropad_Macros/code.py

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""
2-
Add description here
2+
A fairly straightforward macro/hotkey program for Adafruit MACROPAD.
3+
Macro key setups are stored in the /macros folder (configurable below),
4+
load up just the ones you're likely to use. Plug into computer's USB port,
5+
use dial to select an application macro set, press MACROPAD keys to send
6+
key sequences.
37
"""
48

59
# pylint: disable=import-error, unused-import, too-few-public-methods, eval-used
@@ -18,14 +22,16 @@
1822
from adafruit_hid.keyboard import Keyboard
1923
from adafruit_hid.keycode import Keycode
2024

25+
2126
# CONFIGURABLES ------------------------
2227

2328
MACRO_FOLDER = '/macros'
2429

30+
2531
# CLASSES AND FUNCTIONS ----------------
2632

2733
class Key:
28-
""" Add class doccstring here"""
34+
""" Class representing the physical hardware of each MACROPAD key. """
2935
DEBOUNCE_TIME = 1 / 50
3036

3137
def __init__(self, keyname):
@@ -36,7 +42,9 @@ def __init__(self, keyname):
3642
self.last_time = time.monotonic()
3743

3844
def debounce(self):
39-
""" Add function docstring here """
45+
""" Read a key's current state (hardware pin value), filtering out
46+
any "bounce" noise. This function needs to be called frequently,
47+
once for each key on pad, plus encoder switch. """
4048
value = self.pin.value
4149
if value != self.last_value:
4250
now = time.monotonic()
@@ -48,7 +56,8 @@ def debounce(self):
4856
return None
4957

5058
class Macro:
51-
""" Add class doccstring here"""
59+
""" Class representing a single macro sequence - a text label, LED color
60+
for the keypad, and a keycode sequence to issue when activated. """
5261
def __init__(self, desc, color, sequence):
5362
self.desc = desc
5463
self.color = eval(color)
@@ -60,7 +69,8 @@ def __init__(self, desc, color, sequence):
6069
break
6170

6271
class App:
63-
""" Add class doccstring here"""
72+
""" Class representing a host-side application, for which we have a set
73+
of macro sequences. """
6474
def __init__(self, filename):
6575
with open(MACRO_FOLDER + '/' + filename) as jsonfile:
6676
json_data = json.load(jsonfile)
@@ -74,22 +84,24 @@ def __init__(self, filename):
7484
mac['sequence'] if 'sequence' in mac else None))
7585

7686
def switch(self):
77-
""" Add function docstring here """
78-
GROUP[12].text = self.name
87+
""" Activate application settings; update OLED labels and LED
88+
colors. """
89+
GROUP[12].text = self.name # Application name
7990
for i in range(12):
80-
if i < len(self.macros):
91+
if i < len(self.macros): # Key in use, set label + LED color
8192
PIXELS[i] = self.macros[i].color
8293
GROUP[i].text = self.macros[i].desc
83-
else:
94+
else: # Key not in use, no label or LED
8495
PIXELS[i] = 0
8596
GROUP[i].text = ''
8697
PIXELS.show()
8798

88-
# Convert key code name (e.g. "COMMAND") to a numeric value for press/release
8999
def code(name):
90-
""" Add function doccstring here"""
100+
""" Convert a key code name (e.g. 'COMMAND') to a numeric value for
101+
press/release events. """
91102
return eval('Keycode.' + name.upper())
92103

104+
93105
# INITIALIZATION -----------------------
94106

95107
DISPLAY = board.DISPLAY
@@ -133,6 +145,7 @@ def code(name):
133145
APP_INDEX = 0
134146
APPS[APP_INDEX].switch()
135147

148+
136149
# MAIN LOOP ----------------------------
137150

138151
while True:
@@ -147,6 +160,8 @@ def code(name):
147160
if action is not None:
148161
keys = APPS[APP_INDEX].macros[KEY_INDEX].sequence
149162
if action is False: # Macro key pressed
163+
PIXELS[KEY_INDEX] = 0xFFFFFF
164+
PIXELS.show()
150165
if APPS[APP_INDEX].macros[KEY_INDEX].in_order:
151166
for x in APPS[APP_INDEX].macros[KEY_INDEX].sequence:
152167
if x.startswith('+'): # Press and hold key
@@ -156,7 +171,7 @@ def code(name):
156171
else: # Press and release key
157172
KEYBOARD.press(code(x))
158173
KEYBOARD.release(code(x))
159-
else:
174+
else: # Send press events now, release later
160175
for x in APPS[APP_INDEX].macros[KEY_INDEX].sequence:
161176
KEYBOARD.press(code(x))
162177
elif action is True: # Macro key released
@@ -166,3 +181,5 @@ def code(name):
166181
KEYBOARD.release(code(x[1:]))
167182
else:
168183
KEYBOARD.release(code(x))
184+
PIXELS[KEY_INDEX] = APPS[APP_INDEX].macros[KEY_INDEX].color
185+
PIXELS.show()

0 commit comments

Comments
 (0)