ble_hid
- BLE HID¶
The ble_hid
module provides access to the BLE Human Interface Device functionality.
Note
Make sure to enable the BLE HID functionality in card10.cfg and reboot your card10 if you want to use BLE HID.
Also make sure that the adafruit_hid
folder from the card10 release archive is placed
on the file system of your card10.
Warning
At least Ubuntu Linux will keep auto connecting to BLE HID devices once they are paired to the host computer. If you want to connect your card10 to a phone again, you might have to temporarily turn off Bluetooth on your computer.
An example application can be found in the preload directory (named HID Demo
). It provides
examples how to use the card10 as keyboard, mouse or volume control.
Please refer to the Adafruit CircuitPython HID library for examples how to use the HID service. The card10 implements the same HID descriptors as the Adafruit CircuitPython BLE library and should be compatible with all uses of the Adafruit CircuitPython HID library.
Example emulating a keyboard:
Adapted from https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/master/CPB_Keybutton_BLE/cpb_keybutton_ble.py
A more complete version of this example can be found in the HID Demo app on your card10.
import ble_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
k = Keyboard(ble_hid.devices)
kl = KeyboardLayoutUS(k)
k.send(Keycode.BACKSPACE)
# use keyboard_layout for words
kl.write("Demo with a long text to show how fast a card10 can type!")
# add shift modifier
k.send(Keycode.SHIFT, Keycode.F19)
Example emulating a mouse:
import ble_hid
import bhi160
import buttons
from adafruit_hid.mouse import Mouse
m = Mouse(ble_hid.devices)
def send_report(samples):
if len(samples) > 0:
x = -int(samples[0].z)
y = -int(samples[0].y)
m.move(x, y)
sensor = bhi160.BHI160Orientation(sample_rate=10, callback=send_report)
b_old = buttons.read()
while True:
b_new = buttons.read()
if not b_old == b_new:
print(b_new)
b_old = b_new
if b_new == buttons.TOP_RIGHT:
m.click(Mouse.MIDDLE_BUTTON)
elif b_new == buttons.BOTTOM_RIGHT:
m.click(Mouse.RIGHT_BUTTON)
elif b_new == buttons.BOTTOM_LEFT:
m.click(Mouse.LEFT_BUTTON)
Note
Make sure to catch OSError
exceptions in real applications. The exception will be thrown if
there is connection to the host (or if it is lost) and you want to send an event.
-
class
ble_hid.
Report
(report_id, blocking=False, usage_page=None, usage=None)[source]¶ The Report class provides an interface to the Adafruit CircuitPython HID library (https://github.com/adafruit/Adafruit_CircuitPython_HID/).
ble_hid.devices
exposes a list of reports for use with the CircuitPython HID classes. You usually do not have to interact with a report yourself but you can make use of itssend_report
method to send raw HID reports to the host.Example using Adafruit CircuitPython HID library:
import ble_hid from adafruit_hid.mouse import Mouse m = Mouse(ble_hid.devices) m.click(Mouse.MIDDLE_BUTTON)
Example using raw non blocking access: .. code-block:: python
import ble_hid report = ble_hid.Report(report_id=3, blocking=False) # Consumer Control report.send_report(b’xe9x00’) # 0x00E9: Volume Increase report.send_report(b’x00x00’) # 0x0000: Release button
New in version 1.17.
-
send_report
(data)[source]¶ Tries to send a report to the host.
- Parameters
data – The data to be sent. Must not exceed the configured length of the report.
- Return type
bool
- Returns
True if the report was sent, False if the report was queued for sending.
- Raises
OSError – if there is no connection to a host or BLE HID is not enabled.
MemoryError – if there was no space in the queue (only raised if
blocking
was set to False).
-