我正在嘗試編寫代碼來讀取操縱桿軸的值,我最終希望能夠使用這些值來控制電機。經過大量的努力找出C和如何在地球上使用操縱桿API我寫這個代碼。開始它只帶有一個變量未聲明的錯誤,然後我改進了代碼,使其更易讀,更容易理解我得到另一個相同的時候,我去編譯它希望第一個會消失! 這是我的代碼(原諒評論):操縱桿未聲明變量的代碼錯誤
#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; /* fd declared I thought */
fd = open ("/dev/js0", O_RDONLY | O_NONBLOCK);
return fd; /* code likes something to return */
}
int read_joystick_thrust_axis(struct js_event js) /* declare r_j_t_a variable and the js instance */
while (1) { /* loop forever */
while (read (fd, &js, sizeof(js)) > 0) { /* while there is an event */
if (js_event.type == JS_EVENT_AXIS) { /* and if that is an axis event */
if (js_event.number == 1) { /* and if that event is on the right axis */
printf ("Joystick at %8hb\n", js.value); /* print that instance of the joysticks value */
}
}
}
}
return 0; } /* keeping C happy by returning something */
和我從GCC後面的錯誤是:
[email protected] ~/rc $ gcc joystick.c
joystick.c: In function ‘read_joystick_thrust_axis’:
joystick.c:24:16: error: ‘fd’ undeclared (first use in this function)
joystick.c:24:16: note: each undeclared identifier is reported only once for each function it appears in
joystick.c:25:8: error: ‘js_event’ undeclared (first use in this function)
可能有人請解釋一下爲什麼我收到這些錯誤,並建議修復? 預先感謝您。
'fd'在'open_joystick'中聲明,其詞法範圍是'open_joystick'的範圍。 – ouah