Joystick Kernel Program
Hi everyone.. i need to know if someone is able to tell me how i can control the joystick only to ONE button press...
In this program i made, when you press a button in joystick, appears the message a thousand of times.. and i want that appears only one, when the button is pressed..
In this program, try press the button one only one tme and see what i am talking about...
//===============
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/joystick.h>
#define JOY_DEV "/dev/js0"
struct JS_DATA_TYPE js;
int Init( void ) {
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, #_of_axis );
ioctl( joy_fd, JSIOCGBUTTONS, #_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 */
while( 1 ) /* infinite loop */
{
/* read the joystick state */
read(joy_fd, &js, sizeof(struct js_event));
/* see what to do with the event */
switch (js.type )//& ~JS_EVENT_INIT)
{
case JS_EVENT_AXIS:
axis [ js.number ] = js.value;
break;
case JS_EVENT_BUTTON:
button [ js.number ] = js.value;
break;
}
/* print the results */
printf( "X: %6d Y: %6d ", axis[0], axis[1] );
if( num_of_axis > 2 )
printf("Z: %6d ", axis[2] );
if( num_of_axis > 3 )
printf("R: %6d ", axis[3] );
for( x=0 ; x"Be forgotten is worse than death..."
Rafael G. Donato
"Be forgotten is worse than death..."[email=rgdonato@gmail.com]Rafael G. Donato[/email]
If you record the state of the button as you go along, you can compare the previous state with the latest state. Then you test whether the button "was up, but is now down". That way you only get one press detected rather than constant presses while the button is held down.
From the looks of it, you''ll need two axis and two button arrays for previous and current states. Before you update the current state arrays, copy them into the previous state arrays.
From the looks of it, you''ll need two axis and two button arrays for previous and current states. Before you update the current state arrays, copy them into the previous state arrays.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement