From 2856d2b263526dbe6e9cc11754926295fe53b9dc Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Tue, 14 Jan 2020 17:40:59 -0500 Subject: [PATCH 1/2] Disconnect when restarting HID example --- examples/ble_hid_periph.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/examples/ble_hid_periph.py b/examples/ble_hid_periph.py index 761affd..2dc151b 100644 --- a/examples/ble_hid_periph.py +++ b/examples/ble_hid_periph.py @@ -124,15 +124,14 @@ advertisement = ProvideServicesAdvertisement(hid) advertisement.appearance = 961 scan_response = Advertisement() -scan_response.complete_name = "CircuitPython HID" ble = adafruit_ble.BLERadio() -if not ble.connected: - print("advertising") - ble.start_advertising(advertisement, scan_response) -else: - print("already connected") - print(ble.connections) +if ble.connected: + for c in ble.connections: + c.disconnect() + +print("advertising") +ble.start_advertising(advertisement, scan_response) k = Keyboard(hid.devices) kl = KeyboardLayoutUS(k) From dfea28146bd8ff073cf5f7a7153df38af6d426d4 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 16 Jan 2020 21:15:08 -0500 Subject: [PATCH 2/2] Provide a default HID descriptor, and move desccriptor out of ble_hid_periph.py example --- adafruit_ble/services/standard/hid.py | 107 +++++++++++++++++++++++++- examples/ble_hid_periph.py | 107 +------------------------- 2 files changed, 108 insertions(+), 106 deletions(-) diff --git a/adafruit_ble/services/standard/hid.py b/adafruit_ble/services/standard/hid.py index bcb8d0d..c78e93f 100755 --- a/adafruit_ble/services/standard/hid.py +++ b/adafruit_ble/services/standard/hid.py @@ -57,6 +57,111 @@ _APPEARANCE_HID_JOYSTICK = const(963) _APPEARANCE_HID_GAMEPAD = const(964) +#pylint: disable=line-too-long +DEFAULT_HID_DESCRIPTOR = ( + b'\x05\x01' # Usage Page (Generic Desktop Ctrls) + b'\x09\x06' # Usage (Keyboard) + b'\xA1\x01' # Collection (Application) + b'\x85\x01' # Report ID (1) + b'\x05\x07' # Usage Page (Kbrd/Keypad) + b'\x19\xE0' # Usage Minimum (\xE0) + b'\x29\xE7' # Usage Maximum (\xE7) + b'\x15\x00' # Logical Minimum (0) + b'\x25\x01' # Logical Maximum (1) + b'\x75\x01' # Report Size (1) + b'\x95\x08' # Report Count (8) + b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + b'\x81\x01' # Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) + b'\x19\x00' # Usage Minimum (\x00) + b'\x29\x65' # Usage Maximum (\x65) + b'\x15\x00' # Logical Minimum (0) + b'\x25\x65' # Logical Maximum (101) + b'\x75\x08' # Report Size (8) + b'\x95\x06' # Report Count (6) + b'\x81\x00' # Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) + b'\x05\x08' # Usage Page (LEDs) + b'\x19\x01' # Usage Minimum (Num Lock) + b'\x29\x05' # Usage Maximum (Kana) + b'\x15\x00' # Logical Minimum (0) + b'\x25\x01' # Logical Maximum (1) + b'\x75\x01' # Report Size (1) + b'\x95\x05' # Report Count (5) + b'\x91\x02' # Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) + b'\x95\x03' # Report Count (3) + b'\x91\x01' # Output (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) + b'\xC0' # End Collection + b'\x05\x01' # Usage Page (Generic Desktop Ctrls) + b'\x09\x02' # Usage (Mouse) + b'\xA1\x01' # Collection (Application) + b'\x09\x01' # Usage (Pointer) + b'\xA1\x00' # Collection (Physical) + b'\x85\x02' # Report ID (2) + b'\x05\x09' # Usage Page (Button) + b'\x19\x01' # Usage Minimum (\x01) + b'\x29\x05' # Usage Maximum (\x05) + b'\x15\x00' # Logical Minimum (0) + b'\x25\x01' # Logical Maximum (1) + b'\x95\x05' # Report Count (5) + b'\x75\x01' # Report Size (1) + b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + b'\x95\x01' # Report Count (1) + b'\x75\x03' # Report Size (3) + b'\x81\x01' # Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) + b'\x05\x01' # Usage Page (Generic Desktop Ctrls) + b'\x09\x30' # Usage (X) + b'\x09\x31' # Usage (Y) + b'\x15\x81' # Logical Minimum (-127) + b'\x25\x7F' # Logical Maximum (127) + b'\x75\x08' # Report Size (8) + b'\x95\x02' # Report Count (2) + b'\x81\x06' # Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position) + b'\x09\x38' # Usage (Wheel) + b'\x15\x81' # Logical Minimum (-127) + b'\x25\x7F' # Logical Maximum (127) + b'\x75\x08' # Report Size (8) + b'\x95\x01' # Report Count (1) + b'\x81\x06' # Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position) + b'\xC0' # End Collection + b'\xC0' # End Collection + b'\x05\x0C' # Usage Page (Consumer) + b'\x09\x01' # Usage (Consumer Control) + b'\xA1\x01' # Collection (Application) + b'\x85\x03' # Report ID (3) + b'\x75\x10' # Report Size (16) + b'\x95\x01' # Report Count (1) + b'\x15\x01' # Logical Minimum (1) + b'\x26\x8C\x02' # Logical Maximum (652) + b'\x19\x01' # Usage Minimum (Consumer Control) + b'\x2A\x8C\x02' # Usage Maximum (AC Send) + b'\x81\x00' # Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) + b'\xC0' # End Collection + # b'\x05\x01' # Usage Page (Generic Desktop Ctrls) + # b'\x09\x05' # Usage (Game Pad) + # b'\xA1\x01' # Collection (Application) + # b'\x85\x05' # Report ID (5) + # b'\x05\x09' # Usage Page (Button) + # b'\x19\x01' # Usage Minimum (\x01) + # b'\x29\x10' # Usage Maximum (\x10) + # b'\x15\x00' # Logical Minimum (0) + # b'\x25\x01' # Logical Maximum (1) + # b'\x75\x01' # Report Size (1) + # b'\x95\x10' # Report Count (16) + # b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + # b'\x05\x01' # Usage Page (Generic Desktop Ctrls) + # b'\x15\x81' # Logical Minimum (-127) + # b'\x25\x7F' # Logical Maximum (127) + # b'\x09\x30' # Usage (X) + # b'\x09\x31' # Usage (Y) + # b'\x09\x32' # Usage (Z) + # b'\x09\x35' # Usage (Rz) + # b'\x75\x08' # Report Size (8) + # b'\x95\x04' # Report Count (4) + # b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) + # b'\xC0' # End Collection +) +"""Default HID descriptor: provides mouse, keyboard, and consumer control devices.""" +#pylint: enable=line-too-long + # Boot keyboard and mouse not currently supported. _BOOT_KEYBOARD_INPUT_REPORT_UUID_NUM = const(0x2A22) @@ -191,7 +296,7 @@ class HIDService(Service): max_value=1) """Controls whether the device should be suspended (0) or not (1).""" - def __init__(self, hid_descriptor=None, service=None): + def __init__(self, hid_descriptor=DEFAULT_HID_DESCRIPTOR, service=None): super().__init__(report_map=hid_descriptor) if service: # TODO: Add support for connecting to a remote hid server. diff --git a/examples/ble_hid_periph.py b/examples/ble_hid_periph.py index 2dc151b..32b0891 100644 --- a/examples/ble_hid_periph.py +++ b/examples/ble_hid_periph.py @@ -14,111 +14,8 @@ from adafruit_hid.keyboard import Keyboard from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS -#pylint: disable=line-too-long -HID_DESCRIPTOR = ( - b'\x05\x01' # Usage Page (Generic Desktop Ctrls) - b'\x09\x06' # Usage (Keyboard) - b'\xA1\x01' # Collection (Application) - b'\x85\x01' # Report ID (1) - b'\x05\x07' # Usage Page (Kbrd/Keypad) - b'\x19\xE0' # Usage Minimum (\xE0) - b'\x29\xE7' # Usage Maximum (\xE7) - b'\x15\x00' # Logical Minimum (0) - b'\x25\x01' # Logical Maximum (1) - b'\x75\x01' # Report Size (1) - b'\x95\x08' # Report Count (8) - b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - b'\x81\x01' # Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) - b'\x19\x00' # Usage Minimum (\x00) - b'\x29\x65' # Usage Maximum (\x65) - b'\x15\x00' # Logical Minimum (0) - b'\x25\x65' # Logical Maximum (101) - b'\x75\x08' # Report Size (8) - b'\x95\x06' # Report Count (6) - b'\x81\x00' # Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) - b'\x05\x08' # Usage Page (LEDs) - b'\x19\x01' # Usage Minimum (Num Lock) - b'\x29\x05' # Usage Maximum (Kana) - b'\x15\x00' # Logical Minimum (0) - b'\x25\x01' # Logical Maximum (1) - b'\x75\x01' # Report Size (1) - b'\x95\x05' # Report Count (5) - b'\x91\x02' # Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) - b'\x95\x03' # Report Count (3) - b'\x91\x01' # Output (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile) - b'\xC0' # End Collection - b'\x05\x01' # Usage Page (Generic Desktop Ctrls) - b'\x09\x02' # Usage (Mouse) - b'\xA1\x01' # Collection (Application) - b'\x09\x01' # Usage (Pointer) - b'\xA1\x00' # Collection (Physical) - b'\x85\x02' # Report ID (2) - b'\x05\x09' # Usage Page (Button) - b'\x19\x01' # Usage Minimum (\x01) - b'\x29\x05' # Usage Maximum (\x05) - b'\x15\x00' # Logical Minimum (0) - b'\x25\x01' # Logical Maximum (1) - b'\x95\x05' # Report Count (5) - b'\x75\x01' # Report Size (1) - b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - b'\x95\x01' # Report Count (1) - b'\x75\x03' # Report Size (3) - b'\x81\x01' # Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) - b'\x05\x01' # Usage Page (Generic Desktop Ctrls) - b'\x09\x30' # Usage (X) - b'\x09\x31' # Usage (Y) - b'\x15\x81' # Logical Minimum (-127) - b'\x25\x7F' # Logical Maximum (127) - b'\x75\x08' # Report Size (8) - b'\x95\x02' # Report Count (2) - b'\x81\x06' # Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position) - b'\x09\x38' # Usage (Wheel) - b'\x15\x81' # Logical Minimum (-127) - b'\x25\x7F' # Logical Maximum (127) - b'\x75\x08' # Report Size (8) - b'\x95\x01' # Report Count (1) - b'\x81\x06' # Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position) - b'\xC0' # End Collection - b'\xC0' # End Collection - b'\x05\x0C' # Usage Page (Consumer) - b'\x09\x01' # Usage (Consumer Control) - b'\xA1\x01' # Collection (Application) - b'\x85\x03' # Report ID (3) - b'\x75\x10' # Report Size (16) - b'\x95\x01' # Report Count (1) - b'\x15\x01' # Logical Minimum (1) - b'\x26\x8C\x02' # Logical Maximum (652) - b'\x19\x01' # Usage Minimum (Consumer Control) - b'\x2A\x8C\x02' # Usage Maximum (AC Send) - b'\x81\x00' # Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position) - b'\xC0' # End Collection - # b'\x05\x01' # Usage Page (Generic Desktop Ctrls) - # b'\x09\x05' # Usage (Game Pad) - # b'\xA1\x01' # Collection (Application) - # b'\x85\x05' # Report ID (5) - # b'\x05\x09' # Usage Page (Button) - # b'\x19\x01' # Usage Minimum (\x01) - # b'\x29\x10' # Usage Maximum (\x10) - # b'\x15\x00' # Logical Minimum (0) - # b'\x25\x01' # Logical Maximum (1) - # b'\x75\x01' # Report Size (1) - # b'\x95\x10' # Report Count (16) - # b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - # b'\x05\x01' # Usage Page (Generic Desktop Ctrls) - # b'\x15\x81' # Logical Minimum (-127) - # b'\x25\x7F' # Logical Maximum (127) - # b'\x09\x30' # Usage (X) - # b'\x09\x31' # Usage (Y) - # b'\x09\x32' # Usage (Z) - # b'\x09\x35' # Usage (Rz) - # b'\x75\x08' # Report Size (8) - # b'\x95\x04' # Report Count (4) - # b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position) - # b'\xC0' # End Collection -) -#pylint: enable=line-too-long - -hid = HIDService(HID_DESCRIPTOR) +# Use default HID descriptor +hid = HIDService() device_info = DeviceInfoService(software_revision=adafruit_ble.__version__, manufacturer="Adafruit Industries") advertisement = ProvideServicesAdvertisement(hid)