Refactor for nicer pointing device axes handling

This commit is contained in:
xs5871 2023-02-11 21:23:03 +00:00 committed by xs5871
parent ca800331de
commit 9e5d2c24e1
4 changed files with 35 additions and 23 deletions

View File

@ -104,7 +104,7 @@ class AbstractHID:
for mod in key.has_modifiers: for mod in key.has_modifiers:
self.add_modifier(mod) self.add_modifier(mod)
for axis in axes.values(): for axis in axes:
self.move_axis(axis) self.move_axis(axis)
def hid_send(self, evt): def hid_send(self, evt):
@ -136,6 +136,7 @@ class AbstractHID:
self.remove_cc() self.remove_cc()
self.remove_pd() self.remove_pd()
self.clear_axis()
return self return self
@ -218,11 +219,14 @@ class AbstractHID:
self._pd_report[1] = 0x00 self._pd_report[1] = 0x00
def move_axis(self, axis): def move_axis(self, axis):
if axis.delta != 0 or self._pd_report[axis.code + 2] != 0: delta = clamp(axis.delta, -127, 127)
delta = clamp(axis.delta, -127, 127) axis.delta -= delta
axis.delta -= delta self._pd_report[axis.code + 2] = 0xFF & delta
self._pd_report[axis.code + 2] = 0xFF & delta self._pd_pending = True
self._pd_pending = True
def clear_axis(self):
for idx in range(2, len(self._pd_report)):
self._pd_report[idx] = 0x00
class USBHID(AbstractHID): class USBHID(AbstractHID):

View File

@ -43,8 +43,18 @@ class Axis:
return f'Axis(code={self.code}, delta={self.delta})' return f'Axis(code={self.code}, delta={self.delta})'
def move(self, keyboard: Keyboard, delta: int): def move(self, keyboard: Keyboard, delta: int):
keyboard.hid_pending = True
self.delta += delta self.delta += delta
if self.delta:
keyboard.axes.add(self)
keyboard.hid_pending = True
else:
keyboard.axes.discard(self)
class AX:
W = Axis(2)
X = Axis(0)
Y = Axis(1)
def maybe_make_key( def maybe_make_key(

View File

@ -49,7 +49,7 @@ class KMKKeyboard:
##### #####
# Internal State # Internal State
keys_pressed = set() keys_pressed = set()
axes = {} axes = set()
_coordkeys_pressed = {} _coordkeys_pressed = {}
hid_type = HIDModes.USB hid_type = HIDModes.USB
secondary_hid_type = None secondary_hid_type = None
@ -102,12 +102,14 @@ class KMKKeyboard:
if debug.enabled: if debug.enabled:
debug(f'coordkeys_pressed={self._coordkeys_pressed}') debug(f'coordkeys_pressed={self._coordkeys_pressed}')
debug(f'keys_pressed={self.keys_pressed}') debug(f'keys_pressed={self.keys_pressed}')
# debug(f'axis={[ax for ax in self.axis.__iter__()]}')
def _send_hid(self) -> None: def _send_hid(self) -> None:
if not self._hid_send_enabled: if not self._hid_send_enabled:
return return
if self.axes and debug.enabled:
debug(f'axes={self.axes}')
self._hid_helper.create_report(self.keys_pressed, self.axes) self._hid_helper.create_report(self.keys_pressed, self.axes)
try: try:
self._hid_helper.send() self._hid_helper.send()
@ -116,10 +118,9 @@ class KMKKeyboard:
debug(f'HidNotFound(HIDReportType={e})') debug(f'HidNotFound(HIDReportType={e})')
self.hid_pending = False self.hid_pending = False
for axis in self.axes.values():
if axis.delta != 0: for axis in self.axes:
self.hid_pending = True axis.move(self, 0)
break
def _handle_matrix_report(self, kevent: KeyEvent) -> None: def _handle_matrix_report(self, kevent: KeyEvent) -> None:
if kevent is not None: if kevent is not None:

View File

@ -1,5 +1,5 @@
from kmk.hid import HID_REPORT_SIZES, HIDReportTypes from kmk.hid import HID_REPORT_SIZES, HIDReportTypes
from kmk.keys import Axis, make_key, make_mouse_key from kmk.keys import AX, make_key, make_mouse_key
from kmk.kmktime import PeriodicTimer from kmk.kmktime import PeriodicTimer
from kmk.modules import Module from kmk.modules import Module
@ -90,9 +90,6 @@ class MouseKeys(Module):
) )
def during_bootup(self, keyboard): def during_bootup(self, keyboard):
keyboard.axes['W'] = Axis(2)
keyboard.axes['X'] = Axis(0)
keyboard.axes['Y'] = Axis(1)
self._timer = PeriodicTimer(self.acc_interval) self._timer = PeriodicTimer(self.acc_interval)
def before_matrix_scan(self, keyboard): def before_matrix_scan(self, keyboard):
@ -106,18 +103,18 @@ class MouseKeys(Module):
if self.move_step < self.max_speed: if self.move_step < self.max_speed:
self.move_step = self.move_step + 1 self.move_step = self.move_step + 1
if self._right_activated: if self._right_activated:
keyboard.axes['X'].move(keyboard, self.move_step) AX.X.move(keyboard, self.move_step)
if self._left_activated: if self._left_activated:
keyboard.axes['X'].move(keyboard, -self.move_step) AX.X.move(keyboard, -self.move_step)
if self._up_activated: if self._up_activated:
keyboard.axes['Y'].move(keyboard, -self.move_step) AX.Y.move(keyboard, -self.move_step)
if self._down_activated: if self._down_activated:
keyboard.axes['Y'].move(keyboard, self.move_step) AX.Y.move(keyboard, self.move_step)
if self._mw_up_activated: if self._mw_up_activated:
keyboard.axes['W'].move(keyboard, 1) AX.W.move(keyboard, 1)
if self._mw_down_activated: if self._mw_down_activated:
keyboard.axes['W'].move(keyboard, -1) AX.W.move(keyboard, -1)
def before_hid_send(self, keyboard): def before_hid_send(self, keyboard):
return return