1
1
"""
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.
3
7
"""
4
8
5
9
# pylint: disable=import-error, unused-import, too-few-public-methods, eval-used
18
22
from adafruit_hid .keyboard import Keyboard
19
23
from adafruit_hid .keycode import Keycode
20
24
25
+
21
26
# CONFIGURABLES ------------------------
22
27
23
28
MACRO_FOLDER = '/macros'
24
29
30
+
25
31
# CLASSES AND FUNCTIONS ----------------
26
32
27
33
class Key :
28
- """ Add class doccstring here """
34
+ """ Class representing the physical hardware of each MACROPAD key. """
29
35
DEBOUNCE_TIME = 1 / 50
30
36
31
37
def __init__ (self , keyname ):
@@ -36,7 +42,9 @@ def __init__(self, keyname):
36
42
self .last_time = time .monotonic ()
37
43
38
44
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. """
40
48
value = self .pin .value
41
49
if value != self .last_value :
42
50
now = time .monotonic ()
@@ -48,7 +56,8 @@ def debounce(self):
48
56
return None
49
57
50
58
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. """
52
61
def __init__ (self , desc , color , sequence ):
53
62
self .desc = desc
54
63
self .color = eval (color )
@@ -60,7 +69,8 @@ def __init__(self, desc, color, sequence):
60
69
break
61
70
62
71
class App :
63
- """ Add class doccstring here"""
72
+ """ Class representing a host-side application, for which we have a set
73
+ of macro sequences. """
64
74
def __init__ (self , filename ):
65
75
with open (MACRO_FOLDER + '/' + filename ) as jsonfile :
66
76
json_data = json .load (jsonfile )
@@ -74,22 +84,24 @@ def __init__(self, filename):
74
84
mac ['sequence' ] if 'sequence' in mac else None ))
75
85
76
86
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
79
90
for i in range (12 ):
80
- if i < len (self .macros ):
91
+ if i < len (self .macros ): # Key in use, set label + LED color
81
92
PIXELS [i ] = self .macros [i ].color
82
93
GROUP [i ].text = self .macros [i ].desc
83
- else :
94
+ else : # Key not in use, no label or LED
84
95
PIXELS [i ] = 0
85
96
GROUP [i ].text = ''
86
97
PIXELS .show ()
87
98
88
- # Convert key code name (e.g. "COMMAND") to a numeric value for press/release
89
99
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. """
91
102
return eval ('Keycode.' + name .upper ())
92
103
104
+
93
105
# INITIALIZATION -----------------------
94
106
95
107
DISPLAY = board .DISPLAY
@@ -133,6 +145,7 @@ def code(name):
133
145
APP_INDEX = 0
134
146
APPS [APP_INDEX ].switch ()
135
147
148
+
136
149
# MAIN LOOP ----------------------------
137
150
138
151
while True :
@@ -147,6 +160,8 @@ def code(name):
147
160
if action is not None :
148
161
keys = APPS [APP_INDEX ].macros [KEY_INDEX ].sequence
149
162
if action is False : # Macro key pressed
163
+ PIXELS [KEY_INDEX ] = 0xFFFFFF
164
+ PIXELS .show ()
150
165
if APPS [APP_INDEX ].macros [KEY_INDEX ].in_order :
151
166
for x in APPS [APP_INDEX ].macros [KEY_INDEX ].sequence :
152
167
if x .startswith ('+' ): # Press and hold key
@@ -156,7 +171,7 @@ def code(name):
156
171
else : # Press and release key
157
172
KEYBOARD .press (code (x ))
158
173
KEYBOARD .release (code (x ))
159
- else :
174
+ else : # Send press events now, release later
160
175
for x in APPS [APP_INDEX ].macros [KEY_INDEX ].sequence :
161
176
KEYBOARD .press (code (x ))
162
177
elif action is True : # Macro key released
@@ -166,3 +181,5 @@ def code(name):
166
181
KEYBOARD .release (code (x [1 :]))
167
182
else :
168
183
KEYBOARD .release (code (x ))
184
+ PIXELS [KEY_INDEX ] = APPS [APP_INDEX ].macros [KEY_INDEX ].color
185
+ PIXELS .show ()
0 commit comments