Skip to content

Commit abba97d

Browse files
authored
Merge pull request #16 from mchilli:new_keycode_macro
Improved keycode macro
2 parents 6630349 + ffbb406 commit abba97d

File tree

11 files changed

+417
-89
lines changed

11 files changed

+417
-89
lines changed

circuitpython/code.py

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
gc.enable()
2626
supervisor.runtime.autoreload = False
2727

28-
VERSION = "1.4.1"
28+
VERSION = "1.4.2"
2929
# The file in which the settings are saved
3030
SETTINGSFILE = "settings.json"
3131
# The file in which the macros are saved
@@ -173,7 +173,7 @@ def _init_group_label(self) -> None:
173173
padding_bottom=0,
174174
padding_left=0,
175175
padding_right=0,
176-
color=0xFFFFFF,
176+
color=0xffffff,
177177
anchored_position=(
178178
self.macropad.display.width // 2,
179179
self.macropad.display.height - 10),
@@ -196,7 +196,7 @@ def _init_keys(self) -> None:
196196
padding_bottom=1,
197197
padding_left=4,
198198
padding_right=4,
199-
color=0xFFFFFF,
199+
color=0xffffff,
200200
anchored_position=(
201201
(self.macropad.display.width - 2) / 2 * (i % 3) + 1,
202202
self.macropad.display.height / 5 * (i // 3) + 2),
@@ -235,16 +235,32 @@ def run_macro(self, item: tuple[str, list], *args) -> None:
235235
if isinstance(key, (int, float)):
236236
time.sleep(key)
237237
elif isinstance(key, str):
238-
self.macropad.keyboard_layout.write(key)
238+
try:
239+
self.macropad.keyboard_layout.write(key)
240+
except ValueError:
241+
# if any of the characters has no keycode
242+
pass
239243
elif isinstance(key, dict):
240244
if 'kc' in key:
241-
key_name = key['kc'][1:] if key['kc'][:1] == "-" else key['kc']
242-
key_code = getattr(Keycode, key_name.upper(), None)
243-
if key_code:
244-
if key['kc'][:1] != "-":
245-
self.macropad.keyboard.press(key_code)
245+
key_codes = [
246+
getattr(Keycode, key_name, None)
247+
for key_name in key['kc'].lstrip('-+').upper().split(',')
248+
if getattr(Keycode, key_name, None) is not None
249+
]
250+
if key_codes:
251+
if key['kc'] == 'RELALL':
252+
# release all keys
253+
self.macropad.keyboard.release_all()
254+
elif key['kc'][0] == '+':
255+
# tap keys
256+
self.macropad.keyboard.press(*key_codes)
257+
self.macropad.keyboard.release(*key_codes)
258+
elif key['kc'][0] == '-':
259+
# release keys
260+
self.macropad.keyboard.release(*key_codes)
246261
else:
247-
self.macropad.keyboard.release(key_code)
262+
# press keys
263+
self.macropad.keyboard.press(*key_codes)
248264
if 'ccc' in key:
249265
control_code = getattr(
250266
ConsumerControlCode, key['ccc'].upper(), None)
@@ -304,6 +320,11 @@ def go_to_root(self, *args) -> None:
304320
def _update_tab(self) -> None:
305321
""" update the current displayed group tab
306322
"""
323+
key_funcs = {
324+
"macro": self.run_macro,
325+
"group": self.open_group
326+
}
327+
307328
for key in self.keys:
308329
key.clear_props()
309330

@@ -313,33 +334,18 @@ def _update_tab(self) -> None:
313334
if key_id:
314335
macro_data = json.loads(self.macro_store[str(key_id)])
315336
key_type = macro_data["type"]
316-
317-
self.keys[i].type = key_type
318-
self.keys[i].label = "" if key_type == "blank" else macro_data["label"]
319-
self.keys[i].color = (0, 0, 0) if key_type == "blank" else macro_data["color"]
320-
self.keys[i].set_func(self._get_key_func(key_type), (str(key_id), macro_data["content"]))
337+
338+
key = self.keys[i]
339+
key.type = key_type
340+
key.label = macro_data["label"]
341+
key.color = macro_data["color"]
342+
key.set_func(key_funcs.get(key_type), (str(key_id), macro_data["content"]))
321343

322344
self.group_label.text = group["label"]
323345

324346
for key in self.keys:
325347
key.update_colors()
326348

327-
def _get_key_func(self, type: str) -> function:
328-
""" get the specific function for the type
329-
330-
Args:
331-
type (str): the item type
332-
333-
Returns:
334-
function: return the function for type
335-
"""
336-
key_funcs = {
337-
"blank": None,
338-
"group": self.open_group
339-
}
340-
341-
return key_funcs.get(type, self.run_macro)
342-
343349
def _update_encoder_macros(self) -> None:
344350
""" update the rotary encoder macros defined for opened group
345351
"""
@@ -429,7 +435,7 @@ def _handle_serial_data(self, payload: object) -> dict:
429435
return
430436

431437
response['ACK'] = 'Macros received'
432-
response['CONTENT'] = len(self.macro_store)
438+
response['CONTENT'] = len(self.macro_store) - 1
433439
return response
434440

435441
elif command == 'save_macros':
@@ -545,14 +551,14 @@ def start(self) -> None:
545551
if key_event.pressed and not any([key.pressed for key in self.keys]):
546552
self.keys[key_event.key_number].pressed = True
547553
active_key = key_event.key_number
548-
self.active_key_delay = time.monotonic()
554+
active_key_delay = time.monotonic()
549555

550556
elif key_event.released and key_event.key_number == active_key:
551557
self.keys[key_event.key_number].pressed = False
552558
active_key = None
553559

554560
# if a key is pressed continuously, the function triggers again after a short delay
555-
if active_key and time.monotonic() - self.active_key_delay > 0.75:
561+
if active_key is not None and time.monotonic() - active_key_delay > 0.75:
556562
self._display_on()
557563
self.keys[active_key].call_func()
558564

circuitpython/utils/devices.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def color(self, color:tuple) -> None:
108108
def update_colors(self) -> None:
109109
""" update the backgroundcolor and color based on type
110110
"""
111-
if self.type in ["blank", "group"]:
111+
if self.type in [None, "group"]:
112112
self._label.background_color = 0x000000
113113
self._label.color = 0xffffff
114114
else:
@@ -117,12 +117,14 @@ def update_colors(self) -> None:
117117

118118
self._set_led(self.color)
119119

120-
def _set_led(self, color:tuple[int, int, int]) -> None:
120+
def _set_led(self, color:list[int, int, int] | str) -> None:
121121
""" set and update the led color
122122
123123
Args:
124-
color (tuple): the led color (R, G, B)
124+
color (list | string): the led color (R, G, B) | rrggbb
125125
"""
126+
if isinstance(color, str):
127+
color = (int(color[0:2], 16), int(color[2:4], 16), int(color[4:6], 16))
126128
self._macropad.pixels[self._index] = color
127129
self._macropad.pixels.show()
128130

@@ -131,7 +133,7 @@ def clear_props(self) -> None:
131133
"""
132134
self._label.text = ""
133135
self._type = None
134-
self._color = (0, 0, 0)
136+
self._color = '000000'
135137
self._func = None
136138
self._func_args = None
137139

@@ -158,7 +160,7 @@ def _on_pressed(self) -> None:
158160
""" Action that triggered when Key is pressed
159161
"""
160162
if self._func:
161-
self._set_led((255, 255, 255))
163+
self._set_led('ffffff')
162164
self.call_func()
163165

164166
def _on_released(self) -> None:

webui/css/style.css

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8272,6 +8272,8 @@ readers do not read off random characters that represent icons */
82728272
--key-control-border-radius: 10px;
82738273
--dialog-button-size: 40px;
82748274
--dialog-gap: calc(var(--dialog-button-size) / 10);
8275+
--macro-visible-count: 10;
8276+
--macro-entry-min-width: 400px;
82758277
--macro-entry-height: 30px;
82768278
}
82778279

@@ -8602,13 +8604,14 @@ body {
86028604
border: 1px solid var(--color-grey);
86038605
box-sizing: border-box;
86048606
padding: var(--dialog-gap);
8605-
max-height: calc(var(--macro-entry-height) * 5 + var(--dialog-gap) * 6);
8607+
max-height: calc(var(--macro-entry-height) * var(--macro-visible-count) + var(--dialog-gap) * (var(--macro-visible-count) + 1));
86068608
overflow: auto;
86078609
}
86088610
.dialog-container .dialog .dialog-content .dialog-inputs .input-macros .input-sortable .macro-entry-container {
86098611
display: flex;
86108612
gap: var(--dialog-gap);
86118613
align-items: center;
8614+
min-width: var(--macro-entry-min-width);
86128615
height: var(--macro-entry-height);
86138616
padding: var(--dialog-gap);
86148617
background-color: var(--color-green);
@@ -8620,6 +8623,9 @@ body {
86208623
cursor: grab;
86218624
}
86228625
.dialog-container .dialog .dialog-content .dialog-inputs .input-macros .input-sortable .macro-entry-container .macro-entry-content {
8626+
display: flex;
8627+
flex-grow: 1;
8628+
align-items: center;
86238629
text-wrap: nowrap;
86248630
}
86258631
.dialog-container .dialog .dialog-content .dialog-inputs .input-macros .input-sortable .macro-entry-container .macro-entry-controls {

0 commit comments

Comments
 (0)