1
1
#!/usr/bin/env python3
2
2
3
3
import argparse
4
- import os
5
4
import logging
6
- import atexit
7
5
import ctypes
8
- import ctypes .util
9
6
import struct
10
7
11
8
from artiq .management .pc_rpc import simple_server_loop
12
9
13
10
14
11
logger = logging .getLogger (__name__ )
15
12
16
- if "." not in os .environ ["PATH" ].split (";" ):
17
- os .environ ["PATH" ] += ";."
18
- dir = os .path .split (__file__ )[0 ]
19
- if dir not in os .environ ["PATH" ].split (";" ):
20
- os .environ ["PATH" ] += ";%s" % dir
21
-
22
- for n in "hidapi-libusb hidapi-hidraw hidapi" .split ():
23
- path = ctypes .util .find_library (n )
24
- if path :
25
- break
26
- if not path :
27
- raise ImportError ("no hidapi library found" )
28
- hidapi = ctypes .CDLL (path )
29
-
30
-
31
- class HidDeviceInfo (ctypes .Structure ):
32
- pass
33
-
34
-
35
- HidDeviceInfo ._fields_ = [
36
- ("path" , ctypes .c_char_p ),
37
- ("vendor_id" , ctypes .c_ushort ),
38
- ("product_id" , ctypes .c_ushort ),
39
- ("serial" , ctypes .c_wchar_p ),
40
- ("release" , ctypes .c_ushort ),
41
- ("manufacturer" , ctypes .c_wchar_p ),
42
- ("product" , ctypes .c_wchar_p ),
43
- ("usage_page" , ctypes .c_ushort ),
44
- ("usage" , ctypes .c_ushort ),
45
- ("interface" , ctypes .c_int ),
46
- ("next" , ctypes .POINTER (HidDeviceInfo )),
47
- ]
48
-
49
-
50
- hidapi .hid_enumerate .argtypes = [ctypes .c_ushort , ctypes .c_ushort ]
51
- hidapi .hid_enumerate .restype = ctypes .POINTER (HidDeviceInfo )
52
- hidapi .hid_free_enumeration .argtypes = [ctypes .POINTER (HidDeviceInfo )]
53
- hidapi .hid_open .argtypes = [ctypes .c_ushort , ctypes .c_ushort ,
54
- ctypes .c_wchar_p ]
55
- hidapi .hid_open .restype = ctypes .c_void_p
56
- hidapi .hid_read_timeout .argtypes = [ctypes .c_void_p , ctypes .c_char_p ,
57
- ctypes .c_size_t , ctypes .c_int ]
58
- hidapi .hid_read .argtypes = [ctypes .c_void_p , ctypes .c_char_p , ctypes .c_size_t ]
59
- hidapi .hid_write .argtypes = [ctypes .c_void_p , ctypes .c_char_p , ctypes .c_size_t ]
60
- hidapi .hid_send_feature_report .argtypes = [ctypes .c_void_p , ctypes .c_char_p ,
61
- ctypes .c_size_t ]
62
- hidapi .hid_get_feature_report .argtypes = [ctypes .c_void_p , ctypes .c_char_p ,
63
- ctypes .c_size_t ]
64
- hidapi .hid_error .argtypes = [ctypes .c_void_p ]
65
- hidapi .hid_error .restype = ctypes .c_wchar_p
66
-
67
- atexit .register (hidapi .hid_exit )
68
-
69
13
70
14
class HidError (Exception ):
71
15
pass
@@ -127,15 +71,18 @@ def __init__(self, serial=None, product="LDA-102"):
127
71
128
72
"""
129
73
74
+ from artiq .devices .lda .hidapi import hidapi
75
+ self .hidapi = hidapi
130
76
self .product = product
131
77
if serial is None :
132
78
serial = next (self .enumerate (product ))
133
- self ._dev = hidapi .hid_open (self ._vendor_id ,
134
- self ._product_ids [product ], serial )
79
+ self ._dev = self . hidapi .hid_open (self ._vendor_id ,
80
+ self ._product_ids [product ], serial )
135
81
assert self ._dev
136
82
137
83
@classmethod
138
84
def enumerate (cls , product ):
85
+ from artiq .devices .lda .hidapi import hidapi
139
86
devs = hidapi .hid_enumerate (cls ._vendor_id ,
140
87
cls ._product_ids [product ])
141
88
try :
@@ -148,8 +95,8 @@ def enumerate(cls, product):
148
95
149
96
def _check_error (self , ret ):
150
97
if ret < 0 :
151
- err = hidapi .hid_error (self ._dev )
152
- raise HidError ("%s: %s" % (ret , err ))
98
+ err = self . hidapi .hid_error (self ._dev )
99
+ raise HidError ("{}: {}" . format (ret , err ))
153
100
return ret
154
101
155
102
def write (self , command , length , data = bytes ()):
@@ -162,7 +109,8 @@ def write(self, command, length, data=bytes()):
162
109
"""
163
110
# 0 is report id/padding
164
111
buf = struct .pack ("BBB6s" , 0 , command , length , data )
165
- res = self ._check_error (hidapi .hid_write (self ._dev , buf , len (buf )))
112
+ res = self ._check_error (self .hidapi .hid_write (self ._dev , buf ,
113
+ len (buf )))
166
114
assert res == len (buf ), res
167
115
168
116
def set (self , command , data ):
@@ -191,7 +139,7 @@ def get(self, command, length, timeout=1000):
191
139
self .write (command , length )
192
140
buf = ctypes .create_string_buffer (8 )
193
141
while status != command :
194
- res = self ._check_error (hidapi .hid_read_timeout (self ._dev ,
142
+ res = self ._check_error (self . hidapi .hid_read_timeout (self ._dev ,
195
143
buf , len (buf ), timeout ))
196
144
assert res == len (buf ), res
197
145
status , length , data = struct .unpack ("BB6s" , buf .raw )
0 commit comments