-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
449 lines (380 loc) · 18 KB
/
__init__.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# -*- coding: utf-8 -*-
"""
Based on psychopy, modified by Jonas Hauquier
Copyright (C) 2012-2013 iSolver Software Solutions
Distributed under the terms of the GNU General Public License (GPL version 3 or any later version).
"""
from util import printExceptionDetailsToStdErr,print2err
from constants import EventConstants, DeviceConstants
from touch import TouchDevice, TouchEvent,TouchMoveEvent,TouchPressEvent,TouchReleaseEvent
import serial
from serial.tools import list_ports
import math
from collections import deque
from elo_serial import *
#import gevent
import time
currentSec=time.time
getTime=time.time
# OS ' independent' view of the Elo Touch Device
class Touch(TouchDevice):
"""
The Touch class represents a touch screen input device. The Elo implementation
of the Touch Dveice uses a Serial interface for communication between
the Host Computer running ioHub and the Elo Touch Controller. A Serial - USB
converter is generally also used so that any computer with a free USB 2.0
port, running an operating system supported by the Serial - USB
converter and PsychoPy.iohub, can be used as the Host.
Touch position data is mapped to the coordinate space defined in the ioHub
configuration file for the Display index specified. If the touch device is
on a display other than the PsychoPy full screen window Display, then
positional data is returned using the OS desktop pixel bounds for the given
display.
Touch Events are generated independantly of other device events, including
a mouse device. Therefore touch data can be used in parallel to mouse data.
The Elo Touch Device does not require any external driver or native library
other than the pyserial Python package. Infact, unless you wish the
touch device to be registered as a Mouse Device, it is important **not**
to install any additional drivers or software for the Elo Screen.
When using a Serial - USB converter to connect the Elo Device to a Host
Computer USB port, please ensure any required driver or software for
the converter is installed on the Host PC. The converter should create an
active Serial Port on the Host Computer.
Configure the serial port created by the Serial - USB converter as follows:
*. 9600 Baud
*. 8 Data Bits
*. 1 Stop Bit
*. No Parity
*. Hardware Handshaking enabled
*. Software Handshaking disabled
*. Half Duplex
"""
LEAD_IN_BYTE='U'
EVENT_CLASS_NAMES=['TouchEvent','TouchMoveEvent','TouchPressEvent','TouchReleaseEvent']
DEVICE_TYPE_ID=DeviceConstants.TOUCH
DEVICE_TYPE_STRING='TOUCH'
__slots__=['_elo_hw_config','_non_touch_events','_rx_data','_serial_port_hw','serial_port_num','_raw_positions']
def __init__(self,*args,**kwargs):
TouchDevice.__init__(self,*args,**kwargs)
self._non_touch_events=deque(maxlen=64)
serial_config=self.getConfiguration().get('serial')
if serial_config:
sport=serial_config.get('port')
try:
sport=int(sport)-1
except:
sport=sport
self.serial_port_num=sport
self.__class__.force_uncalibrated = False
self._raw_positions=False
self._rx_data=''
self._serial_port_hw=None
self._connectSerial()
self._elo_hw_config=dict(JUMPERS=self.queryDevice('Jumpers'))#_getJumpers())
self._elo_hw_config['OWNER']=self.queryDevice('Owner')
self._elo_hw_config['ID']=self.queryDevice('ID')
self._elo_hw_config['REPORT']=self.queryDevice('Report')
self._elo_hw_config['DIAGNOSTICS']=self.queryDevice('Diagnostics')
def setDisplay(self, display):
self.__class__._display_device = display
def getHardwareConfiguration(self):
return self._elo_hw_config
def queryDevice(self,query_type, *args,**kwargs):
"""
Send the underlying touch screen device a query request and return the response.
"""
self._query(query_type,*args,**kwargs)
if query_type in RESPONSE_PACKET_TYPES.keys():
stime=getTime()
while getTime()-stime<0.10:
self._poll()
while len(self._non_touch_events):
reply=self._non_touch_events.popleft()
if reply.__class__.__name__.endswith(query_type):
return reply.asdict()
#gevent.sleep(0)
time.sleep(0.01)
return None
def commandDevice(self,cmd_type, *args,**kwargs):
"""
Send the underlying touch screen device a command and return the response.
"""
self._command(cmd_type,*args,**kwargs)
if cmd_type in RESPONSE_PACKET_TYPES.keys():
stime=getTime()
while getTime()-stime<0.010:
self._poll()
while len(self._non_touch_events):
reply=self._non_touch_events.popleft()
if reply.__class__.__name__.endswith(cmd_type):
return reply.asdict()
#gevent.sleep(0)
time.sleep(0.01)
return None
def getPositionType(self):
if self._raw_positions is True:
return 'RAW'
return 'CALIBRATED'
def _connectSerial(self):
print "Connecting to serial interface %s" % self.serial_port_num
self._serial_port_hw = serial.Serial(self.serial_port_num, 9600, timeout=0)
if self._serial_port_hw is None:
raise ValueError("Error: Serial Port Connection Failed: %s"%(str(self.serial_port_num)))
self._flushSerialInput()
def _readAnyRx(self,max_bytes=256):
self._rx(num_bytes=max_bytes)
def _flushSerialInput(self):
self._serial_port_hw.flushInput()
self._readAnyRx(max_bytes=100)
while self._serial_port_hw.inWaiting():
self._serial_port_hw.read(self._serial_port_hw.inWaiting())
def _tx(self,packet_bytes):
tx_count=self._serial_port_hw.write(packet_bytes)
self._serial_port_hw.flush()
return tx_count
def _query(self,query_type,*args,**kwargs):
qpkt=query_type
if isinstance(query_type,basestring):
qpkt=QUERY_PACKET_TYPES[query_type](*args,**kwargs)
self._tx(qpkt._packet_bytes)
return qpkt
def _command(self,cmd_type,*args,**kwargs):
qpkt=cmd_type
if isinstance(cmd_type,basestring):
qpkt=COMMAND_PACKET_TYPES[cmd_type](*args,**kwargs)
self._tx(qpkt._packet_bytes)
return qpkt
def _rx(self,async=False,num_bytes=10):
if async:
while self._serial_port_hw.inWaiting()>0:
self._rx_data+=self._serial_port_hw.read(self._serial_port_hw.inWaiting())
else:
self._rx_data+=self._serial_port_hw.read(num_bytes)
def saveConfiguration(self):
# Save elo device settings to NVRAM
pkt=self._command('N',*(1,7,0))
reply_packets=self._poll()
def restoreConfiguration(self):
# Save elo device settings to NVRAM
pkt=self._command('N',*(0,7,0))
reply_packets=self._poll()
def initCalibration(self):
try:
# set flag indicating raw touch coords should be used in touch events, as
# data is not calibrated
self.clearEvents()
self._raw_positions=True
# To acquire calibration points, controller must be in raw coordinate
# mode. We use the point of untouch as our calibration point. */
# Get current Mode settings
pkt=self._query('m')
reply_packets=self._poll()
# Set Mode to utouch events only
pkt=self._command('M',*(132,))
reply_packets=self._poll()
# Get current swap-axis state
pkt=self._query('c',*(0,True))
reply_packets=self._poll()
# Disable swap-axis
pkt=self._command('C',*(0,0,0,0,0,0,False))
reply_packets=self._poll()
# Get current mode settings
pkt=self._query('m')
reply_packets=self._poll()
except Exception, e:
print2err("Exception During Touch.initCalibration: ",str(e))
def applyCalibrationData(self,xmin,xmax,ymin,ymax,x1,y1,x2,y2,sx,sy,leftx,uppery,rightx,lowery):
# set calibration and scaling params on controller */
try:
# compute number of touch points per screen coordinate */
xunit = (x2-x1) / (rightx-leftx)
yunit = (y2-y1) / (lowery-uppery)
#/* extrapolate the calibration points to corner points of screen image */
xhigh = x2 + (xunit * (xmax-rightx))
xlow = x1 - (xunit * (leftx-xmin))
if xlow < 1:
xlow = 1
if xhigh < 1:
xhigh = 1 # in case axis inverted */
yhigh = y2 + (yunit * (ymax - lowery))
ylow = y1 - (yunit * (uppery - ymin))
if ylow < 1:
ylow = 1
if yhigh < 1:
yhigh = 1
# detect touchscreen orientation corrections */
xyswap = math.fabs(sx-x1) < math.fabs(sy-y1)
#xinv = xhigh < xlow
#yinv = yhigh < ylow
# set calibration x, y data
pkt=self._command('C',*('X',xlow,xhigh,0,0,0,False))
reply_packets=self._poll()
pkt=self._command('C',*('Y',ylow,yhigh,0,0,0,False))
reply_packets=self._poll()
# set swap_axis flag
pkt=self._command('C',*(0,0,0,0,0,0,xyswap))
reply_packets=self._poll()
# set scaling .....
pkt=self._command('S',*('X',xmin,xmax))
reply_packets=self._poll()
# set scaling .....
pkt=self._command('S',*('Y',ymin,ymax))
reply_packets=self._poll()
# Change mode to send touch, untouch, and touch movement events
pkt=self._command('M',*( 199,14))
reply_packets=self._poll()
# Get current Mode settings
pkt=self._query('m')
reply_packets=self._poll()
# set flag indicating calibrated touch coords should be
# used in touch events, as data is not calibrated
self.clearEvents()
self._raw_positions=False
except Exception, e:
print2err("Exception During Touch.applyCalibrationData: ",str(e))
def clearEvents(self):
try:
self._non_touch_events.clear()
self._flushSerialInput()
TouchDevice.clearEvents(self)
except Exception, e:
print2err("Exception During Touch.clearEvents: ",str(e))
def _poll(self):
"""
Checks for any new Touch Response Packets...
"""
try:
poll_time=currentSec()
self._rx()
while self._rx_data or self._serial_port_hw.inWaiting():
self._rx(num_bytes=self._serial_port_hw.inWaiting())
while self._rx_data:
while self._rx_data and self._rx_data[0]!=self.LEAD_IN_BYTE:
#print2err('Non LEAD_IN_BYTE: ',ord(self._rx_data[0]))
self._rx_data=self._rx_data[1:]
rx_size=len(self._rx_data)
if rx_size<10:
self._rx(num_bytes=10-len(self._rx_data))
if len(self._rx_data)<10:
self._last_poll_time=poll_time
#print2err('Poll < 10 bytes: ',currentSec()-poll_time)
return self._non_touch_events
if self._rx_data[0]=='U' and self._rx_data[1] == 'T':
response_class=RESPONSE_PACKET_TYPES.get('T')
touch_event=response_class(poll_time,bytearray(self._rx_data[:10]))
#print2err('packet_bytes: ',[b for b in touch_event._packet_bytes])
self._rx_data=self._rx_data[10:]
if touch_event._valid_response is True:
etype=EventConstants.TOUCH_MOVE
if touch_event.touch_type==touch_event.TOUCH_PRESS:
etype=EventConstants.TOUCH_PRESS
elif touch_event.touch_type==touch_event.TOUCH_RELEASE:
etype=EventConstants.TOUCH_RELEASE
confidence_interval=poll_time-self._last_poll_time # confidence interval
# TODO: Calculate Delay more accurately if possible
delay=poll_time-touch_event.time # delay
# TODO: Set Display ID correctly
display_id=0
self._lastPosition=self._position
if self._raw_positions is True or self.force_uncalibrated:
self._position=touch_event.x,touch_event.y
else:
self._position=self._pixelToDisplayCoords(touch_event.x,touch_event.y)
event =[
0, # exp id
0, # session id
0, #device id (not currently used)
0, # event id
etype, # event type
touch_event.time, # device time
poll_time, # logged time
touch_event.time, # hub time
confidence_interval, # confidence interval
delay, # delay
0, # filter_id
display_id,
self._position[0],
self._position[1],
touch_event.z
]
#print2err('Poll TouchEvent: ',currentSec()-poll_time)
self._addNativeEventToBuffer(event)
elif self._rx_data[0]=='U':
response_class=RESPONSE_PACKET_TYPES.get(self._rx_data[1])
# TODO: Checksum validation should be done here.
if response_class:
rc=response_class(poll_time,bytearray(self._rx_data[:10]))
self._rx_data=self._rx_data[10:]
if rc._valid_response is True:
self._non_touch_events.append(rc)
else:
print2err("Invalid Response:",rc.asdict())
else:
print2err('Warning: UNHANDLED RX PACKET TYPE: %d %s'%(poll_time,str([c for c in self._rx_data[:10]])))
self._rx_data=self._rx_data[10:]
#print2err('Poll Non TouchEvent: ',currentSec()-poll_time)
# if self._non_touch_events or touch_event_received:
# print2err('Poll end: %.6f %.6f'%(currentSec()-poll_time,poll_time-self._last_poll_time))
self._last_poll_time=poll_time
return self._non_touch_events
except:
print2err("Exception During Touch._poll: ")
printExceptionDetailsToStdErr()
def _pixelToDisplayCoords(self,px,py):
"""
Converts 0,0,pix_width,pix_height coord space to display device coord space.
"""
try:
#print '------'
'''
dw,dh=self._display_device.getPixelResolution()
# Relative display positions
rx=px/float(dw)
ry=py/float(dh)
print 'rx = ', px, '/', float(dw), '=', rx
print 'ry = ', py, '/', float(dh), '=', ry
# Touch coordinate width and height
left,top,right,bottom=self._display_device.getCoordBounds()
print 'left', left, 'top', top, 'right', right, 'bottom', bottom
w,h=right-left,top-bottom
print 'w', w, 'h', h
# Calculate touch coordinates in display pixel coordinates
x, y = left+w*rx, bottom+h*(1.0-ry)
print left, '+', w, '*', rx, '=', x
print bottom, '+', h, '* (1.0-', ry, ') =', y
return x,y
'''
dw,dh=self._display_device.getPixelResolution()
left,top,right,bottom=self._display_device.getCoordBounds()
w, h = right-left, top-bottom
rx, ry = (px-left)/float(w), (py-bottom)/float(h) # Relative touch positions
rx = abs(1-rx) # Flip X axis
def _crop(v):
return min(max(v, 0.0), 1.0)
rx = _crop(rx)
ry = _crop(ry)
#print 'rx', rx, 'ry', ry
#print dw*rx, dh*ry
x, y = dw*rx, dh*ry
#print 'x', x, 'y', y
return x, y
except:
print2err("Error During EloDevice._pixelToDisplayCoords:")
printExceptionDetailsToStdErr()
return px,py
def _closeSerial(self):
"""
"""
if self._serial_port_hw:
self._serial_port_hw.close()
self._serial_port_hw=None
return True
return False
def _close(self):
"""
"""
try:
self._closeSerial()
Device._close(self)
except:
pass