Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Six Axis HID #1092

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/en/boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ bootcfg(
mouse: bool = True,
nkro: bool = False,
pan: bool = False,
six_axis: bool = False,
storage: bool = True,
usb_id: Optional[tuple[str, str]] = None,
**kwargs,
Expand Down Expand Up @@ -119,6 +120,11 @@ Enable panning, aka horizontal scrolling, for the pointing device, aka mouse,
hid endpoint.


#### `six_axis`
Enable a HID endpoint for a six-axis spacemouse (with range +/-500) and change
the VID/PID to a SpaceMouse Compact.


#### `storage`
Disable storage if you don't want your computer to go "there's a new thumb drive
I have to mount!" every time you plug in your keyboard.
Expand Down
39 changes: 32 additions & 7 deletions kmk/bootcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def bootcfg(
mouse: bool = True,
nkro: bool = False,
pan: bool = False,
six_axis: bool = False,
storage: bool = True,
usb_id: Optional[tuple[str, str]] = None,
**kwargs,
Expand All @@ -46,6 +47,37 @@ def bootcfg(

# configure HID devices
devices = []
if (usb_id is not None) or six_axis:
import supervisor

if hasattr(supervisor, 'set_usb_identification'):
usb_args = {}
if usb_id is not None:
usb_args['manufacturer'] = usb_id[0]
usb_args['product'] = usb_id[1]
if six_axis:
from kmk.hid_reports import six_axis

usb_args['vid'] = 0x256F
usb_args['pid'] = 0xC635 # SpaceMouse Compact

if keyboard:
if nkro:
devices.append(six_axis.NKRO_KEYBOARD)
else:
devices.append(six_axis.KEYBOARD)
keyboard = False
if mouse:
if pan:
devices.append(six_axis.POINTER)
else:
devices.append(six_axis.MOUSE)
mouse = False
if consumer_control:
devices.append(six_axis.CONSUMER_CONTROL)
consumer_control = False
devices.append(six_axis.SIX_AXIS)
supervisor.set_usb_identification(**usb_args)
if keyboard:
if nkro:
from kmk.hid_reports import nkro_keyboard
Expand Down Expand Up @@ -73,13 +105,6 @@ def bootcfg(

usb_midi.disable()

# configure usb vendor and product id
if usb_id is not None:
import supervisor

if hasattr(supervisor, 'set_usb_identification'):
supervisor.set_usb_identification(*usb_id)

# configure data serial
if cdc_data:
import usb_cdc
Expand Down
67 changes: 65 additions & 2 deletions kmk/hid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@

from struct import pack, pack_into

from kmk.keys import Axis, ConsumerKey, KeyboardKey, ModifierKey, MouseKey
from kmk.keys import (
Axis,
ConsumerKey,
KeyboardKey,
ModifierKey,
MouseKey,
SixAxis,
SpacemouseKey,
)
from kmk.scheduler import cancel_task, create_task
from kmk.utils import Debug, clamp

Expand Down Expand Up @@ -32,18 +40,22 @@ class HIDModes:
_USAGE_PAGE_CONSUMER = const(0x0C)
_USAGE_PAGE_KEYBOARD = const(0x01)
_USAGE_PAGE_MOUSE = const(0x01)
_USAGE_PAGE_SIXAXIS = const(0x01)
_USAGE_PAGE_SYSCONTROL = const(0x01)

_USAGE_CONSUMER = const(0x01)
_USAGE_KEYBOARD = const(0x06)
_USAGE_MOUSE = const(0x02)
_USAGE_SIXAXIS = const(0x08)
_USAGE_SYSCONTROL = const(0x80)

_REPORT_SIZE_CONSUMER = const(2)
_REPORT_SIZE_KEYBOARD = const(8)
_REPORT_SIZE_KEYBOARD_NKRO = const(16)
_REPORT_SIZE_MOUSE = const(4)
_REPORT_SIZE_MOUSE_HSCROLL = const(5)
_REPORT_SIZE_SIXAXIS = const(12)
_REPORT_SIZE_SIXAXIS_BUTTON = const(2)
_REPORT_SIZE_SYSCONTROL = const(8)


Expand Down Expand Up @@ -172,6 +184,42 @@ def __init__(self):
super().__init__(_REPORT_SIZE_MOUSE_HSCROLL)


class SixAxisDeviceReport(Report):
def __init__(self, size=_REPORT_SIZE_SIXAXIS):
super().__init__(size)

def move_six_axis(self, axis):
delta = clamp(axis.delta, -500, 500)
axis.delta -= delta
index = 2 * axis.code
try:
self.buffer[index] = 0xFF & delta
self.buffer[index + 1] = 0xFF & (delta >> 8)
self.pending = True
except IndexError:
if debug.enabled:
debug(axis, ' not supported')

def get_action_map(self):
return {SixAxis: self.move_six_axis}


class SixAxisDeviceButtonReport(Report):
def __init__(self, size=_REPORT_SIZE_SIXAXIS_BUTTON):
super().__init__(size)

def add_six_axis_button(self, key):
self.buffer[0] |= key.code
self.pending = True

def remove_six_axis_button(self, key):
self.buffer[0] &= ~key.code
self.pending = True

def get_action_map(self):
return {SpacemouseKey: self.add_six_axis_button}


class AbstractHID:
def __init__(self):
self.report_map = {}
Expand All @@ -192,7 +240,12 @@ def create_report(self, keys):
def send(self):
for report in self.device_map.keys():
if report.pending:
self.device_map[report].send_report(report.buffer)
if hasattr(report, 'move_six_axis'):
self.device_map[report].send_report(report.buffer, 1)
elif hasattr(report, 'add_six_axis_button'):
self.device_map[report].send_report(report.buffer, 3)
else:
self.device_map[report].send_report(report.buffer)
report.pending = False

def setup(self):
Expand All @@ -203,6 +256,7 @@ def setup(self):
self.setup_keyboard_hid()
self.setup_consumer_control()
self.setup_mouse_hid()
self.setup_sixaxis_hid()

cancel_task(self._setup_task)
self._setup_task = None
Expand Down Expand Up @@ -243,6 +297,15 @@ def setup_mouse_hid(self):
self.report_map.update(report.get_action_map())
self.device_map[report] = device

def setup_sixaxis_hid(self):
if device := find_device(self.devices, _USAGE_PAGE_SIXAXIS, _USAGE_SIXAXIS):
report = SixAxisDeviceReport()
self.report_map.update(report.get_action_map())
self.device_map[report] = device
report = SixAxisDeviceButtonReport()
self.report_map.update(report.get_action_map())
self.device_map[report] = device

def show_debug(self):
for report in self.device_map.keys():
debug('use ', report.__class__.__name__)
Expand Down
Loading