Skip to content

Instantly share code, notes, and snippets.

@hansemro
Created September 9, 2022 21:46
Show Gist options
  • Save hansemro/acdb861d4bb9c5b52bac57d4dbf39601 to your computer and use it in GitHub Desktop.
Save hansemro/acdb861d4bb9c5b52bac57d4dbf39601 to your computer and use it in GitHub Desktop.
CM Pro S RGB usb com test
#!/usr/bin/env python3
import usb.core
import usb.util
def send_recv_cmd(dev, cmd):
"""
Write USB command over endpoint 4 and return response from endpoint 3 as an
array.
"""
try:
dev.write(4, cmd)
except:
pass
try:
out = dev.read(0x83, 64)
except:
return []
#out = "".join(chr(x) for x in out)
return out
def send_cmd(dev, cmd):
"""
Write USB command over endpoint 4.
"""
try:
dev.write(4, cmd)
except:
pass
def get_status_report(dev):
"""
get_status_report of commands sent to dev.
dev maintains a 64-byte buffer that clears when status report is read.
This function returns empty array if status report buffer is empty.
Known statuses:
79 (0x4f) : success
70 (0x46) : fail
0 : empty
"""
try:
return dev.ctrl_transfer(
bmRequestType=0xa1,
bRequest=0x01,
wValue=0x0100,
wIndex=1,
data_or_wLength=64,
timeout=5)
except:
return []
# try FW device
dev = usb.core.find(idVendor=0x2516, idProduct=0x003c)
if dev is None:
# try USB-HID device
dev = usb.core.find(idVendor=0x2516, idProduct=0x003d)
if dev is None:
exit(1)
if dev is None:
raise ValueError('CM Pro not found')
else:
print("CM Pro found")
if dev.is_kernel_driver_active(1):
try:
dev.detach_kernel_driver(1)
print("kernel driver detached")
except usb.core.USBERROR as e:
print(f"Could not detach kernel driver: {e}")
exit(1)
try:
usb.util.claim_interface(dev, 1)
print("Claimed usb device")
except Exception as e:
print(f"{e}")
exit(1)
ver_req1 = [ 0x01,0x02 ]
#cmd = [ 0x04,0x00 ] # reboot to alt application
ver = send_recv_cmd(dev, ver_req1)
print("".join(chr(x) for x in ver))
for i in range(15):
ver = send_recv_cmd(dev, ver_req1)
status = get_status_report(dev)
print(status)
print(type(status))
print(len(status))
usb.util.dispose_resources(dev)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment