Skip to content

Commit 0c20445

Browse files
fallensbourdeauducq
authored andcommittedDec 1, 2014
lda: allow to simulate without needing hidapi
This also fixes some old style string formating
1 parent c591f1a commit 0c20445

File tree

2 files changed

+67
-62
lines changed

2 files changed

+67
-62
lines changed
 

‎artiq/devices/lda/hidapi.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import os
2+
import atexit
3+
import ctypes
4+
import ctypes.util
5+
6+
if "." not in os.environ["PATH"].split(";"):
7+
os.environ["PATH"] += ";."
8+
dir = os.path.split(__file__)[0]
9+
if dir not in os.environ["PATH"].split(";"):
10+
os.environ["PATH"] += ";{}".format(dir)
11+
12+
for n in "hidapi-libusb hidapi-hidraw hidapi".split():
13+
path = ctypes.util.find_library(n)
14+
if path:
15+
break
16+
if not path:
17+
raise ImportError("no hidapi library found")
18+
hidapi = ctypes.CDLL(path)
19+
20+
21+
class HidDeviceInfo(ctypes.Structure):
22+
pass
23+
24+
25+
HidDeviceInfo._fields_ = [
26+
("path", ctypes.c_char_p),
27+
("vendor_id", ctypes.c_ushort),
28+
("product_id", ctypes.c_ushort),
29+
("serial", ctypes.c_wchar_p),
30+
("release", ctypes.c_ushort),
31+
("manufacturer", ctypes.c_wchar_p),
32+
("product", ctypes.c_wchar_p),
33+
("usage_page", ctypes.c_ushort),
34+
("usage", ctypes.c_ushort),
35+
("interface", ctypes.c_int),
36+
("next", ctypes.POINTER(HidDeviceInfo)),
37+
]
38+
39+
40+
hidapi.hid_enumerate.argtypes = [ctypes.c_ushort, ctypes.c_ushort]
41+
hidapi.hid_enumerate.restype = ctypes.POINTER(HidDeviceInfo)
42+
hidapi.hid_free_enumeration.argtypes = [ctypes.POINTER(HidDeviceInfo)]
43+
hidapi.hid_open.argtypes = [ctypes.c_ushort, ctypes.c_ushort,
44+
ctypes.c_wchar_p]
45+
hidapi.hid_open.restype = ctypes.c_void_p
46+
hidapi.hid_read_timeout.argtypes = [ctypes.c_void_p, ctypes.c_char_p,
47+
ctypes.c_size_t, ctypes.c_int]
48+
hidapi.hid_read.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_size_t]
49+
hidapi.hid_write.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_size_t]
50+
hidapi.hid_send_feature_report.argtypes = [ctypes.c_void_p, ctypes.c_char_p,
51+
ctypes.c_size_t]
52+
hidapi.hid_get_feature_report.argtypes = [ctypes.c_void_p, ctypes.c_char_p,
53+
ctypes.c_size_t]
54+
hidapi.hid_error.argtypes = [ctypes.c_void_p]
55+
hidapi.hid_error.restype = ctypes.c_wchar_p
56+
57+
atexit.register(hidapi.hid_exit)

‎artiq/devices/lda/lda_controller.py

+10-62
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,15 @@
11
#!/usr/bin/env python3
22

33
import argparse
4-
import os
54
import logging
6-
import atexit
75
import ctypes
8-
import ctypes.util
96
import struct
107

118
from artiq.management.pc_rpc import simple_server_loop
129

1310

1411
logger = logging.getLogger(__name__)
1512

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-
6913

7014
class HidError(Exception):
7115
pass
@@ -127,15 +71,18 @@ def __init__(self, serial=None, product="LDA-102"):
12771
12872
"""
12973

74+
from artiq.devices.lda.hidapi import hidapi
75+
self.hidapi = hidapi
13076
self.product = product
13177
if serial is None:
13278
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)
13581
assert self._dev
13682

13783
@classmethod
13884
def enumerate(cls, product):
85+
from artiq.devices.lda.hidapi import hidapi
13986
devs = hidapi.hid_enumerate(cls._vendor_id,
14087
cls._product_ids[product])
14188
try:
@@ -148,8 +95,8 @@ def enumerate(cls, product):
14895

14996
def _check_error(self, ret):
15097
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))
153100
return ret
154101

155102
def write(self, command, length, data=bytes()):
@@ -162,7 +109,8 @@ def write(self, command, length, data=bytes()):
162109
"""
163110
# 0 is report id/padding
164111
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)))
166114
assert res == len(buf), res
167115

168116
def set(self, command, data):
@@ -191,7 +139,7 @@ def get(self, command, length, timeout=1000):
191139
self.write(command, length)
192140
buf = ctypes.create_string_buffer(8)
193141
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,
195143
buf, len(buf), timeout))
196144
assert res == len(buf), res
197145
status, length, data = struct.unpack("BB6s", buf.raw)

0 commit comments

Comments
 (0)
Please sign in to comment.