hid 1.0.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hid/__init__.py ADDED
@@ -0,0 +1,266 @@
1
+ import os
2
+ import ctypes
3
+ import atexit
4
+ import enum
5
+
6
+ __all__ = ['HIDException', 'DeviceInfo', 'Device', 'enumerate', 'BusType']
7
+
8
+
9
+ hidapi = None
10
+ library_paths = (
11
+ 'libhidapi-hidraw.so',
12
+ 'libhidapi-hidraw.so.0',
13
+ 'libhidapi-libusb.so',
14
+ 'libhidapi-libusb.so.0',
15
+ 'libhidapi-iohidmanager.so',
16
+ 'libhidapi-iohidmanager.so.0',
17
+ 'libhidapi.dylib',
18
+ 'hidapi.dll',
19
+ 'libhidapi-0.dll'
20
+ )
21
+
22
+ for lib in library_paths:
23
+ try:
24
+ hidapi = ctypes.cdll.LoadLibrary(lib)
25
+ break
26
+ except OSError:
27
+ pass
28
+ else:
29
+ error = "Unable to load any of the following libraries:{}"\
30
+ .format(' '.join(library_paths))
31
+ raise ImportError(error)
32
+
33
+
34
+ hidapi.hid_init()
35
+ atexit.register(hidapi.hid_exit)
36
+
37
+
38
+ class HIDException(Exception):
39
+ pass
40
+
41
+ class APIVersion(ctypes.Structure):
42
+ _fields_ = [
43
+ ('major', ctypes.c_int),
44
+ ('minor', ctypes.c_int),
45
+ ('patch', ctypes.c_int),
46
+ ]
47
+
48
+ try:
49
+ hidapi.hid_version.argtypes = []
50
+ hidapi.hid_version.restype = ctypes.POINTER(APIVersion)
51
+
52
+ version = hidapi.hid_version()
53
+ version = (
54
+ version.contents.major,
55
+ version.contents.minor,
56
+ version.contents.patch,
57
+ )
58
+ except AttributeError:
59
+ #
60
+ # hid_version API was added in
61
+ # https://github.com/libusb/hidapi/commit/8f72236099290345928e646d2f2c48f0187ac4af
62
+ # so if it is missing we are dealing with hidapi 0.8.0 or older
63
+ #
64
+ version = (0, 8, 0)
65
+
66
+ if version >= (0, 13, 0):
67
+ bus_type = [
68
+ ('bus_type', ctypes.c_int),
69
+ ]
70
+ else:
71
+ bus_type = []
72
+
73
+ class BusType(enum.Enum):
74
+ UNKNOWN = 0x00
75
+ USB = 0x01
76
+ BLUETOOTH = 0x02
77
+ I2C = 0x03
78
+ SPI = 0x04
79
+
80
+ class DeviceInfo(ctypes.Structure):
81
+ def as_dict(self):
82
+ ret = {}
83
+ for name, type in self._fields_:
84
+ if name == 'next':
85
+ continue
86
+ ret[name] = getattr(self, name, None)
87
+
88
+ if name == 'bus_type':
89
+ ret[name] = BusType(ret[name])
90
+
91
+ return ret
92
+
93
+ DeviceInfo._fields_ = [
94
+ ('path', ctypes.c_char_p),
95
+ ('vendor_id', ctypes.c_ushort),
96
+ ('product_id', ctypes.c_ushort),
97
+ ('serial_number', ctypes.c_wchar_p),
98
+ ('release_number', ctypes.c_ushort),
99
+ ('manufacturer_string', ctypes.c_wchar_p),
100
+ ('product_string', ctypes.c_wchar_p),
101
+ ('usage_page', ctypes.c_ushort),
102
+ ('usage', ctypes.c_ushort),
103
+ ('interface_number', ctypes.c_int),
104
+ ('next', ctypes.POINTER(DeviceInfo)),
105
+ ] + bus_type
106
+
107
+ hidapi.hid_init.argtypes = []
108
+ hidapi.hid_init.restype = ctypes.c_int
109
+ hidapi.hid_exit.argtypes = []
110
+ hidapi.hid_exit.restype = ctypes.c_int
111
+ hidapi.hid_enumerate.argtypes = [ctypes.c_ushort, ctypes.c_ushort]
112
+ hidapi.hid_enumerate.restype = ctypes.POINTER(DeviceInfo)
113
+ hidapi.hid_free_enumeration.argtypes = [ctypes.POINTER(DeviceInfo)]
114
+ hidapi.hid_free_enumeration.restype = None
115
+ hidapi.hid_open.argtypes = [ctypes.c_ushort, ctypes.c_ushort, ctypes.c_wchar_p]
116
+ hidapi.hid_open.restype = ctypes.c_void_p
117
+ hidapi.hid_open_path.argtypes = [ctypes.c_char_p]
118
+ hidapi.hid_open_path.restype = ctypes.c_void_p
119
+ hidapi.hid_write.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_size_t]
120
+ hidapi.hid_write.restype = ctypes.c_int
121
+ hidapi.hid_read_timeout.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_size_t, ctypes.c_int]
122
+ hidapi.hid_read_timeout.restype = ctypes.c_int
123
+ hidapi.hid_read.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_size_t]
124
+ hidapi.hid_read.restype = ctypes.c_int
125
+ hidapi.hid_get_input_report.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_size_t]
126
+ hidapi.hid_get_input_report.restype = ctypes.c_int
127
+ hidapi.hid_set_nonblocking.argtypes = [ctypes.c_void_p, ctypes.c_int]
128
+ hidapi.hid_set_nonblocking.restype = ctypes.c_int
129
+ hidapi.hid_send_feature_report.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int]
130
+ hidapi.hid_send_feature_report.restype = ctypes.c_int
131
+ hidapi.hid_get_feature_report.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_size_t]
132
+ hidapi.hid_get_feature_report.restype = ctypes.c_int
133
+ hidapi.hid_close.argtypes = [ctypes.c_void_p]
134
+ hidapi.hid_close.restype = None
135
+ hidapi.hid_get_manufacturer_string.argtypes = [ctypes.c_void_p, ctypes.c_wchar_p, ctypes.c_size_t]
136
+ hidapi.hid_get_manufacturer_string.restype = ctypes.c_int
137
+ hidapi.hid_get_product_string.argtypes = [ctypes.c_void_p, ctypes.c_wchar_p, ctypes.c_size_t]
138
+ hidapi.hid_get_product_string.restype = ctypes.c_int
139
+ hidapi.hid_get_serial_number_string.argtypes = [ctypes.c_void_p, ctypes.c_wchar_p, ctypes.c_size_t]
140
+ hidapi.hid_get_serial_number_string.restype = ctypes.c_int
141
+ hidapi.hid_get_indexed_string.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_wchar_p, ctypes.c_size_t]
142
+ hidapi.hid_get_indexed_string.restype = ctypes.c_int
143
+ hidapi.hid_error.argtypes = [ctypes.c_void_p]
144
+ hidapi.hid_error.restype = ctypes.c_wchar_p
145
+
146
+
147
+ def enumerate(vid=0, pid=0):
148
+ ret = []
149
+ info = hidapi.hid_enumerate(vid, pid)
150
+ c = info
151
+
152
+ while c:
153
+ ret.append(c.contents.as_dict())
154
+ c = c.contents.next
155
+
156
+ hidapi.hid_free_enumeration(info)
157
+
158
+ return ret
159
+
160
+
161
+ class Device(object):
162
+ def __init__(self, vid=None, pid=None, serial=None, path=None):
163
+ if path:
164
+ self.__dev = hidapi.hid_open_path(path)
165
+ elif serial:
166
+ serial = ctypes.create_unicode_buffer(serial)
167
+ self.__dev = hidapi.hid_open(vid, pid, serial)
168
+ elif vid and pid:
169
+ self.__dev = hidapi.hid_open(vid, pid, None)
170
+ else:
171
+ raise ValueError('specify vid/pid or path')
172
+
173
+ if not self.__dev:
174
+ raise HIDException('unable to open device')
175
+
176
+ def __enter__(self):
177
+ return self
178
+
179
+ def __exit__(self, exc_type, exc_value, exc_traceback):
180
+ self.close()
181
+
182
+ def __hidcall(self, function, *args, **kwargs):
183
+ if not self.__dev:
184
+ raise HIDException('device closed')
185
+
186
+ ret = function(*args, **kwargs)
187
+
188
+ if ret == -1:
189
+ err = hidapi.hid_error(self.__dev)
190
+ raise HIDException(err)
191
+ return ret
192
+
193
+ def __readstring(self, function, max_length=255):
194
+ buf = ctypes.create_unicode_buffer(max_length)
195
+ self.__hidcall(function, self.__dev, buf, max_length)
196
+ return buf.value
197
+
198
+ def write(self, data):
199
+ return self.__hidcall(hidapi.hid_write, self.__dev, data, len(data))
200
+
201
+ def read(self, size, timeout=None):
202
+ data = ctypes.create_string_buffer(size)
203
+
204
+ if timeout is None:
205
+ size = self.__hidcall(hidapi.hid_read, self.__dev, data, size)
206
+ else:
207
+ size = self.__hidcall(
208
+ hidapi.hid_read_timeout, self.__dev, data, size, timeout)
209
+
210
+ return data.raw[:size]
211
+
212
+ def get_input_report(self, report_id, size):
213
+ data = ctypes.create_string_buffer(size)
214
+
215
+ # Pass the id of the report to be read.
216
+ data[0] = bytearray((report_id,))
217
+
218
+ size = self.__hidcall(
219
+ hidapi.hid_get_input_report, self.__dev, data, size)
220
+ return data.raw[:size]
221
+
222
+ def send_feature_report(self, data):
223
+ return self.__hidcall(hidapi.hid_send_feature_report,
224
+ self.__dev, data, len(data))
225
+
226
+ def get_feature_report(self, report_id, size):
227
+ data = ctypes.create_string_buffer(size)
228
+
229
+ # Pass the id of the report to be read.
230
+ data[0] = bytearray((report_id,))
231
+
232
+ size = self.__hidcall(
233
+ hidapi.hid_get_feature_report, self.__dev, data, size)
234
+ return data.raw[:size]
235
+
236
+ def close(self):
237
+ if self.__dev:
238
+ hidapi.hid_close(self.__dev)
239
+ self.__dev = None
240
+
241
+ @property
242
+ def nonblocking(self):
243
+ return getattr(self, '_nonblocking', 0)
244
+
245
+ @nonblocking.setter
246
+ def nonblocking(self, value):
247
+ self.__hidcall(hidapi.hid_set_nonblocking, self.__dev, value)
248
+ setattr(self, '_nonblocking', value)
249
+
250
+ @property
251
+ def manufacturer(self):
252
+ return self.__readstring(hidapi.hid_get_manufacturer_string)
253
+
254
+ @property
255
+ def product(self):
256
+ return self.__readstring(hidapi.hid_get_product_string)
257
+
258
+ @property
259
+ def serial(self):
260
+ return self.__readstring(hidapi.hid_get_serial_number_string)
261
+
262
+ def get_indexed_string(self, index, max_length=255):
263
+ buf = ctypes.create_unicode_buffer(max_length)
264
+ self.__hidcall(hidapi.hid_get_indexed_string,
265
+ self.__dev, index, buf, max_length)
266
+ return buf.value
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Austin Morton
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,106 @@
1
+ Metadata-Version: 2.1
2
+ Name: hid
3
+ Version: 1.0.6
4
+ Summary: ctypes bindings for hidapi
5
+ Home-page: https://github.com/apmorton/pyhidapi
6
+ Author: Austin Morton
7
+ Author-email: amorton@juvsoft.com
8
+ License: MIT
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python
13
+ Classifier: Programming Language :: Python :: 3
14
+ Description-Content-Type: text/markdown
15
+ License-File: LICENSE
16
+
17
+ # Installing pyhidapi
18
+ pyhidapi is available on [PyPI](https://pypi.org/project/hid/) and can be installed using pip.
19
+ ```
20
+ pip install hid
21
+ ```
22
+
23
+ pyhidapi is dependant upon the [hidapi library](https://github.com/libusb/hidapi), which must be installed separately.
24
+
25
+ # Installing hidapi
26
+
27
+ ## Linux
28
+ Installation procedures vary depending on your distribution.
29
+
30
+ ### Arch Linux
31
+ Binary distributions are available in the community repository.
32
+
33
+ 1. Enable the community repository in `/etc/pacman.conf`
34
+ ```
35
+ [community]
36
+ Include = /etc/pacman.d/mirrorlist
37
+ ```
38
+ 2. Install hidapi
39
+ ```
40
+ pacman -Sy hidapi
41
+ ```
42
+
43
+ ### CentOS/RHEL
44
+ Binary distributions are available through [EPEL](https://fedoraproject.org/wiki/EPEL).
45
+ ```
46
+ yum install hidapi
47
+ ```
48
+
49
+ ### Fedora
50
+ Binary distributions are available.
51
+ ```
52
+ dnf install hidapi
53
+ ```
54
+
55
+ ### Ubuntu/Debian
56
+ Binary distributions are available.
57
+
58
+ ```
59
+ apt install libhidapi-hidraw0
60
+ ```
61
+ or
62
+ ```
63
+ apt install libhidapi-libusb0
64
+ ```
65
+
66
+ ### Others
67
+ Binary distributions may be available in your package repositories. If not, you can build from source as described [in the libusb/hidapi README](https://github.com/libusb/hidapi#build-instructions).
68
+
69
+ ## Windows
70
+ Installation procedure for Windows is described [in the libusb/hidapi README](https://github.com/libusb/hidapi#building-on-windows)
71
+
72
+ Binary distributions are provided by [libusb/hidapi](https://github.com/libusb/hidapi/releases)
73
+
74
+ ## OSX
75
+ There are currently no official binary distributions for Mac, so you must build hidapi yourself.
76
+
77
+ Installation instructions are described [in the libusb/hidapi README](https://github.com/libusb/hidapi#mac)
78
+
79
+ You can also use brew:
80
+ ```
81
+ brew install hidapi
82
+ ```
83
+
84
+ ## FreeBSD
85
+ Binary distributions are available.
86
+
87
+ ```
88
+ pkg install -g 'py3*-hid'
89
+ ```
90
+
91
+ # Sample usage code
92
+
93
+ The details about a HID device can be printed with following code:
94
+
95
+ ```python
96
+ import hid
97
+
98
+ vid = 0x046d # Change it for your device
99
+ pid = 0xc534 # Change it for your device
100
+
101
+ with hid.Device(vid, pid) as h:
102
+ print(f'Device manufacturer: {h.manufacturer}')
103
+ print(f'Product: {h.product}')
104
+ print(f'Serial Number: {h.serial}')
105
+ ```
106
+
@@ -0,0 +1,6 @@
1
+ hid/__init__.py,sha256=YH3k4Hmp4ksTgOa15JWcajRNNy86-KkDHWvxGW0Di-0,8373
2
+ hid-1.0.6.dist-info/LICENSE,sha256=VSS0Jays3-3so_lZmA8M04sAF267spLkqWxSDWB0NT0,1070
3
+ hid-1.0.6.dist-info/METADATA,sha256=6SgTivZu5Y33YidLlw431r9ndBORtcsmaYmeexF1Fkc,2579
4
+ hid-1.0.6.dist-info/WHEEL,sha256=oiQVh_5PnQM0E3gPdiz09WCNmwiHDMaGer_elqB3coM,92
5
+ hid-1.0.6.dist-info/top_level.txt,sha256=bXxY3NZgkyomYfSEeN2D5AV8XF79TNH_H5WknGUmLOg,4
6
+ hid-1.0.6.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: bdist_wheel (0.42.0)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ hid