27 #define MAX_JOYSTICKS 3
34 joystick_ev =
new js_event();
68 strncpy(dev_path, port, 256);
73 joystick_fd = open(dev_path, O_RDONLY | O_NONBLOCK);
77 ioctl(joystick_fd, JSIOCGNAME(256), name);
78 ioctl(joystick_fd, JSIOCGVERSION, &version);
79 ioctl(joystick_fd, JSIOCGAXES, &axes);
80 ioctl(joystick_fd, JSIOCGBUTTONS, &buttons);
82 joystick_st->
axis.reserve(axes);
83 joystick_st->
button.reserve(buttons);
97 pthread_join(thread, 0);
115 int bytes = read(joystick_fd, joystick_ev,
sizeof(*joystick_ev));
118 joystick_ev->type &= ~JS_EVENT_INIT;
119 if (joystick_ev->type & JS_EVENT_BUTTON)
121 joystick_st->
button[joystick_ev->number] = joystick_ev->value;
124 if (joystick_ev->type & JS_EVENT_AXIS)
126 joystick_st->
axis[joystick_ev->number] = joystick_ev->value;
127 int joystick_n = joystick_ev->number;
128 if (joystick_n % 2 != 0)
148 usleep(pollMS * 1000);
155 if (n > -1 && n < axes)
157 int i0 = n, i1 = n + 1;
158 float x0 = joystick_st->
axis[i0] / 32767.0f, y0 = -joystick_st->
axis[i1] / 32767.0f;
159 float x = x0 * sqrt(1 - pow(y0, 2) / 2.0f), y = y0 * sqrt(1 - pow(x0, 2) / 2.0f);
164 pos.
theta = atan2(y, x) * (180.0 / 3.141592653589);
165 pos.
r = sqrt(pow(y, 2) + pow(x, 2));
170 pos.
r = -joystick_st->
axis[i1];
191 else if (pos.
theta < 0)
200 pos.
theta = pos.
r = pos.
x = pos.
y = 0.0f;
208 return n > -1 && n < buttons ? joystick_st->
button[n] : 0;
247 int n_joysticks = axes / 2;
The JoyStickDriver class provides basic functionality to read events from supported game pads under L...
buttonFunc buttonCallbackFunc
static void buttonEvent(int button_n, int value)
static void * loop(void *obj)
static void axisEvent(int axis_n, int value)
void setButtonCallback(buttonFunc buttonCallback)
axisFunc axisCallbackFunc
joystickFunc joystickCallbackFunc
std::function< void(int axis_n, double value)> axisFunc
std::function< void(int joystick_n, double mag, double angle)> joystickFunc
void setAxisCallback(axisFunc axisCallback)
static void joystickEvent(int joystick_n, double mag, double angle)
void setJoystickCallback(joystickFunc joystickCallback)
bool buttonPressed(int n)
void setPort(const char *port)
std::function< void(int button_n, int value)> buttonFunc
joystick_position joystickPosition(int n)
std::vector< signed short > axis
std::vector< signed short > button