emulating joystick USB-HID
emulating joystick USB-HID
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.
Re: emulating joystick USB-HID
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).
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).
Re: emulating joystick USB-HID
This page isn't about MicroPython but it seems to have information about how to select or generate the necessary descriptor.
Re: emulating joystick USB-HID
I just made a simple 3-axis (X,Y,Z) joystick emulator. Here's my code in boot.py:
I see the three axis values in Linux; haven't bothered to test it in Windows.
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))
Re: emulating joystick USB-HID
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