-
Notifications
You must be signed in to change notification settings - Fork 12
/
usb-driver.py
executable file
·97 lines (85 loc) · 2.67 KB
/
usb-driver.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
#!/usr/bin/env python
import usb.core
import usb.util
import sys
from evdev import UInput, AbsInfo, ecodes as e
# find our device
dev = usb.core.find(idVendor=0x2914, idProduct=0x0100)
if dev.is_kernel_driver_active(1):
dev.detach_kernel_driver(1)
if dev.is_kernel_driver_active(0):
try:
dev.detach_kernel_driver(0)
except:
dev.attach_kernel_driver(1)
#dev.set_configuration()
#usb.util.claim_interface(dev, 1)
#usb.util.claim_interface(dev, 0)
# knock into tablet mode
payload = '\x05\x00\x03'
while True:
try:
assert dev.ctrl_transfer(0x21, 0x09, 0x0305, 1, payload, 100) == len(payload)
break
except usb.USBError as err:
if err.args != (110, 'Operation timed out') and err.args != (32, 'Pipe error'):
raise err
print('payload transfer failed, retrying')
print('Payload sent')
# Pull out interrupt endpoint
cfg = dev[0]
intf = cfg[(1,0)]
ep = intf[0]
# Maximum position possible
minxpos = 0
minypos = 0
maxxpos = 19780
maxypos = 13442
minpressure = 0
maxpressure = 255
# Initialise UInput device
cap = {
e.EV_KEY : (e.BTN_TOUCH, e.BTN_STYLUS2),
e.EV_ABS : [
(e.ABS_PRESSURE, AbsInfo(value=minpressure, max=maxpressure, min=0, fuzz=0, flat=0, resolution=0)),
(e.ABS_X, AbsInfo(value=minxpos, max=maxxpos, min=0, fuzz=0, flat=0, resolution=0)),
(e.ABS_Y, AbsInfo(value=minypos, max=maxypos, min=0, fuzz=0, flat=0, resolution=0))]
}
ui = UInput(cap, name='boogie-board-sync-pen')
try:
while True:
try:
data = ep.read(8, 100)
except usb.USBError as err:
if err.args != (110, 'Operation timed out'):
raise err
continue
xpos = data[1] | data[2] << 8
ypos = data[3] | data[4] << 8
if xpos < minxpos:
minxpos = xpos
print('updated minxpos to %d' % minxpos)
if xpos > maxxpos:
maxxpos = xpos
print('updated maxxpos to %d' % maxxpos)
if ypos < minypos:
minypos = ypos
print('updated minypos to %d' % minypos)
if ypos > maxypos:
maxypos = ypos
print('updated maxypos to %d' % maxypos)
pressure = data[5] | data[6] << 8
touch = data[7] & 0x01
stylus = (data[7] & 0x02)>>1
ui.write(e.EV_ABS, e.ABS_PRESSURE, pressure)
ui.write(e.EV_ABS, e.ABS_X, xpos)
ui.write(e.EV_ABS, e.ABS_Y, ypos)
ui.write(e.EV_KEY,e.BTN_TOUCH,touch)
ui.write(e.EV_KEY,e.BTN_STYLUS2,stylus)
ui.syn()
except KeyboardInterrupt:
pass
usb.util.release_interface(dev, 0)
usb.util.release_interface(dev, 1)
dev.attach_kernel_driver(0)
dev.attach_kernel_driver(1)