/
joyreader.cpp
executable file
·55 lines (50 loc) · 1.19 KB
/
joyreader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "joyreader.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <linux/joystick.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <QDebug>
int fp;
bool JoyReader::joy_init(char *joy_path)
{
fp = open(joy_path, O_RDONLY);
if (fp < 0) {
return false;
}
else {
fcntl( fp, F_SETFL, O_NONBLOCK );
return true;
}
}
JoyReader::directions JoyReader::get_buttons(void)
{
struct js_event jse;
int16_t nbytes;
nbytes = read(fp, &jse, sizeof(jse));
if(nbytes > 0) {
switch (jse.type) {
case JS_EVENT_AXIS:
if((jse.value == 32767) && (jse.number == 1)) {
return joy_down;
}
if((jse.value == 32767) && (jse.number == 0)) {
return joy_right;
}
if((jse.value == -32767) && (jse.number == 0)) {
return joy_left;
}
if((jse.value == -32767) && (jse.number == 1)) {
return joy_up;
}
break;
case JS_EVENT_BUTTON:
qDebug() << "buttons";
break;
default:
break;
}
}
return joy_none;
}