Skip to content

Instantly share code, notes, and snippets.

@johnp789
Created December 10, 2017 18:06
Show Gist options
  • Save johnp789/1561fdc51f4d4255c1dbf0e204960ee2 to your computer and use it in GitHub Desktop.
Save johnp789/1561fdc51f4d4255c1dbf0e204960ee2 to your computer and use it in GitHub Desktop.
import os
import sys
from evdev import UInput, ecodes, AbsInfo
PEN_MAX_X = 1 << 16 - 1
PEN_MAX_Y = 1 << 16 - 1
PEN_MAX_Z = 8191
RESOLUTION = 5080
#pressure sensitive pen tablet area with 2 stylus buttons and no eraser
cap_pen = {
ecodes.EV_KEY: [ecodes.BTN_TOUCH, ecodes.BTN_TOOL_PEN, ecodes.BTN_STYLUS, ecodes.BTN_STYLUS2],
ecodes.EV_ABS: [
(ecodes.ABS_X, AbsInfo(0,0,PEN_MAX_X,0,0,RESOLUTION)), #value, min, max, fuzz, flat, resolution
(ecodes.ABS_Y, AbsInfo(0,0,PEN_MAX_Y,0,0,RESOLUTION)),
(ecodes.ABS_PRESSURE, AbsInfo(0,0,PEN_MAX_Z,0,0,0)),
],
#ecodes.EV_MSC: [ecodes.MSC_SCAN], #not sure why, but it appears to be needed
}
vpen = UInput(events=cap_pen, name="kamvas-pen", version=0x3)
with os.popen('usbhid-dump -t0 -es -m 256c:006e') as p:
for line in p:
# example data line:
# 0A 80 EA 5A D2 6B 00 00
if line[0] != ' ':
continue
data = [int(x, base=16) for x in line.split()]
print(','.join(map(str, data)))
X = (data[3] << 8) + data[2]
Y = (data[5] << 8) + data[4]
PRESS = (data[7] << 8) + data[6]
TOUCH = data[1] == 193
BTN1 = data[1] == 194
BTN2 = data[1] == 196
print("X %6d Y%6d PRESS %4d (%s %s %s))" % (X, Y, PRESS, TOUCH, BTN1, BTN2))
vpen.write(ecodes.EV_ABS, ecodes.ABS_X, X)
vpen.write(ecodes.EV_ABS, ecodes.ABS_Y, Y)
vpen.write(ecodes.EV_ABS, ecodes.ABS_PRESSURE, PRESS)
vpen.write(ecodes.EV_KEY, ecodes.BTN_TOUCH, TOUCH and 1 or 0)
vpen.write(ecodes.EV_KEY, ecodes.BTN_STYLUS, BTN1 and 1 or 0)
vpen.write(ecodes.EV_KEY, ecodes.BTN_STYLUS2, BTN2 and 1 or 0)
vpen.syn()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment