forked from dualshock-tools/ds4-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds4-calibration-tool.py
executable file
·231 lines (181 loc) · 7.42 KB
/
ds4-calibration-tool.py
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python3
import usb.core
import usb.util
import array
import struct
import sys
import binascii
import time
from construct import *
dev = None
VALID_DEVICE_IDS = [
(0x054c, 0x05c4),
(0x054c, 0x09cc)
]
def wait_for_device():
global dev
print("Waiting for a DualShock 4...")
while True:
for i in VALID_DEVICE_IDS:
dev = usb.core.find(idVendor=i[0], idProduct=i[1])
if dev is not None:
print("Found a DualShock 4: vendorId=%04x productId=%04x" % (i[0], i[1]))
return
time.sleep(1)
class HID_REQ:
DEV_TO_HOST = usb.util.build_request_type(
usb.util.CTRL_IN, usb.util.CTRL_TYPE_CLASS, usb.util.CTRL_RECIPIENT_INTERFACE)
HOST_TO_DEV = usb.util.build_request_type(
usb.util.CTRL_OUT, usb.util.CTRL_TYPE_CLASS, usb.util.CTRL_RECIPIENT_INTERFACE)
GET_REPORT = 0x01
SET_REPORT = 0x09
def hid_get_report(dev, report_id, size):
assert isinstance(size, int), 'get_report size must be integer'
assert report_id <= 0xff, 'only support report_type == 0'
return dev.ctrl_transfer(HID_REQ.DEV_TO_HOST, HID_REQ.GET_REPORT, report_id, 0, size + 1)[1:].tobytes()
def hid_set_report(dev, report_id, buf):
assert isinstance(buf, (bytes, array.array)
), 'set_report buf must be buffer'
assert report_id <= 0xff, 'only support report_type == 0'
buf = struct.pack('B', report_id) + buf
return dev.ctrl_transfer(HID_REQ.HOST_TO_DEV, HID_REQ.SET_REPORT, (3 << 8) | report_id, 0, buf)
def dump_93_data():
data = hid_get_report(dev, 0x93, 13)
assert len(data) == 13
deviceId, targetId, numChunks, curChunk, dataLen = struct.unpack('BBBBBxxxxxxxx', data)
if deviceId == 0xff and targetId == 0xff:
print("No data to read")
return []
theDeviceId, theTargetId = deviceId, targetId
print("Data is split in %d chunks; we are at %d" % (numChunks, curChunk))
if numChunks == 0:
return []
assert dataLen >= 0 and dataLen <= 8
out = [data[5:5+dataLen]]
while curChunk < numChunks - 1:
data = hid_get_report(dev, 0x93, 13)
assert len(data) == 13
deviceId, targetId, numChunks, curChunk, dataLen = struct.unpack('BBBBBxxxxxxxx', data)
if deviceId == 0xff or targetId == 0xff:
print("No more data")
return out
assert (deviceId, targetId) == (theDeviceId, theTargetId)
out += [data[5:5+dataLen]]
return out
def do_trigger_calibration():
print("Starting trigger calibration...")
deviceId = 3
hid_set_report(dev, 0x90, struct.pack('BBBB', 1, deviceId, 0, 3))
for i in range(2):
print("L2: release and press enter")
input()
hid_set_report(dev, 0x90, struct.pack('BBBB', 3, deviceId, 1, 1))
for i in range(2):
print("L2: mid and press enter")
input()
hid_set_report(dev, 0x90, struct.pack('BBBB', 3, deviceId, 2, 1))
for i in range(2):
print("L2: full and press enter")
input()
hid_set_report(dev, 0x90, struct.pack('BBBB', 3, deviceId, 3, 1))
for i in range(2):
print("R2: release and press enter")
input()
hid_set_report(dev, 0x90, struct.pack('BBBB', 3, deviceId, 1, 2))
for i in range(2):
print("R2: mid and press enter")
input()
hid_set_report(dev, 0x90, struct.pack('BBBB', 3, deviceId, 2, 2))
for i in range(2):
print("R2: full and press enter")
input()
hid_set_report(dev, 0x90, struct.pack('BBBB', 3, deviceId, 3, 2))
print("Write.")
hid_set_report(dev, 0x90, struct.pack('BBBB', 2, deviceId, 0, 3))
print("Trigger calibration done!!")
print()
print("Here is some debug data from the DS4 about the calibration")
data = dump_93_data()
for i in range(len(data)):
print("Sample %d, data=%s" % (i, binascii.hexlify(data[i]).decode('utf-8')))
def do_stick_center_calibration():
print("Starting analog center calibration...")
deviceId = 1
targetId = 1
hid_set_report(dev, 0x90, struct.pack('BBB', 1, deviceId, targetId))
while True:
assert hid_get_report(dev, 0x91, 3) == bytes([deviceId,targetId,1])
assert hid_get_report(dev, 0x92, 3) == bytes([deviceId,targetId,0xff])
print("Press S to sample data or W to store calibration (followed by enter)")
X = input("> ").upper()
if X == "S":
hid_set_report(dev, 0x90, struct.pack('BBB', 3, deviceId, targetId))
elif X == "W":
hid_set_report(dev, 0x90, struct.pack('BBB', 2, deviceId, targetId))
break
else:
print("Invalid command")
assert hid_get_report(dev, 0x91, 3) == bytes([deviceId,targetId,2])
assert hid_get_report(dev, 0x92, 3) == bytes([deviceId,targetId,1])
print("Stick calibration done!!")
print()
print("Here is some debug data from the DS4 about the calibration")
data = dump_93_data()
for i in range(len(data)):
print("Sample %d, data=%s" % (i, binascii.hexlify(data[i]).decode('utf-8')))
def do_stick_minmax_calibration():
print("Starting analog min-max calibration...")
deviceId = 1
targetId = 2
hid_set_report(dev, 0x90, struct.pack('BBB', 1, deviceId, targetId))
assert hid_get_report(dev, 0x91, 3) == bytes([deviceId,targetId,1])
assert hid_get_report(dev, 0x92, 3) == bytes([deviceId,targetId,0xff])
print("DualShock 4 is now sampling data. Move the analogs all around their range")
print("When done, press any key to store calibration.")
input()
hid_set_report(dev, 0x90, struct.pack('BBB', 2, deviceId, targetId))
assert hid_get_report(dev, 0x91, 3) == bytes([deviceId,targetId,2])
assert hid_get_report(dev, 0x92, 3) == bytes([deviceId,targetId,1])
print("Stick calibration done!!")
print()
print("Here is some debug data from the DS4 about the calibration")
data = dump_93_data()
for i in range(len(data)):
print("Sample %d, data=%s" % (i, binascii.hexlify(data[i]).decode('utf-8')))
def menu():
print("")
print("Choose what you want to calibrate:")
print("1. Analog stick center")
print("2. Analog stick range (min-max)")
print("3. L2 / R2 (beta, let me know if works)")
choice_int = -1
try:
choice_int = int(input("> "))
except:
print("Invalid choice.")
return
if choice_int == 1:
do_stick_center_calibration()
if choice_int == 2:
do_stick_minmax_calibration()
if choice_int == 3:
do_trigger_calibration()
if __name__ == "__main__":
print("*********************************************************")
print("* Welcome to the fantastic DualShock 4 Calibration Tool *")
print("* *")
print("* This tool may break your controller. *")
print("* Use at your own risk. Good luck! <3 *")
print("* *")
print("* Version 0.01 ~ by the_al ~ *")
print("*********************************************************")
wait_for_device()
# Detach kernel driver
if sys.platform != 'win32' and dev.is_kernel_driver_active(0):
try:
dev.detach_kernel_driver(0)
except usb.core.USBError as e:
sys.exit('Could not detatch kernel driver: %s' % str(e))
if dev != None:
print("DualShock 4 online!")
menu()