fullmetalfelix
Posts: 3
Joined: Thu Jun 10, 2021 12:43 pm

PiZeroW as bluetooth Joystick in C

Thu Jun 10, 2021 4:53 pm

Hello everyone,

I have a pi zero w that will be the processing unit on a lightgun. The camera sees 4 IR LEDs in a rectangle shape, the pi processes the frames and calculates where it is pointing. I got this part sorted out nicely, and to make it fast enough, coded everything in C using libmmal, since python did not get high enough framerate.

Now I would like to have the pi zero behave as a bluetooth HID joystick, and send reports to a main host computer. Ideally the pi zero sits there, headless, until the main computer pairs to it.
I have seen many similar things in python but managed to get none to work, nor understand.

I would really appreciate some help on:
1. how to configure the pizero bluetooth to behave like HID joystick
2. send HID reports from my C program via bluetooth
3. [optional] if not paired via bluetooth, send the reports using the onboard USB (I think it can be done with write() on some /dev/hidg0 but at this point I am beyond confused)

This has been giving me quite some grief, and the pizero is now not even listed by other bluetooth devices as a result of the tinkering!
Any help is immensely welcome.

fullmetalfelix
Posts: 3
Joined: Thu Jun 10, 2021 12:43 pm

Re: PiZeroW as bluetooth Joystick in C

Sun Jun 13, 2021 6:45 am

Hi,

here is the current status, after more fiddling.
The Pizero is now a USB HID gadget (joystick), and my computer detects it as such when connected via USB cable. This was achieved by following part of this project: https://github.com/milador/RaspberryPi- ... s_Joystick
In practice I just coped the script that configures the gadget (https://github.com/milador/RaspberryPi- ... ystick_usb) and had it run from /etc/rc.local

Following other guides I managed to get this:

Code: Select all

pi@raspberrypi:~ $ hciconfig 
hci0:   Type: Primary  Bus: UART
        BD Address: XX:XX:XX:XX:XX:XX  ACL MTU: 1021:8  SCO MTU: 64:1
        UP RUNNING PSCAN ISCAN 
        RX bytes:2235 acl:0 sco:0 events:138 errors:0
        TX bytes:5695 acl:0 sco:0 commands:138 errors:0
(mac address obscured here)

I paired it with my tablet, and now to tablet can connect to the pizero via bluetooth. The tablet shows the gamepad icon next to the pizero in the bluetooth settings panel, so I guess it is advertised correctly as HID joystick.

In my limited understanding, this seems alright... advertising and connecting is handled by the OS, and all that is left to do is send HID reports via bluetooth. How is this done in C?
Or am I totally off-track?

deepo
Posts: 830
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: PiZeroW as bluetooth Joystick in C

Sun Jun 13, 2021 9:16 pm

There are plenty of python projects that are capable of emulating a joystick (through USB at least), so yeah why is there no C code to be found?
The libraries that python uses are some times written in C, so maybe it's possible to track something down?

fullmetalfelix
Posts: 3
Joined: Thu Jun 10, 2021 12:43 pm

Re: PiZeroW as bluetooth Joystick in C

Mon Jun 28, 2021 3:24 pm

Hi,

in the end I managed to do what I wanted with bluekitchen's BTstack (https://github.com/bluekitchen/btstack).
Documentation is helpful and there are many examples in the repo, so I was able to adapt one to my needs.

If anyone needs something like this I can provide the code.

Cheers!

Return to “Other projects”