Small pimoroni_trackball.py refactors

This commit is contained in:
tonasz 2022-02-06 16:18:59 +01:00 committed by Kyle Brown
parent 827862b34b
commit 7fba42f28c
2 changed files with 24 additions and 29 deletions

View File

@ -10,20 +10,15 @@ Declare I2C bus and add this module in your main class.
```python ```python
from kmk.modules.pimoroni_trackball import Trackball, TrackballMode from kmk.modules.pimoroni_trackball import Trackball, TrackballMode
import bitbangio as io
i2c = io.I2C(scl=board.D3, sda=board.D2)
trackball = Trackball(i2c, mode=TrackballMode.MOUSE_MODE)
```
Theoretically, it should work with normal IO library, but I encountered some issues on my RP2040 board.
```python
import busio as io import busio as io
i2c = io.I2C(scl=board.D3, sda=board.D2) i2c = io.I2C(scl=board.D3, sda=board.D2)
trackball = Trackball(i2c, mode=TrackballMode.MOUSE_MODE)
keyboard.modules.append(trackball)
``` ```
Module will also work when you cannot use `busio` and do `import bitbangio as io` instead.
Setup backlight color using below commands: Setup backlight color using below commands:
```python ```python
@ -34,4 +29,5 @@ trackball.set_blue(brightness)
trackball.set_white(brightness) trackball.set_white(brightness)
``` ```
This module exposes one keycode `TB_MODE`, which on hold switches between `MOUSE_MODE` and `SCROLL_MODE`. This module exposes one keycode `TB_MODE`, which on hold switches between `MOUSE_MODE` and `SCROLL_MODE`.
To choose the default mode, pass it in `Trackball` constructor.

View File

@ -2,12 +2,13 @@
Extension handles usage of Trackball Breakout by Pimoroni Extension handles usage of Trackball Breakout by Pimoroni
Product page: https://shop.pimoroni.com/products/trackball-breakout Product page: https://shop.pimoroni.com/products/trackball-breakout
''' '''
import struct
import math import math
from micropython import const import struct
from kmk.modules.mouse_keys import PointingDevice
from kmk.keys import make_key from kmk.keys import make_key
from kmk.modules import Module
from kmk.modules.mouse_keys import PointingDevice
from micropython import const
I2C_ADDRESS = 0x0A I2C_ADDRESS = 0x0A
I2C_ADDRESS_ALTERNATIVE = 0x0B I2C_ADDRESS_ALTERNATIVE = 0x0B
@ -52,7 +53,7 @@ class TrackballMode:
SCROLL_MODE = const(1) SCROLL_MODE = const(1)
class Trackball: class Trackball(Module):
'''Module handles usage of Trackball Breakout by Pimoroni''' '''Module handles usage of Trackball Breakout by Pimoroni'''
def __init__(self, i2c, mode=TrackballMode.MOUSE_MODE, address=I2C_ADDRESS): def __init__(self, i2c, mode=TrackballMode.MOUSE_MODE, address=I2C_ADDRESS):
@ -96,39 +97,40 @@ class Trackball:
self.pointing_device.report_y[0] = 0xFF & y_axis self.pointing_device.report_y[0] = 0xFF & y_axis
self.pointing_device.hid_pending = x_axis != 0 or y_axis != 0 self.pointing_device.hid_pending = x_axis != 0 or y_axis != 0
else: # SCROLL_MODE else: # SCROLL_MODE
if up > 0: if up >= 0:
self.pointing_device.report_w[0] = up self.pointing_device.report_w[0] = up
self.pointing_device.hid_pending = True
if down > 0: if down > 0:
self.pointing_device.report_w[0] = 0xFF & (0 - down) self.pointing_device.report_w[0] = 0xFF & (0 - down)
self.pointing_device.hid_pending = True self.pointing_device.hid_pending = up != 0 or down != 0
if up == 0 and down == 0:
self.pointing_device.report_w[0] = 0
self.pointing_device.hid_pending = False
if switch == 1: # Button pressed if switch == 1: # Button pressed
self.pointing_device.button_status[0] |= self.pointing_device.MB_LMB self.pointing_device.button_status[0] |= self.pointing_device.MB_LMB
self.pointing_device.hid_pending = True self.pointing_device.hid_pending = True
if not state and self.previous_state == True: # Button released if not state and self.previous_state is True: # Button released
self.pointing_device.button_status[0] &= ~self.pointing_device.MB_LMB self.pointing_device.button_status[0] &= ~self.pointing_device.MB_LMB
self.pointing_device.hid_pending = True self.pointing_device.hid_pending = True
self.previous_state = state self.previous_state = state
return
def after_matrix_scan(self, keyboard): def after_matrix_scan(self, keyboard):
return keyboard return
def before_hid_send(self, keyboard): def before_hid_send(self, keyboard):
return keyboard return
def after_hid_send(self, keyboard): def after_hid_send(self, keyboard):
if self.pointing_device.hid_pending: if self.pointing_device.hid_pending:
keyboard._hid_helper.hid_send(self.pointing_device._evt) keyboard._hid_helper.hid_send(self.pointing_device._evt)
return return
def on_powersave_enable(self, keyboard):
return
def on_powersave_disable(self, keyboard):
return
def set_rgbw(self, r, g, b, w): def set_rgbw(self, r, g, b, w):
"""Set all LED brightness as RGBW.""" """Set all LED brightness as RGBW."""
self._i2c_rdwr([REG_LED_RED, r, g, b, w]) self._i2c_rdwr([REG_LED_RED, r, g, b, w])
@ -200,6 +202,3 @@ class Trackball:
def _tb_mode_press(self, key, keyboard, *args, **kwargs): def _tb_mode_press(self, key, keyboard, *args, **kwargs):
self.mode = not self.mode self.mode = not self.mode
def _tb_mode_press(self, key, keyboard, *args, **kwargs):
self.mode = not self.mode