forked from hardillb/node-red-node-joystick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JoyStick.js
156 lines (135 loc) · 4.86 KB
/
JoyStick.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
var HID = require('node-hid');
var events = require('events');
var util = require('util');
function JoyStick(name) {
var devices = HID.devices();
var stick = this;
for (var i=0; i<devices.length; i++) {
if (devices[i].manufacturer === name) {
this._device = new HID.HID(devices[i].path);
this._device.on("data", function(data) {
//console.log(data);
var state = {};
var changed = false;
var i1 = data.readUInt8(0);
var i2 = data.readUInt8(1);
// var i3 = data.readUInt8(2);
// var i4 = data.readUInt8(3);
// var i5 = data.readUInt8(4);
var i6 = data.readUInt8(5);
var i7 = data.readUInt8(6);
// var i8 = data.readUInt8(7);
//console.log(i7);
switch (i1) {
case 0:
state.left = 1;
state.right = 0;
break;
case 127:
state.left = 0;
state.right = 0;
break;
case 255:
state.left = 0;
state.right = 1;
break;
}
switch (i2) {
case 0:
state.up = 1;
state.down = 0;
break;
case 127:
state.up = 0;
state.down = 0;
break;
case 255:
state.up = 0;
state.down = 1;
break;
}
state.button1 = 0;
state.button2 = 0;
state.button3 = 0;
state.button4 = 0;
var test = i6 >> 4;
if (test & 1) {
state.button1 = 1
}
if (test & 2) {
state.button2 = 1;
}
if (test & 4) {
state.button3 = 1;
}
if (test & 8) {
state.button4 = 1;
}
state.rightTrigger1 = 0;
state.rightTrigger2 = 0;
state.leftTrigger1 = 0;
state.leftTrigger2 = 0;
var tigger = i7 - 64;
if (tigger & 1) {
state.leftTrigger1 = 1
}
if (tigger & 2) {
state.rightTrigger1 = 1;
}
if (tigger & 4) {
state.leftTrigger2 = 1;
}
if (tigger & 8) {
state.rightTrigger2 = 1;
}
if (stick.old_state) {
if (state.left != stick.old_state.left ||
state.right != stick.old_state.right ||
state.up != stick.old_state.up ||
state.down != stick.old_state.down) {
changed = true;
}
if (state.button1 != stick.old_state.button1 ||
state.button2 != stick.old_state.button2 ||
state.button3 != stick.old_state.button3 ||
state.button4 != stick.old_state.button4) {
changed = true;
}
if (state.leftTrigger1 != stick.old_state.leftTrigger1 ||
state.leftTrigger2 != stick.old_state.leftTrigger2 ||
state.rightTrigger1 != stick.old_state.rightTrigger1 ||
state.rightTrigger2 != stick.old_state.rightTrigger2) {
changed = true;
}
} else {
changed = true;
}
if (changed) {
stick.old_state = state;
stick.emit('changed', state);
}
// re-arrange raw data so left and right are the same order
// then add other buttons as MSBs.
var i = ((i1 & 0x80) >> 6) | (!(i1 & 0x01) << 3);
var j = ((i2 & 0x80) >> 5) | (!(i2 & 0x01) << 0);
i = (i | j) << 4;
var r = i6 >> 4;
i = i | r;
j = i7 & 0x7f;
r = j * 256 + i;
stick.emit('raw', r);
});
break;
}
}
}
util.inherits(JoyStick, events.EventEmitter);
JoyStick.prototype.close = function() {
if (this._device) {
this._device.close();
}
}
JoyStick.devices = function(){
return HID.devices();
}
module.exports = JoyStick;