-
Notifications
You must be signed in to change notification settings - Fork 0
/
voc.py
64 lines (47 loc) · 1.79 KB
/
voc.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
import hidapi
class Voc():
def __init__(self):
self.seq1 = 0x0001
self.seq2 = 0x0068
self.handle = None
# Setup Device
hidapi.hid_init()
for dev in hidapi.hid_enumerate(vendor_id=0x03eb, product_id=0x2013):
self.handle = hidapi.hid_open_path(dev.path)
self.tx_type1(self.handle, '*IDN?')
self.info = self.rx(self.handle)
def version(self):
import array
return str("" + array.array('B', self.info).tostring() + "")
def shutdown(self):
hidapi.hid_close(self.handle)
hidapi.hid_exit()
def to_bytes(self, n, length, endianess='big'):
h = '%x' % n
s = ('0' * (len(h) % 2) + h).zfill(length * 2).decode('hex')
return s if endianess == 'big' else s[::-1]
def from_bytes(self, bytes, byteorder='little'):
if byteorder == 'little':
little_ordered = list(bytes)
elif byteorder == 'big':
little_ordered = list(reversed(bytes))
n = sum(little_ordered[i] << i * 8 for i in range(len(little_ordered)))
return n
def tx_type1(self, handle, msg):
msg = bytes('@{:04X}{}\n@@@@@@@@@@'.format(self.seq1, msg))
hidapi.hid_write(handle, msg)
def tx_type2(self, handle, msg):
msg = bytes('@') + self.to_bytes(self.seq2, 1) + bytes('{}\n@@@@@@@@@@@@@'.format(msg))
hidapi.hid_write(handle, msg)
def rx(self, handle, numBytes=0x10):
in_data = bytes()
while True:
ret = hidapi.hid_read_timeout(handle, numBytes, 1000)
if len(ret) == 0:
break
in_data += ret
return in_data
def getPpm(self):
self.tx_type2(self.handle, '*TR')
data = self.rx(self.handle)
return self.from_bytes(data[2:4])