qmk_firmware/lib/python/xap_client/client.py

40 lines
1.1 KiB
Python
Raw Normal View History

2022-07-18 23:46:08 +00:00
# Copyright 2022 QMK
# SPDX-License-Identifier: GPL-2.0-or-later
2022-09-06 16:33:19 +00:00
from typing import List
2022-07-17 00:28:54 +00:00
2022-09-26 17:09:36 +00:00
2022-07-17 00:28:54 +00:00
class XAPClient:
2022-07-18 23:46:08 +00:00
"""XAP device discovery
"""
2022-07-17 00:28:54 +00:00
@staticmethod
2022-09-06 16:33:19 +00:00
def devices(search: str = None) -> List[dict]:
2022-07-17 00:28:54 +00:00
"""Find compatible XAP devices
2022-07-18 23:46:08 +00:00
Args:
search: optional search string to filter results by
2022-07-17 00:28:54 +00:00
"""
def _is_xap_usage(x):
return x['usage_page'] == 0xFF51 and x['usage'] == 0x0058
def _is_filtered_device(x):
name = '%04x:%04x' % (x['vendor_id'], x['product_id'])
return name.lower().startswith(search.lower())
2022-09-06 16:33:19 +00:00
# lazy import to avoid compile issues
import hid
2022-07-17 00:28:54 +00:00
devices = filter(_is_xap_usage, hid.enumerate())
if search:
devices = filter(_is_filtered_device, devices)
return list(devices)
2022-07-18 23:46:08 +00:00
def connect(self, device: dict):
2022-07-17 00:28:54 +00:00
"""Connect to a given XAP device
2022-07-18 23:46:08 +00:00
Args:
device: item from a previous `XAPClient.devices()` call
2022-07-17 00:28:54 +00:00
"""
2022-07-17 01:58:14 +00:00
from .device import XAPDevice
2022-07-18 23:46:08 +00:00
return XAPDevice(device)