Merge pull request #154 from ekx/topic-ble

Basic BLE functionality
This commit is contained in:
Josh Klar 2020-07-07 20:40:22 -07:00 committed by GitHub
commit ee83a32e87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 8 deletions

64
kmk/ble.py Normal file
View File

@ -0,0 +1,64 @@
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
from kmk.hid import (
HID_REPORT_SIZES,
AbstractHID,
HIDReportTypes,
HIDUsage,
HIDUsagePage,
)
BLE_APPEARANCE_HID_KEYBOARD = 961
class BLEHID(AbstractHID):
def post_init(self):
self.devices = {}
hid = HIDService()
advertisement = ProvideServicesAdvertisement(hid)
advertisement.appearance = BLE_APPEARANCE_HID_KEYBOARD
scan_response = Advertisement()
scan_response.complete_name = 'KMK Keyboard'
ble = BLERadio()
if not ble.connected:
ble.start_advertising(advertisement, scan_response)
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]
while len(evt) < report_size + 1:
evt.append(0)
return self.devices[reporting_device_const].send_report(
evt[1 : report_size + 1]
)

View File

@ -221,10 +221,3 @@ class USBHID(AbstractHID):
return self.devices[reporting_device_const].send_report(
evt[1 : HID_REPORT_SIZES[reporting_device_const] + 1]
)
class BLEHID(AbstractHID):
def __init__(self, *args, **kwargs):
raise NotImplementedError(
'BLE HID is not supported by upstream CircuitPython yet'
)

View File

@ -7,8 +7,9 @@ import busio
import gc
from kmk import led, rgb
from kmk.ble import BLEHID
from kmk.consts import KMK_RELEASE, LeaderMode, UnicodeMode
from kmk.hid import BLEHID, USBHID, AbstractHID, HIDModes
from kmk.hid import USBHID, AbstractHID, HIDModes
from kmk.internal_state import InternalState
from kmk.keys import KC
from kmk.kmktime import sleep_ms