Add support for millisecond delays within a macro sequence

This commit is contained in:
Josh Klar 2018-09-30 21:34:16 -07:00
parent 2c05efa805
commit fde9b7e274
No known key found for this signature in database
GPG Key ID: 220F99BD7DB7A99E
4 changed files with 28 additions and 1 deletions

View File

@ -40,12 +40,15 @@ class RawKeycodes:
KC_UC_MODE = 1109
KC_MACRO_SLEEP_MS = 1110
# These shouldn't have all the fancy shenanigans Keycode allows
# such as no_press, because they modify KMK internal state in
# ways we need to tightly control. Thus, we can get away with
# a lighter-weight namedtuple implementation here
LayerKeycode = namedtuple('LayerKeycode', ('code', 'layer'))
MacroSleepKeycode = namedtuple('MacroSleepKeycode', ('code', 'ms'))
class UnicodeModeKeycode(namedtuple('UnicodeModeKeycode', ('code', 'mode'))):
@ -455,6 +458,10 @@ class KMK(KeycodeCategory):
)
KC_UC_MODE_WINC = UnicodeModeKeycode.from_mode_const(UnicodeModes.WINC)
@staticmethod
def KC_MACRO_SLEEP_MS(ms):
return MacroSleepKeycode(RawKeycodes.KC_MACRO_SLEEP_MS, ms)
class Layers(KeycodeCategory):
@staticmethod

View File

@ -2,13 +2,18 @@ import string
from kmk.common.event_defs import (hid_report_event, keycode_down_event,
keycode_up_event)
from kmk.common.keycodes import Keycodes, char_lookup
from kmk.common.keycodes import Keycodes, RawKeycodes, char_lookup
from kmk.common.macros import KMKMacro
from kmk.common.util import sleep_ms
def simple_key_sequence(seq):
def _simple_key_sequence(state):
for key in seq:
if key.code == RawKeycodes.KC_MACRO_SLEEP_MS:
sleep_ms(key.ms)
continue
if not getattr(key, 'no_press', None):
yield keycode_down_event(key)
yield hid_report_event()

View File

@ -29,3 +29,16 @@ def reset_bootloader():
import microcontroller
microcontroller.on_next_reset(microcontroller.RunMode.BOOTLOADER)
microcontroller.reset()
def sleep_ms(ms):
'''
Tries to sleep for a number of milliseconds in a cross-implementation
way. Will raise an ImportError if time is not available on the platform.
'''
try:
import time
time.sleep_ms(ms)
except AttributeError:
import time
time.sleep(ms / 1000)

View File

@ -22,6 +22,8 @@ MACRO_TEST_SIMPLE = simple_key_sequence([
KC.SPACE,
KC.MACRO_SLEEP_MS(500),
KC.LSHIFT(KC.K),
KC.LSHIFT(KC.M),
KC.LSHIFT(KC.K),