Advertisement
Guest User

PASTEBIN

a guest
Sep 26th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/ioctl.h>
  6. #include <linux/joystick.h>
  7.  
  8. #define JOY_DEV "/dev/input/js0"
  9.  
  10. int main()
  11. {
  12. int joy_fd, *axis=NULL, num_of_axis=0, num_of_buttons=0, x;
  13. char *button=NULL, name_of_joystick[80];
  14. struct js_event js;
  15.  
  16. if( ( joy_fd = open( JOY_DEV , O_RDONLY)) == -1 )
  17. {
  18. printf( "Couldn't open joystick\n" );
  19. return -1;
  20. }
  21.  
  22. ioctl( joy_fd, JSIOCGAXES, &num_of_axis );
  23. ioctl( joy_fd, JSIOCGBUTTONS, &num_of_buttons );
  24. ioctl( joy_fd, JSIOCGNAME(80), &name_of_joystick );
  25.  
  26. axis = (int *) calloc( num_of_axis, sizeof( int ) );
  27. button = (char *) calloc( num_of_buttons, sizeof( char ) );
  28.  
  29. printf("Joystick detected: %s\n\t%d axis\n\t%d buttons\n\n"
  30. , name_of_joystick
  31. , num_of_axis
  32. , num_of_buttons );
  33.  
  34. fcntl( joy_fd, F_SETFL, O_NONBLOCK ); /* use non-blocking mode */
  35.  
  36. while( 1 ) /* infinite loop */
  37. {
  38.  
  39. /* read the joystick state */
  40. read(joy_fd, &js, sizeof(struct js_event));
  41.  
  42. /* see what to do with the event */
  43. switch (js.type & ~JS_EVENT_INIT)
  44. {
  45. case JS_EVENT_AXIS:
  46. axis [ js.number ] = js.value;
  47. break;
  48. case JS_EVENT_BUTTON:
  49. button [ js.number ] = js.value;
  50. break;
  51. }
  52.  
  53. /* print the results */
  54. printf( "X: %6d Y: %6d ", axis[0], axis[1] );
  55.  
  56. if( num_of_axis > 2 )
  57. printf("Z: %6d ", axis[2] );
  58.  
  59. if( num_of_axis > 3 )
  60. printf("R: %6d ", axis[3] );
  61.  
  62. for( x=0 ; x<num_of_buttons ; ++x )
  63. printf("B%d: %d ", x, button[x] );
  64.  
  65. printf(" \r");
  66. fflush(stdout);
  67. }
  68.  
  69. close( joy_fd ); /* too bad we never get here */
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement