2020-05-26 08:30:16 +02:00
|
|
|
from adafruit_ble import BLERadio
|
|
|
|
from adafruit_ble.advertising import Advertisement
|
|
|
|
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
|
|
|
|
from adafruit_ble.services.standard.hid import HIDService
|
2020-07-07 20:30:32 -07:00
|
|
|
from kmk.hid import (
|
|
|
|
HID_REPORT_SIZES,
|
|
|
|
AbstractHID,
|
|
|
|
HIDReportTypes,
|
|
|
|
HIDUsage,
|
|
|
|
HIDUsagePage,
|
|
|
|
)
|
2020-05-26 08:30:16 +02:00
|
|
|
|
|
|
|
BLE_APPEARANCE_HID_KEYBOARD = 961
|
|
|
|
|
|
|
|
|
|
|
|
class BLEHID(AbstractHID):
|
2020-09-15 01:16:14 +03:00
|
|
|
def post_init(self, ble_name='KMK Keyboard', **kwargs):
|
2020-05-26 08:30:16 +02:00
|
|
|
self.devices = {}
|
|
|
|
|
|
|
|
hid = HIDService()
|
2020-07-07 20:30:32 -07:00
|
|
|
|
2020-05-26 08:30:16 +02:00
|
|
|
advertisement = ProvideServicesAdvertisement(hid)
|
|
|
|
advertisement.appearance = BLE_APPEARANCE_HID_KEYBOARD
|
2020-07-07 20:30:32 -07:00
|
|
|
|
2020-05-26 08:30:16 +02:00
|
|
|
ble = BLERadio()
|
2020-09-15 01:16:14 +03:00
|
|
|
ble.name = ble_name
|
|
|
|
# ble.tx_power = 2
|
|
|
|
|
2020-05-26 08:30:16 +02:00
|
|
|
if not ble.connected:
|
2020-09-15 01:16:14 +03:00
|
|
|
ble.start_advertising(advertisement)
|
2020-05-26 08:30:16 +02:00
|
|
|
while not ble.connected:
|
|
|
|
pass
|
|
|
|
|
|
|
|
for device in hid.devices:
|
|
|
|
us = device.usage
|
|
|
|
up = device.usage_page
|
|
|
|
|
|
|
|
if up == HIDUsagePage.CONSUMER and us == HIDUsage.CONSUMER:
|
|
|
|
self.devices[HIDReportTypes.CONSUMER] = device
|
|
|
|
continue
|
|
|
|
|
|
|
|
if up == HIDUsagePage.KEYBOARD and us == HIDUsage.KEYBOARD:
|
|
|
|
self.devices[HIDReportTypes.KEYBOARD] = device
|
|
|
|
continue
|
|
|
|
|
|
|
|
if up == HIDUsagePage.MOUSE and us == HIDUsage.MOUSE:
|
|
|
|
self.devices[HIDReportTypes.MOUSE] = device
|
|
|
|
continue
|
|
|
|
|
|
|
|
if up == HIDUsagePage.SYSCONTROL and us == HIDUsage.SYSCONTROL:
|
|
|
|
self.devices[HIDReportTypes.SYSCONTROL] = device
|
|
|
|
continue
|
|
|
|
|
|
|
|
def hid_send(self, evt):
|
|
|
|
# int, can be looked up in HIDReportTypes
|
|
|
|
reporting_device_const = self.report_device[0]
|
|
|
|
|
|
|
|
report_size = HID_REPORT_SIZES[reporting_device_const]
|
|
|
|
|
2020-07-07 20:30:32 -07:00
|
|
|
while len(evt) < report_size + 1:
|
2020-05-26 08:30:16 +02:00
|
|
|
evt.append(0)
|
2020-07-07 20:30:32 -07:00
|
|
|
|
2020-05-26 08:30:16 +02:00
|
|
|
return self.devices[reporting_device_const].send_report(
|
|
|
|
evt[1 : report_size + 1]
|
2020-07-07 20:30:32 -07:00
|
|
|
)
|
2020-09-15 01:52:33 +03:00
|
|
|
|
|
|
|
def clear_bonds(self):
|
|
|
|
import _bleio
|
|
|
|
|
|
|
|
_bleio.adapter.erase_bonding()
|