-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoystick.js
More file actions
114 lines (87 loc) · 3.08 KB
/
joystick.js
File metadata and controls
114 lines (87 loc) · 3.08 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
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
const
vigemclient = require('vigemclient'),
{ SerialPort } = require('serialport'),
{ argv, exit } = require('process');
const
vjoy = new vigemclient(),
serialport = new SerialPort({ path: `COM${argv[2]}`, baudRate: Math.floor(argv[3]), autoOpen: false });
vjoy.connect();
const x360 = vjoy.createX360Controller();
x360.connect();
let check = false;
serialport.on('data', (serialBuffer) =>
{
try
{
const payload = JSON.parse(serialBuffer.toString());
if (!check)
{
console.log('Received JSON data !');
console.log('\nTo inspect controller actions: Run > joy.cpl > Double click "Controller (XBOX 360 For Windows)" > Test\nor simply go to https://hardwaretester.com/gamepad');
check = true;
}
if (!payload.ENABLED) return;
if (payload.LX !== undefined) x360.axis.leftX.setValue(payload.LX / 100);
if (payload.LY !== undefined) x360.axis.leftY.setValue(payload.LY / 100);
if (payload.L2 !== undefined) x360.axis.leftTrigger.setValue(payload.L2 / 100);
if (payload.RX !== undefined) x360.axis.rightX.setValue(payload.RX / 100);
if (payload.RY !== undefined) x360.axis.rightY.setValue(payload.RY / 100);
if (payload.R2 !== undefined) x360.axis.rightTrigger.setValue(payload.R2 / 100);
if (payload.D !== undefined)
{
const { L, R, D, U } = payload.D;
L ? x360.axis.dpadHorz.setValue(-1) : null;
R ? x360.axis.dpadHorz.setValue(1) : null;
D ? x360.axis.dpadVert.setValue(-1) : null;
U ? x360.axis.dpadVert.setValue(1) : null;
}
else
{
x360.axis.dpadHorz.setValue(0);
x360.axis.dpadVert.setValue(0);
}
if (payload.BTN !== undefined)
{
const { L1, L3, R1, R3, X, Y, A, B, BACK, MISC, START } = payload.BTN;
x360.button.LEFT_SHOULDER.setValue(L1);
x360.button.LEFT_THUMB.setValue(L3);
x360.button.RIGHT_SHOULDER.setValue(R1);
x360.button.RIGHT_THUMB.setValue(R3);
x360.button.X.setValue(X);
x360.button.Y.setValue(Y);
x360.button.A.setValue(A);
x360.button.B.setValue(B);
x360.button.BACK.setValue(BACK);
x360.button.GUIDE.setValue(MISC);
x360.button.START.setValue(START);
}
else
{
for (const BTN in x360.button)
{
x360.button[BTN].setValue(false);
}
}
}
catch (E) { }
});
const dataFrame =
`
{
// Number (-100 to 100) %
LX, LY, RX, RY
// Number (0 to 100) %
L2, R2
// Boolean
D: { L, R, D, U }
BTN: { L1, L3, R1, R3, X, Y, A, B, BACK, MISC, START }
ENABLED: true
}
`;
console.log('Expected Serialised JSON object for XInput Mapping', dataFrame);
serialport.open((err) =>
{
if (err === null) return console.log(`Expecting JSON data @ ${serialport.path} ...`);
console.log(err, `| Try again.\n${'_'.repeat(60)}\n`);
exit(1);
});