1 '''
2 pure python module for discovering HID devices attached to a computer
3 and interacting with those devices.
4 '''
5
6 import logging
7
8 import os
9
10 import logging
11 from threading import Thread
12
13 __all__ = ['find_hid_devices','HIDDevice']
14
16 '''
17 absract class representing a HID device on the host computer
18 '''
20 self.vendor=vendor
21 self.product=product
22 self._callback=None
23 self._running=False
24 self._thread=None
25
27 '''
28 closes the device
29 '''
30 self.close()
31
33 '''close the device and stop the callback thread'''
34 self._running=False
35 if self._thread:
36 self._thread.join(1)
37 self._thread=None
38
40 raise RuntimeError("not implemented")
41
43 '''
44 open this HID device for use (must be called before setting callbacks
45 or setting reports)
46 '''
47 raise RuntimeError("not implemented")
48
50 '''
51 "set" a report - send the data to the device (which must have been opened previously)
52 '''
53 if not self.is_open():
54 raise RuntimeError("device not open")
55
56 logging.info('set_report(%r)',report_data)
57
58
60 '''
61 register a callback for events from the device
62 callback should be of form:
63
64 def mycallback(device,report_data):
65 pass
66
67 '''
68 if not self.is_open():
69 raise RuntimeError("device not open")
70
71 self._callback=callback
72 if not self._running:
73 self._running=True
74
75 class CallbackLoop(Thread):
76 def __init__(self,device,report_buffer_size):
77 Thread.__init__(self)
78 self.device=device
79 self.report_buffer_size=report_buffer_size
80
81 def run(self):
82 self.device._run_interrupt_callback_loop(self.report_buffer_size)
83
84 self._thread=CallbackLoop(self,report_buffer_size)
85 self._thread.setDaemon(True)
86 self._thread.start()
87
89 raise RuntimeError("not implemented")
90
92 return "(vendor=0x%04x,product=0x%04x)" % (self.vendor,self.product)
93
94 module_names=['hid.win32','hid.osx']
95
96 find_hid_devices=None
97
98 for name in module_names:
99
100 try:
101 hid=__import__(name,globals(),locals(),['find_hid_devices'])
102 logging.info("loading HID code from: %s" % name)
103 find_hid_devices=hid.find_hid_devices
104 break
105 except:
106 pass
107
108 if find_hid_devices is None:
109 raise RuntimeError("could not find a module for this operating system")
110