HID fixup
This commit is contained in:
@@ -91,23 +91,21 @@ class Layers(Extension):
|
|||||||
return
|
return
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _df_pressed(key, state, *args, **kwargs):
|
def _df_pressed(key, keyboard, *args, **kwargs):
|
||||||
'''
|
'''
|
||||||
Switches the default layer
|
Switches the default layer
|
||||||
'''
|
'''
|
||||||
state.active_layers[-1] = key.meta.layer
|
keyboard.active_layers[-1] = key.meta.layer
|
||||||
return state
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _mo_pressed(key, state, *args, **kwargs):
|
def _mo_pressed(key, keyboard, *args, **kwargs):
|
||||||
'''
|
'''
|
||||||
Momentarily activates layer, switches off when you let go
|
Momentarily activates layer, switches off when you let go
|
||||||
'''
|
'''
|
||||||
state.active_layers.insert(0, key.meta.layer)
|
keyboard.active_layers.insert(0, key.meta.layer)
|
||||||
return state
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _mo_released(key, state, KC, *args, **kwargs):
|
def _mo_released(key, keyboard, KC, *args, **kwargs):
|
||||||
# remove the first instance of the target layer
|
# remove the first instance of the target layer
|
||||||
# from the active list
|
# from the active list
|
||||||
# under almost all normal use cases, this will
|
# under almost all normal use cases, this will
|
||||||
@@ -117,95 +115,88 @@ class Layers(Extension):
|
|||||||
# triggered by MO() and then defaulting to the MO()'s layer
|
# triggered by MO() and then defaulting to the MO()'s layer
|
||||||
# would result in no layers active
|
# would result in no layers active
|
||||||
try:
|
try:
|
||||||
del_idx = state.active_layers.index(key.meta.layer)
|
del_idx = keyboard.active_layers.index(key.meta.layer)
|
||||||
del state.active_layers[del_idx]
|
del keyboard.active_layers[del_idx]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return state
|
def _lm_pressed(self, key, keyboard, *args, **kwargs):
|
||||||
|
|
||||||
def _lm_pressed(self, key, state, *args, **kwargs):
|
|
||||||
'''
|
'''
|
||||||
As MO(layer) but with mod active
|
As MO(layer) but with mod active
|
||||||
'''
|
'''
|
||||||
state.hid_pending = True
|
keyboard.hid_pending = True
|
||||||
# Sets the timer start and acts like MO otherwise
|
# Sets the timer start and acts like MO otherwise
|
||||||
state.keys_pressed.add(key.meta.kc)
|
keyboard.keys_pressed.add(key.meta.kc)
|
||||||
return self._mo_pressed(key, state, *args, **kwargs)
|
self._mo_pressed(key, keyboard, *args, **kwargs)
|
||||||
|
|
||||||
def _lm_released(self, key, state, *args, **kwargs):
|
def _lm_released(self, key, keyboard, *args, **kwargs):
|
||||||
'''
|
'''
|
||||||
As MO(layer) but with mod active
|
As MO(layer) but with mod active
|
||||||
'''
|
'''
|
||||||
state.hid_pending = True
|
keyboard.hid_pending = True
|
||||||
state.keys_pressed.discard(key.meta.kc)
|
keyboard.keys_pressed.discard(key.meta.kc)
|
||||||
return self._mo_released(key, state, *args, **kwargs)
|
self._mo_released(key, keyboard, *args, **kwargs)
|
||||||
|
|
||||||
def _lt_pressed(self, key, state, *args, **kwargs):
|
def _lt_pressed(self, key, keyboard, *args, **kwargs):
|
||||||
# Sets the timer start and acts like MO otherwise
|
# Sets the timer start and acts like MO otherwise
|
||||||
self.start_time[LayerType.LT] = accurate_ticks()
|
self.start_time[LayerType.LT] = accurate_ticks()
|
||||||
return self._mo_pressed(key, state, *args, **kwargs)
|
self._mo_pressed(key, keyboard, *args, **kwargs)
|
||||||
|
|
||||||
def _lt_released(self, key, state, *args, **kwargs):
|
def _lt_released(self, key, keyboard, *args, **kwargs):
|
||||||
# On keyup, check timer, and press key if needed.
|
# On keyup, check timer, and press key if needed.
|
||||||
if self.start_time[LayerType.LT] and (
|
if self.start_time[LayerType.LT] and (
|
||||||
accurate_ticks_diff(
|
accurate_ticks_diff(
|
||||||
accurate_ticks(), self.start_time[LayerType.LT], state.tap_time
|
accurate_ticks(), self.start_time[LayerType.LT], keyboard.tap_time
|
||||||
)
|
)
|
||||||
):
|
):
|
||||||
state.hid_pending = True
|
keyboard.hid_pending = True
|
||||||
state.tap_key(key.meta.kc)
|
keyboard.tap_key(key.meta.kc)
|
||||||
|
|
||||||
self._mo_released(key, state, *args, **kwargs)
|
self._mo_released(key, keyboard, *args, **kwargs)
|
||||||
self.start_time[LayerType.LT] = None
|
self.start_time[LayerType.LT] = None
|
||||||
return state
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _tg_pressed(key, state, *args, **kwargs):
|
def _tg_pressed(key, keyboard, *args, **kwargs):
|
||||||
'''
|
'''
|
||||||
Toggles the layer (enables it if not active, and vise versa)
|
Toggles the layer (enables it if not active, and vise versa)
|
||||||
'''
|
'''
|
||||||
# See mo_released for implementation details around this
|
# See mo_released for implementation details around this
|
||||||
try:
|
try:
|
||||||
del_idx = state.active_layers.index(key.meta.layer)
|
del_idx = keyboard.active_layers.index(key.meta.layer)
|
||||||
del state.active_layers[del_idx]
|
del keyboard.active_layers[del_idx]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
state.active_layers.insert(0, key.meta.layer)
|
keyboard.active_layers.insert(0, key.meta.layer)
|
||||||
|
|
||||||
return state
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _to_pressed(key, state, *args, **kwargs):
|
def _to_pressed(key, keyboard, *args, **kwargs):
|
||||||
'''
|
'''
|
||||||
Activates layer and deactivates all other layers
|
Activates layer and deactivates all other layers
|
||||||
'''
|
'''
|
||||||
state.active_layers.clear()
|
keyboard.active_layers.clear()
|
||||||
state.active_layers.insert(0, key.meta.layer)
|
keyboard.active_layers.insert(0, key.meta.layer)
|
||||||
|
|
||||||
return state
|
def _tt_pressed(self, key, keyboard, *args, **kwargs):
|
||||||
|
|
||||||
def _tt_pressed(self, key, state, *args, **kwargs):
|
|
||||||
'''
|
'''
|
||||||
Momentarily activates layer if held, toggles it if tapped repeatedly
|
Momentarily activates layer if held, toggles it if tapped repeatedly
|
||||||
'''
|
'''
|
||||||
if self.start_time[LayerType.TT] is None:
|
if self.start_time[LayerType.TT] is None:
|
||||||
# Sets the timer start and acts like MO otherwise
|
# Sets the timer start and acts like MO otherwise
|
||||||
self.start_time[LayerType.TT] = accurate_ticks()
|
self.start_time[LayerType.TT] = accurate_ticks()
|
||||||
return self._mo_pressed(key, state, *args, **kwargs)
|
self._mo_pressed(key, keyboard, *args, **kwargs)
|
||||||
|
return
|
||||||
elif accurate_ticks_diff(
|
elif accurate_ticks_diff(
|
||||||
accurate_ticks(), self.start_time[LayerType.TT], state.tap_time
|
accurate_ticks(), self.start_time[LayerType.TT], keyboard.tap_time
|
||||||
):
|
):
|
||||||
self.start_time[LayerType.TT] = None
|
self.start_time[LayerType.TT] = None
|
||||||
return self._tg_pressed(key, state, *args, **kwargs)
|
self._tg_pressed(key, keyboard, *args, **kwargs)
|
||||||
|
return
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def _tt_released(self, key, state, *args, **kwargs):
|
def _tt_released(self, key, keyboard, *args, **kwargs):
|
||||||
if self.start_time[LayerType.TT] is None or not accurate_ticks_diff(
|
if self.start_time[LayerType.TT] is None or not accurate_ticks_diff(
|
||||||
accurate_ticks(), self.start_time[LayerType.TT], state.tap_time
|
accurate_ticks(), self.start_time[LayerType.TT], keyboard.tap_time
|
||||||
):
|
):
|
||||||
# On first press, works like MO. On second press, does nothing unless let up within
|
# On first press, works like MO. On second press, does nothing unless let up within
|
||||||
# time window, then acts like TG.
|
# time window, then acts like TG.
|
||||||
self.start_time[LayerType.TT] = None
|
self.start_time[LayerType.TT] = None
|
||||||
return self._mo_released(key, state, *args, **kwargs)
|
self._mo_released(key, keyboard, *args, **kwargs)
|
||||||
|
|
||||||
return state
|
|
||||||
|
26
kmk/hid.py
26
kmk/hid.py
@@ -3,11 +3,20 @@ from micropython import const
|
|||||||
|
|
||||||
from kmk.keys import FIRST_KMK_INTERNAL_KEY, ConsumerKey, ModifierKey
|
from kmk.keys import FIRST_KMK_INTERNAL_KEY, ConsumerKey, ModifierKey
|
||||||
|
|
||||||
|
try:
|
||||||
|
from adafruit_ble import BLERadio
|
||||||
|
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
||||||
|
from adafruit_ble.services.standard.hid import HIDService
|
||||||
|
from storage import getmount
|
||||||
|
except ImportError:
|
||||||
|
# BLE not supported on this platform
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class HIDModes:
|
class HIDModes:
|
||||||
NOOP = 0 # currently unused; for testing?
|
NOOP = 0 # currently unused; for testing?
|
||||||
USB = 1
|
USB = 1
|
||||||
BLE = 2 # currently unused; for bluetooth
|
BLE = 2
|
||||||
|
|
||||||
ALL_MODES = (NOOP, USB, BLE)
|
ALL_MODES = (NOOP, USB, BLE)
|
||||||
|
|
||||||
@@ -225,12 +234,6 @@ class USBHID(AbstractHID):
|
|||||||
|
|
||||||
|
|
||||||
class BLEHID(AbstractHID):
|
class BLEHID(AbstractHID):
|
||||||
try:
|
|
||||||
from adafruit_ble import BLERadio
|
|
||||||
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
|
||||||
from adafruit_ble.services.standard.hid import HIDService
|
|
||||||
from storage import getmount
|
|
||||||
|
|
||||||
BLE_APPEARANCE_HID_KEYBOARD = const(961)
|
BLE_APPEARANCE_HID_KEYBOARD = const(961)
|
||||||
# Hardcoded in CPy
|
# Hardcoded in CPy
|
||||||
MAX_CONNECTIONS = const(2)
|
MAX_CONNECTIONS = const(2)
|
||||||
@@ -238,9 +241,9 @@ class BLEHID(AbstractHID):
|
|||||||
def post_init(self, ble_name=str(getmount('/').label), **kwargs):
|
def post_init(self, ble_name=str(getmount('/').label), **kwargs):
|
||||||
self.conn_id = -1
|
self.conn_id = -1
|
||||||
|
|
||||||
self.ble = self.BLERadio()
|
self.ble = BLERadio()
|
||||||
self.ble.name = ble_name
|
self.ble.name = ble_name
|
||||||
self.hid = self.HIDService()
|
self.hid = HIDService()
|
||||||
self.hid.protocol_mode = 0 # Boot protocol
|
self.hid.protocol_mode = 0 # Boot protocol
|
||||||
|
|
||||||
# Security-wise this is not right. While you're away someone turns
|
# Security-wise this is not right. While you're away someone turns
|
||||||
@@ -318,13 +321,10 @@ class BLEHID(AbstractHID):
|
|||||||
self.conn_id = (self.conn_id - 1) % len(self.devices)
|
self.conn_id = (self.conn_id - 1) % len(self.devices)
|
||||||
|
|
||||||
def start_advertising(self):
|
def start_advertising(self):
|
||||||
advertisement = self.ProvideServicesAdvertisement(self.hid)
|
advertisement = ProvideServicesAdvertisement(self.hid)
|
||||||
advertisement.appearance = self.BLE_APPEARANCE_HID_KEYBOARD
|
advertisement.appearance = self.BLE_APPEARANCE_HID_KEYBOARD
|
||||||
|
|
||||||
self.ble.start_advertising(advertisement)
|
self.ble.start_advertising(advertisement)
|
||||||
|
|
||||||
def stop_advertising(self):
|
def stop_advertising(self):
|
||||||
self.ble.stop_advertising()
|
self.ble.stop_advertising()
|
||||||
|
|
||||||
except ImportError:
|
|
||||||
print('Bluetooth unsupported')
|
|
||||||
|
@@ -335,10 +335,6 @@ class KMKKeyboard:
|
|||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
||||||
# Only one GC to allow for extentions to have room. There are random memory allocations
|
|
||||||
# issues due to some devices not properly cleaning memory on reset
|
|
||||||
gc.collect()
|
|
||||||
|
|
||||||
def go(self, hid_type=HIDModes.USB, **kwargs):
|
def go(self, hid_type=HIDModes.USB, **kwargs):
|
||||||
self.hid_type = hid_type
|
self.hid_type = hid_type
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user