emulating joystick USB-HID

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
mikhail
Posts: 3
Joined: Sat Sep 01, 2018 2:22 am

emulating joystick USB-HID

Post by mikhail » Thu Oct 31, 2019 4:59 am

Hello. I would like to make a USB joystick with pyboard. I know there are examples of using pyboard to emulate a keyboard and a mouse, but I have no clue what do I have to change in a mouse example so the connected pyboard will be recognized by the PC as a Joystick. Is it even possible with pyboard? Thank you.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: emulating joystick USB-HID

Post by jimmo » Tue Nov 05, 2019 1:57 am

Hi,

I think you will need to make some changes to the firmware to support this. At first glance, I think what's needed is:

Add a hid_joystick to the pyb module (alongside hid_mouse and hid_keyboard) which can be passed to pyb.usb_mode(...hid=pyb.hid_joystick). This is defined in stm32/usb.c and added to the pyb module in modpyb.c.

Then you need to define this descriptor, similar to how pyb_usb_hid_mouse_obj and pyb_usb_hid_keyboard_obj are defined.

Then hopefully it's just a matter of figuring out the right byte sequences to send (like in the mouse and keyboard examples).

nekomatic
Posts: 37
Joined: Thu May 08, 2014 9:31 pm

Re: emulating joystick USB-HID

Post by nekomatic » Tue Nov 05, 2019 9:24 am

This page isn't about MicroPython but it seems to have information about how to select or generate the necessary descriptor.

nedkonz
Posts: 24
Joined: Thu Aug 05, 2021 9:58 pm

Re: emulating joystick USB-HID

Post by nedkonz » Fri Dec 17, 2021 1:58 am

I just made a simple 3-axis (X,Y,Z) joystick emulator. Here's my code in boot.py:

Code: Select all

# simple joystick with 8-bit x,y,z
pyb.usb_mode('VCP+HID', hid=(1, 2, 3, 8, bytes([0x05, 0x01, 0x09, 0x04, 0xa1, 0x01, 0x09, 0x01, 0xa1, 0x00, 0x09, 0x30, 0x09, 0x31, 0x09, 0x32, 0x25, 0x7f, 0x15, 0x81, 0x75, 0x08, 0x95, 0x03, 0x81, 0x02, 0xc0, 0xc0])))

# Send joystick report. Values are -127 to +127.
# hid = pyb.USB_HID()
# hid.send((x, y, z))
I see the three axis values in Linux; haven't bothered to test it in Windows.

Cadguru
Posts: 1
Joined: Tue Dec 28, 2021 9:01 am

Re: emulating joystick USB-HID

Post by Cadguru » Tue Dec 28, 2021 9:11 am

Works as expected in Windows 10 and Windows 11. I did have to add at least a 270ms delay before running any additional code, but it works well if a wait a third of a second. I Blame Microsoft for that ;)

Post Reply