Repository navigation
Expand file tree
/
Copy pathhidsensor.py
More file actions
195 lines (156 loc) · 5.98 KB
/
Copy pathhidsensor.py
File metadata and controls
195 lines (156 loc) · 5.98 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
"""
Generic interface to any USB HID sensor.
Copyright (C) Sarah Mount 2008.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have rceeived a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import struct
import hid
import os
import sys
__date__ = 'November 2008'
__version__ = '0.1'
#DEBUG = True
DEBUG = False
class HIDError(Exception):
def __init__(self, value):
"""Wrapper for exceptions resulting from calls to libhid.
value should be (<method-name>, <hid-retval>).
"""
super(HIDError, self).__init__()
self.value = value
return
def __str__(self):
return 'Call to %s failed with return code %d' % self.value
class HIDMatcher(object):
def __init__(self, vid, pid, errstream=sys.stderr):
self.matcher = hid.HIDInterfaceMatcher()
self.matcher.vendor_id = vid
self.matcher.product_id = pid
return
@property
def vendor(self):
return self.matcher.vendor_id
@property
def product(self):
return self.matcher.product_id
class HIDSensor(object):
def __init__(self, id=None, errstream=sys.stderr):
self._id = None
self._error = errstream
# Debugging
if DEBUG:
hid.hid_set_debug(hid.HID_DEBUG_ALL)
hid.hid_set_debug_stream(self._error)
hid.hid_set_usb_debug(0)
# Initialisation
if not hid.hid_is_initialised(): # Doesn't seem to work
try: # Belt AND braces, Sir?
hid.hid_init()
except HIDError:
pass
self._interface = hid.hid_new_HIDInterface()
# Ensure we only use one HIDMatcher class per
# type of sensor, with some reflection-fu.
#if not 'MATCHER' in self.__class__.__dict__:
if not hasattr(self.__class__, 'MATCHER'):
self.__class__.MATCHER = HIDMatcher(self.__class__.VID,
self.__class__.PID)
return
def open(self):
self._check(hid.hid_force_open(self._interface,
0,
self.__class__.MATCHER.matcher,
3),
'hid_force_open')
self._id = self._interface.id
with os.tmpfile() as tmpfile:
hid.hid_write_identification(tmpfile, self._interface)
tmpfile.flush()
tmpfile.seek(0)
details = tmpfile.read() + '\n'
return details
def __del__(self): # Destructor
# try:
### WHY IS THIS BROKE?
if self._interface is None: return
# elif hid.hid_is_opened(self._interface):
# hid.hid_delete_HIDInterface(self._interface)
# else:
# print 'Closing interface %s' % self._id
# self._check(hid.hid_close(self._interface), 'hid_close')
return
# finally:
# super(HIDSensor, self).__del__()
def interrupt_read(self, endpoint, size, timeout):
ret, bytes = hid.hid_interrupt_read(self._interface,
endpoint, size, timeout)
self._check(ret, 'hid_interrupt_read')
return bytes
def _check(self, retval, method, success=hid.HID_RET_SUCCESS):
if retval != success:
raise HIDError((method, retval))
@property
def vendor(self):
return self.__class__.MATCHER._vendor
@property
def product(self):
return self.__class__.MATCHER._product
@property
def id(self):
return self._id
def set_feature_report(self, path, buffer):
self._check(hid.hid_set_feature_report(self._interface, path, buffer),
'set_feature_report')
return
def get_feature_report(self, path, size):
reply = hid.hid_get_feature_report(self._interface, path, size)
#self._check(ret, 'get_feature_report')
return reply
class HIDSensorCollection:
"""Interface to all attached sensors of known types."""
MAX_HID_DEVS = 20 # Limited by libhid
def __init__(self, hidclasses):
self._hidtypes = {}
for hidclass in hidclasses:
print('Searching for %s sensors...' % hidclass.__name__)
self._hidtypes[hidclass] = HIDMatcher(hidclass.VID, hidclass.PID)
self._interfaces = {}
retval = hid.hid_init()
if retval != hid.HID_RET_SUCCESS:
raise HIDError(('hid_init', retval))
return
def __del__(self):
return
def open(self):
for hidclass in self._hidtypes:
for i in range(HIDSensorCollection.MAX_HID_DEVS):
try:
hidif = hidclass()
details = hidif.open()
if details:
print('Found HID sensor: %s' % hidif._id)
self._interfaces[hidif._id] = hidif
except HIDError as e:
del hidif
#except Error, e:
#continue
return
def get_all_data(self):
return [hidif.get_data() for hidif in list(self._interfaces.values())]
def _debug(self):
print(len(self._interfaces), 'HID sensors attached')
for hidif in self._interfaces:
print('Interface: %s,' % hidif, end=' ')
print(self._interfaces[hidif]._debug_str() % \
self._interfaces[hidif].get_data())