-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemsusb2.py
192 lines (167 loc) · 5.44 KB
/
emsusb2.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
"""
Show all HID devices information
HID\VID_0B43&PID_0003&REV_1912&Col01
HID\VID_0B43&PID_0003&REV_1912&Col02
pip install python-uinput
pip install pywinusb
#JOY1
Joy1_16 - Left
Joy1_14 - Right
Joy1_13 - Up
Joy1_15 - Down
Joy1_3 - Up-Left
Joy1_2 - Up-Right
Joy1_10 - Start
Joy1_9 - Select
u'HID\\VID_0B43&PID_0003&COL01\\7&10F50921&4&0000'
"""
"""
Handling raw data inputs example
"""
from msvcrt import kbhit
from time import sleep
import pywinusb.hid as hid
import win32api
import win32com.client
import win32gui
import win32process
import re
def sample_handler(data):
signal = data.data
#print(signal)
if signal[1] != 0 or signal[2] != 0:
pressed = []
#print("Button!")
joy = signal[0]
if signal[1] != 0:
pressed.extend(angle_dirs(signal[1]))
if signal[2] != 0:
pressed.extend(card_dirs(signal[2]))
#print (pressed)
button_mapper(joy, pressed)
#print("Raw data: {0}".format(data))
def card_dirs(value):
dirval = {128:'left', 64:'down', 32:'right', 16:'up', 2:'start', 1:'select' }
pressed = []
for key in reversed(sorted(dirval.iterkeys())):
if value >= key:
pressed.append(dirval[key])
value -= key
return pressed
def angle_dirs(value):
dirval = {8:'dnright', 4:'upleft', 2:'upright', 1:'dnleft' }
pressed = []
for key in reversed(sorted(dirval.iterkeys())):
if value >= key:
pressed.append(dirval[key])
value -= key
return pressed
def button_mapper(joy, pressed):
# do I want to hard code this???
PAD1_CODE = {'start':0x0D, #'enter'
'select':0x1B, #'esc'
'left':0x25, #'left_arrow'
'up':0x26, #'up arrow'
'right':0x27, #right arrow
'down':0x28, #down arrow
'upleft':0x41, # 'a'
'upright':0x42, # 'b'
'dnleft':0x43, #'c'
'dnright':0x44} #'d'
PAD2_CODE = {'start':0xDB, # '['
'select':0xDD, #']'
#'numpad_0':0x60,
'dnleft':0x61, # 'numpad_1'
'down':0x62, # 'numpad_2'
'dnright':0x63, # 'numpad_3'
'left':0x64, # 'numpad_4'
#'numpad_5':0x65, # 'numpad_5'
'right':0x66, # 'numpad_6'
'upleft':0x67, # 'numpad_7'
'up':0x68, # 'numpad_8'
'upright':0x69} # 'numpad_9'
mapping = [PAD1_CODE, PAD2_CODE]
tmp = 1
for val in pressed:
shell = win32com.client.Dispatch("WScript.Shell")
tmp = 1
#win32api.keybd_event(mapping[joy][val],0,0,0)
#sleep(.05)
#win32api.keybd_event(mapping[joy][val],0 ,win32con.KEYEVENTF_KEYUP ,0)
def detect_device():
all_hids = hid.find_all_hid_devices()
pads = []
for hids in all_hids:
tmp = 1
matchObj = re.match(r'HID\\VID_0B43&PID_0003', hids.instance_id, re.M)
if matchObj:
pads.append(hids)
return pads
def pad_init():
pads = detect_device()
if pads:
#pads[1].open()
#pads[1].set_raw_data_handler(sample_handler)
for pad in pads:
pad.open()
pad.set_raw_data_handler(sample_handler)
print("\nWaiting for data...\nPress any (system keyboard) key to stop...")
#while not kbhit() and pads[0].is_plugged():
while True: #not kbhit(): #and device.is_plugged()
#just keep the device opened to receive events
#if kbhit():
# sleep(0.5)
# print ("Pausing...")
sleep(0.0001)
return
else:
print "No EMS USB2 device detected. Exiting"
sys.exit(1)
def raw_test():
detect_device()
all_hids = hid.find_all_hid_devices()
if all_hids:
while True:
print("Choose a device to monitor raw input reports:\n")
print("0 => Exit")
for index, device in enumerate(all_hids):
device_name = unicode("{0.vendor_name} {0.product_name}" \
"(vID=0x{1:04x}, pID=0x{2:04x})"\
"".format(device, device.vendor_id, device.product_id))
print("{0} => {1}".format(index+1, device_name))
print("\n\tDevice ('0' to '%d', '0' to exit?) " \
"[press enter after number]:" % len(all_hids))
index_option = raw_input()
if index_option.isdigit() and int(index_option) <= len(all_hids):
# invalid
break;
int_option = int(index_option)
if int_option:
device = all_hids[int_option-1]
try:
device.open()
#set custom raw data handler
device.set_raw_data_handler(sample_handler)
print("\nWaiting for data...\nPress any (system keyboard) key to stop...")
while not kbhit() and device.is_plugged():
#just keep the device opened to receive events
sleep(0.5)
return
finally:
device.close()
else:
print("There's not any non system HID class device available")
#
if __name__ == '__main__':
# first be kind with local encodings
import sys
if sys.version_info >= (3,):
# as is, don't handle unicodes
unicode = str
raw_input = input
else:
# allow to show encoded strings
import codecs
sys.stdout = codecs.getwriter('mbcs')(sys.stdout)
pad_init()
#raw_test()