I am unsure if this is the correct place for this question. I am attempting to obtain the axis position values from a joystick /dev/input/js0
on my system. If I run jstest /dev/input/js0
it will give me live feedback on all buttons and axis positions.
I am trying to feed this information into my C program to control servos. Is there a function for doing this? I have not worked much with input devices in programming so this is all new to me.
This data in 32 byte 'evdev' (Event Device) structures. Its better to look at it with something like: od -h /dev/input/event1. They represents Linux user input devices like keyboards, mouse or touchpads. On Android they represent sensors too.
The evdev interface serves the purpose of passing events generated in the kernel directly to userspace through character devices that are typically located in /dev/input/. This package also comes with bindings to uinput, the userspace input subsystem.
evdev (short for 'event device') is a generic input event interface in the Linux kernel and FreeBSD. It generalizes raw input events from device drivers and makes them available through character devices in the /dev/input/ directory.
You can run this python code to read events.
You can also use a high level module python-evdev.
#!/usr/bin/env python
import struct
infile_path = "/dev/input/js0"
EVENT_SIZE = struct.calcsize("llHHI")
file = open(infile_path, "rb")
event = file.read(EVENT_SIZE)
while event:
print(struct.unpack("llHHI", event))
(tv_sec, tv_usec, type, code, value) = struct.unpack("llHHI", event)
event = file.read(EVENT_SIZE)
Sample output:
(73324490, 8454144, 55242, 1118, 25231360)
(73324490, 42008576, 55242, 1118, 58785792)
(73324490, 75563008, 55242, 1118, 92340224)
(73324490, 109117440, 55242, 1118, 125894656)
(73324490, 142671872, 55242, 1118, 159449088)
(73324490, 176226304, 55242, 1118, 193003520)
(73324490, 209780736, 55242, 1118, 226557952)
(73324490, 243335168, 55242, 1118, 8519680)
(73324490, 25296896, 55242, 1118, 42074112)
(73324490, 58884097, 55242, 1118, 75661313)
(73324490, 92405760, 55242, 1118, 109215745)
(73324490, 125992961, 55242, 1118, 142737408)
(73324490, 159514624, 55242, 1118, 176324609)
(73327790, 84041474, 58542, 1118, 84049919)
(73328030, 84044852, 58782, 1118, 84017152)
(73331790, 33749013, 62542, 1118, 33742256)
(73331790, 33736851, 62562, 1118, 33731108)
(73331830, 33723339, 62602, 1118, 33718273)
(73332090, 33723339, 62862, 1118, 33685504)
Building on the post from kev
From the linux/joystick.h file:
struct js_event {
__u32 time; /* event timestamp in milliseconds */
__s16 value; /* value */
__u8 type; /* event type */
__u8 number; /* axis/button number */
};
So the python format should be:"LhBB"
infile_path = "/dev/input/js0"
EVENT_SIZE = struct.calcsize("LhBB")
file = open(infile_path, "rb")
event = file.read(EVENT_SIZE)
while event:
print(struct.unpack("LhBB", event))
(tv_msec, value, type, number) = struct.unpack("LhBB", event)
event = file.read(EVENT_SIZE)
sample output from XBOX One S controller:
// type=button, number=button number
//msec, value, type, number
(2114530, 1, 1, 0) // A pressed
(2114670, 0, 1, 0) // A released
(2116490, 1, 1, 1) // B pressed
(2116620, 0, 1, 1) // B released
(2117370, 1, 1, 2) // X pressed
(2117520, 0, 1, 2) // X released
(2118220, 1, 1, 3) // Y pressed
(2118360, 0, 1, 3) // Y released
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With