data conversion refactoring

This commit is contained in:
Smasty 2018-02-01 22:44:28 +01:00
parent f3dbe5f3c2
commit f1ab2b29bd
No known key found for this signature in database
GPG Key ID: 9F7DB39EC36B10EA

View File

@ -9,6 +9,7 @@ import sys
import usb.core
import usb.util
import re
import binascii
g203_vendor_id = 0x046d
g203_product_id = 0xc084
@ -18,7 +19,7 @@ default_brightness = 100
dev = None
intf = None
wIndex = None
def help():
@ -101,37 +102,37 @@ def set_led_cycle(rate, brightness):
def set_led(mode, data):
global dev
global wIndex
prefix = '11ff0e3b00'
suffix = '000000000000'
data = prefix + mode + data + suffix
data = [ int(''.join([data[i], data[i+1]]), base=16) for i in range(0, len(data), 2)]
attach_mouse()
dev.ctrl_transfer(0x21, 0x09, 0x0211, 0x01, data)
dev.ctrl_transfer(0x21, 0x09, 0x0211, wIndex, binascii.unhexlify(data))
detach_mouse()
def attach_mouse():
global dev
global intf
global wIndex
dev = usb.core.find(idVendor=g203_vendor_id, idProduct=g203_product_id)
if dev is None:
print_error('Device {:04x}:{:04x} not found.'.format(g203_vendor_id, g203_product_id))
intf = 1
if dev.is_kernel_driver_active(intf) is True:
dev.detach_kernel_driver(intf)
usb.util.claim_interface(dev, intf)
wIndex = 0x01
if dev.is_kernel_driver_active(wIndex) is True:
dev.detach_kernel_driver(wIndex)
usb.util.claim_interface(dev, wIndex)
def detach_mouse():
global dev
global intf
if intf is not None:
usb.util.release_interface(dev, intf)
dev.attach_kernel_driver(intf)
global wIndex
if wIndex is not None:
usb.util.release_interface(dev, wIndex)
dev.attach_kernel_driver(wIndex)
dev = None
intf = None
wIndex = None
if __name__ == '__main__':