2014-02-26 86 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");