Maybe I'm looking at this from the wrong angle, but I found code from a Japanese blog that translates the pro controller's gyro controls on switch to a mouse. One problem with this is that the Procontroller still sends it's gyro position through the USB Gadget /dev/hidg0. I was wondering if there was a viable option for being able to take the HIDraw output and map it to a mouse/keyboard without the controller actively sending a signal to through hidg0. Obviously I'll keep poking around until I figure something out, just wondering if anyone else has attempted something like this before
I was able to translate most of the comments in the code and get a basic understanding of what the code is doing, but any time I think I'm onto something the program terminates.
I've found that mouse_int[0] is left middle and right click. mouse_int[2] & mouse_int[3] are the sensor's x and y movements.
so far my search has lead me to a reverse engineering mock up of a Procontroller and lots of fumbling around with HID documentation, granted I think the reverse engineering was most of the work needed.
#assign gadget, procon and mouse to their respective devices
##terminal 'lsusb' to find serial
##ex: Bus 001 Device 021: ID ***057e***:2009 Nintendo Co., Ltd Switch Pro Controller
gadget = os.open('/dev/hidg0', os.O_RDWR | os.O_NONBLOCK) #hidg0 is our device outputting as Procon
procon = os.open('/dev/hidraw3', os.O_RDWR | os.O_NONBLOCK) #terminal: sudo dmesg | grep -A7 057e
mouse = os.open('/dev/hidraw4', os.O_RDWR | os.O_NONBLOCK) #terminal: sudo dmesg | grep -A7 046d
#046d is Logitech's G502 HERO mouse
mouse_int = bytes([0,0,0,0])
'''
mouse_int[0] = left, middle, right clicks
mouse_int[2] = sensor
'''
I've started questioning if I even need to read the mouse as an hidraw and mabye using import keyboard or pyautogui But most of what I'm doing is dissecting what each piece of code does so I can manipulate what I'm wanting
This is the portion of code they used to adjust gyro controls with the mouse sensor
'''something to do with mouse sensor, translated "If you don't put a
lot into sensor you'll run out'''
if mouse_int[1] != 0:
b = 127
else:
b=0
if mouse_int[2] != 0:
b = 127
else:
b = 0
d = bytes([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
ac0 = bytes([255]) if output_data[14] + b > 255 else bytes([output_data[14] + b])
ac1 = bytes([255]) if output_data[16] + b > 255 else bytes([output_data[16] + b])
ac2 = bytes([255]) if output_data[18] + b > 255 else bytes([output_data[18] + b])
ac0_1 = bytes([255]) if output_data[26] + b > 255 else bytes([output_data[26] + b])
ac1_1 = bytes([255]) if output_data[28] + b > 255 else bytes([output_data[28] + b])
ac2_1 = bytes([255]) if output_data[30] + b > 255 else bytes([output_data[30] + b])
ac0_2 = bytes([255]) if output_data[38] + b > 255 else bytes([output_data[38] + b])
ac1_2 = bytes([255]) if output_data[40] + b > 255 else bytes([output_data[40] + b])
ac2_2 = bytes([255]) if output_data[42] + b > 255 else bytes([output_data[42] + b])
#changes mouse movement to gyro
gy0_0 = convert(output_data[19:21], mouse_int[1:2], 250, False)#
gy1_0 = convert(output_data[21:23], mouse_int[2:3], 250, False)#
gy2_0 = convert(output_data[23:25], mouse_int[2:3], 0, False)#
gy0_1 = convert(output_data[31:33], mouse_int[1:2], 250, False)#
gy1_1 = convert(output_data[33:35], mouse_int[2:3], 250, False)#
gy2_1 = convert(output_data[35:37], mouse_int[2:3], 0, False)#
gy0_2 = convert(output_data[43:45], mouse_int[1:2], 250, False)#
gy1_2 = convert(output_data[45:47], mouse_int[2:3], 250, False)#
gy2_2 = convert(output_data[47:49], mouse_int[2:3], 0, False)#
e = a+output_data[13:14]+ac0+output_data[15:16]+ac1+output_data[17:18]+ac2 \
+gy0_0+gy1_0+gy2_0 \
+output_data[25:26]+ac0_1+output_data[27:28]+ac1_1+output_data[29:30]+ac2_1 \
+gy0_1+gy1_1+gy2_1 \
+output_data[37:38]+ac0_2+output_data[39:40]+ac1_2+output_data[41:42]+ac2_2 \
+gy0_2+gy1_2+gy2_2 \
+d
#print(int.from_bytes(gy1_0, byteorder='little'))
#print(mouse_int[1])
return e
This looks to be what's writing to the gadget aka /dev/hidg0. Unfortunately for me it doesn't overwrite the actual controller in any way
def procon_output():
global mouse_int
while True:
try:
output_data = os.read(procon, 128)
#output_mouse = os.read(mouse, 128)
#print('<<<', output_data.hex())
#print(output_data)
e = replace_mouse(output_data, mouse_int)
#print(e.hex())
os.write(gadget, e)#output_data
mouse_int = bytes([0,0,0,0])
except BlockingIOError:
pass
except Exception as g:
print(type(g))
print(g)
os._exit(1)