diff --git a/kmk/boards/converter/keebio/nyquist_r2.py b/kmk/boards/converter/keebio/nyquist_r2.py index 3100ee1..63eed3a 100644 --- a/kmk/boards/converter/keebio/nyquist_r2.py +++ b/kmk/boards/converter/keebio/nyquist_r2.py @@ -14,12 +14,11 @@ class KMKKeyboard(_KMKKeyboard): split_type = 'UART' split_flip = True - split_offsets = [6, 6, 6, 6, 6] uart_pin = board.SCL rgb_pixel_pin = board.TX extra_data_pin = board.SDA rgb_ext = RGB(pixel_pin=rgb_pixel_pin, num_pixels=12) 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] diff --git a/kmk/boards/nice_nano/crkbd.py b/kmk/boards/nice_nano/crkbd.py index 2169f09..0d6a667 100644 --- a/kmk/boards/nice_nano/crkbd.py +++ b/kmk/boards/nice_nano/crkbd.py @@ -1,7 +1,6 @@ import board from kmk.extensions.layers import Layers -from kmk.extensions.split import Split from kmk.kmk_keyboard import KMKKeyboard as _KMKKeyboard from kmk.matrix import DiodeOrientation from kmk.matrix import intify_coordinate as ic @@ -24,6 +23,7 @@ class KMKKeyboard(_KMKKeyboard): uart_pin = board.P0_08 rgb_pixel_pin = board.P0_06 extra_data_pin = board.SDA # TODO This is incorrect. Find better solution + i2c = board.I2C coord_mapping = [] 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)) layers_ext = Layers() - split = Split(uart_pin=uart_pin) extensions = [layers_ext] diff --git a/kmk/kmk_keyboard.py b/kmk/kmk_keyboard.py index 7c05bd7..65e5751 100644 --- a/kmk/kmk_keyboard.py +++ b/kmk/kmk_keyboard.py @@ -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.hid import BLEHID, USBHID, AbstractHID, HIDModes from kmk.keys import KC @@ -16,12 +11,13 @@ class KMKKeyboard: # User-configurable debug_enabled = False - keymap = None + keymap = [] coord_mapping = None row_pins = None col_pins = None diode_orientation = None + matrix = None matrix_scanner = MatrixScanner uart_buffer = [] @@ -34,10 +30,15 @@ class KMKKeyboard: # Internal State _keys_pressed = set() _coord_keys_pressed = {} + hid_type = HIDModes.USB _hid_helper = None _hid_pending = False + _state_layer_key = None _matrix_update = None _matrix_modify = None + _state_changed = False + _old_timeouts_len = None + _new_timeouts_len = None # this should almost always be PREpended to, replaces # former use of reversed_active_layers which had pointless @@ -110,43 +111,44 @@ class KMKKeyboard: def _handle_matrix_report(self, update=None): if update is not None: self._on_matrix_changed(update[0], update[1], update[2]) - self.state_changed = True + self._state_changed = True ##### # SPLICE: INTERNAL STATE # TODO FIXME CLEAN THIS ##### - def _find_key_in_map(self, row, col): - ic = intify_coordinate(row, col) - + def _find_key_in_map(self, int_coord, row, col): + self._state_layer_key = None try: - idx = self.coord_mapping.index(ic) + idx = self.coord_mapping.index(int_coord) except ValueError: if self.debug_enabled: print( - 'CoordMappingNotFound(ic={}, row={}, col={})'.format(ic, row, col) + 'CoordMappingNotFound(ic={}, row={}, col={})'.format( + int_coord, row, col + ) ) return None 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 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): if self.debug_enabled: print('MatrixChange(col={} row={} pressed={})'.format(col, row, is_pressed)) 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: print('MatrixUndefinedCoordinate(col={} row={})'.format(col, row)) @@ -367,13 +369,13 @@ class KMKKeyboard: self._print_debug_cycle(init=True) while True: - self.state_changed = False + self._state_changed = False for ext in self.extensions: try: self._handle_matrix_report(ext.before_matrix_scan(self)) - except Exception as e: - print('Failed to run pre matrix function: ', e) + except Exception as err: + print('Failed to run pre matrix function: ', err) self._matrix_update = self.matrix.scan_for_changes() @@ -384,8 +386,8 @@ class KMKKeyboard: ) if self._matrix_modify is not None: self._matrix_update = self._matrix_modify - except Exception as e: - print('Failed to run post matrix function: ', e) + except Exception as err: + print('Failed to run post matrix function: ', err) self._handle_matrix_report(self._matrix_update) self._matrix_update = None @@ -393,18 +395,18 @@ class KMKKeyboard: for ext in self.extensions: try: ext.before_hid_send(self) - except Exception as e: - print('Failed to run pre hid function: ', e) + except Exception as err: + print('Failed to run pre hid function: ', err) if self._hid_pending: self._send_hid() - old_timeouts_len = len(self._timeouts) + self._old_timeouts_len = len(self._timeouts) self._process_timeouts() - new_timeouts_len = len(self._timeouts) + self._new_timeouts_len = len(self._timeouts) - if old_timeouts_len != new_timeouts_len: - self.state_changed = True + if self._old_timeouts_len != self._new_timeouts_len: + self._state_changed = True if self._hid_pending: self._send_hid() @@ -412,8 +414,8 @@ class KMKKeyboard: for ext in self.extensions: try: ext.after_hid_send(self) - except Exception as e: - print('Failed to run post hid function: ', e) + except Exception as err: + print('Failed to run post hid function: ', err) - if self.state_changed: + if self._state_changed: self._print_debug_cycle() diff --git a/kmk/preload_imports.py b/kmk/preload_imports.py deleted file mode 100644 index 1b7cdf6..0000000 --- a/kmk/preload_imports.py +++ /dev/null @@ -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 diff --git a/setup.cfg b/setup.cfg index c2c1f10..3c3a984 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,8 +22,6 @@ per-file-ignores = # multiple spaces after commas in lists (for grid alignment) user_keymaps/**/*.py: E131,F401,E501,E241,E131,BLK100 tests/test_data/keymaps/**/*.py: F401,E501 -# Forgive me for my RAM hack sins - kmk/preload_imports.py: I001,I003,I004,F401 [isort] known_standard_library = diff --git a/user_keymaps/kdb424/corne.py b/user_keymaps/kdb424/corne.py index 501dabc..8d46577 100644 --- a/user_keymaps/kdb424/corne.py +++ b/user_keymaps/kdb424/corne.py @@ -1,6 +1,3 @@ -# OLED -import board - import adafruit_displayio_ssd1306 import displayio import terminalio @@ -26,16 +23,20 @@ SUPER_L = KC.LM(4, KC.LGUI) keyboard.tap_time = 300 keyboard.debug_enabled = False -# TODO Get this out of here -rgb_pixel_pin = board.P0_06 -rgb_ext = RGB(pixel_pin=rgb_pixel_pin, num_pixels=27, val_limit=100, hue_default=190, sat_default=100, val_default=5) +rgb_ext = RGB( + pixel_pin=keyboard.rgb_pixel_pin, + num_pixels=27, + val_limit=100, + hue_default=190, + sat_default=100, + val_default=5 +) -split = BLE_Split(split_side=split_side) -keyboard.extensions = [split, rgb_ext] +ble_split = BLE_Split(split_side=split_side) +keyboard.extensions = [ble_split, rgb_ext] displayio.release_displays() -i2c = board.I2C() -display_bus = displayio.I2CDisplay(i2c, device_address=0x3c) +display_bus = displayio.I2CDisplay(keyboard.i2c, device_address=0x3c) display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=128, height=32) splash = displayio.Group(max_size=10) display.show(splash)