adafruit / Adafruit_CircuitPython_HID

USB Human Interface Device drivers.
MIT License
364 stars 106 forks source link

BLE Keyboard Functionality Issue on EFR32xG24 Explorer Kit #123

Closed HonestQiao closed 5 months ago

HonestQiao commented 6 months ago

CircuitPython version

Adafruit CircuitPython 9.0.0-alpha.6 on 2023-12-12; SiLabs xG24 Explorer Kit with EFR32MG24B210F1536IM48

board EFR32xG24 Explorer Kit

adafruit_hid version

>>> adafruit_hid.__version__
'6.0.3'

Code/REPL

# SPDX-FileCopyrightText: 2020 John Park for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
This example acts as a BLE HID keyboard to peer devices.
Attach five buttons with pullup resistors to Feather nRF52840
  each button will send a configurable keycode to mobile device or computer
"""
import time
import board
from digitalio import DigitalInOut, Direction

import adafruit_ble
from adafruit_ble.advertising import Advertisement
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.standard.hid import HIDService
from adafruit_ble.services.standard.device_info import DeviceInfoService
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

button_1 = DigitalInOut(board.PB2)
button_2 = DigitalInOut(board.PB3)
button_3 = DigitalInOut(board.PD2)
button_4 = DigitalInOut(board.PD3)
button_5 = DigitalInOut(board.PC9)

button_1.direction = Direction.INPUT
button_2.direction = Direction.INPUT
button_3.direction = Direction.INPUT
button_4.direction = Direction.INPUT
button_5.direction = Direction.INPUT

hid = HIDService()

device_info = DeviceInfoService(software_revision=adafruit_ble.__version__,
                                manufacturer="Adafruit Industries")
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)

k = Keyboard(hid.devices)
kl = KeyboardLayoutUS(k)
while True:
    while not ble.connected:
        pass
    print("Start typing:")

    while ble.connected:
        if not button_1.value:  # pull up logic means button low when pressed
            print("button_1: back")  # for debug in REPL
            k.send(Keycode.BACKSPACE)
            time.sleep(0.1)

        if not button_2.value:
            print("button_2: Bluefruit")
            kl.write("Bluefruit")  # use keyboard_layout for words
            time.sleep(0.4)

        if False and not button_3.value:
            k.send(Keycode.SHIFT, Keycode.L)  # add shift modifier
            time.sleep(0.4)

        if False and not button_4.value:
            kl.write("e")
            time.sleep(0.4)

        if False and not button_5.value:
            k.send(Keycode.ENTER)
            time.sleep(0.4)

    ble.start_advertising(advertisement)

Behavior After connecting the Bluetooth HID device to my computer , pressing the button again, the program outputs and sends the button information, but the computer does not receive the operation.

>>> %Run -c $EDITOR_CONTENT
advertising
Start typing:
button_1: back
button_1: back
button_2: Bluefruit
button_2: Bluefruit
button_1: back
button_1: back

Description I want to implement the EFR32xG24 Explorer Kit as a Bluetooth HID device, sending corresponding keyboard events to connected devices (computers or phones) through buttons. I have successfully started the BLE UART service on this board and completed communication tests with the application on the phone. However, the corresponding circuitpython for this board does not have a built-in usb-hid module.Thus, the above error occurred. I want to know if there is a way to bypass usb-hid?

Additional information I also noticed https://github.com/adafruit/Adafruit_CircuitPython_HID/issues/121 and https://github.com/adafruit/Adafruit_CircuitPython_HID/releases/tag/6.0.2. But no solution was found.

HonestQiao commented 5 months ago

This PR solves the problem: