-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouchscreen_protocol.py
227 lines (179 loc) · 7.15 KB
/
touchscreen_protocol.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
import logging
import pygame
from abc import ABC, abstractmethod
import time
from hal import isRaspberryPI, LiqReward, IRSensor, Sound
from keyboard import keyboard
from utils import POINTERPRESSED, POINTERMOTION, POINTERRELEASED, getPosition, tsColors
from return_to_menu import return_to_menu
import shapes
from sound import tTone
from csv_logging import CSVLogger
logger = logging.getLogger('TouchProtocols')
class tsEvent(object):
def __init__(self,typeEv=None,posEv=(-1,-1)):
self.type = typeEv
self.position = posEv
def __str__(self):
string = 'tsEvent: {}, Coord: {}'.format(self.get_type(),self.position)
return string
def __eq__(self, event):
if self.get_type() == event.get_type() and self.position == event.position:
return True
return False
def get_type(self):
if self.type == POINTERPRESSED:
return 'PointerPressed'
elif self.type == POINTERMOTION:
return 'PointerMotion'
elif self.type == POINTERRELEASED:
return 'PointerReleased'
else:
return 'None'
class BaseProtocol(ABC):
def __init__(self, surface, subject=None, experimenter=None):
self._type = 'BaseProtocol'
self.surface = surface
self.liqrew = LiqReward()
self.sensor = IRSensor(self.sensor_handler_in, self.sensor_handler_out)
self.sound = Sound()
logger.debug('Sensor variant {}'.format(self.sensor.get_type()))
logger.debug('Sound variant {}'.format(self.sound.get_type()))
logger.debug('Liquid reward variant {}'.format(self.liqrew.get_type()))
self._logfile = None
self.subject = subject
self.experimenter = experimenter
self.logger = logging.getLogger('Protocol')
def _init(self,logHdlr):
self._logHdlr = logHdlr
self._logfile = self._logHdlr.baseFilename
self.logger = logging.getLogger(self.__class__.__name__)
logger.info('Setting log file to {}'.format(self._logfile))
self.logger.addHandler(self._logHdlr)
logger.debug('Protocol type {}'.format(self._type))
self.init()
def _run(self):
logger.info('Start running baseprotocol {}'.format(self.__class__.__name__))
self.main()
def _end(self):
logger.info('End running baseprotocol {}'.format(self.__class__.__name__))
self.end()
def set_log_filename(self, filename):
import datetime, os
oldlogfile = self._logHdlr.baseFilename
logPath = os.path.dirname(oldlogfile)
self._logHdlr.close()
#preparing log filename
now = datetime.datetime.now().strftime('%Y%m%d-%H%M')
filename = filename + '_' + now + '.log'
filename = filename.replace(' ','_')
self._logHdlr.baseFilename = os.path.join(logPath, filename)
self._logfile = self._logHdlr.baseFilename
logger.info('Changing log file to {}'.format(self._logfile))
self.logger = logging.getLogger(self.__class__.__name__)
self.logger.addHandler(self._logHdlr)
#delete old file
if os.path.isfile(oldlogfile):
logger.debug('Removing old log file to {}'.format(oldlogfile))
os.remove(oldlogfile)
def log(self,string):
self.logger.info(string)
def sensor_handler_in(self):
pass
def sensor_handler_out(self):
pass
def init(self):
pass
def main(self):
raise NotImplementedError
def end(self):
pass
class Protocol(BaseProtocol):
class Screen(object):
def __init__(self,surface,backcolor=(0,0,0)):
self.surface = surface
self.backcolor = backcolor
self.width, self.height = surface.get_size()
def setBackcolor(self,color):
self.backcolor = color
def update(self):
return pygame.display.flip()
def fill(self,color):
return self.surface.fill(color)
def clean(self):
return self.surface.fill(self.backcolor)
def get_size(self):
return self.width, self.height
def get_height(self):
return self.height
def get_width(self):
return self.width
def __init__(self, surface, subject=None, experimenter=None, backcolor=tsColors['black']):
super().__init__(surface, subject, experimenter)
self.screen = Protocol.Screen(surface,backcolor)
self.draw = shapes.Draw(surface)
self._archpi = isRaspberryPI()
self._type = 'Protocol'
self._exit = False
self._fps = 60
self.csvlogger = CSVLogger(self.__class__.__name__, subject, experimenter)
def _run(self):
self.screen.clean()
self.screen.update()
logger.info('Start running protocol {}'.format(self.__class__.__name__))
pressed = False
clock = pygame.time.Clock()
# check that csvlogger was activated or activate it
if not self.csvlogger.is_active():
self.csvlogger.start()
while not self._exit:
clock.tick(self._fps)
events = pygame.event.get()
if events:
last_event = tsEvent()
for event in events:
logger.debug(event)
tEvent = tsEvent()
tEvent.position = getPosition(event)
if event.type in [pygame.MOUSEBUTTONDOWN, pygame.FINGERDOWN]:
tEvent.type = POINTERPRESSED
pressed = True
elif event.type in [pygame.MOUSEMOTION, pygame.FINGERMOTION] and pressed:
tEvent.type = POINTERMOTION
elif event.type in [pygame.MOUSEBUTTONUP, pygame.FINGERUP]:
tEvent.type = POINTERRELEASED
pressed = False
if tEvent.type and tEvent != last_event:
if not return_to_menu.closing:
self.csvlogger.log(event=tEvent, method='main')
self.logger.debug('{}: Coord ({},{})'.format(str(tEvent.get_type()),tEvent.position[0],tEvent.position[1]))
last_event = tEvent
# only on PC
if (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) or event.type == pygame.QUIT:
return
if return_to_menu(event):
return
self.main(tEvent)
else:
self.main(tsEvent())
def _end(self):
logger.info('End running protocol {}'.format(self.__class__.__name__))
self.end()
del self.csvlogger
def main(self,event):
pass
def pause(self,sleeptime):
time.sleep(sleeptime)
def now(self):
return time.time()
def quit(self):
self._exit = True
def set_max_fps(self, fps: int):
#assert isinstance(fps, int)
self._fps = fps
def set_note(self,title=None):
note = keyboard(self.surface)
if note != '':
if title == None:
title = 'Note'
self.log('{}: {}'.format(title,note))