Repository navigation
Expand file tree
/
Copy pathEdgeModuleUtility.py
More file actions
328 lines (287 loc) · 11.3 KB
/
Copy pathEdgeModuleUtility.py
File metadata and controls
328 lines (287 loc) · 11.3 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
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python
import time
import hid
VENDOR_ID = 0x054C
PRODUCT_ID_EDGE = 0x0DF2
def print_disclaimer():
print("########################################")
print("# Script Author: lewy20041 / Driftguard")
print("# Disclaimer:")
print("# This script is provided as-is without any warranty.")
print("# Use at your own risk. The author is not responsible")
print("# for any damage or unintended side effects caused")
print("# by using this script.")
print("########################################\n")
########################################
# HID Device Initialization
########################################
def open_device():
device = hid.device()
try:
device.open(VENDOR_ID, PRODUCT_ID_EDGE)
print("Device opened successfully (DualSense Edge).")
return device
except Exception as e:
print(f"[ERROR] Could not open device: {e}")
return None
########################################
# Feature Report Handling with hidapi
########################################
def send_feature_report(device, data, length):
if device is None:
print("[ERROR] No device connection.")
return False
if length > len(data):
print("[ERROR] Length exceeds data array size.")
return False
out_data = bytes(data[:length])
try:
res = device.send_feature_report(out_data)
if res == length:
# Optionally format data for debugging
formatted_data = list(out_data) + [0] * (64 - len(out_data))
# print(f"[DEBUG] Sent feature report successfully: {formatted_data}")
return True
else:
print(f"[ERROR] Failed to send full feature report. Sent: {res}/{length}")
return False
except Exception as e:
print(f"[ERROR] Exception in send_feature_report: {e}")
return False
def get_feature_report(device, report_id, length):
if device is None:
print("[ERROR] No device connection.")
return None
try:
report = device.get_feature_report(report_id, length)
if len(report) > 0:
# print(f"[DEBUG] Received feature report (len={len(report)}): {report}")
return bytearray(report)
else:
print("[ERROR] Received empty report.")
return None
except Exception as e:
print(f"[ERROR] Exception in get_feature_report: {e}")
return None
########################################
# Device Status Functions
########################################
def check_module_status(device):
"""Check the status of both left and right modules"""
if device is None:
return None, None
try:
# Query left module status
send_feature_report(device, [128, 21, 5, 0], 4)
time.sleep(0.2)
left_status_report = get_feature_report(device, 129, 64)
# Query right module status
send_feature_report(device, [128, 21, 5, 1], 4)
time.sleep(0.2)
right_status_report = get_feature_report(device, 129, 64)
left_status = left_status_report[5] if left_status_report and len(left_status_report) > 5 else None
right_status = right_status_report[5] if right_status_report and len(right_status_report) > 5 else None
return left_status, right_status
except Exception as e:
print(f"[ERROR] Exception in check_module_status: {e}")
return None, None
def get_module_status_text(status_code):
"""Convert module status code to readable text"""
if status_code == 132:
return "UNLOCKED"
elif status_code == 140:
return "LOCKED"
else:
return "UNKNOWN"
########################################
# Device Operation Functions
########################################
def ensure_device_connection(device):
"""Ensure the device is connected, attempting to reconnect if necessary."""
if device is None:
print("\nNo device connection. Attempting to connect...")
return open_device()
# Try a simple command to check if the device is still responsive
try:
device.get_feature_report(129, 64)
return device
except Exception:
print("\nDevice connection lost. Attempting to reconnect...")
try:
device.close()
except Exception:
pass
return open_device()
def calibrate_center_point(device):
device = ensure_device_connection(device)
if device is None:
print("[ERROR] Cannot calibrate: No device connection.")
return device
# Recalibrate sticks center point using the provided sequence
print("\nCalibrating sticks center point...")
time.sleep(0.2)
send_feature_report(device, [130, 1, 1, 1], 4)
time.sleep(0.2)
send_feature_report(device, [130, 3, 1, 1], 4)
time.sleep(0.2)
send_feature_report(device, [130, 2, 1, 1], 4)
time.sleep(0.2)
report = get_feature_report(device, 131, 64)
if report:
print("Sticks center point calibrated successfully.")
else:
print("Calibration of sticks center point failed.")
return device
def unlock_modules(device):
device = ensure_device_connection(device)
if device is None:
print("[ERROR] Cannot unlock: No device connection.")
return device
print("\nSometimes you need to run this option twice to unlock the modules.")
print("If modules are still locked, please try again.")
# Unlock Left Module
print("\nUnlocking left module...")
time.sleep(0.2)
send_feature_report(device, [128, 21, 6, 0, 11], 5)
time.sleep(0.2)
send_feature_report(device, [128, 21, 5, 0], 4)
time.sleep(1.0)
status_left = get_feature_report(device, 129, 64)
if status_left and len(status_left) > 5 and status_left[5] == 132:
print("Left module unlocked successfully.")
print("Saving changes to the left edge module complete!")
else:
print("Left module unlock failed.")
# Unlock Right Module
print("\nUnlocking right module...")
time.sleep(0.2)
send_feature_report(device, [128, 21, 6, 1, 11], 5)
time.sleep(0.2)
send_feature_report(device, [128, 21, 5, 1], 4)
time.sleep(1.0)
status_right = get_feature_report(device, 129, 64)
if status_right and len(status_right) > 5 and status_right[5] == 132:
print("Right module unlocked successfully.")
print("Saving changes to the right edge module complete!")
else:
print("Right module unlock failed.")
return device
def lock_modules(device):
device = ensure_device_connection(device)
if device is None:
print("[ERROR] Cannot lock: No device connection.")
return device
# Lock Left Module
print("\nLocking left module...")
time.sleep(0.2)
send_feature_report(device, [128, 21, 4, 0, 8], 5)
time.sleep(0.2)
send_feature_report(device, [128, 21, 5, 0], 4)
time.sleep(0.2)
status_left = get_feature_report(device, 129, 64)
if status_left and len(status_left) > 5 and status_left[5] == 140:
print("Left module locked successfully.")
print("Saving changes to the left edge module complete!")
else:
print("Left module lock failed.")
# Lock Right Module
print("\nLocking right module...")
time.sleep(0.2)
send_feature_report(device, [128, 21, 4, 1, 8], 5)
time.sleep(0.2)
send_feature_report(device, [128, 21, 5, 1], 4)
time.sleep(0.2)
status_right = get_feature_report(device, 129, 64)
time.sleep(0.2)
if status_right and len(status_right) > 5 and status_right[5] == 140:
print("Right module locked successfully.")
print("Saving changes to the right edge module complete!")
else:
print("Right module lock failed.")
return device
def backup_calibration_values(device):
device = ensure_device_connection(device)
if device is None:
print("[ERROR] Cannot backup: No device connection.")
return device
print("\nBacking up default modules calibration values...")
# Retrieve the calibration values (assuming they are available at report_id=131)
send_feature_report(device, [128, 12, 2], 3)
time.sleep(0.2)
report = get_feature_report(device, 129, 64)
if report:
filename = "default_calibration_backup.bin"
try:
with open(filename, "wb") as f:
f.write(report)
print(f"Calibration values backed up successfully to '{filename}'.")
except Exception as e:
print(f"[ERROR] Could not write calibration backup to file: {e}")
else:
print("Failed to retrieve calibration values for backup.")
return device
def display_menu_with_status(device):
"""Display menu with current module status"""
# Check module status
left_status, right_status = check_module_status(device)
# Convert to readable text
left_status_text = get_module_status_text(left_status)
right_status_text = get_module_status_text(right_status)
# Print header with status
print("\n==== DualSense Edge Controller Status ====")
if device is None:
print("Controller Status: DISCONNECTED")
else:
print("Controller Status: CONNECTED")
print(f"Left Module: {left_status_text}")
print(f"Right Module: {right_status_text}")
print("=========================================")
# Print menu options
print("\nPlease select an option:")
print("1. Unlock DualSense Edge Modules")
print("2. Lock DualSense Edge Modules")
print("3. Calibrate DualSense Edge Modules Center Point")
print("4. Back Up Default Modules Calibration Values to a File")
print("5. Exit")
return input("Enter your choice (1/2/3/4/5): ").strip()
########################################
# Main Script Execution
########################################
if __name__ == "__main__":
print_disclaimer() # Display the disclaimer at the start
device = None
try:
# Initial device connection attempt
device = open_device()
if device is None:
print("[WARNING] No device found initially. The script will attempt to connect when an operation is selected.")
while True:
# Check device connection before displaying menu
device = ensure_device_connection(device)
choice = display_menu_with_status(device)
if choice == "1":
device = unlock_modules(device)
input("\nPress Enter to return to the menu...")
elif choice == "2":
device = lock_modules(device)
input("\nPress Enter to return to the menu...")
elif choice == "3":
device = calibrate_center_point(device)
input("\nPress Enter to return to the menu...")
elif choice == "4":
device = backup_calibration_values(device)
input("\nPress Enter to return to the menu...")
elif choice == "5":
print("Exiting program.")
break
else:
print("Invalid choice. Please try again.")
except Exception as e:
print("[ERROR] Could not complete the operation:", e)
finally:
try:
if device:
device.close()
print("Device closed.")
except Exception:
pass