-
Notifications
You must be signed in to change notification settings - Fork 0
/
tftevdev.py
148 lines (128 loc) · 5.55 KB
/
tftevdev.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/python
##################################################################################
# IMPORTS
##################################################################################
import evdev
import pygame
from Queue import Queue
from threading import Thread, Event
from pygame.locals import *
from tftutility import logger, Screen
# Class for handling events from piTFT
class TftTouchscreen(Thread):
def __init__(self, device_path="/dev/input/touchscreen"):
super(TftTouchscreen, self).__init__()
self.device_path = device_path
self.events = Queue()
self.shutdown = Event()
self.rotation = 0
def run(self):
thread_process = Thread(target=self.process_device)
thread_process.daemon = True
thread_process.start()
self.shutdown.wait()
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 process_device(self):
device = None
try:
device = evdev.InputDevice(self.device_path)
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))
logger.error(message)
raise OSError(message)
finally:
if device is None:
self.shutdown.set()
logger.debug("Loaded device {} successfully.".format(self.device_path))
event = {'time': None, 'id': None, 'x': None, 'y': None, 'touch': None}
while True:
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:
event['time'] = input_event.timestamp()
print("{}".format(event))
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
def __del__(self):
self.shutdown.set()
class TftEvHandler(object):
pitft = TftTouchscreen(Screen.TouchscreenInput)
prev_loc = {'x': False, 'y': False}
event_state = 0
def start(self, rotation = 90):
self.pitft.rotation = rotation
self.pitft.start()
def run(self):
pass
def stop(self):
self.pitft.shutdown.set()
class TftCapacitiveEvHandler(TftEvHandler):
def run(self):
while not self.pitft.queue_empty():
for ts_event in self.pitft.get_event():
pg_event = {'y': self.prev_loc['y'], 'x': self.prev_loc['x']}
if ts_event['x'] is not None:
pg_event['y'] = ts_event['x']
if ts_event['y'] is not None:
pg_event['x'] = ts_event['y']
if pg_event['x'] is None or pg_event['y'] is None:
break
self.prev_loc = {'y': pg_event['y'], 'x': pg_event['x']}
if self.pitft.rotation == 90:
pg_event = {'x': pg_event['x'], 'y': 240 - pg_event['y']}
elif self.pitft.rotation == 270:
pg_event = {'x': 320 - pg_event['x'], 'y': pg_event['y']}
else:
raise (Exception("Unsupported display rotation"))
pg_dict = {}
pg_event_type = MOUSEBUTTONUP if ts_event['touch'] == 0 else (
MOUSEBUTTONDOWN if self.event_state == 0 else MOUSEMOTION)
if pg_event_type == MOUSEBUTTONDOWN:
self.event_state = 1
pg_dict['button'] = 1
pg_dict['pos'] = (pg_event['x'], pg_event['y'])
pygame.mouse.set_pos(pg_event['x'], pg_event['y'])
elif pg_event_type == MOUSEBUTTONUP:
self.event_state = 0
pg_dict['button'] = 1
pg_dict['pos'] = (pg_event['x'], pg_event['y'])
else:
pg_dict['buttons'] = (True, False, False)
pg_dict['rel'] = (0, 0)
pg_dict['pos'] = (pg_event['x'], pg_event['y'])
pygame.mouse.set_pos(pg_event['x'], pg_event['y'])
pe = pygame.event.Event(pg_event_type, pg_dict)
pygame.event.post(pe)