-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgamepad.cpp
More file actions
executable file
·87 lines (80 loc) · 2.21 KB
/
gamepad.cpp
File metadata and controls
executable file
·87 lines (80 loc) · 2.21 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "gamepad.hpp"
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/joystick.h>
#include <string.h>
#include <stdint.h>
using namespace std;
GamePad::GamePad(const char* filename){
fd=open(filename,O_RDONLY);
if (fd<0){
perror("GamePad Cannot open\n");
exit(1);
}else{
printf("Successed GamePad[%d] Connection\n",fd);
}
//ボタン数取得
char name[80];
ioctl(fd, JSIOCGBUTTONS, &button_num);
ioctl(fd, JSIOCGAXES, &axis_num);
ioctl(fd, JSIOCGNAME(80), &name);
//fcntl(fd, F_SETFL, O_NONBLOCK);
//ボタン数に応じて配列を作成
buttons=new bool[button_num];
axises=new int[axis_num];
printf("name=%s button=%d,axis=%d",name,button_num,axis_num);
//イベント発生機構
button_event=nullptr;
axis_event=nullptr;
//非同期取得システム
task=make_unique<thread>(&GamePad::Update,this);
}
GamePad::~GamePad(){
if (fd>=0){
printf("GamePad Close\n");
close(fd);
}
delete[] buttons;
delete[] axises;
}
void GamePad::Status()const{
//ボタン情報
printf("%d",buttons[0]);
for (int idx=1;idx<button_num;idx++){
printf(",%d",buttons[idx]);
}
printf("\n");
//スティック情報
printf("(%d,%d)",axises[0],axises[1]);
for (int idx=2;idx<axis_num;idx+=2){
printf("(%d,%d)",axises[idx],axises[idx+1]);
}
printf("\n");
fflush(stdout);
}
void GamePad::Update(){
struct js_event event;
//データがある限り読み込む
while (read(fd,&event,sizeof(event))==sizeof(event)){
switch (event.type&~JS_EVENT_INIT) {
case JS_EVENT_BUTTON:
buttons[event.number]=event.value;
if (button_event!=nullptr){
button_event(this,(ButtonNames)event.number,(bool)event.value);
}
continue;
case JS_EVENT_AXIS:
axises[event.number]=event.value;
if (axis_event!=nullptr){
axis_event(this,(AxisNames)event.number,(float)event.value/(float)INT16_MAX);
}
continue;
default:
continue;
}
}
}