-
Notifications
You must be signed in to change notification settings - Fork 0
/
dualshock.js
130 lines (118 loc) · 3.19 KB
/
dualshock.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
// const WebSocket = require('ws');
const Dualshock = require('dualshock')
// const ws = new WebSocket('ws://localhost:8000');
let digital = JSON.stringify({
a: false,
b: false,
x: false,
y: false,
up: false,
down: false,
left: false,
right: false,
l1: false,
l2: false,
l3: false,
r1: false,
r2: false,
r3: false,
select: false,
start: false,
ps: false,
pad: false,
t1: false,
t2: false
})
let analog = JSON.stringify({
lStickX: 127,
lStickY: 127,
rStickX: 127,
rStickY: 127,
l2: 0,
r2: 0,
// t1X: 180,
// t1Y: 639,
// t2X: 0,
// t2Y: 0
})
// ws.on('open', function open() {
// ws.send(JSON.stringify({ t: 'init' }))
//
// });
// ws.on('close', function close() {
// console.log('> disconnected');
// });
function connect() {
let devicesList = Dualshock.getDevices();
if (devicesList.length != 0) {
let device = devicesList[0]
let gamepad = Dualshock.open(device, {
smoothAnalog: 10,
smoothMotion: 15,
joyDeadband: 4,
moveDeadband: 4
})
console.log(JSON.stringify({
t: 'detection',
d: device
}))
let ignoredIndex = ["t1X", "t1Y", "t2X", "t2Y"]
gamepad.onmotion = false;
gamepad.onstatus = false;
gamepad.onupdate = function () {
if (JSON.stringify(this.digital) != digital) {
digital = JSON.stringify(this.digital)
console.log(JSON.stringify({
t: 'input',
i: 'digital',
d: this.digital
}))
}
let analogRaw = {
lStickX: this.analog.lStickX,
lStickY: this.analog.lStickY,
rStickX: this.analog.rStickX,
rStickY: this.analog.rStickY,
l2: this.analog.l2,
r2: this.analog.r2
}
if (JSON.stringify(analogRaw) != analog) {
//console.log((new Date()).toString() + ' > send analog')
analog = JSON.stringify(analogRaw)
console.log(JSON.stringify({
t: 'input',
i: 'analog',
d: this.analog
}))
}
}
// gamepad.ondigital = function (button, value) {
// ws.send(JSON.stringify({
// t: 'input',
// d: [button, value]
// }))
// }
// gamepad.onanalog = function (axis, value) {
// ws.send(JSON.stringify({
// t: 'input',
// d: [axis, value]
// }))
// }
gamepad.ondisconnect = function () {
connect()
console.log(JSON.stringify({
t: 'disconnection'
}))
}
// ws.on('message', (data) => {
// let body = JSON.parse(data)
// if (body.t == 'led') {
// gamepad.setLed(body.d[0], body.d[1], body.d[2])
// }
// })
return true
} else {
setTimeout(connect, 2000)
}
}
connect()