diff --git a/docs/pimoroni_trackball.md b/docs/pimoroni_trackball.md index 3971afe..2e007c0 100644 --- a/docs/pimoroni_trackball.md +++ b/docs/pimoroni_trackball.md @@ -13,26 +13,26 @@ from kmk.modules.pimoroni_trackball import Trackball, TrackballMode import busio as io i2c = io.I2C(scl=board.D3, sda=board.D2) -trackball = Trackball(i2c, mode=TrackballMode.MOUSE_MODE) +trackball = Trackball(i2c) keyboard.modules.append(trackball) ``` Module will also work when you cannot use `busio` and do `import bitbangio as io` instead. -### Key inputs - -If you have used this thing on a mobile device, you will know it excels at cursor movement. You can get the same for any of your Layer: +### Key inputs, other handler combinations +If you have used this thing on a mobile device, you will know it excels at cursor movement ```python -from kmk.modules.pimoroni_trackball import Trackball, TrackballMode, PointingHandler, KeyHandler +from kmk.modules.pimoroni_trackball import Trackball, TrackballMode, PointingHandler, KeyHandler, ScrollHandler trackball = Trackball(i2c, mode=TrackballMode.MOUSE_MODE, handlers=[ # act like an encoder, input arrow keys KeyHandler(KC.UP, KC.RIGHT, KC.DOWN, KC.LEFT, KC.ENTER), # on layer 1 and above use the default pointing behavior - PointingHandler() + PointingHandler(), + ScrollHandler() ]) # now you can use these KeyCodes: diff --git a/kmk/modules/pimoroni_trackball.py b/kmk/modules/pimoroni_trackball.py index 42a0c28..bc46a0d 100644 --- a/kmk/modules/pimoroni_trackball.py +++ b/kmk/modules/pimoroni_trackball.py @@ -71,20 +71,15 @@ class TrackballHandler: class PointingHandler(TrackballHandler): def handle(self, keyboard, trackball, x, y, switch, state): - if trackball.mode == TrackballMode.MOUSE_MODE: - if x >= 0: - trackball.pointing_device.report_x[0] = x - else: - trackball.pointing_device.report_x[0] = 0xFF & x - if y >= 0: - trackball.pointing_device.report_y[0] = y - else: - trackball.pointing_device.report_y[0] = 0xFF & y - trackball.pointing_device.hid_pending = x != 0 or y != 0 - else: # SCROLL_MODE - trackball.pointing_device.report_w[0] = y - trackball.pointing_device.hid_pending = y != 0 - + if x >= 0: + trackball.pointing_device.report_x[0] = x + else: + trackball.pointing_device.report_x[0] = 0xFF & x + if y >= 0: + trackball.pointing_device.report_y[0] = y + else: + trackball.pointing_device.report_y[0] = 0xFF & y + trackball.pointing_device.hid_pending = x != 0 or y != 0 if switch == 1: # Button pressed trackball.pointing_device.button_status[ 0 @@ -100,6 +95,23 @@ class PointingHandler(TrackballHandler): trackball.previous_state = state +class ScrollHandler(TrackballHandler): + def handle(self, keyboard, trackball, x, y, switch, state): + pointing_device = trackball.pointing_device + pointing_device.report_w[0] = 0xFF & y + pointing_device.hid_pending = y != 0 + + if switch == 1: # Button pressed + pointing_device.button_status[0] |= pointing_device.MB_LMB + pointing_device.hid_pending = True + + if not state and trackball.previous_state is True: # Button released + pointing_device.button_status[0] &= ~pointing_device.MB_LMB + pointing_device.hid_pending = True + + trackball.previous_state = state + + class KeyHandler(TrackballHandler): x = 0 y = 0 @@ -149,8 +161,10 @@ class Trackball(Module): handlers=None, ): self.angle_offset = angle_offset - if handlers is None or len(handlers) == 0: - handlers = [PointingHandler()] + if not handlers: + handlers = [PointingHandler(), ScrollHandler()] + if mode == TrackballMode.SCROLL_MODE: + handlers.reverse() self._i2c_address = address self._i2c_bus = i2c @@ -168,13 +182,7 @@ class Trackball(Module): ) make_key( - names=("TB_MODE",), - on_press=self._tb_mode_press, - on_release=self._tb_mode_press, - ) - - make_key( - names=("TB_NEXT_HANDLER", "TB_N"), + names=("TB_MODE", "TB_NEXT_HANDLER", "TB_N"), on_press=self._tb_handler_next_press, ) @@ -291,9 +299,6 @@ class Trackball(Module): finally: self._i2c_bus.unlock() - def _tb_mode_press(self, key, keyboard, *args, **kwargs): - self.mode = not self.mode - def _tb_handler_press(self, key, keyboard, *args, **kwargs): self.activate_handler(key.meta.handler)