2014-02-26 44 views
0

好吧,所以我回来了,我的代码现在编译成功,它只是在我运行它时什么也不做,甚至不打印“游戏杆打开成功”。我看不出任何明显的原因,有什么想法?游戏杆程序没有运行(没有数值打印)

Again excuse my commenting: 
#include <stdio.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <stdlib.h> 
#include <string.h> 
#include <linux/joystick.h>    /* lots of included headers because */ 
              /* I wasn't sure which I needed! */ 

#define JS_EVENT_AXIS   0x02   /* joystick moved */ 


int open_joystick(int fd) {     /* function to open the  joystick */ 

    fd = open ("/dev/js0", O_RDONLY | O_NONBLOCK);  /* setting fd to open the joystick */ 
     if (fd >= 0) {         /* in non blocking mode   */ 
      printf("Open Joystick Successful"); 
           }     
return fd;       /* functions like to return something */ 
} 

void read_joystick_thrust_axis(int fd, struct js_event js) { /* another function, including the integer fd, */ 
           /* the structure js 

while (read (fd, &js, sizeof(js)) > 0) {   /* while there is a joystick event to read */ 
    if (js.type == JS_EVENT_AXIS && js.number == 1){ /* and if that event is an axis event */ 
              /* and if that event is on the right axis */ 
      printf ("Thrust Reading: %8hd\n", js.value); /* print the values of it */ 
     } 
    } 

int main() { 
    int fd; 
    struct js_event js; 
    fd = open_joystick(fd); 
    while (1) { 
     read_joystick_thrust_axis(fd, js); 
    } 
    return 0; 
} 
+0

如果它不打印您的成功消息,也许它不成功。尝试检查'errno'或使用'perror()'来检查它是否真的失败。 – rodrigo

回答

1

此行是错误的:

if (fd < 0) { 
    printf("Open Joystick Successful"); 

open回报-1时失败,或者说>=0如果成功,那么你有逻辑错误。

可能您的open()失败,因为您缺少打开此设备的权限。或者也许它不存在...(/dev/input/js0?)。只需检查errno以确保:

if (fd < 0) 
    perror("/dev/js0");