forked from respeaker/respeaker_python_library
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
80 lines (61 loc) · 2.14 KB
/
test.py
File metadata and controls
80 lines (61 loc) · 2.14 KB
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
import respeaker.usb_hid as usb_hid
class MicArray:
def __init__(self):
self.hid = usb_hid.get()
def write(self, address, data):
data = self.to_bytearray(data)
length = len(data)
if self.hid:
packet = bytearray([address & 0xFF, (address >> 8) & 0x7F, length & 0xFF, (length >> 8) & 0xFF]) + data
packet = list(packet)
self.hid.write(packet)
def read(self, address, length):
self.hid.write(list(bytearray([address & 0xFF, (address >> 8) & 0xFF | 0x80, length & 0xFF, (length >> 8) & 0xFF])))
for _ in range(6):
data = self.hid.read()
# print [int(x) for x in data]
# skip VAD data
if int(data[0]) != 0xFF and int(data[1]) != 0xFF:
return data[4:(4 + length)]
@staticmethod
def to_bytearray(data):
if type(data) is int:
array = bytearray([data & 0xFF])
elif type(data) is bytearray:
array = data
elif type(data) is str:
array = bytearray(data)
elif type(data) is list:
array = bytearray(data)
else:
raise TypeError('%s is not supported' % type(data))
return array
def main():
import sys
import struct
mic = MicArray()
print("Using: %s (hidapiusb)" % usb_hid.usb_backend)
if len(sys.argv) < 3:
print('Usage: python {} w 0x0 0x000003'.format(sys.argv[0]))
sys.exit(1)
try:
if sys.argv[2].startswith('0x'):
address = int(sys.argv[2], 16)
else:
address = int(sys.argv[2])
if sys.argv[1] == 'w':
if sys.argv[3].startswith('0x'):
data = int(sys.argv[3], 16)
else:
data = int(sys.argv[3])
if data > 0xFFFF:
data = struct.pack('<I', data)
elif data > 0xFF:
data = struct.pack('<H', data)
mic.write(address, data)
else:
print [int(x) for x in mic.read(address, 4)]
except Exception as e:
print(e.message)
if __name__ == '__main__':
main()