-
Notifications
You must be signed in to change notification settings - Fork 0
/
HID.py
127 lines (97 loc) · 2.98 KB
/
HID.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import hid
import BTCommands
# USB Parameters
VID = 0x03EB
PID = 0x2678
# HID Report IDs
ID_IN_DATA = 0x01
ID_OUT_DATA = 0x02
# HID Report Sizes
SIZE_IN_DATA = 64
SIZE_OUT_DATA = 64
SIZE_MAX_WRITE = 62
SIZE_MAX_READ = 62
def GetNumHidDevices():
return len(hid.enumerate(VID, PID))
def GetMaxReportRequest(device):
return SIZE_OUT_DATA
def GetHidString(deviceIndex):
count = 0
for device in hid.enumerate(VID, PID):
if count == deviceIndex:
return device["path"]
else:
count += 1
return ""
def Open():
handle = hid.Device(VID, PID)
handle.read(64, 500)
return handle
def Close(handle):
handle.close()
return True
def IsOpen(handle):
return True
def TransmitData(handle, buffer):
success = False
reportSize = SIZE_OUT_DATA
report = bytes(reportSize)
bytesWritten = 0
bytesToWrite = len(buffer)
while bytesWritten < bytesToWrite:
transferSize = min(bytesToWrite - bytesWritten, SIZE_MAX_WRITE)
report = bytearray(reportSize)
report[0] = ID_OUT_DATA
report[1] = transferSize
report[2:2+transferSize] = buffer[bytesWritten:bytesWritten+transferSize]
sent = handle.write(bytes(report))
if sent != len(report):
success = False
break
bytesWritten += transferSize
# Write completed successfully
if bytesWritten == bytesToWrite:
success = True
return success
def ReceiveData(handle, bufferSize, timeout):
try:
success = False
buffer = bytearray(bufferSize)
buffer = handle.read(bufferSize, timeout)
success = True
except Exception as err:
success = False
return success, bytes(buffer)
def main():
# Module unit testing
handle = 0
NumOfConnectedDevices = GetNumHidDevices()
print("CS710S USB library loaded")
print("Number of USB device(s) connected: ", NumOfConnectedDevices)
for i in range(NumOfConnectedDevices):
print("Device {}: HIDString={}".format(i, GetHidString(i)))
if NumOfConnectedDevices > 0:
handle = Open(0)
if handle != None:
print("Device 0 opened: {}".format(handle))
else:
print("Unable to open device 0")
return
print("Get BT firmware version")
command = BTCommands.GetVersion()
if not TransmitData(handle, command):
print("Device failed to transmit data.")
else:
bufferSize = GetMaxReportRequest(handle) * SIZE_MAX_READ
status, result = ReceiveData(handle, bufferSize)
if not status:
print("Device failed to receive data.")
return
else:
print("Received Data: {}".format(result.hex().upper()))
if Close(handle):
print("Device 0 closed")
else:
print("Unable to close device 0")
if __name__ == '__main__':
main()