diff --git a/boards/atreus62/README.md b/boards/atreus62/README.md index 0e8c971..7766a50 100644 --- a/boards/atreus62/README.md +++ b/boards/atreus62/README.md @@ -9,7 +9,7 @@ kb.py is designed to work with the Teensy 4.1 Retailers (USA) [Atreus62](https://shop.profetkeyboards.com/product/atreus62-keyboard) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [RGB](https://github.com/KMKfw/kmk_firmware/tree/master/docs/rgb.md) Light it up - [Encoder](https://github.com/KMKfw/kmk_firmware/tree/master/docs/encoder.md) Twist control for all the things diff --git a/boards/boardsource/3x4/README.md b/boards/boardsource/3x4/README.md index f7ff40c..28afe21 100644 --- a/boards/boardsource/3x4/README.md +++ b/boards/boardsource/3x4/README.md @@ -9,5 +9,5 @@ kb.py is designed to work with the nice!nano Retailers (USA) [Boardsource](https://boardsource.xyz/store/5ecc2008eee64242946c98c1) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. diff --git a/boards/boardsource/4x12/README.md b/boards/boardsource/4x12/README.md index 35cef12..ed670c2 100644 --- a/boards/boardsource/4x12/README.md +++ b/boards/boardsource/4x12/README.md @@ -13,9 +13,9 @@ Retailers (USA) Low profile 4x12 [Boardsource](https://boardsource.xyz/store/5ecb7dad86879c9a0c22db32) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [MediaKeys](https://github.com/KMKfw/kmk_firmware/tree/master/docs/media_keys.md) Control volume and other media functions -Common Extentions +Common Extensions - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/boardsource/5x12/README.md b/boards/boardsource/5x12/README.md index 7195bcc..47de424 100644 --- a/boards/boardsource/5x12/README.md +++ b/boards/boardsource/5x12/README.md @@ -13,9 +13,9 @@ Retailers (USA) Low Profile 5x12 [Boardsource](https://boardsource.xyz/store/5ecb822386879c9a0c22db84) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [MediaKeys](https://github.com/KMKfw/kmk_firmware/tree/master/docs/media_keys.md) Control volume and other media functions -Common Extentions +Common Extensions - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/boardsource/microdox/README.md b/boards/boardsource/microdox/README.md index cec0c53..21b088e 100644 --- a/boards/boardsource/microdox/README.md +++ b/boards/boardsource/microdox/README.md @@ -9,11 +9,11 @@ kb.py is designed to work with the nice!nano Retailers (USA) [Boardsource](https://boardsource.xyz/store/5f2e7e4a2902de7151494f92) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [BLE_Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves without wires - [ModTap](https://github.com/KMKfw/kmk_firmware/tree/master/docs/modtap.md) Allows mod keys to act as different keys when tapped. -Common Extentions +Common Extensions - [Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves using a wire - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/crkbd/README.md b/boards/crkbd/README.md index 2e5de1b..623dfdc 100644 --- a/boards/crkbd/README.md +++ b/boards/crkbd/README.md @@ -17,11 +17,11 @@ Corne Corne LP [Boardsource](https://boardsource.xyz/store/5f2efc462902de7151495057) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [RGB](https://github.com/KMKfw/kmk_firmware/tree/master/docs/rgb.md) Light it up - [BLE_Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves without wires -Common Extentions +Common Extensions - [Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves using a wire - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/ergo_travel/README.md b/boards/ergo_travel/README.md index 226d902..50dd813 100644 --- a/boards/ergo_travel/README.md +++ b/boards/ergo_travel/README.md @@ -11,11 +11,11 @@ Hardware Availability: [PCB & Case Source](https://github.com/jpconstantineau/Er Retailers (USA) [Boardsource](https://boardsource.xyz/store/5eed23430883e03ef9a69d6a) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [BLE_Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves without wires - [MediaKeys](https://github.com/KMKfw/kmk_firmware/tree/master/docs/media_keys.md) Control volume and other media functions -Common Extentions +Common Extensions - [Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves using a wire - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/fourtypercentclub/gherkin/README.md b/boards/fourtypercentclub/gherkin/README.md index 30641a5..d351cae 100644 --- a/boards/fourtypercentclub/gherkin/README.md +++ b/boards/fourtypercentclub/gherkin/README.md @@ -8,10 +8,10 @@ kb.py is designed to work with the nice!nano Hardware Availability: [Gherkin project on 40% Keyboards](http://www.40percent.club/2016/11/gherkin.html) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [ModTap](https://github.com/KMKfw/kmk_firmware/tree/master/docs/modtap.md) Allows mod keys to act as different keys when tapped. - [LED](https://github.com/KMKfw/kmk_firmware/tree/master/docs/led.md) Light your keys up -Common Extentions +Common Extensions - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/fourtypercentclub/luddite/README.md b/boards/fourtypercentclub/luddite/README.md index d4f16cd..daa1a94 100644 --- a/boards/fourtypercentclub/luddite/README.md +++ b/boards/fourtypercentclub/luddite/README.md @@ -9,10 +9,10 @@ kb_converter.py is designed to work with an itsybitsy with converter board found Hardware Availability: [Luddite project on 40% Keyboards](http://www.40percent.club/search/label/luddite) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [RGB](https://github.com/KMKfw/kmk_firmware/tree/master/docs/rgb.md) RGB underglow - [LED](https://github.com/KMKfw/kmk_firmware/tree/master/docs/led.md) Light your keys up -Common Extentions +Common Extensions - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/keebio/iris/README.md b/boards/keebio/iris/README.md index ae2a07c..7e06dca 100644 --- a/boards/keebio/iris/README.md +++ b/boards/keebio/iris/README.md @@ -5,11 +5,11 @@ A split keyboard with a 4x6 layout with additional 4 thumb buttons kb.py is designed to work with the nice!nano kb_converter.py is designed to work with an itsybitsy with converter board found [here](https://github.com/KMKfw/kmk_firmware/tree/master/hardware) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [RGB](https://github.com/KMKfw/kmk_firmware/tree/master/docs/rgb.md) Light it up - [BLE_Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves without wires -Common Extentions +Common Extensions - [Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves using a wire - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/keebio/levinson/README.md b/boards/keebio/levinson/README.md index af15c9b..8b30f89 100644 --- a/boards/keebio/levinson/README.md +++ b/boards/keebio/levinson/README.md @@ -5,11 +5,11 @@ A split keyboard with a 4x6 layout kb.py is designed to work with the nice!nano kb_converter.py is designed to work with an itsybitsy with converter board found [here](https://github.com/KMKfw/kmk_firmware/tree/master/hardware) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [RGB](https://github.com/KMKfw/kmk_firmware/tree/master/docs/rgb.md) Light it up - [BLE_Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves without wires -Common Extentions +Common Extensions - [Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves using a wire - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/keebio/nyquist/README.md b/boards/keebio/nyquist/README.md index b567df0..511fe7f 100644 --- a/boards/keebio/nyquist/README.md +++ b/boards/keebio/nyquist/README.md @@ -6,11 +6,11 @@ kb.py is designed to work with the nice!nano kb_converter.py is designed to work with an itsybitsy with converter board found [here](https://github.com/KMKfw/kmk_firmware/tree/master/hardware) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [RGB](https://github.com/KMKfw/kmk_firmware/tree/master/docs/rgb.md) Light it up - [BLE_Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves without wires -Common Extentions +Common Extensions - [Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves using a wire - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/lily58/README.md b/boards/lily58/README.md index d820bd3..174d909 100644 --- a/boards/lily58/README.md +++ b/boards/lily58/README.md @@ -10,11 +10,11 @@ kb.py is designed to work with the nice!nano Retailers (USA) [Boardsource](https://boardsource.xyz/store/5ec9df84c6b834480de6c3d0) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [RGB](https://github.com/KMKfw/kmk_firmware/tree/master/docs/rgb.md) Light it up - [BLE_Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves without wires -Common Extentions +Common Extensions - [Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves using a wire - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/reviung39/README.md b/boards/reviung39/README.md index 65bcf0f..69b1e15 100644 --- a/boards/reviung39/README.md +++ b/boards/reviung39/README.md @@ -11,10 +11,10 @@ Hardware Availability: [PCB & Case Data](https://github.com/gtips/reviung) Retailers (USA) [Boardsource](https://boardsource.xyz/store/5ecb734486879c9a0c22dab3) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [RGB](https://github.com/KMKfw/kmk_firmware/tree/master/docs/rgb.md) Light it up - [ModTap](https://github.com/KMKfw/kmk_firmware/tree/master/docs/modtap.md) Allows mod keys to act as different keys when tapped. -Common Extentions +Common Extensions - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/reviung41/README.md b/boards/reviung41/README.md index 2b415a7..abb7d42 100644 --- a/boards/reviung41/README.md +++ b/boards/reviung41/README.md @@ -11,10 +11,10 @@ Hardware Availability: [PCB & Case Data](https://github.com/gtips/reviung/tree/m Retailers (USA) [Boardsource](https://boardsource.xyz/store/5f2ef1b52bf5e8714a60f613) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [RGB](https://github.com/KMKfw/kmk_firmware/tree/master/docs/rgb.md) Light it up - [ModTap](https://github.com/KMKfw/kmk_firmware/tree/master/docs/modtap.md) Allows mod keys to act as different keys when tapped. -Common Extentions +Common Extensions - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/rhymestone/README.md b/boards/rhymestone/README.md index 62b8059..10eff45 100644 --- a/boards/rhymestone/README.md +++ b/boards/rhymestone/README.md @@ -9,11 +9,11 @@ kb.py is designed to work with the nice!nano Retailers (USA) [Boardsource](https://boardsource.xyz/store/5ecb6aee86879c9a0c22da89) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [BLE_Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves without wires - [ModTap](https://github.com/KMKfw/kmk_firmware/tree/master/docs/modtap.md) Allows mod keys to act as different keys when tapped. -Common Extentions +Common Extensions - [Split](https://github.com/KMKfw/kmk_firmware/tree/master/docs/split.md) Connects halves using a wire - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/boards/tg4x/README.md b/boards/tg4x/README.md index aef7d06..eb9170c 100644 --- a/boards/tg4x/README.md +++ b/boards/tg4x/README.md @@ -9,9 +9,9 @@ kb.py is designed to work with the nice!nano Retailers (USA) [Boardsource](https://boardsource.xyz/store/5eff7ead037395179221b90c) -Extentions enabled by default +Extensions enabled by default - [Layers](https://github.com/KMKfw/kmk_firmware/tree/master/docs/layers.md) Need more keys than switches? Use layers. - [RGB](https://github.com/KMKfw/kmk_firmware/tree/master/docs/rgb.md) Light it up -Common Extentions +Common Extensions - [Power](https://github.com/KMKfw/kmk_firmware/tree/master/docs/power.md) Powersaving features for battery life diff --git a/docs/ble_hid.md b/docs/ble_hid.md index 368e07b..380108b 100644 --- a/docs/ble_hid.md +++ b/docs/ble_hid.md @@ -1,9 +1,9 @@ # BLE HID -Bluetooth connections help clean up the wire mess! +Bluetooth connections help clean up the wire mess! ## Circuitpython -If not running KMKpython, this does require the adafruit_ble library from Adafruit. -This can be downloaded +If not running KMKpython, this does require the adafruit_ble library from Adafruit. +This can be downloaded [here](https://github.com/adafruit/Adafruit_CircuitPython_BLE/tree/master/adafruit_ble). It is part of the [Adafruit CircuitPython Bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle). Simply put this in the "root" of your circuitpython device. If unsure, it's the folder with main.py in it, and should be the first folder you see when you open the device. @@ -14,12 +14,14 @@ To enable BLE hid, change the keyboard.go(). By default, the advertised name will be the name of the "flash drive". By default this is CIRCUITPY ```python +from kmk.hid import HIDModes + if __name__ == '__main__': keyboard.go(hid_type=HIDModes.BLE) ``` ## Changing the advertise name -There are two ways to change the advertising name. The first would be to +There are two ways to change the advertising name. The first would be to [change the name of the drive](https://learn.adafruit.com/welcome-to-circuitpython/the-circuitpy-drive). The second would be to change the keyboard.go() like this. diff --git a/docs/keycodes.md b/docs/keycodes.md index dc84b98..eff5121 100644 --- a/docs/keycodes.md +++ b/docs/keycodes.md @@ -169,8 +169,8 @@ |Key |Aliases |Description | |------------------------|------------------------------|-----------------------| -|`KC.NONUS_HASH` |`KC.NUHS` |ISO Right of LSHIFT | -|`KC.NONUS_BSLASH` |`KC.NUBS` |ISO Left of Return | +|`KC.NONUS_HASH` |`KC.NUHS` |ISO Left of Return | +|`KC.NONUS_BSLASH` |`KC.NUBS` |ISO Right of LSHIFT | |`KC.APPLICATION` |`KC.APP`,`KC.SEL`,`KC.WINMENU`|Menu Key (Near RCTRL) | |`KC.INT1` |`KC.RO` | | |`KC.INT2` |`KC.KANA` | | diff --git a/docs/rgb.md b/docs/rgb.md index 458705d..da4f93d 100644 --- a/docs/rgb.md +++ b/docs/rgb.md @@ -21,8 +21,8 @@ Changing the **Hue** cycles around the circle. Changing the **Saturation** moves between the inner and outer sections of the wheel, affecting the intensity of the color. Changing the **Value** sets the overall brightness. -## Enabling the extention -The only required values that you need to give the RGB extention would be the pixel pin, and the number of pixels/LED's. If using a split keyboard, this number is per side, and not the total of both sides. +## Enabling the extension +The only required values that you need to give the RGB extension would be the pixel pin, and the number of pixels/LED's. If using a split keyboard, this number is per side, and not the total of both sides. ```python from kmk.extensions.RGB import RGB from kb import rgb_pixel_pin # This can be imported or defined manually diff --git a/kmk/handlers/stock.py b/kmk/handlers/stock.py index 73069b8..ba301eb 100644 --- a/kmk/handlers/stock.py +++ b/kmk/handlers/stock.py @@ -1,4 +1,4 @@ -from kmk.kmktime import sleep_ms +from time import sleep def passthrough(key, keyboard, *args, **kwargs): @@ -8,9 +8,6 @@ def passthrough(key, keyboard, *args, **kwargs): def default_pressed(key, keyboard, KC, coord_int=None, coord_raw=None, *args, **kwargs): keyboard.hid_pending = True - if coord_int is not None: - keyboard._coordkeys_pressed[coord_int] = key - keyboard.keys_pressed.add(key) return keyboard @@ -22,10 +19,6 @@ def default_released( keyboard.hid_pending = True keyboard.keys_pressed.discard(key) - if coord_int is not None: - keyboard.keys_pressed.discard(keyboard._coordkeys_pressed.get(coord_int, None)) - keyboard._coordkeys_pressed[coord_int] = None - return keyboard @@ -102,7 +95,7 @@ def bkdl_released(key, keyboard, KC, *args, **kwargs): def sleep_pressed(key, keyboard, KC, *args, **kwargs): - sleep_ms(key.meta.ms) + sleep(key.meta.ms / 1000) return keyboard diff --git a/kmk/hid.py b/kmk/hid.py index 26f7739..391363b 100644 --- a/kmk/hid.py +++ b/kmk/hid.py @@ -52,7 +52,7 @@ HID_REPORT_SIZES = { class AbstractHID: REPORT_BYTES = 8 - def __init__(self): + def __init__(self, **kwargs): self._evt = bytearray(self.REPORT_BYTES) self.report_device = memoryview(self._evt)[0:1] self.report_device[0] = HIDReportTypes.KEYBOARD @@ -239,11 +239,15 @@ class BLEHID(AbstractHID): # Hardcoded in CPy MAX_CONNECTIONS = const(2) - def post_init(self, ble_name=str(getmount('/').label), **kwargs): + def __init__(self, ble_name=str(getmount('/').label), **kwargs): + self.ble_name = ble_name + super().__init__() + + def post_init(self): self.conn_id = -1 self.ble = BLERadio() - self.ble.name = ble_name + self.ble.name = self.ble_name self.hid = HIDService() self.hid.protocol_mode = 0 # Boot protocol diff --git a/kmk/keys.py b/kmk/keys.py index 4ebc5cf..2ca9978 100644 --- a/kmk/keys.py +++ b/kmk/keys.py @@ -401,10 +401,6 @@ class Key: self.no_press = bool(no_press) self.no_release = bool(no_press) - self._pre_press_handlers = [] - self._post_press_handlers = [] - self._pre_release_handlers = [] - self._post_release_handlers = [] self._handle_press = on_press self._handle_release = on_release self.meta = meta @@ -424,26 +420,30 @@ class Key: return 'Key(code={}, has_modifiers={})'.format(self.code, self.has_modifiers) def on_press(self, state, coord_int, coord_raw): - for fn in self._pre_press_handlers: - if not fn(self, state, KC, coord_int, coord_raw): - return None + if hasattr(self, '_pre_press_handlers'): + for fn in self._pre_press_handlers: + if not fn(self, state, KC, coord_int, coord_raw): + return None ret = self._handle_press(self, state, KC, coord_int, coord_raw) - for fn in self._post_press_handlers: - fn(self, state, KC, coord_int, coord_raw) + if hasattr(self, '_post_press_handlers'): + for fn in self._post_press_handlers: + fn(self, state, KC, coord_int, coord_raw) return ret def on_release(self, state, coord_int, coord_raw): - for fn in self._pre_release_handlers: - if not fn(self, state, KC, coord_int, coord_raw): - return None + if hasattr(self, '_pre_release_handlers'): + for fn in self._pre_release_handlers: + if not fn(self, state, KC, coord_int, coord_raw): + return None ret = self._handle_release(self, state, KC, coord_int, coord_raw) - for fn in self._post_release_handlers: - fn(self, state, KC, coord_int, coord_raw) + if hasattr(self, '_post_release_handlers'): + for fn in self._post_release_handlers: + fn(self, state, KC, coord_int, coord_raw) return ret @@ -485,6 +485,8 @@ class Key: calls of this method will be executed before those provided by later calls. ''' + if not hasattr(self, '_pre_press_handlers'): + self._pre_press_handlers = [] self._pre_press_handlers.append(fn) return self @@ -508,6 +510,8 @@ class Key: calls of this method will be executed before those provided by later calls. ''' + if not hasattr(self, '_post_press_handlers'): + self._post_press_handlers = [] self._post_press_handlers.append(fn) return self @@ -532,6 +536,8 @@ class Key: calls of this method will be executed before those provided by later calls. ''' + if not hasattr(self, '_pre_release_handlers'): + self._pre_release_handlers = [] self._pre_release_handlers.append(fn) return self @@ -555,6 +561,8 @@ class Key: calls of this method will be executed before those provided by later calls. ''' + if not hasattr(self, '_post_release_handlers'): + self._post_release_handlers = [] self._post_release_handlers.append(fn) return self diff --git a/kmk/kmk_keyboard.py b/kmk/kmk_keyboard.py index b292d0c..e223d0e 100644 --- a/kmk/kmk_keyboard.py +++ b/kmk/kmk_keyboard.py @@ -1,7 +1,8 @@ +from supervisor import ticks_ms + from kmk.consts import KMK_RELEASE, UnicodeMode from kmk.hid import BLEHID, USBHID, AbstractHID, HIDModes from kmk.keys import KC -from kmk.kmktime import ticks_ms from kmk.matrix import MatrixScanner, intify_coordinate from kmk.types import TapDanceKeyMeta @@ -42,7 +43,7 @@ class KMKKeyboard: secondary_hid_type = None _hid_helper = None hid_pending = False - state_layer_key = None + current_key = None matrix_update = None secondary_matrix_update = None _matrix_modify = None @@ -52,6 +53,7 @@ class KMKKeyboard: _trigger_powersave_enable = False _trigger_powersave_disable = False i2c_deinit_count = 0 + _go_args = None # this should almost always be PREpended to, replaces # former use of reversed_active_layers which had pointless @@ -119,7 +121,6 @@ class KMKKeyboard: self.state_changed = True def _find_key_in_map(self, int_coord, row, col): - self.state_layer_key = None try: idx = self.coord_mapping.index(int_coord) except ValueError: @@ -133,28 +134,39 @@ class KMKKeyboard: return None for layer in self.active_layers: - self.state_layer_key = self.keymap[layer][idx] + layer_key = self.keymap[layer][idx] - if not self.state_layer_key or self.state_layer_key == KC.TRNS: + if not layer_key or layer_key == KC.TRNS: continue if self.debug_enabled: - print('KeyResolution(key={})'.format(self.state_layer_key)) + print('KeyResolution(key={})'.format(layer_key)) - return self.state_layer_key + return 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(int_coord, row, col) + if not is_pressed: + self.current_key = self._coordkeys_pressed[int_coord] + if self.debug_enabled: + print('PressedKeyResolution(key={})'.format(self.current_key)) - if kc_changed is None: + if self.current_key is None: + self.current_key = self._find_key_in_map(int_coord, row, col) + + if is_pressed: + self._coordkeys_pressed[int_coord] = self.current_key + else: + self._coordkeys_pressed[int_coord] = None + + if self.current_key is None: print('MatrixUndefinedCoordinate(col={} row={})'.format(col, row)) return self - return self.process_key(kc_changed, is_pressed, int_coord, (row, col)) + return self.process_key(self.current_key, is_pressed, int_coord, (row, col)) def process_key(self, key, is_pressed, coord_int=None, coord_raw=None): if self._tapping and not isinstance(key.meta, TapDanceKeyMeta): @@ -324,7 +336,7 @@ class KMKKeyboard: self._hid_helper = BLEHID else: self._hid_helper = AbstractHID - self._hid_helper = self._hid_helper() + self._hid_helper = self._hid_helper(**self._go_args) def _init_matrix(self): self.matrix = MatrixScanner( @@ -426,6 +438,7 @@ class KMKKeyboard: print('Failed to run post hid function in extension: ', err, ext) def go(self, hid_type=HIDModes.USB, secondary_hid_type=None, **kwargs): + self._go_args = kwargs self.hid_type = hid_type self.secondary_hid_type = secondary_hid_type @@ -444,13 +457,14 @@ class KMKKeyboard: ext.during_bootup(self) except Exception: if self.debug_enabled: - print('Failed to load extention', ext) + print('Failed to load extension', ext) self._init_matrix() self._print_debug_cycle(init=True) while True: + self.current_key = None self.state_changed = False self.sandbox.active_layers = self.active_layers.copy() diff --git a/kmk/kmktime.py b/kmk/kmktime.py index 18b62a8..82ed7cd 100644 --- a/kmk/kmktime.py +++ b/kmk/kmktime.py @@ -1,23 +1,15 @@ -import time +from micropython import const + +_TICKS_PERIOD = const(1 << 29) +_TICKS_MAX = const(_TICKS_PERIOD - 1) +_TICKS_HALFPERIOD = const(_TICKS_PERIOD // 2) -def sleep_ms(ms): - return time.sleep(ms / 1000) +def ticks_diff(new, start): + diff = (new - start) & _TICKS_MAX + diff = ((diff + _TICKS_HALFPERIOD) & _TICKS_MAX) - _TICKS_HALFPERIOD + return diff -def ticks_ms(): - '''Has .25s granularity, but is cheap''' - return time.monotonic() * 1000 - - -def ticks_diff(new, old): - return new - old - - -def accurate_ticks(): - '''Is more expensive, but good for time critical things''' - return time.monotonic_ns() - - -def accurate_ticks_diff(new, old, ms): - return bool(new - old < ms * 1000000) +def check_deadline(new, start, ms): + return ticks_diff(new, start) < ms diff --git a/kmk/modules/encoder.py b/kmk/modules/encoder.py index 86c841f..a7aca5c 100644 --- a/kmk/modules/encoder.py +++ b/kmk/modules/encoder.py @@ -1,6 +1,6 @@ import digitalio +from supervisor import ticks_ms -from kmk.kmktime import ticks_ms from kmk.modules import Module diff --git a/kmk/modules/layers.py b/kmk/modules/layers.py index c369476..b67e8d3 100644 --- a/kmk/modules/layers.py +++ b/kmk/modules/layers.py @@ -1,5 +1,6 @@ '''One layer isn't enough. Adds keys to get to more of them''' from micropython import const +from supervisor import ticks_ms from kmk.key_validators import layer_key_validator from kmk.keys import make_argumented_key diff --git a/kmk/modules/modtap.py b/kmk/modules/modtap.py index 6b31a73..43de0fe 100644 --- a/kmk/modules/modtap.py +++ b/kmk/modules/modtap.py @@ -1,3 +1,5 @@ +from supervisor import ticks_ms + from kmk.key_validators import mod_tap_validator from kmk.keys import make_argumented_key from kmk.modules.holdtap import HoldTap diff --git a/kmk/modules/power.py b/kmk/modules/power.py index b0a09d6..aa72eb1 100644 --- a/kmk/modules/power.py +++ b/kmk/modules/power.py @@ -1,9 +1,12 @@ import board import digitalio +from supervisor import ticks_ms + +from time import sleep from kmk.handlers.stock import passthrough as handler_passthrough from kmk.keys import make_key -from kmk.kmktime import sleep_ms, ticks_diff, ticks_ms +from kmk.kmktime import check_deadline from kmk.modules import Module @@ -100,10 +103,10 @@ class Power(Module): ''' Sleeps longer and longer to save power the more time in between updates. ''' - if ticks_diff(ticks_ms(), self._powersave_start) <= 60000: - sleep_ms(8) - elif ticks_diff(ticks_ms(), self._powersave_start) >= 240000: - sleep_ms(180) + if check_deadline(ticks_ms(), self._powersave_start) <= 60000: + sleep(8 / 1000) + elif check_deadline(ticks_ms(), self._powersave_start) >= 240000: + sleep(180 / 1000) return def psave_time_reset(self): @@ -120,7 +123,7 @@ class Power(Module): return def usb_rescan_timer(self): - return bool(ticks_diff(ticks_ms(), self._usb_last_scan) > 5000) + return bool(check_deadline(ticks_ms(), self._usb_last_scan) > 5000) def usb_time_reset(self): self._usb_last_scan = ticks_ms() diff --git a/kmk/modules/split.py b/kmk/modules/split.py index 0de7e17..c5dfd1c 100644 --- a/kmk/modules/split.py +++ b/kmk/modules/split.py @@ -1,10 +1,11 @@ '''Enables splitting keyboards wirelessly or wired''' import busio from micropython import const +from supervisor import ticks_ms from storage import getmount -from kmk.kmktime import ticks_diff, ticks_ms +from kmk.kmktime import check_deadline from kmk.matrix import intify_coordinate from kmk.modules import Module @@ -241,7 +242,7 @@ class Split(Module): def ble_rescan_timer(self): '''If true, the rescan timer is up''' - return bool(ticks_diff(ticks_ms(), self._ble_last_scan) > 5000) + return bool(check_deadline(ticks_ms(), self._ble_last_scan) > 5000) def ble_time_reset(self): '''Resets the rescan timer'''