Skip to content

Instantly share code, notes, and snippets.

@solarkraft
Last active September 20, 2020 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save solarkraft/920ba0d1e46b5946b92e1d0b3a512631 to your computer and use it in GitHub Desktop.
Save solarkraft/920ba0d1e46b5946b92e1d0b3a512631 to your computer and use it in GitHub Desktop.
evdev touch viewer
import sys
import tkinter
import time
try:
from evdev import InputDevice, categorize, ecodes
except ImportError:
print("This script requires the evdev package (https://pypi.org/project/evdev/). ")
exit()
try:
dev_path = sys.argv[1]
dev = InputDevice(dev_path)
except (OSError, FileNotFoundError, IndexError):
print("Provide the path to a touch input device (looks like /dev/input/event0 and can be identified using evemu-describe) and run the script as root. ")
exit()
print(dev)
# normalization
max_x = dev.absinfo(0).max
max_y = dev.absinfo(1).max
# init window
window = tkinter.Tk()
window.title("Touches")
window_width = 1024
canvas = tkinter.Canvas(window, width=window_width,height=window_width/(max_x/max_y))
last_paint = time.time()
# update window
def draw(window, canvas, touches, override_skip=False):
global last_paint
t = time.time()
if t-last_paint < 0.0003 and not override_skip:
# print("Skipped")
return
print(touches)
canvas.delete(tkinter.ALL)
i = 1
for touch in touches:
touch_color = "#%02x%02x%02x" % (i*80 % 255, i*220 % 255, i*190 % 255)
if touch[0] != -1 and touch[1] != -1:
x = touch[0]*canvas.winfo_width()
y = touch[1]*canvas.winfo_width()
canvas.create_oval(x, y, x+20, y+20, fill=touch_color)
i += 1
canvas.pack()
window.update()
last_paint = time.time()
draw(window, canvas, [])
# list of x, y tuples
touches_supported = 10
touches = [(-1, -1) for _ in range(touches_supported)]
slot = 0
for event in dev.read_loop():
# react to slot changes
if event.code == ecodes.ABS_MT_SLOT:
slot = event.value
# set coordinates on right slot
if event.type == ecodes.EV_ABS:
if event.code == ecodes.ABS_MT_POSITION_X:
x = event.value
touches[slot] = (x / max_x, touches[slot][1])
if event.code == ecodes.ABS_MT_POSITION_Y:
y = event.value
touches[slot] = (touches[slot][0], y / max_x)
draw(window, canvas, touches)
# reset coordinates (tracking id becomes -1 with a switch to the respective slot before hand)
if event.code == ecodes.ABS_MT_TRACKING_ID and event.value == -1:
touches[slot] = (-1, -1)
draw(window, canvas, touches, override_skip=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment