-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
171 lines (146 loc) · 3.82 KB
/
index.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var HID = require('node-hid');
var vid = 10462;
var pid = 4418;
var commands = require('./commands.json');
var current;
var device;
function steamController(){
steamController.prototype.connect = function(){
device = new HID.HID(vid,pid);
device.read(function(err,data) {
console.log(data);
});
device.on("error", function(err) {
console.log(err);
});
process.stdin.resume();//so the program will not close instantly
function exitHandler(options, err) {
if (options.cleanup) device.close();
if (err) console.log(err.stack);
if (options.exit) process.exit();
}
//do something when app is closing
process.on('exit', exitHandler.bind(null,{cleanup:true}));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, {exit:true}));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, {exit:true}));
};
};
steamController.prototype.read = function(callback){
device.on("data", function(data) {
current = commands;
// buttons
switch(data[8]) {
case 128:
current.button.A = true;
break;
case 32:
current.button.B = true;
break;
case 16:
current.button.Y = true;
break;
case 64:
current.button.X = true;
break;
case 8:
current.button.LB = true;
break;
case 4:
current.button.RB = true;
break;
default:
current.button.RB = false;
current.button.LB = false;
current.button.A = false;
current.button.B = false;
current.button.Y = false;
current.button.X = false;
}
// Bottom buttons + center buttons
switch(data[9]) {
case 128:
current.bottom.left = true;
break;
case 16:
current.center.L = true;
break;
case 64:
current.center.R = true;
break;
case 32:
current.center.STEAM = true;
break;
case 8:
current.pad.value = 'DOWN';
break;
case 2:
current.pad.value = 'RIGHT';
break;
case 4:
current.pad.value = 'LEFT';
break;
case 1:
current.pad.value = 'UP';
break;
default:
current.pad.value = 'idle';
current.bottom.left = false;
current.center.L = false;
current.center.R = false;
current.center.STEAM = false;
}
switch(data[10]) {
case 24:
current.mouse.touched = true;
current.pad.touched = true;
break;
case 17:
current.mouse.touched = true;
current.bottom.right = true;
break;
case 25:
current.mouse.touched = true;
current.bottom.right = true;
current.pad.touched = true;
break;
case 2:
current.thumbstick.pressed = true;
break;
case 1:
current.bottom.right = true;
break;
case 16:
current.mouse.touched = true;
case 8:
current.pad.touched = true;
break;
default:
current.mouse.touched = false;
current.bottom.right = false;
current.pad.touched = false;
current.thumbstick.pressed = false;
}
// triggers
current.trigger.left = data[11];
current.trigger.right = data[12];
// joystick
current.joystick.xdir = data[16];
current.joystick.x = data[17];
current.joystick.ydir = data[18];
current.joystick.y = data[19];
current.thumbstick.xdir = data[16];
current.thumbstick.x = data[17];
current.thumbstick.ydir = data[18];
current.thumbstick.y = data[19];
// mouse
current.mouse.a = data[20];
current.mouse.b = data[21];
current.mouse.c = data[22];
current.mouse.d = data[23];
//console.log(current);
callback(current);
});
};
exports.steamController = steamController;