Resolves #56 by moving kmk.common.* up a level to kmk.*

This commit is contained in:
Josh Klar
2018-10-11 18:02:13 -07:00
parent 30cd5da3f1
commit 00899d1b0f
35 changed files with 85 additions and 88 deletions

0
kmk/macros/__init__.py Normal file
View File

View File

@@ -0,0 +1,76 @@
import math
from kmk.event_defs import (hid_report_event, keycode_down_event,
keycode_up_event)
from kmk.keycodes import Media
from kmk.rotary_encoder import RotaryEncoder
VAL_FALSE = False + 1
VAL_NONE = True + 2
VAL_TRUE = True + 1
VOL_UP_PRESS = keycode_down_event(Media.KC_AUDIO_VOL_UP)
VOL_UP_RELEASE = keycode_up_event(Media.KC_AUDIO_VOL_UP)
VOL_DOWN_PRESS = keycode_down_event(Media.KC_AUDIO_VOL_DOWN)
VOL_DOWN_RELEASE = keycode_up_event(Media.KC_AUDIO_VOL_DOWN)
class RotaryEncoderMacro:
def __init__(self, pos_pin, neg_pin, slop_history=1, slop_threshold=1):
self.encoder = RotaryEncoder(pos_pin, neg_pin)
self.max_history = slop_history
self.history = bytearray(slop_history)
self.history_idx = 0
self.history_threshold = math.floor(slop_threshold * slop_history)
def scan(self):
# Anti-slop logic
self.history[self.history_idx] = 0
reading = self.encoder.direction()
self.history[self.history_idx] = VAL_NONE if reading is None else reading + 1
self.history_idx += 1
if self.history_idx >= self.max_history:
self.history_idx = 0
nones = 0
trues = 0
falses = 0
for val in self.history:
if val == VAL_NONE:
nones += 1
elif val == VAL_TRUE:
trues += 1
elif val == VAL_FALSE:
falses += 1
if nones >= self.history_threshold:
return None
if trues >= self.history_threshold:
return self.on_increase()
if falses >= self.history_threshold:
return self.on_decrease()
def on_decrease(self):
pass
def on_increase(self):
pass
class VolumeRotaryEncoder(RotaryEncoderMacro):
def on_decrease(self):
yield VOL_DOWN_PRESS
yield hid_report_event
yield VOL_DOWN_RELEASE
yield hid_report_event
def on_increase(self):
yield VOL_UP_PRESS
yield hid_report_event
yield VOL_UP_RELEASE
yield hid_report_event

58
kmk/macros/simple.py Normal file
View File

@@ -0,0 +1,58 @@
import string
from kmk.event_defs import (hid_report_event, keycode_down_event,
keycode_up_event)
from kmk.keycodes import Keycodes, Macro, RawKeycodes, char_lookup
from kmk.kmktime import sleep_ms
kc_lookup_cache = {}
def lookup_kc_with_cache(char):
found_code = kc_lookup_cache.get(
char,
getattr(Keycodes.Common, 'KC_{}'.format(char.upper())),
)
kc_lookup_cache[char] = found_code
kc_lookup_cache[char.upper()] = found_code
kc_lookup_cache[char.lower()] = found_code
return found_code
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
if not getattr(key, 'no_release', None):
yield keycode_up_event(key)
yield hid_report_event
return Macro(keydown=_simple_key_sequence)
def send_string(message):
seq = []
for char in message:
kc = None
if char in char_lookup:
kc = char_lookup[char]
elif char in string.ascii_letters + string.digits:
kc = lookup_kc_with_cache(char)
if char.isupper():
kc = Keycodes.Modifiers.KC_LSHIFT(kc)
seq.append(kc)
return simple_key_sequence(seq)

109
kmk/macros/unicode.py Normal file
View File

@@ -0,0 +1,109 @@
from kmk.consts import UnicodeModes
from kmk.event_defs import (hid_report_event, keycode_down_event,
keycode_up_event)
from kmk.keycodes import Common, Macro, Modifiers
from kmk.macros.simple import lookup_kc_with_cache, simple_key_sequence
from kmk.util import get_wide_ordinal
IBUS_KEY_COMBO = Modifiers.KC_LCTRL(Modifiers.KC_LSHIFT(Common.KC_U))
IBUS_KEY_DOWN = keycode_down_event(IBUS_KEY_COMBO)
IBUS_KEY_UP = keycode_up_event(IBUS_KEY_COMBO)
RALT_DOWN = keycode_down_event(Modifiers.KC_RALT)
RALT_UP = keycode_up_event(Modifiers.KC_RALT)
U_DOWN = keycode_down_event(Common.KC_U)
U_UP = keycode_up_event(Common.KC_U)
ENTER_DOWN = keycode_down_event(Common.KC_ENTER)
ENTER_UP = keycode_up_event(Common.KC_ENTER)
RALT_DOWN_NO_RELEASE = keycode_down_event(Modifiers.KC_RALT(no_release=True))
RALT_UP_NO_PRESS = keycode_up_event(Modifiers.KC_RALT(no_press=True))
def generate_codepoint_keysym_seq(codepoint):
# To make MacOS and Windows happy, always try to send
# sequences that are of length 4 at a minimum
# On Linux systems, we can happily send longer strings.
# They will almost certainly break on MacOS and Windows,
# but this is a documentation problem more than anything.
# Not sure how to send emojis on Mac/Windows like that,
# though, since (for example) the Canadian flag is assembled
# from two five-character codepoints, 1f1e8 and 1f1e6
seq = [Common.KC_0 for _ in range(max(len(codepoint), 4))]
for idx, codepoint_fragment in enumerate(reversed(codepoint)):
seq[-(idx + 1)] = lookup_kc_with_cache(codepoint_fragment)
return seq
def unicode_string_sequence(unistring):
'''
Allows sending things like (╯°□°)╯︵ ┻━┻ directly, without
manual conversion to Unicode codepoints.
'''
return unicode_codepoint_sequence([
hex(get_wide_ordinal(s))[2:]
for s in unistring
])
def unicode_codepoint_sequence(codepoints):
kc_seqs = (
generate_codepoint_keysym_seq(codepoint)
for codepoint in codepoints
)
kc_macros = [
simple_key_sequence(kc_seq)
for kc_seq in kc_seqs
]
def _unicode_sequence(state):
if state.unicode_mode == UnicodeModes.IBUS:
yield from _ibus_unicode_sequence(kc_macros, state)
elif state.unicode_mode == UnicodeModes.RALT:
yield from _ralt_unicode_sequence(kc_macros, state)
elif state.unicode_mode == UnicodeModes.WINC:
yield from _winc_unicode_sequence(kc_macros, state)
return Macro(keydown=_unicode_sequence)
def _ralt_unicode_sequence(kc_macros, state):
for kc_macro in kc_macros:
yield RALT_DOWN_NO_RELEASE
yield hid_report_event
yield from kc_macro.keydown(state)
yield RALT_UP_NO_PRESS
yield hid_report_event
def _ibus_unicode_sequence(kc_macros, state):
for kc_macro in kc_macros:
yield IBUS_KEY_DOWN
yield hid_report_event
yield IBUS_KEY_UP
yield hid_report_event
yield from kc_macro.keydown(state)
yield ENTER_DOWN
yield hid_report_event
yield ENTER_UP
yield hid_report_event
def _winc_unicode_sequence(kc_macros, state):
'''
Send unicode sequence using WinCompose:
http://wincompose.info/
https://github.com/SamHocevar/wincompose
'''
for kc_macro in kc_macros:
yield RALT_DOWN
yield hid_report_event
yield RALT_UP
yield hid_report_event
yield U_DOWN
yield hid_report_event
yield U_UP
yield hid_report_event
yield from kc_macro.keydown(state)