|
| 1 | +""" |
| 2 | +This example acts as a BLE HID keyboard to peer devices. |
| 3 | +Attach five buttons with pullup resistors to Feather nRF52840 |
| 4 | + each button will send a configurable keycode to mobile device or computer |
| 5 | +""" |
| 6 | +import time |
| 7 | +import board |
| 8 | +from digitalio import DigitalInOut, Direction |
| 9 | + |
| 10 | +import adafruit_ble |
| 11 | +from adafruit_ble.advertising import Advertisement |
| 12 | +from adafruit_ble.advertising.standard import ProvideServicesAdvertisement |
| 13 | +from adafruit_ble.services.standard.hid import HIDService |
| 14 | +from adafruit_ble.services.standard.device_info import DeviceInfoService |
| 15 | +from adafruit_hid.keyboard import Keyboard |
| 16 | +from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS |
| 17 | +from adafruit_hid.keycode import Keycode |
| 18 | + |
| 19 | +button_1 = DigitalInOut(board.D11) |
| 20 | +button_2 = DigitalInOut(board.D10) |
| 21 | +button_3 = DigitalInOut(board.D9) |
| 22 | +button_4 = DigitalInOut(board.D6) |
| 23 | +button_5 = DigitalInOut(board.D5) |
| 24 | + |
| 25 | +button_1.direction = Direction.INPUT |
| 26 | +button_2.direction = Direction.INPUT |
| 27 | +button_3.direction = Direction.INPUT |
| 28 | +button_4.direction = Direction.INPUT |
| 29 | +button_5.direction = Direction.INPUT |
| 30 | + |
| 31 | +hid = HIDService() |
| 32 | + |
| 33 | +device_info = DeviceInfoService(software_revision=adafruit_ble.__version__, |
| 34 | + manufacturer="Adafruit Industries") |
| 35 | +advertisement = ProvideServicesAdvertisement(hid) |
| 36 | +advertisement.appearance = 961 |
| 37 | +scan_response = Advertisement() |
| 38 | +scan_response.complete_name = "CircuitPython HID" |
| 39 | + |
| 40 | +ble = adafruit_ble.BLERadio() |
| 41 | +if not ble.connected: |
| 42 | + print("advertising") |
| 43 | + ble.start_advertising(advertisement, scan_response) |
| 44 | +else: |
| 45 | + print("already connected") |
| 46 | + print(ble.connections) |
| 47 | + |
| 48 | +k = Keyboard(hid.devices) |
| 49 | +kl = KeyboardLayoutUS(k) |
| 50 | +while True: |
| 51 | + while not ble.connected: |
| 52 | + pass |
| 53 | + print("Start typing:") |
| 54 | + |
| 55 | + while ble.connected: |
| 56 | + if not button_1.value: # pull up logic means button low when pressed |
| 57 | + #print("back") # for debug in REPL |
| 58 | + k.send(Keycode.BACKSPACE) |
| 59 | + time.sleep(0.1) |
| 60 | + |
| 61 | + if not button_2.value: |
| 62 | + kl.write("Bluefruit") # use keyboard_layout for words |
| 63 | + time.sleep(0.4) |
| 64 | + |
| 65 | + if not button_3.value: |
| 66 | + k.send(Keycode.SHIFT, Keycode.L) # add shift modifier |
| 67 | + time.sleep(0.4) |
| 68 | + |
| 69 | + if not button_4.value: |
| 70 | + kl.write("e") |
| 71 | + time.sleep(0.4) |
| 72 | + |
| 73 | + if not button_5.value: |
| 74 | + k.send(Keycode.ENTER) |
| 75 | + time.sleep(0.4) |
| 76 | + |
| 77 | + ble.start_advertising(advertisement) |
0 commit comments