-
Notifications
You must be signed in to change notification settings - Fork 0
/
hidapi.py
101 lines (75 loc) · 2.7 KB
/
hidapi.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
from ctypes import *
# Define the structures that belong to hidapi
class hid_device_info(Structure):
pass
hid_device_info._fields_ = \
[
("path", c_char_p),
("vendor_id", c_ushort),
("product_id", c_ushort),
("serial_number", c_wchar_p),
("release_number", c_ushort),
("manufacturer_string", c_wchar_p),
("product_string", c_wchar_p),
("usage_page", c_ushort),
("usage", c_ushort),
("interface_number", c_int),
("next", POINTER(hid_device_info)),
]
class hid_device(Structure):
pass
# Load the hidapp.dll library
pathOfScript = os.path.realpath(__file__)
pathToDll = os.path.dirname(pathOfScript) + "/lib/hidapi.dll"
_loadedDll = CDLL(pathToDll)
# enumerate()
_loadedDll.hid_enumerate.restype = POINTER(hid_device_info)
_loadedDll.hid_enumerate.argtypes = [c_ushort, c_ushort]
# free_enumeration()
_loadedDll.hid_free_enumeration.argtypes = [POINTER(hid_device_info)]
# hid_open()
_loadedDll.hid_open.restype = POINTER(hid_device)
_loadedDll.hid_open.argtypes = [c_ushort,c_ushort,c_wchar_p]
# hid_open_path
_loadedDll.hid_open_path.restype = POINTER(hid_device)
_loadedDll.hid_open_path.argtypes = [c_char_p]
# hid_close
_loadedDll.hid_close.argtypes = [POINTER(hid_device)]
# hid_set_nonblocking
_loadedDll.hid_set_nonblocking.argtypes = [POINTER(hid_device), c_int]
# hid_read
_loadedDll.hid_read.argtypes = [POINTER(hid_device), POINTER(c_ubyte), c_size_t]
# hid_read_timeout
_loadedDll.hid_read_timeout.argtypes = [POINTER(hid_device), POINTER(c_ubyte), c_size_t, c_int]
# hid_write
_loadedDll.hid_write.argtypes = [POINTER(hid_device), POINTER(c_ubyte), c_size_t]
# Define the python API for hidapi
def Init():
_loadedDll.hid_init()
def Shutdown():
_loadedDll.hid_exit()
def Enumerate(vendorId = 0, productId = 0):
return _loadedDll.hid_enumerate(vendorId, productId)
def FreeEnumeration(enumeration):
_loadedDll.hid_free_enumeration(enumeration)
def Open(vendorId, productId, serialNumber = None):
return _loadedDll.hid_open(vendorId, productId, serialNumber);
def OpenPath(path):
return _loadedDll.hid_open_path(path);
def Close(device):
_loadedDll.hid_close(device)
def CreateBuffer(size):
bufferType = c_ubyte * size
return bufferType()
def ZeroBuffer(buffer):
memset(buffer, 0, len(buffer))
def SetNonBlocking(device, nonblocking = True):
_loadedDll.hid_set_nonblocking(device, nonblocking)
def Read(device, buffer):
return _loadedDll.hid_read(device, buffer, len(buffer))
def ReadTimeout(device, buffer, timeout):
return _loadedDll.hid_read_timeout(device, buffer, len(buffer), timeout)
def Write(device, buffer):
towrite = len(buffer) - 1
return _loadedDll.hid_write(device, buffer, towrite)