forked from dccharacter/AHRS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
myJoystick.py
106 lines (87 loc) · 4.01 KB
/
myJoystick.py
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
import pywinusb.hid as hid
class myJoystick:
def __init__(self, dataProcessCallback):
self.device = None
self.callback = dataProcessCallback
def connect(self):
if self.device:
raise Exception('A device is already connected')
else:
self.device = hid.HidDeviceFilter(vendor_id = 0x483).get_devices()
if self.device:
self.device.open()
device.add_event_handler(usage_telephony_hook,
self.callback, hid.HID_EVT_CHANGED)
else:
raise Exception('No STM32 HID devices in the system')
def disconnect(self):
if self.device:
self.device.close()
else:
raise Exception('No connected devices')
#
"""
Simple example on how to handle usage control events
"""
from time import sleep
from msvcrt import kbhit
import pywinusb.hid as hid
def test_telephony_hook():
"""Browse for non system HID class devices, if a telephony page
hook usage control is available monitor value change events"""
# play with this value (or set it if you know your device capabilities)
# this allows to poll the telephony device for the current usage value
input_interrupt_transfers = False
# get all currently connected HID devices we could filter by doing
# something like hid.HidDeviceFilter(vendor_id = 0x1234), product Id
# filters (with masks) and other capabilities also available
all_devices = hid.HidDeviceFilter(vendor_id = 0x483).get_devices()
if not all_devices:
print("No HID class devices attached.")
else:
# search for our target usage (the hook button)
#target pageId, usageId
usage_telephony_hook = hid.get_full_usage_id(0x1, 0x1)
def hook_pressed(new_value, event_type):
"simple usage control handler"
# this simple handler is called on 'pressed' events
# this means the usage value has changed from '1' to '0'
# no need to check the value
event_type = event_type #avoid pylint warnings
if new_value:
print("On Hook!")
else:
print("Off Hook!")
for device in all_devices:
try:
device.open()
# browse input reports
all_input_reports = device.find_input_reports()
for input_report in all_input_reports:
#for v in input_report.keys():
# print input_report[v]
if usage_telephony_hook in input_report:
#found a telephony device w/ hook button
print("\nMonitoring {0.vendor_name} {0.product_name} "\
"device.\n".format(device))
print("Press any key to exit monitoring " \
"(or remove HID device)...")
# add event handler (example of other available
# events: EVT_PRESSED, EVT_RELEASED, EVT_ALL, ...)
device.add_event_handler(usage_telephony_hook,
hook_pressed, hid.HID_EVT_CHANGED) #level usage
if input_interrupt_transfers:
# poll the current value (GET_REPORT directive),
# allow handler to process result
input_report.get()
while not kbhit() and device.is_plugged():
#just keep the device opened to receive events
sleep(0.5)
return
finally:
device.close()
print("Sorry, no one of the attached HID class devices "\
"provide any Telephony Hook button")
#
if __name__ == '__main__':
test_telephony_hook()