Skip to content

Instantly share code, notes, and snippets.

@senid231
Forked from pieromarini/nari_ultimate_testing.py
Created September 5, 2022 12:13
Show Gist options
  • Save senid231/54cf4fd8822d2cbeea49465714be4f83 to your computer and use it in GitHub Desktop.
Save senid231/54cf4fd8822d2cbeea49465714be4f83 to your computer and use it in GitHub Desktop.
Razer Nari Ultimate Testing
import usb.core
import usb.util
import usb.control
import time
import decimal
INTERFACE_NUMBER = 5
TIMEOUT = 2000
reattach = False
# NOTE: this part looks like it's static. The color bytes are inserted after this chunk of data.
header = b'\xFF\x0A\x00\xFF\x04\x12\xF1\x05\x72'
# NOTE: looks static too. (for changing colors)
rest = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
# Util functions
def clamp(x):
return max(0, min(x, 255))
def rgb_to_hex(color):
r, g, b = color
return "{0:02x}{1:02x}{2:02x}".format(clamp(r), clamp(g), clamp(b))
# NOTE: lerp function returning a list of colors.
def create_intermediate_colors(color1, color2, steps):
output = []
r1, g1, b1 = color1
r2, g2, b2 = color2
rdelta = (r2-r1) / steps
gdelta = (g2-g1) / steps
bdelta = (b2-b1) / steps
for _ in range(steps):
r1 += rdelta
g1 += gdelta
b1 += bdelta
output.append((int(r1), int(g1), int(b1)))
return output
def get_device():
global reattach
# NOTE: found from output of lsusb
dev = usb.core.find(idVendor=0x1532, idProduct=0x051a)
if dev is None:
raise ValueError('Device not found')
if dev.is_kernel_driver_active(INTERFACE_NUMBER):
reattach = True
dev.detach_kernel_driver(INTERFACE_NUMBER)
return dev
def send_message(device, color):
packet = header + color + rest
# NOTE: Control transfer parameters extracted from packet captures.
bytes_sent = device.ctrl_transfer(0x21, 9, 0x03ff, 5, packet)
assert(bytes_sent == len(packet))
time.sleep(0.08) # NOTE: very arbitrary, just from testing to achieve a "transition".
def cleanup(device):
global reattach
usb.util.dispose_resources(device)
if reattach:
device.attach_kernel_driver(INTERFACE_NUMBER)
dev = get_device()
for color in create_intermediate_colors((0, 255, 0), (255, 0, 0), 40):
hex_color = rgb_to_hex(color)
bytes_color = bytes(bytearray.fromhex(hex_color))
send_message(dev, bytes_color)
cleanup(dev)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment