-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpitft_touchscreen.py
More file actions
113 lines (103 loc) · 4.16 KB
/
pitft_touchscreen.py
File metadata and controls
113 lines (103 loc) · 4.16 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
# piTFT touchscreen handling using evdev
import os
try:
import evdev
except ImportError:
print("Evdev package is not installed. Run 'pip3 install evdev' or 'pip install evdev' (Python 2.7) to install.")
raise(ImportError("Evdev package not found."))
import threading
try:
# python 3.5+
import queue
except ImportError:
# python 2.7
import Queue as queue
# Class for handling events from piTFT
class pitft_touchscreen(threading.Thread):
def __init__(self, device_path=os.getenv("PIGAME_TS") or "/dev/input/touchscreen", grab=False):
super(pitft_touchscreen, self).__init__()
self.device_path = device_path
self.grab = grab
self.events = queue.Queue()
self.shutdown = threading.Event()
def run(self):
thread_process = threading.Thread(target=self.process_device)
# run thread as a daemon so it gets cleaned up on exit.
thread_process.daemon = True
thread_process.start()
self.shutdown.wait()
# thread function
def process_device(self):
device = None
# if the path to device is not found, InputDevice raises an OSError
# exception. This will handle it and close thread.
try:
device = evdev.InputDevice(self.device_path)
if self.grab:
device.grab()
except Exception as ex:
message = "Unable to load device {0} due to a {1} exception with" \
" message: {2}.".format(self.device_path,
type(ex).__name__, str(ex))
raise Exception(message)
finally:
if device is None:
self.shutdown.set()
# Loop for getting evdev events
event = {'time': None, 'id': None, 'x': None, 'y': None, 'touch': None}
dropping = False
while not self.shutdown.is_set():
for input_event in device.read_loop():
if input_event.type == evdev.ecodes.EV_ABS:
if input_event.code == evdev.ecodes.ABS_X:
event['x'] = input_event.value
elif input_event.code == evdev.ecodes.ABS_Y:
event['y'] = input_event.value
elif input_event.code == evdev.ecodes.ABS_MT_TRACKING_ID:
event['id'] = input_event.value
if input_event.value == -1:
event['x'] = None
event['y'] = None
event['touch'] = None
elif input_event.code == evdev.ecodes.ABS_MT_POSITION_X:
pass
elif input_event.code == evdev.ecodes.ABS_MT_POSITION_Y:
pass
elif input_event.type == evdev.ecodes.EV_KEY:
event['touch'] = input_event.value
elif input_event.type == evdev.ecodes.SYN_REPORT:
if dropping:
event['x'] = None
event['y'] = None
event['touch'] = None
dropping = False
else:
event['time'] = input_event.timestamp()
self.events.put(event)
e = event
event = {'x': e['x'], 'y': e['y']}
try:
event['id'] = e['id']
except KeyError:
event['id'] = None
try:
event['touch'] = e['touch']
except KeyError:
event['touch'] = None
elif input_event.type == evdev.ecodes.SYN_DROPPED:
dropping = True
if self.grab:
device.ungrab()
def get_event(self):
if not self.events.empty():
event = self.events.get()
yield event
else:
yield None
def queue_empty(self):
return self.events.empty()
def stop(self):
self.shutdown.set()
def __del__(self):
self.shutdown.set()