I'm trying to read data from a joystick using C code. I have found online the way to do it.
This solution seems to work fine but with a problem. When the code reachers the the read() function, it stops and waits until there is change in the joystick device:
int read_event(int fd, struct js_event *event)
{
ssize_t bytes;
bytes = read(fd, event, sizeof(*event));
if (bytes == sizeof(*event))
return 0;
return -1;
}
I'm trying to find a way to make the code run continously and if there is no change in the control device, just return the previous state. So far I did not succeed. Maybe someone can help.
opened the file descriptor?read()blocks until it can transfer at least one byte, except for files that are open in non-blocking mode. You should be able to open the device in non-blocking mode by including the flagO_NONBLOCKin the options bitmask you pass toopen().