-
Notifications
You must be signed in to change notification settings - Fork 8
/
usblinux.py
54 lines (46 loc) · 1.87 KB
/
usblinux.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
""" Linux related implementation of USB code """
import usb.core
import usb.util
import drevo.usbadapter as usbadapter
class Usblinux(usbadapter.Usbadapter):
""" Class that implements Linux specific USB handling """
def __init__(self):
dev = usb.core.find(
idVendor=0x0483, # STMicroelectronics
idProduct=0x4010 # Drevo Calibur Keyboard
)
if dev is None:
# Device not present, or user is not allowed to access device.
raise ValueError(
"Keyboard not present or insufficient permissions")
self.reattach = False
if dev.is_kernel_driver_active(self.rgbinterface):
self.reattach = True
dev.detach_kernel_driver(self.rgbinterface)
conf = dev.get_active_configuration()
interface = conf[(self.rgbinterface, 0)]
self.dev = dev
writergbendpoint = usb.util.find_descriptor(
interface,
bEndpointAddress=0x1
)
if writergbendpoint is None:
raise ValueError("Endpoint for writing not found")
self.writergbendpoint = writergbendpoint
self.write = writergbendpoint.write
readrgbendpoint = usb.util.find_descriptor(
interface,
bEndpointAddress=0x84
)
if readrgbendpoint is None:
raise ValueError("Endpoint for reading not found")
self.readrgbendpoint = readrgbendpoint
self.read = readrgbendpoint.read
def __del__(self):
# This is needed to release interface, otherwise attach_kernel_driver fails
# due to "Resource busy"
usb.util.dispose_resources(self.dev)
# In theory you don't even need to reattach the kernel.
# The kernel does not use this part of the device
if self.reattach:
self.dev.attach_kernel_driver(self.rgbinterface)