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:
self.add_modifier(mod)
for axis in axes.values():
for axis in axes:
self.move_axis(axis)
def hid_send(self, evt):
@ -136,6 +136,7 @@ class AbstractHID:
self.remove_cc()
self.remove_pd()
self.clear_axis()
return self
@ -218,12 +219,15 @@ class AbstractHID:
self._pd_report[1] = 0x00
def move_axis(self, axis):
if axis.delta != 0 or self._pd_report[axis.code + 2] != 0:
delta = clamp(axis.delta, -127, 127)
axis.delta -= delta
self._pd_report[axis.code + 2] = 0xFF & delta
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):
REPORT_BYTES = 9

View File

@ -43,8 +43,18 @@ class Axis:
return f'Axis(code={self.code}, delta={self.delta})'
def move(self, keyboard: Keyboard, delta: int):
keyboard.hid_pending = True
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(

View File

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

View File

@ -1,5 +1,5 @@
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.modules import Module
@ -90,9 +90,6 @@ class MouseKeys(Module):
)
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)
def before_matrix_scan(self, keyboard):
@ -106,18 +103,18 @@ class MouseKeys(Module):
if self.move_step < self.max_speed:
self.move_step = self.move_step + 1
if self._right_activated:
keyboard.axes['X'].move(keyboard, self.move_step)
AX.X.move(keyboard, self.move_step)
if self._left_activated:
keyboard.axes['X'].move(keyboard, -self.move_step)
AX.X.move(keyboard, -self.move_step)
if self._up_activated:
keyboard.axes['Y'].move(keyboard, -self.move_step)
AX.Y.move(keyboard, -self.move_step)
if self._down_activated:
keyboard.axes['Y'].move(keyboard, self.move_step)
AX.Y.move(keyboard, self.move_step)
if self._mw_up_activated:
keyboard.axes['W'].move(keyboard, 1)
AX.W.move(keyboard, 1)
if self._mw_down_activated:
keyboard.axes['W'].move(keyboard, -1)
AX.W.move(keyboard, -1)
def before_hid_send(self, keyboard):
return