aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhesicong <derekhe@april1985.com>2016-04-19 22:08:50 +0800
committerhesicong <derekhe@april1985.com>2016-04-19 22:08:50 +0800
commit31b91ea7ca2274e60f089499e2c8337eae47bff2 (patch)
tree3d5fefa22c6918ba53b595fa3735f72d2d15c13b
parentcdd8ebaefe28e2068f87b66c5ebd73ef08efcdac (diff)
parent046a24dbf4fcd45e4b8123c82c46e89394191f13 (diff)
downloadwaveshare-7inch-touchscreen-driver-31b91ea7ca2274e60f089499e2c8337eae47bff2.tar.xz
waveshare-7inch-touchscreen-driver-31b91ea7ca2274e60f089499e2c8337eae47bff2.zip
Merge pull request #17 from noisysoil/master
Added support for async driver for systems with Python 3.4 and above.
Diffstat (limited to '')
-rw-r--r--install.sh15
-rw-r--r--touch_async.py111
2 files changed, 125 insertions, 1 deletions
diff --git a/install.sh b/install.sh
index 9af1910..f5cbd0a 100644
--- a/install.sh
+++ b/install.sh
@@ -1,5 +1,12 @@
#!/bin/bash
+
+# For checking version numbers greater than or equal.
+check_ver_lte() {
+ [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
+}
+
+
chmod +x *.sh *.py
sudo patch -b /boot/config.txt 7inch.patch
sudo apt-get install -y python3-pip libudev-dev
@@ -7,7 +14,13 @@ sudo pip-3.2 install python-uinput pyudev
#if pip-3.2 can't be found, please use pip3
#sudo pip3 install python-uinput pyudev
-sudo cp touch.py /usr/bin/
+PYTHON_VERSION=`python3 -c 'import sys; print(".".join(map(str, sys.version_info[:3])))'`
+if check_ver_lte "3.4" $PYTHON_VERSION; then
+ echo "Python $PYTHON_VERSION detected - using async driver."
+ sudo cp touch_async.py /usr/bin/touch.py
+else
+ sudo cp touch.py /usr/bin/
+fi
sudo cp touch.sh /etc/init.d/
sudo chmod +x /usr/bin/touch.py
diff --git a/touch_async.py b/touch_async.py
new file mode 100644
index 0000000..6d3d47e
--- /dev/null
+++ b/touch_async.py
@@ -0,0 +1,111 @@
+#!/usr/bin/env python3
+
+import struct
+import time
+import math
+import glob
+import uinput
+import pyudev
+import os
+import asyncio
+
+
+def check_device(thedevice):
+ # Currently we don't get a full udev dictionary from the device on
+ # Raspberry pi, so we do a hard search for the device vendor/id hex.
+ if '0EEF:0005' in (thedevice.get('DEVPATH')):
+ print("Device found at: ", thedevice.device_node)
+ with open(thedevice.device_node, 'rb') as f:
+ print("Opening device and initiating mouse emulation.")
+ tasks.clear()
+ tasks.append(asyncio.async(read_and_emulate_mouse(f)))
+ loop.run_until_complete(asyncio.wait(tasks))
+ print("Device async task terminated.")
+
+
+@asyncio.coroutine
+def async_read_data(fd, length):
+ yield from asyncio.sleep(0)
+ return fd.read(length)
+
+
+@asyncio.coroutine
+def read_and_emulate_mouse(fd):
+
+ cnt = 0
+
+ input_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:
+ cnt = cnt + 1
+ try:
+ touch_data = yield from async_read_data(fd, 25)
+ # print('Data' + ': ' + str(cnt) + ' - ' + str(len(touch_data)))
+ if touch_data == 0:
+ break;
+ except IOError:
+ return 0
+
+ (tag, btnLeft, x, y) = struct.unpack_from('>c?HH', touch_data)
+ print(btnLeft, x, y)
+
+ if btnLeft:
+ input_device.emit(uinput.ABS_X, x, True)
+ input_device.emit(uinput.ABS_Y, y, True)
+
+ if not clicked:
+ print("Left click.")
+ input_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.")
+ input_device.emit(uinput.BTN_RIGHT, 1)
+ input_device.emit(uinput.BTN_RIGHT, 0)
+ rightClicked = True
+ else:
+ print("Release.")
+ clicked = False
+ rightClicked = False
+ input_device.emit(uinput.BTN_LEFT, 0)
+
+ fd.close()
+
+
+if __name__ == "__main__":
+ os.system("modprobe uinput")
+
+ tasks = []
+ loop = asyncio.get_event_loop()
+
+ context = pyudev.Context()
+ print("Checking devices already plugged-in...")
+ for device in context.list_devices(subsystem='hidraw'):
+ check_device(device)
+
+ monitor = pyudev.Monitor.from_netlink(context)
+ monitor.filter_by(subsystem='hidraw')
+ print("Waiting for touch device connection...")
+
+ for device in iter(monitor.poll, None):
+ print("HID device notification. ACTION: ", device.get('ACTION'))
+# print(device.device_node)
+
+ if 'add' in (device.get('ACTION')):
+ check_device(device)
+