cleanup, less hacks, less GC

This commit is contained in:
Kyle Brown
2020-10-28 22:35:33 -07:00
parent 6d941a39bf
commit 2edad3c371
6 changed files with 46 additions and 82 deletions

View File

@@ -14,12 +14,11 @@ class KMKKeyboard(_KMKKeyboard):
split_type = 'UART' split_type = 'UART'
split_flip = True split_flip = True
split_offsets = [6, 6, 6, 6, 6]
uart_pin = board.SCL uart_pin = board.SCL
rgb_pixel_pin = board.TX rgb_pixel_pin = board.TX
extra_data_pin = board.SDA extra_data_pin = board.SDA
rgb_ext = RGB(pixel_pin=rgb_pixel_pin, num_pixels=12) rgb_ext = RGB(pixel_pin=rgb_pixel_pin, num_pixels=12)
layers_ext = Layers() layers_ext = Layers()
split = Split(uart_pin=uart_pin, split_offsets=split_offsets) split = Split(uart_pin=uart_pin)
extensions = [rgb_ext, split, layers_ext] extensions = [rgb_ext, split, layers_ext]

View File

@@ -1,7 +1,6 @@
import board import board
from kmk.extensions.layers import Layers from kmk.extensions.layers import Layers
from kmk.extensions.split import Split
from kmk.kmk_keyboard import KMKKeyboard as _KMKKeyboard from kmk.kmk_keyboard import KMKKeyboard as _KMKKeyboard
from kmk.matrix import DiodeOrientation from kmk.matrix import DiodeOrientation
from kmk.matrix import intify_coordinate as ic from kmk.matrix import intify_coordinate as ic
@@ -24,6 +23,7 @@ class KMKKeyboard(_KMKKeyboard):
uart_pin = board.P0_08 uart_pin = board.P0_08
rgb_pixel_pin = board.P0_06 rgb_pixel_pin = board.P0_06
extra_data_pin = board.SDA # TODO This is incorrect. Find better solution extra_data_pin = board.SDA # TODO This is incorrect. Find better solution
i2c = board.I2C
coord_mapping = [] coord_mapping = []
coord_mapping.extend(ic(0, x) for x in range(12)) coord_mapping.extend(ic(0, x) for x in range(12))
@@ -34,5 +34,4 @@ class KMKKeyboard(_KMKKeyboard):
coord_mapping.extend(ic(3, x) for x in range(3, 9)) coord_mapping.extend(ic(3, x) for x in range(3, 9))
layers_ext = Layers() layers_ext = Layers()
split = Split(uart_pin=uart_pin)
extensions = [layers_ext] extensions = [layers_ext]

View File

@@ -1,8 +1,3 @@
# There's a chance doing preload RAM hacks this late will cause recursion
# errors, but we'll see. I'd rather do it here than require everyone copy-paste
# a line into their keymaps.
import kmk.preload_imports # isort:skip # NOQA
from kmk.consts import KMK_RELEASE, UnicodeMode from kmk.consts import KMK_RELEASE, UnicodeMode
from kmk.hid import BLEHID, USBHID, AbstractHID, HIDModes from kmk.hid import BLEHID, USBHID, AbstractHID, HIDModes
from kmk.keys import KC from kmk.keys import KC
@@ -16,12 +11,13 @@ class KMKKeyboard:
# User-configurable # User-configurable
debug_enabled = False debug_enabled = False
keymap = None keymap = []
coord_mapping = None coord_mapping = None
row_pins = None row_pins = None
col_pins = None col_pins = None
diode_orientation = None diode_orientation = None
matrix = None
matrix_scanner = MatrixScanner matrix_scanner = MatrixScanner
uart_buffer = [] uart_buffer = []
@@ -34,10 +30,15 @@ class KMKKeyboard:
# Internal State # Internal State
_keys_pressed = set() _keys_pressed = set()
_coord_keys_pressed = {} _coord_keys_pressed = {}
hid_type = HIDModes.USB
_hid_helper = None _hid_helper = None
_hid_pending = False _hid_pending = False
_state_layer_key = None
_matrix_update = None _matrix_update = None
_matrix_modify = None _matrix_modify = None
_state_changed = False
_old_timeouts_len = None
_new_timeouts_len = None
# this should almost always be PREpended to, replaces # this should almost always be PREpended to, replaces
# former use of reversed_active_layers which had pointless # former use of reversed_active_layers which had pointless
@@ -110,43 +111,44 @@ class KMKKeyboard:
def _handle_matrix_report(self, update=None): def _handle_matrix_report(self, update=None):
if update is not None: if update is not None:
self._on_matrix_changed(update[0], update[1], update[2]) self._on_matrix_changed(update[0], update[1], update[2])
self.state_changed = True self._state_changed = True
##### #####
# SPLICE: INTERNAL STATE # SPLICE: INTERNAL STATE
# TODO FIXME CLEAN THIS # TODO FIXME CLEAN THIS
##### #####
def _find_key_in_map(self, row, col): def _find_key_in_map(self, int_coord, row, col):
ic = intify_coordinate(row, col) self._state_layer_key = None
try: try:
idx = self.coord_mapping.index(ic) idx = self.coord_mapping.index(int_coord)
except ValueError: except ValueError:
if self.debug_enabled: if self.debug_enabled:
print( print(
'CoordMappingNotFound(ic={}, row={}, col={})'.format(ic, row, col) 'CoordMappingNotFound(ic={}, row={}, col={})'.format(
int_coord, row, col
)
) )
return None return None
for layer in self._active_layers: for layer in self._active_layers:
layer_key = self.keymap[layer][idx] self._state_layer_key = self.keymap[layer][idx]
if not layer_key or layer_key == KC.TRNS: if not self._state_layer_key or self._state_layer_key == KC.TRNS:
continue continue
if self.debug_enabled: if self.debug_enabled:
print('KeyResolution(key={})'.format(layer_key)) print('KeyResolution(key={})'.format(self._state_layer_key))
return layer_key return self._state_layer_key
def _on_matrix_changed(self, row, col, is_pressed): def _on_matrix_changed(self, row, col, is_pressed):
if self.debug_enabled: if self.debug_enabled:
print('MatrixChange(col={} row={} pressed={})'.format(col, row, is_pressed)) print('MatrixChange(col={} row={} pressed={})'.format(col, row, is_pressed))
int_coord = intify_coordinate(row, col) int_coord = intify_coordinate(row, col)
kc_changed = self._find_key_in_map(row, col) kc_changed = self._find_key_in_map(int_coord, row, col)
if kc_changed is None: if kc_changed is None:
print('MatrixUndefinedCoordinate(col={} row={})'.format(col, row)) print('MatrixUndefinedCoordinate(col={} row={})'.format(col, row))
@@ -367,13 +369,13 @@ class KMKKeyboard:
self._print_debug_cycle(init=True) self._print_debug_cycle(init=True)
while True: while True:
self.state_changed = False self._state_changed = False
for ext in self.extensions: for ext in self.extensions:
try: try:
self._handle_matrix_report(ext.before_matrix_scan(self)) self._handle_matrix_report(ext.before_matrix_scan(self))
except Exception as e: except Exception as err:
print('Failed to run pre matrix function: ', e) print('Failed to run pre matrix function: ', err)
self._matrix_update = self.matrix.scan_for_changes() self._matrix_update = self.matrix.scan_for_changes()
@@ -384,8 +386,8 @@ class KMKKeyboard:
) )
if self._matrix_modify is not None: if self._matrix_modify is not None:
self._matrix_update = self._matrix_modify self._matrix_update = self._matrix_modify
except Exception as e: except Exception as err:
print('Failed to run post matrix function: ', e) print('Failed to run post matrix function: ', err)
self._handle_matrix_report(self._matrix_update) self._handle_matrix_report(self._matrix_update)
self._matrix_update = None self._matrix_update = None
@@ -393,18 +395,18 @@ class KMKKeyboard:
for ext in self.extensions: for ext in self.extensions:
try: try:
ext.before_hid_send(self) ext.before_hid_send(self)
except Exception as e: except Exception as err:
print('Failed to run pre hid function: ', e) print('Failed to run pre hid function: ', err)
if self._hid_pending: if self._hid_pending:
self._send_hid() self._send_hid()
old_timeouts_len = len(self._timeouts) self._old_timeouts_len = len(self._timeouts)
self._process_timeouts() self._process_timeouts()
new_timeouts_len = len(self._timeouts) self._new_timeouts_len = len(self._timeouts)
if old_timeouts_len != new_timeouts_len: if self._old_timeouts_len != self._new_timeouts_len:
self.state_changed = True self._state_changed = True
if self._hid_pending: if self._hid_pending:
self._send_hid() self._send_hid()
@@ -412,8 +414,8 @@ class KMKKeyboard:
for ext in self.extensions: for ext in self.extensions:
try: try:
ext.after_hid_send(self) ext.after_hid_send(self)
except Exception as e: except Exception as err:
print('Failed to run post hid function: ', e) print('Failed to run post hid function: ', err)
if self.state_changed: if self._state_changed:
self._print_debug_cycle() self._print_debug_cycle()

View File

@@ -1,35 +0,0 @@
# Welcome to RAM and stack size hacks central, I'm your host, klardotsh!
# Our import structure is deeply nested enough that stuff
# breaks in some truly bizarre ways, including:
# - explicit RuntimeError exceptions, complaining that our
# stack depth is too deep
#
# - silent hard locks of the device (basically unrecoverable without
# UF2 flash if done in main.py, fixable with a reboot if done
# in REPL)
#
# However, there's a hackaround that works for us! Because sys.modules
# caches everything it sees (and future imports will use that cached
# copy of the module), let's take this opportunity _way_ up the import
# chain to import _every single thing_ KMK eventually uses in a normal
# workflow, in nesting order
#
# GC runs automatically after CircuitPython imports.
# First, system-provided deps
import busio
import collections
import supervisor
# Now "light" KMK stuff with few/no external deps
import kmk.consts # isort:skip
import kmk.kmktime # isort:skip
import kmk.types # isort:skip
# Now handlers that will be used in keys later
import kmk.handlers.stock # isort:skip
# Now stuff that depends on the above (and so on)
import kmk.hid # isort:skip
import kmk.keys # isort:skip
import kmk.matrix # isort:skip

View File

@@ -22,8 +22,6 @@ per-file-ignores =
# multiple spaces after commas in lists (for grid alignment) # multiple spaces after commas in lists (for grid alignment)
user_keymaps/**/*.py: E131,F401,E501,E241,E131,BLK100 user_keymaps/**/*.py: E131,F401,E501,E241,E131,BLK100
tests/test_data/keymaps/**/*.py: F401,E501 tests/test_data/keymaps/**/*.py: F401,E501
# Forgive me for my RAM hack sins
kmk/preload_imports.py: I001,I003,I004,F401
[isort] [isort]
known_standard_library = known_standard_library =

View File

@@ -1,6 +1,3 @@
# OLED
import board
import adafruit_displayio_ssd1306 import adafruit_displayio_ssd1306
import displayio import displayio
import terminalio import terminalio
@@ -26,16 +23,20 @@ SUPER_L = KC.LM(4, KC.LGUI)
keyboard.tap_time = 300 keyboard.tap_time = 300
keyboard.debug_enabled = False keyboard.debug_enabled = False
# TODO Get this out of here rgb_ext = RGB(
rgb_pixel_pin = board.P0_06 pixel_pin=keyboard.rgb_pixel_pin,
rgb_ext = RGB(pixel_pin=rgb_pixel_pin, num_pixels=27, val_limit=100, hue_default=190, sat_default=100, val_default=5) num_pixels=27,
val_limit=100,
hue_default=190,
sat_default=100,
val_default=5
)
split = BLE_Split(split_side=split_side) ble_split = BLE_Split(split_side=split_side)
keyboard.extensions = [split, rgb_ext] keyboard.extensions = [ble_split, rgb_ext]
displayio.release_displays() displayio.release_displays()
i2c = board.I2C() display_bus = displayio.I2CDisplay(keyboard.i2c, device_address=0x3c)
display_bus = displayio.I2CDisplay(i2c, device_address=0x3c)
display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=32) display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=32)
splash = displayio.Group(max_size=10) splash = displayio.Group(max_size=10)
display.show(splash) display.show(splash)