aboutsummaryrefslogtreecommitdiff
path: root/touch.py
diff options
context:
space:
mode:
authorderekhe <derekhe@april1985.com>2015-06-27 18:15:41 +0800
committerderekhe <derekhe@april1985.com>2015-06-27 18:15:41 +0800
commitd7c40afa4334c25c967e8a83401ac1a968a0c084 (patch)
tree8fb36b1428ff96d6f03a75bfbbc47510fa82c37a /touch.py
downloadwaveshare-7inch-touchscreen-driver-d7c40afa4334c25c967e8a83401ac1a968a0c084.zip
waveshare-7inch-touchscreen-driver-d7c40afa4334c25c967e8a83401ac1a968a0c084.tar.xz
Add touch support.
Diffstat (limited to 'touch.py')
-rw-r--r--touch.py84
1 files changed, 84 insertions, 0 deletions
diff --git a/touch.py b/touch.py
new file mode 100644
index 0000000..f32a28c
--- /dev/null
+++ b/touch.py
@@ -0,0 +1,84 @@
+#!/usr/bin/env python3
+
+import struct
+import time
+import math
+import glob
+import uinput
+import pyudev
+import os
+
+# Wait and find devices
+def read_and_emulate_mouse(deviceFound):
+ with open(deviceFound, 'rb') as f:
+ print("Read buffer")
+
+ device = uinput.Device([
+ uinput.BTN_LEFT,
+ uinput.BTN_RIGHT,
+ uinput.ABS_X,
+ uinput.ABS_Y,
+ ])
+
+ clicked = False
+ rightClicked = False
+ (lastX, lastY) = (0, 0)
+ startTime = time.time()
+
+ while True:
+ b = f.read(25)
+ (tag, btnLeft, x, y) = struct.unpack_from('>c?HH', b)
+ print(btnLeft, x, y)
+
+ if btnLeft:
+ device.emit(uinput.ABS_X, x, True)
+ device.emit(uinput.ABS_Y, y, True)
+
+ if not clicked:
+ print("Left click")
+ device.emit(uinput.BTN_LEFT, 1)
+ clicked = True
+ startTime = time.time()
+ (lastX, lastY) = (x, y)
+
+ duration = time.time() - startTime
+ movement = math.sqrt(pow(x - lastX, 2) + pow(y - lastY, 2))
+
+ if clicked and (not rightClicked) and (duration > 1) and (movement < 20):
+ print("Right click")
+ device.emit(uinput.BTN_RIGHT, 1)
+ device.emit(uinput.BTN_RIGHT, 0)
+ rightClicked = True
+ else:
+ print("Release")
+ clicked = False
+ rightClicked = False
+ device.emit(uinput.BTN_LEFT, 0)
+
+
+if __name__ == "__main__":
+ os.system("modprobe uinput")
+ os.system("chmod 666 /dev/hidraw*")
+ os.system("chmod 666 /dev/uinput*")
+
+ while True:
+ # try:
+ print("Waiting device")
+ hidrawDevices = glob.glob("/dev/hidraw*")
+
+ context = pyudev.Context()
+
+ deviceFound = None
+ for hid in hidrawDevices:
+ device = pyudev.Device.from_device_file(context, hid)
+ if "0EEF:0005" in device.device_path:
+ deviceFound = hid
+
+ if deviceFound:
+ print("Device found", deviceFound)
+ read_and_emulate_mouse(deviceFound)
+ # except:
+ # print("Error:", sys.exc_info())
+ # pass
+ # finally:
+ # time.sleep(1)