-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdcs_export_hid.py
More file actions
511 lines (476 loc) · 33.5 KB
/
Copy pathdcs_export_hid.py
File metadata and controls
511 lines (476 loc) · 33.5 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import hid
import time
# write some data to the device
# print("Write the data")
# h.write([0, 63, 35, 35] + [0] * 61)
# # wait
# time.sleep(0.05)
class Device:
def __init__(self,dcs_states = None, name=None):
if name is None:
hid.enumerate()
return
self.name = name
self.vendor_id , self.product_id = self.find_device_id(name)
self.h = hid.device()
self.h.open(self.vendor_id,self.product_id)
self.h.set_nonblocking(1)
self.dcs_states = dcs_states
self.flag = 0
if name == "T.16000M":
self.last_d = [0, 0, 63, 0, 32, 0, 32, 128, 255]
elif name == "TWCS Throttle":
self.last_d = [1, 0, 0, 0, 0, 0, 2, 255, 3, 255, 3, 0, 2, 0, 0, 255, 255, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20, 0, 0, 6, 218, 229, 247, 41, 41, 137, 126, 32, 32, 160, 160, 45, 45, 160, 160, 15, 243, 240, 214, 141, 197, 193, 26, 0, 0, 128, 2]
elif name == "Throttle - HOTAS Warthog":
self.last_d = [1, 0, 32, 32, 9, 15, 0, 2, 0, 2, 0, 0, 255, 63, 255, 63, 130, 48, 129, 240, 9, 128, 194, 123, 195, 45, 8, 2, 0, 0, 0, 0, 0, 0, 0, 0]
elif name == "Joystick - HOTAS Warthog":
self.last_d = [1, 0, 64, 240, 0, 0, 0, 0, 0, 0, 0, 0]
self.last_d_1_str = ' '
print(f"Device: {self.name} initialized")
def find_device_id(self,name):
for device_dict in hid.enumerate():
if device_dict['product_string'] == name:
# print(device_dict)
return device_dict["vendor_id"] , device_dict["product_id"]
def read(self):
#本函数用于读取设备的输入数据,但是直接读取到的数据并没有实际意义,需要根据json/dcs/patch中的数据进行解析
try:
while True:
d = self.h.read(64) # 尝试读取数据包
if d: # 如果读取到了数据
# print(d) # 打印数据
#处理T.16000M的数据
if self.name == "T.16000M":
if self.dcs_states is not None:
if d[0] == 1 and self.last_d[0] != 1:
self.dcs_states.write_numeric("PLT_Trigger", 1)
# print("Trigger pressed")
if d[0] == 0 and self.last_d[0] != 0:
self.dcs_states.write_numeric("PLT_Trigger", 0)
# print("Trigger released")
if d[2] == 1 and self.last_d[2] != 1:
# print("POV = 1")
pass
if d[2] == 3 and self.last_d[2] != 3:
# print("POV = 3")
pass
if d[2] == 2 and self.last_d[2] != 2:
# print("POV = 2")
if self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") == 3:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 4)
elif self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") == 4:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 3)
if d[2] == 4 and self.last_d[2] != 4:
# print("POV = 4")#武器选择下移
if self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") is None:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 0)
else:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR",min(max(self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR")-1,0),2))
if d[2] == 0 and self.last_d[2] != 0:
# print("POV = 0")#武器选择上移
if self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") is None:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 1)
else:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", min(self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR")+1,3))
if d[2] == 6 and self.last_d[2] != 6:
# print("POV = 6")
if self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") == 3:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 4)
elif self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") == 4:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 3)
if d[2] == 5 and self.last_d[2] != 5:
# print("POV = 5")
pass
if d[2] == 7 and self.last_d[2] != 7:
# print("POV = 7")
pass
if d[0] == 16 and self.last_d[0] != 16:
# print("Button 1 pressed")
self.dcs_states.write_numeric("Target_Designate_SWITCH", 1)
pass
if d[0] != 16 and self.last_d[0] == 16:
self.dcs_states.write_numeric("Target_Designate_SWITCH", 0)
if d[0] == 32 and self.last_d[0] != 32:
# print("Button 2 pressed")
self.dcs_states.write_numeric("Target_Designate_SWITCH", 2)
pass
if d[0] != 32 and self.last_d[0] == 32:
self.dcs_states.write_numeric("Target_Designate_SWITCH", 0)
if d[0] == 64 and self.last_d[0] != 64:
# print("Button 3 pressed")
self.dcs_states.write_numeric("Target_Designate_SWITCH", 3)
pass
if d[0] != 64 and self.last_d[0] == 64:
self.dcs_states.write_numeric("Target_Designate_SWITCH", 0)
if d[1] == 2 and self.last_d[1] != 2:
# print("Button 4 pressed")
self.dcs_states.write_numeric("PLT_CAGE/SEAM_BTN", 1)
pass
if d[1] == 0 and self.last_d[1] != 0:
# print("Button 4 released")
self.dcs_states.write_numeric("PLT_CAGE/SEAM_BTN", 0)
if d[1] == 1 and self.last_d[1] != 1:
# print("Button 5 pressed")
pass
if d[0] == 128 and self.last_d[0] != 128:
# print("Button 6 pressed")
pass
elif self.name == "TWCS Throttle":
# 处理其他设备的数据
#由于手上的设备坏了需要滤波一下
# 滤波处理 - 将指定位置的错误信号置为0
error_positions = [1,2,3,4,13,20,21, 22,23, 24,25, 26,27, 28,29, 30,31,32,34,35]
for pos in error_positions:
d[pos] = 0
# 比较当前数据与上一次数据是否有变化
if self.last_d is not None and d != self.last_d:
# 找出变化的位置和数据
changed_positions = []
for i in range(len(d)):
if i < len(self.last_d) and d[i] != self.last_d[i]:
changed_positions.append((i, d[i]))
# print(d)
# print(f"TWCS Throttle 数据变化: 位置 {[pos for pos, val in changed_positions]}, 值 {[val for pos, val in changed_positions]}")
###正式开始处理数据
if self.dcs_states is not None:
#button
if d[17] == 40 and self.last_d[17] != 40:
self.dcs_states.write_numeric("PLT_CAGE/SEAM_BTN", 1)
if d[17] != 40 and self.last_d[17] == 40:
self.dcs_states.write_numeric("PLT_CAGE/SEAM_BTN", 0)
if d[17] == 72 and self.last_d[17] != 72:
self.dcs_states.write_numeric("PLT_PLM_BTN", 1)
if d[17] != 72 and self.last_d[17] == 72:
self.dcs_states.write_numeric("PLT_PLM_BTN", 0)
#mic
if d[17] == 0 and self.last_d[17] != 0:
self.dcs_states.write_numeric("PLT_MIC_BTN_UHF-2", 1)
if d[17] == 2 and self.last_d[17] != 2:
self.dcs_states.write_numeric("PLT_MIC_BTN_ICS", 1)
if d[17] == 4 and self.last_d[17] != 4:
self.dcs_states.write_numeric("PLT_MIC_BTN_UHF-1", 1)
#wind
if d[18] == 8 and self.last_d[18] != 8:
self.dcs_states.write_numeric("SPEED_BRAKE_MODE", 2)
if d[18] == 32 and self.last_d[18] != 32:
self.dcs_states.write_numeric("SPEED_BRAKE_MODE", 1)
if d[18] == 128 and self.last_d[18] != 128:
self.dcs_states.write_numeric("WING_SWEEP", 2)#forward
if d[19] == 2 and self.last_d[19] != 2:
self.dcs_states.write_numeric("WING_SWEEP", 1)#after
if d[18] == 64 and self.last_d[18] != 64:
self.dcs_states.write_numeric("WING_SWEEP_MODE", 2)#auto
if d[19] == 1 and self.last_d[19] != 1:
self.dcs_states.write_numeric("WING_SWEEP_MODE", 1)#bomb
elif self.name == "Throttle - HOTAS Warthog":
# 滤波处理 - 将指定位置的错误信号置为0
error_positions = [12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35]
for pos in error_positions:
d[pos] = 0
# 比较当前数据与上一次数据是否有变化
if self.last_d is not None and d != self.last_d:
# 找出变化的位置和数据
changed_positions = []
for i in range(len(d)):
if i < len(self.last_d) and d[i] != self.last_d[i]:
changed_positions.append((i, d[i]))
# print(d)
if self.dcs_states is not None:
d_1_str = f"{d[1]:8b}"
#button
if d[2] == 96 and self.last_d[2] != 96:
self.dcs_states.write_numeric("PLT_CAGE/SEAM_BTN", 1)
if d[2] != 96 and self.last_d[2] == 96:
self.dcs_states.write_numeric("PLT_CAGE/SEAM_BTN", 0)
if d[1] == 1 and self.last_d[1] != 1:
self.dcs_states.write_numeric("PLT_PLM_BTN", 1)
if d[1] != 1 and self.last_d[1] == 1:
self.dcs_states.write_numeric("PLT_PLM_BTN", 0)
#mic
#wind
if d[1] == 64 and self.last_d[1] != 64:
self.dcs_states.write_numeric("SPEED_BRAKE_MODE", 2)
if d[1] == 0 and self.last_d[1] != 0:
self.dcs_states.write_numeric("SPEED_BRAKE_MODE", 1)
if d[1] == 8 and self.last_d[1] != 8:
self.dcs_states.write_numeric("WING_SWEEP", 2)#forward
if d[1] == 32 and self.last_d[1] != 32:
self.dcs_states.write_numeric("WING_SWEEP", 1)#after
if d[1] == 4 and self.last_d[1] != 4:
self.dcs_states.write_numeric("WING_SWEEP_MODE", 2)#auto
if d[1] == 16 and self.last_d[1] != 16:
self.dcs_states.write_numeric("WING_SWEEP_MODE", 1)#bomb
if d[3] == 32 and self.last_d[3] != 32:
self.dcs_states.write_numeric("PLT_FLAPS", 2)
if d[3] == 0 and self.last_d[3] != 0:
self.dcs_states.write_numeric("PLT_FLAPS", 1)
if d[3] == 64 and self.last_d[3] != 64:
self.dcs_states.write_numeric("PLT_FLAPS", 0)
elif self.name == "Joystick - HOTAS Warthog":
# 处理其他设备的数据
#由于手上的设备坏了需要滤波一下
# 滤波处理 - 将指定位置的错误信号置为0
error_positions = [4,5,6,7,8,9,10,11]
for pos in error_positions:
d[pos] = 0
# 比较当前数据与上一次数据是否有变化
if self.last_d is not None and d != self.last_d:
# 找出变化的位置和数据
changed_positions = []
for i in range(len(d)):
if i < len(self.last_d) and d[i] != self.last_d[i]:
changed_positions.append((i, d[i]))
# print(d)
d_1_str = f"{d[1]:8b}"
# print(f"Joystick - HOTAS Warthog 数据变化: 位置 {[pos for pos, val in changed_positions]}, 值 {[val for pos, val in changed_positions]}")
###正式开始处理数据
if self.dcs_states is not None:
if d[1] == 2 and self.last_d[1] != 2:
self.dcs_states.write_numeric("PLT_BOMB_RELEASE_BTN", 1)
if d[1] != 2 and self.last_d[1] == 2:
self.dcs_states.write_numeric("PLT_BOMB_RELEASE_BTN", 0)
if d[3] == 0 and self.last_d[3] != 0:
self.dcs_states.write_numeric("PLT_PITCH_TRIM", 2)
if d[3] == 64 and self.last_d[3] != 64:
self.dcs_states.write_numeric("PLT_PITCH_TRIM", 1)
if d[3] == 32 and self.last_d[3] != 32:
self.dcs_states.write_numeric("PLT_ROLL_TRIM", 2)
if d[3] == 96 and self.last_d[3] != 96:
self.dcs_states.write_numeric("PLT_ROLL_TRIM", 1)
if d[2] == 2 and self.last_d[2] != 2:
if self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") == 3:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 4)
elif self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") == 4:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 3)
if d[2] == 1 and self.last_d[2] != 1:
#武器选择下移
if self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") is None:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 0)
else:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR",min(max(self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR")-1,0),2))
if d[1] == 64 and self.last_d[1] != 64:
#武器选择上移
if self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") is None:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 1)
else:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", min(self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR")+1,3))
if d[1] == 128 and self.last_d[1] != 128:
if self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") == 3:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 4)
elif self.dcs_states.read_numeric("PLT_WEAPON_SELECTOR") == 4:
self.dcs_states.write_numeric("PLT_WEAPON_SELECTOR", 3)
if d[1] == 33 and self.last_d[1] != 33:
self.dcs_states.write_numeric("PLT_Trigger", 1)
if d[1] != 33 and self.last_d[1] == 33:
self.dcs_states.write_numeric("PLT_Trigger", 0)
if d[1] == 8 and self.last_d[1] != 8:
self.dcs_states.write_numeric("PLT_AUTOPILOT_EMERGENCY_DISENGAGE", 1)
if d[1] != 8 and self.last_d[1] == 8:
self.dcs_states.write_numeric("PLT_AUTOPILOT_EMERGENCY_DISENGAGE", 0)
if d[1] == 4 and self.last_d[1] != 4:
self.dcs_states.write_numeric("NOSEWHEEL_STEER_BTN", 1)
self.dcs_states.write_numeric("PLT_ENGAGE_AUTOPILOT_BTN", 1)
if d[1] != 4 and self.last_d[1] == 4:
self.dcs_states.write_numeric("NOSEWHEEL_STEER_BTN", 0)
self.dcs_states.write_numeric("PLT_ENGAGE_AUTOPILOT_BTN", 0)
if d[3] == 244 and self.last_d[3] != 244:
self.dcs_states.write_numeric("DLC_ENGAGE_BTN", 1)
self.dcs_states.write_numeric("PLT_COUNTERMEASURE_DISPENSE_BTN", 1)
if d[3] != 244 and self.last_d[3] == 244:
self.dcs_states.write_numeric("DLC_ENGAGE_BTN", 0)
self.dcs_states.write_numeric("PLT_COUNTERMEASURE_DISPENSE_BTN", 0)
if d[2] == 64 and self.last_d[2] != 64:
self.dcs_states.write_numeric("DLC_SPOILER_CMD_WHEEL", 1)
self.dcs_states.write_numeric("MANEUVER_FLAP_CMD_WHEEL", 1)
if d[2] != 64 and self.last_d[2] == 64:
self.dcs_states.write_numeric("DLC_SPOILER_CMD_WHEEL", 0)
self.dcs_states.write_numeric("MANEUVER_FLAP_CMD_WHEEL", 0)
if d[3] == 241 and self.last_d[3] != 241:
self.dcs_states.write_numeric("DLC_SPOILER_CMD_WHEEL", 2)
self.dcs_states.write_numeric("MANEUVER_FLAP_CMD_WHEEL", 2)
if d[3] != 241 and self.last_d[3] == 241:
self.dcs_states.write_numeric("DLC_SPOILER_CMD_WHEEL", 0)
self.dcs_states.write_numeric("MANEUVER_FLAP_CMD_WHEEL", 0)
if d[2] == 16 and self.last_d[2] != 16:
self.dcs_states.write_numeric("Target_Designate_SWITCH", 2)
if d[2] != 16 and self.last_d[2] == 16:
self.dcs_states.write_numeric("Target_Designate_SWITCH", 0)
if d[2] == 4 and self.last_d[2] != 4:
self.dcs_states.write_numeric("Target_Designate_SWITCH", 1)
if d[2] != 4 and self.last_d[2] == 4:
self.dcs_states.write_numeric("Target_Designate_SWITCH", 0)
if d[2] == 8 and self.last_d[2] != 8:
self.dcs_states.write_numeric("Target_Designate_SWITCH", 3)
if d[2] != 8 and self.last_d[2] == 8:
self.dcs_states.write_numeric("Target_Designate_SWITCH", 0)
elif self.name == "Joystick - HOTAS Warthog" and False:
error_positions = [4,5,6,7,8,9,10,11]
for pos in error_positions:
d[pos] = 0
# 比较当前数据与上一次数据是否有变化
if self.last_d is not None and d != self.last_d:
# 找出变化的位置和数据
changed_positions = []
for i in range(len(d)):
if i < len(self.last_d) and d[i] != self.last_d[i]:
changed_positions.append((i, d[i]))
# print(d)
# print(f"Joystick - HOTAS Warthog 数据变化: 位置 {[pos for pos, val in changed_positions]}, 值 {[val for pos, val in changed_positions]}")
###正式开始处理数据
if self.dcs_states is not None:
d_1_str = f"{d[1]:8b}"
# HCU_Radar_Direction / RIO_LANTIRN
if d[3] == 0 and self.last_d[3] != 0:
self.dcs_states.write_numeric("HCU_Radar_Direction" if self.flag == 0 else "RIO_LANTIRN_Left_S4_Hat", 1)
if d[3] != 0 and self.last_d[3] == 0:
self.dcs_states.write_numeric("HCU_Radar_Direction" if self.flag == 0 else "RIO_LANTIRN_Left_S4_Hat", 0)
if d[3] == 64 and self.last_d[3] != 64:
self.dcs_states.write_numeric("HCU_Radar_Direction" if self.flag == 0 else "RIO_LANTIRN_Left_S4_Hat", 2)
if d[3] != 64 and self.last_d[3] == 64:
self.dcs_states.write_numeric("HCU_Radar_Direction" if self.flag == 0 else "RIO_LANTIRN_Left_S4_Hat", 0)
if d[3] == 96 and self.last_d[3] != 96:
self.dcs_states.write_numeric("HCU_Radar_Direction" if self.flag == 0 else "RIO_LANTIRN_Left_S4_Hat", 3)
if d[3] != 96 and self.last_d[3] == 96:
self.dcs_states.write_numeric("HCU_Radar_Direction" if self.flag == 0 else "RIO_LANTIRN_Left_S4_Hat", 0)
if d[3] == 32 and self.last_d[3] != 32:
self.dcs_states.write_numeric("HCU_Radar_Direction" if self.flag == 0 else "RIO_LANTIRN_Left_S4_Hat", 4)
if d[3] != 32 and self.last_d[3] == 32:
self.dcs_states.write_numeric("HCU_Radar_Direction" if self.flag == 0 else "RIO_LANTIRN_Left_S4_Hat", 0)
# HCU_Offset / RIO_LANTIRN_Toggle
if d_1_str[6] == '1' and self.last_d_1_str[6] != '1':
self.dcs_states.write_numeric("HCU_Offset" if self.flag == 0 else "RIO_LANTIRN_Toggle_FOV", 1)
if d_1_str[6] != '1' and self.last_d_1_str[6] == '1':
self.dcs_states.write_numeric("HCU_Offset" if self.flag == 0 else "RIO_LANTIRN_Toggle_FOV", 0)
# HCU_MRI / RIO_LANTIRN_Laser_Latched
if d_1_str[3] == '1' and self.last_d_1_str[3] != '1':
self.dcs_states.write_numeric("HCU_MRI" if self.flag == 0 else "RIO_LANTIRN_Laser_Latched", 1)
if d_1_str[3] != '1' and self.last_d_1_str[3] == '1':
self.dcs_states.write_numeric("HCU_MRI" if self.flag == 0 else "RIO_LANTIRN_Laser_Latched", 0)
# HCU_Radar_Other / RIO_LANTIRN_S4_Hat
if d[2] == 4 and self.last_d[2] != 4:
self.dcs_states.write_numeric("HCU_Radar_Other" if self.flag == 0 else "RIO_LANTIRN_S4_Hat", 1)
if d[2] != 4 and self.last_d[2] == 4:
self.dcs_states.write_numeric("HCU_Radar_Other" if self.flag == 0 else "RIO_LANTIRN_S4_Hat", 0)
if d[2] == 16 and self.last_d[2] != 16:
self.dcs_states.write_numeric("HCU_Radar_Other" if self.flag == 0 else "RIO_LANTIRN_S4_Hat", 2)
if d[2] != 16 and self.last_d[2] == 16:
self.dcs_states.write_numeric("HCU_Radar_Other" if self.flag == 0 else "RIO_LANTIRN_S4_Hat", 0)
if d[2] == 32 and self.last_d[2] != 32:
self.dcs_states.write_numeric("HCU_Radar_Other" if self.flag == 0 else "RIO_LANTIRN_S4_Hat", 3)
if d[2] != 32 and self.last_d[2] == 32:
self.dcs_states.write_numeric("HCU_Radar_Other" if self.flag == 0 else "RIO_LANTIRN_S4_Hat", 0)
if d[2] == 8 and self.last_d[2] != 8:
self.dcs_states.write_numeric("HCU_Radar_Other" if self.flag == 0 else "RIO_LANTIRN_S4_Hat", 4)
if d[2] != 8 and self.last_d[2] == 8:
self.dcs_states.write_numeric("HCU_Radar_Other" if self.flag == 0 else "RIO_LANTIRN_S4_Hat", 0)
# RIO_HCU_Zoom
if d_1_str[1] == '1' and self.last_d_1_str[1] != '1':
self.dcs_states.write_numeric("RIO_HCU_Zoom", 1)
if d_1_str[1] != '1' and self.last_d_1_str[1] == '1':
self.dcs_states.write_numeric("RIO_HCU_Zoom", 0)
if d[2] == 1 and self.last_d[2] != 1:
self.dcs_states.write_numeric("RIO_HCU_Zoom", 2)
if d[2] != 1 and self.last_d[2] == 1:
self.dcs_states.write_numeric("RIO_HCU_Zoom", 0)
# RIO_HCU_Trigger / RIO_LANTIRN_Trigger
if d_1_str[7] == '1' and self.last_d_1_str[7] != '1':
self.dcs_states.write_numeric("RIO_HCU_Trigger" if self.flag == 0 else "RIO_LANTIRN_Trigger", 1)
if d_1_str[7] != '1' and self.last_d_1_str[7] == '1':
self.dcs_states.write_numeric("RIO_HCU_Trigger" if self.flag == 0 else "RIO_LANTIRN_Trigger", 0)
if (d_1_str[2] == '1' and d_1_str[7] == '1') and (self.last_d_1_str[2] != '1' ):
self.dcs_states.write_numeric("RIO_HCU_Trigger" if self.flag == 0 else "RIO_LANTIRN_Trigger", 2)
if (d_1_str[2] != '1' and d_1_str[7] != '1') and (self.last_d_1_str[2] == '1' and self.last_d_1_str[7] == '1'):
self.dcs_states.write_numeric("RIO_HCU_Trigger" if self.flag == 0 else "RIO_LANTIRN_Trigger", 0)
# HCD_Mode / RIO_LANTIRN_Mode
if d[2] == 64 and self.last_d[2] != 64:
if self.flag == 0:
self.dcs_states.write_numeric("HCU_Mode", 1)
else:
self.dcs_states.write_numeric("RIO_LANTIRN_Center_Hat_Switch",1)
if d[2] != 64 and self.last_d[2] == 64:
if self.flag == 0:
self.dcs_states.write_numeric("HCU_Mode", 0)
else:
self.dcs_states.write_numeric("RIO_LANTIRN_Center_Hat_Switch",0)
if d[2] == 128 and self.last_d[2] != 128:
if self.flag == 0:
self.dcs_states.write_numeric("HCU_Mode" , 4)
else:
self.dcs_states.write_numeric("RIO_LANTIRN_Slider_AGC/MGC", 1 - (self.dcs_states.read_numeric("RIO_LANTIRN_Slider_AGC/MGC") or 0))
if d[2] != 128 and self.last_d[2] == 128:
if self.flag == 0:
self.dcs_states.write_numeric("HCU_Mode", 0)
if d[3] == 242 and self.last_d[3] != 242:
if self.flag == 0:
self.dcs_states.write_numeric("HCU_Mode", 3)
else:
self.dcs_states.write_numeric("RIO_LANTIRN_Slider_laser/focus", 1 - (self.dcs_states.read_numeric("RIO_LANTIRN_Slider_laser/focus") or 0))
if d[3] != 242 and self.last_d[3] == 242:
if self.flag == 0:
self.dcs_states.write_numeric("HCU_Mode", 0)
if d[3] == 241 and self.last_d[3] != 241:
if self.flag == 0:
self.dcs_states.write_numeric("HCU_Mode", 2)
else:
self.dcs_states.write_numeric("RIO_LANTIRN_Right_S4_Hat",5)
if d[3] != 241 and self.last_d[3] == 241:
if self.flag == 0:
self.dcs_states.write_numeric("HCU_Mode", 0)
else:
self.dcs_states.write_numeric("RIO_LANTIRN_Right_S4_Hat",0)
if d[3] == 244 and self.last_d[3] != 244:
if self.flag == 0:
pass
else:
self.dcs_states.write_numeric("RIO_LANTIRN_MODE_Toggle", 1 - (self.dcs_states.read_numeric("RIO_LANTIRN_MODE_Toggle") or 0))
if d[3] != 244 and self.last_d[3] == 244:
if self.flag == 0:
pass
else:
pass
# HCU_DDD_CURSOR / RIO_LANTIRN_Undesignate
if d_1_str[5] == '1' and self.last_d_1_str[5] != '1':
self.dcs_states.write_numeric("HCU_DDD_CURSOR" if self.flag == 0 else "RIO_LANTIRN_Undesignate", 1)
if d_1_str[5] != '1' and self.last_d_1_str[5] == '1':
self.dcs_states.write_numeric("HCU_DDD_CURSOR" if self.flag == 0 else "RIO_LANTIRN_Undesignate", 0)
# Transform_Control
if d_1_str[4] == '1' and self.last_d_1_str[4] != '1':
self.dcs_states.write_numeric("Transform_Control", 1)
self.flag=1
if d_1_str[4] != '1' and self.last_d_1_str[4] == '1':
self.dcs_states.write_numeric("Transform_Control", 0)
self.flag=0
pass
self.last_d = d
self.last_d_1_str = d_1_str
# 可选:为了避免CPU过度使用,可以在每次循环后短暂休眠
# time.sleep(0.1)
except KeyboardInterrupt:
# 当用户尝试停止脚本时(如按下Ctrl+C),优雅地处理中断
print("\nStopping...")
def close(self):
self.h.close()
print(f"Device: {self.name} closed")
def listen_and_process_hid_data(dcs_states,name):
hotas = Device(dcs_states=dcs_states,name=name)
hotas.read()
class DCS_state:
def write_numeric(self,name,val):
pass
def read_numeric(self,name):
pass
def list_hid_devices():
devices = hid.enumerate() # 返回所有设备的列表
for d in devices:
print("-" * 40)
for key, value in d.items():
print(f"{key}: {value}")
if __name__ == "__main__":
fake_dcs_states = DCS_state()
# tm = Device(dcs_states=fake_dcs_states, name="TWCS Throttle")
# tm = Device(dcs_states=fake_dcs_states, name="T.16000M")
# tm = Device(dcs_states=fake_dcs_states,name="Joystick - HOTAS Warthog")
tm = Device(dcs_states=fake_dcs_states,name="Throttle - HOTAS Warthog")
tm.read()
tm.close()
# list_hid_devices()