Adafruit is open and shipping! Let's build back better, together!
0

BBC Micro:BIT Anyone using bluetooth HID
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

BBC Micro:BIT Anyone using bluetooth HID

by rjnerd on Tue Sep 01, 2020 3:49 pm

I need to turn a u:BIT into a keyboard. I have been casting about for BLE support code, and the stuff I am finding is all years old and unsupported. I can't even get the published examples to compile without error. (using the mbed web based compiler). Anyone else have a recipe that results in a route to a key press?

Have been poking around trying to compile things (the Lancaster U and Seed packages) using the ardunino compiler, as well as trying to adapt parts of the CLUE ardunio libraries to the u:bit. (the last would be my preferred option, as the CLUE api is whole lot easier to use.)

rjnerd
 
Posts: 6
Joined: Tue Aug 09, 2011 4:27 pm

Re: BBC Micro:BIT Anyone using bluetooth HID

by mikeysklar on Thu Sep 03, 2020 3:47 pm

Hello rjnerd,

I don't know of any microbit specific BLE keyboard examples. However, we do have some nRF52840 CircuitPython examples that should be partially compatible with the microbit, but more so with the Sense.

https://learn.adafruit.com/ble-hid-keyb ... s-and-code

Code: Select all | TOGGLE FULL SIZE
"""
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.D11)
button_2 = DigitalInOut(board.D10)
button_3 = DigitalInOut(board.D9)
button_4 = DigitalInOut(board.D6)
button_5 = DigitalInOut(board.D5)

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("back")  # for debug in REPL
            k.send(Keycode.BACKSPACE)
            time.sleep(0.1)

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

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

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

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

    ble.start_advertising(advertisement)

mikeysklar
 
Posts: 2345
Joined: Mon Aug 01, 2016 8:10 pm

Re: BBC Micro:BIT Anyone using bluetooth HID

by rjnerd on Thu Sep 03, 2020 5:56 pm

Thanks, but circuit python for the u:bit doesn't include any ble support, as python doesn't leave enough room for the BLE code to fit in the chips limited memory. (The smallest BLE option takes about 100 kb,).

By being very selective about what I use, I have been able to get a deprecated version of HID to compile with only warnings on the MBED toolchain. Not far enough along to test yet, but getting there.

rjnerd
 
Posts: 6
Joined: Tue Aug 09, 2011 4:27 pm

Re: BBC Micro:BIT Anyone using bluetooth HID

by mikeysklar on Fri Sep 04, 2020 5:41 pm

I see. Thank you for pointing out the memory constraint on the BBC.

Glad you have made progress with the depreciated HID release.

Would it be logical to move your project to the Sense with 1MB of flash, M4 processor, a bunch of sensors and solid bluetooth? I know....it's almost 2x the price....

mikeysklar
 
Posts: 2345
Joined: Mon Aug 01, 2016 8:10 pm

Re: BBC Micro:BIT Anyone using bluetooth HID

by rjnerd on Fri Sep 04, 2020 7:25 pm

There are two things that the u:bit (and the Adafruit Clue) have that would require making a shield for the other alternatives like the Sense, specifically the buttons and display. (I need 4 items for my app, at least one button two preferred, a display capable of displaying one or two digits of numeric data, the accellerometer and HID bluetooth keyboard support.) By using u:bit, or clue all I will be distributing is code, and the user of the app won't need to do any soldering, etc.

I am developing for the CLUE as well, and if I didn't get the MBED route to work, was actually thinking about getting ladyada's code for the Clue to work on the u:bit. Its certainly a more welcoming environment, papering over the heap of wallpaper code needed to get the mbed set up. I don't need to know the gory details of how to advertise, and the battery level service. I just want to be able to say "start being a bluetooth keyboard" and "send this keystroke".

Cost is a bit of a factor, especially since two would be needed. But there is another cost related factor, the most likely user of the application is British. This means they have a u:bit at home already, one that their 7th grade kid received, and is now bored with. (the UK educational system supplies a u:bit to every 11-12 year old student). the second one is only a tenner new, and likely could be found used at a car boot sale for pocket change.

rjnerd
 
Posts: 6
Joined: Tue Aug 09, 2011 4:27 pm

Re: BBC Micro:BIT Anyone using bluetooth HID

by mikeysklar on Tue Sep 08, 2020 3:08 pm

rjnerd,

Thank you for sharing the constraints around your project. Also the ubit wide adoption in the UK is new to me so good to know. Good luck with the MBED route.

mikeysklar
 
Posts: 2345
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.