client.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright 2022 QMK
  2. # SPDX-License-Identifier: GPL-2.0-or-later
  3. from typing import List
  4. class XAPClient:
  5. """XAP device discovery
  6. """
  7. @staticmethod
  8. def devices(search: str = None) -> List[dict]:
  9. """Find compatible XAP devices
  10. Args:
  11. search: optional search string to filter results by
  12. """
  13. def _is_xap_usage(x):
  14. return x['usage_page'] == 0xFF51 and x['usage'] == 0x0058
  15. def _is_filtered_device(x):
  16. name = '%04x:%04x' % (x['vendor_id'], x['product_id'])
  17. return name.lower().startswith(search.lower())
  18. # lazy import to avoid compile issues
  19. import hid
  20. devices = filter(_is_xap_usage, hid.enumerate())
  21. if search:
  22. devices = filter(_is_filtered_device, devices)
  23. return list(devices)
  24. def connect(self, device: dict):
  25. """Connect to a given XAP device
  26. Args:
  27. device: item from a previous `XAPClient.devices()` call
  28. """
  29. from .device import XAPDevice
  30. return XAPDevice(device)