I am wondering if there is a way to get the axis position values from a joystick to feed into a C program. I can run the command jstest /dev/input/js0
and it will display live feedback of current button, axis positions. I would like to get the values fed into my C program to control a Servo I am just having trouble finding information on how I would get the live values. If someone could point me in the right direction that would be great thanks.
-
C can interface with the driver API directly ([example]()). However I have found this which appears to get input from each axis via pygame. I have yet to test/adapt it though)– WilfCommented Oct 23, 2015 at 16:47
-
whoops example– WilfCommented Oct 23, 2015 at 18:29
1 Answer
Have a look at the Joystick API of the Linux kernel. The API is really simple, you just need to open the device and read data from it.
There are several ioctl parameters to query the number of axes and buttons.
There is a nice, but old tutorial on the linuxgames mailing list, and all the important stuff is defined in the linux/joystick.h
header.
If you want to use a portable solution, that works outside of Linux, I would suggest you take a look at the SDL Joystick API. SDL works on a variety of platforms.
-
The first example from the mailing list tutorial works like a charm for me on my cheap USB gamepad. Just be sure to modify the #define JOY_DEV to "/dev/input/js0".– KozuchCommented Feb 26, 2016 at 23:24