Der Joystick soll mir für ein projekt mit einem BeagleBoard helfen. Will nen Quadro Copter mit BeagleBoard basteln und wenn ich nun via joystick controller, Beschleunigungssensoren ansprechen könnte wäre das sehr von vorteil. Will also anstatt den verbauten Potis im Joystick einfach Beschleunigungssensoren verwenden. aber idealer wäre natürlich eine OnChange lösung da ich davon ausgehe dass die weniger prozessor leistung frisst XD Die jetztige frisst 100% ^^
Code: Alles auswählen
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/joystick.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#define JOY_DEV "/dev/input/js0"
int main()
{
int joy_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
char *button=NULL, name_of_joystick[80];
struct js_event js;
if( ( joy_fd = open( JOY_DEV , O_RDONLY)) == -1 )
{
printf( "Couldn't open joystick\n" );
return -1;
}
ioctl( joy_fd, JSIOCGAXES, &num_of_axis );
ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons );
ioctl( joy_fd, JSIOCGNAME(80), &name_of_joystick );
axis = (int *) calloc( num_of_axis, sizeof( int ) );
button = (char *) calloc( num_of_buttons, sizeof( char ) );
printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n"
, name_of_joystick
, num_of_axis
, num_of_buttons );
fcntl( joy_fd, F_SETFL, O_NONBLOCK ); /* use non-blocking mode */
int before = 0;
int ts = time(0)+100;
int i = 0;
while( ts > time(0) ) /* infinite loop */
{
i++;
/* read the joystick state */
read(joy_fd, &js, sizeof(struct js_event));
if(js.type == JS_EVENT_AXIS && js.number == 3 && js.value != before)
{
std::cout << js.value << std::endl;
before = js.value;
}
}
std::cout << i/100 << std::endl;
close( joy_fd ); /* too bad we never get here */
return 0;
}