Checkpoint alpha: Reflow macros and keycodes into a consistent structure. Most internal state functionality largely untouched (just moved)

This commit is contained in:
Josh Klar
2018-12-29 04:44:52 -08:00
parent af140a16a6
commit 39a6465658
12 changed files with 767 additions and 967 deletions

View File

@@ -1,76 +0,0 @@
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

View File

@@ -1,28 +0,0 @@
from kmk.keycodes import Keycodes, Macro, char_lookup, lookup_kc_with_cache
from kmk.string import ascii_letters, digits
def simple_key_sequence(seq):
def _simple_key_sequence(state):
return seq
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 ascii_letters + digits:
kc = lookup_kc_with_cache(char)
if char.isupper():
kc = Keycodes.Modifiers.KC_LSHIFT(kc)
seq.append(kc)
return simple_key_sequence(seq)

View File

@@ -1,16 +1,15 @@
from kmk.consts import UnicodeMode
from kmk.keycodes import (Common, Macro, Modifiers,
generate_codepoint_keysym_seq)
from kmk.keycodes import ALL_KEYS, KC, Macro
from kmk.macros.simple import simple_key_sequence
from kmk.types import AttrDict
from kmk.util import get_wide_ordinal
IBUS_KEY_COMBO = Modifiers.KC_LCTRL(Modifiers.KC_LSHIFT(Common.KC_U))
RALT_KEY = Modifiers.KC_RALT
U_KEY = Common.KC_U
ENTER_KEY = Common.KC_ENTER
RALT_DOWN_NO_RELEASE = Modifiers.KC_RALT(no_release=True)
RALT_UP_NO_PRESS = Modifiers.KC_RALT(no_press=True)
IBUS_KEY_COMBO = KC.LCTRL(KC.LSHIFT(KC.U))
RALT_KEY = KC.RALT
U_KEY = KC.U
ENTER_KEY = KC.ENTER
RALT_DOWN_NO_RELEASE = KC.RALT(no_release=True)
RALT_UP_NO_PRESS = KC.RALT(no_press=True)
def compile_unicode_string_sequences(string_table):
@@ -31,6 +30,26 @@ def unicode_string_sequence(unistring):
])
def generate_codepoint_keysym_seq(codepoint, expected_length=4):
# 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
#
# As a bonus, this function can be pretty useful for
# leader dictionary keys as strings.
seq = [KC.N0 for _ in range(max(len(codepoint), expected_length))]
for idx, codepoint_fragment in enumerate(reversed(codepoint)):
seq[-(idx + 1)] = ALL_KEYS.get(codepoint_fragment)
return seq
def unicode_codepoint_sequence(codepoints):
kc_seqs = (
generate_codepoint_keysym_seq(codepoint)