def __init__(self, stims, Q_c2g, E_g2p, Q_g2s): """ stims: dict to define a stimulus. eg. stims = {'cue':{'class':'Block','parm':{'size':(100,40),'position':(0,0)}}} Q_c2g: multiprocessing.Queue, used for accepting stimulus control command from core process E_g2p: multiprocessing.Event, used for sending user termination event to the phase process Q_g2s: multiprocessing.Queue, used for sending accurate trigger to sigpro process """ super(GUImachine, self).__init__() pygame.init() self.Q_c2g = Q_c2g self.E_g2p = E_g2p self.Q_g2s = Q_g2s self.trigger_on = False self.trigger_event = {} self.stp = False self.lock = threading.Lock() if stims['screen']['type'].lower() == 'fullscreen': self.screen = pygame.display.set_mode( (0, 0), FULLSCREEN | DOUBLEBUF | HWSURFACE, 32) self.vsync = True else: if stims['screen'].has_key('frameless'): if stims['screen'].has_key('frameless'): self.screen = pygame.display.set_mode( stims['screen']['size'], pygame.NOFRAME) else: self.screen = pygame.display.set_mode( stims['screen']['size']) else: self.screen = pygame.display.set_mode(stims['screen']['size']) self.vsync = False if OS != 'Windows': self.vsync = False self.screen_color = stims['screen']['color'] self.screen.fill(self.screen_color) pygame.display.set_caption('BCI GUI') del stims['screen'] self.setDaemon(True) self.ask_4_update_gui = False #线程接收到刷新请求后,进行绘图准备,并通知主进程刷新 self.update_in_this_frame = False #主进程在一帧真实的刷新帧中确定能够进行刷新 self.__update_per_frame_list = [] #register stimulis for ID in stims: element = stims[ID] if element['class'] == 'Block': self.stimuli[ID] = Block(self.screen, **element['parm']) elif element['class'] == 'Imagebox': self.stimuli[ID] = Imagebox(self.screen, **element['parm']) elif element['class'] == 'sinBlock': self.stimuli[ID] = sinBlock(self.screen, **element['parm']) self.__update_per_frame_list.append(self.stimuli[ID]) elif element['class'] == 'mBlock': self.stimuli[ID] = mBlock(self.screen, **element['parm']) self.__update_per_frame_list.append(self.stimuli[ID]) else: pass
def __init__(self, stims, Q_c2g, E_g2c, server_address): """ stims: dict to define a stimulus. eg. stims = {'cue':{'class':'Block','parm':{'size':(100,40),'position':(0,0)}}} Q_c2g: multiprocessing.Queue, used for accepting stimulus control command from core process kwargs: server_address = ?, the target server's address to accept marker property for describe screen size: (width,height) type: fullscreen/normal frameless: True/False color: (R,G,B) caption: string Fps: int, strongly suggest you set Fps as the same with system's Fps """ super(GuiEngine, self).__init__() self.Q_c2g = Q_c2g self.E_g2c = E_g2c self.marker_on = False self.marker_event = {} self.stp = False self.lock = threading.Lock() pygame.init() #初始化screen # 当且仅当window环境变量设置成功且fullscreen时,SCREEN_SYNC=True if stims['screen']['type'].lower() == 'fullscreen': self.screen = pygame.display.set_mode( (0, 0), FULLSCREEN | DOUBLEBUF | HWSURFACE, 32) else: # 将窗口置中 if OS == 'windows': w, h = stims['screen']['size'] x = int((SCRW - w) / 2.) y = int((SCRH - 50 - h) / 2.) - 50 # 扣除任务栏高度 os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (x, y) self.screen = pygame.display.set_mode(stims['screen']['size'], NOFRAME | DOUBLEBUF, 32) SCREEN_SYNC = False self.screen_color = stims['screen']['color'] self.screen.fill(self.screen_color) pygame.display.set_caption(stims['screen']['caption']) self.Fps = stims['screen']['Fps'] del stims['screen'] self.ask_4_update_gui = False #线程接收到刷新请求后,通知主进程刷新 self.update_in_this_frame = False #主进程在一帧真实的刷新帧中确定能够进行刷新 self.__update_per_frame_list = [] #接受帧刷新对象 if server_address is None: class Marker(): def __init__(self, sa): pass def send_marker(self, marker): raise Exception( 'marker_sender was not initilized because server_address parameter was not given' ) self.marker_sender = Marker(server_address) #注册刺激,生成实例 for ID in stims: element = stims[ID] if element['class'] == 'Block': self.stimuli[ID] = Block(self.screen, **element['parm']) elif element['class'] == 'Imagebox': self.stimuli[ID] = Imagebox(self.screen, **element['parm']) elif element['class'] == 'sinBlock': self.stimuli[ID] = sinBlock(self.screen, **element['parm']) self.__update_per_frame_list.append( self.stimuli[ID]) #帧刷新对象的列表 elif element['class'] == 'mBlock': self.stimuli[ID] = mBlock(self.screen, **element['parm']) self.__update_per_frame_list.append(self.stimuli[ID]) else: pass self.setDaemon(True) #子线程随主线程退出 self.start()
def __init__(self, stims, Q_c2g, E_g2p, Q_g2s): """ stims: dict to define a stimulus. eg. stims = {'cue':{'class':'Block','parm':{'size':(100,40),'position':(0,0)}}} Q_c2g: multiprocessing.Queue, used for accepting stimulus control command from core process E_g2p: multiprocessing.Event, used for sending user termination event to the phase process Q_g2s: multiprocessing.Queue, used for sending accurate trigger to sigpro process property for describe screen size: (width,height) type: fullscreen/normal frameless: True/False color: (R,G,B) caption: string Fps: int, strongly suggest you set Fps as the same with system's Fps """ super(GuiEngine, self).__init__() pygame.init() self.Q_c2g = Q_c2g self.E_g2p = E_g2p self.Q_g2s = Q_g2s self.trigger_on = False self.trigger_event = {} self.stp = False self.lock = threading.Lock() #初始化screen if stims['screen']['type'].lower() == 'fullscreen': self.screen = pygame.display.set_mode( (0, 0), FULLSCREEN | DOUBLEBUF | HWSURFACE, 32) self.vsync = True print self.vsync, 'vsync' else: if stims['screen'].has_key('frameless'): self.screen = pygame.display.set_mode(stims['screen']['size'], NOFRAME | DOUBLEBUF, 32) else: self.screen = pygame.display.set_mode(stims['screen']['size'], DOUBLEBUF, 32) self.vsync = False if OS != 'windows': self.vsync = False self.screen_color = stims['screen']['color'] self.screen.fill(self.screen_color) pygame.display.set_caption(stims['screen']['caption']) self.Fps = stims['screen']['Fps'] del stims['screen'] self.ask_4_update_gui = False #线程接收到刷新请求后,通知主进程刷新 self.update_in_this_frame = False #主进程在一帧真实的刷新帧中确定能够进行刷新 self.__update_per_frame_list = [] #接受帧刷新对象 #注册刺激,生成实例 for ID in stims: element = stims[ID] if element['class'] == 'Block': self.stimuli[ID] = Block(self.screen, **element['parm']) elif element['class'] == 'Imagebox': self.stimuli[ID] = Imagebox(self.screen, **element['parm']) elif element['class'] == 'sinBlock': self.stimuli[ID] = sinBlock(self.screen, **element['parm']) self.__update_per_frame_list.append( self.stimuli[ID]) #帧刷新对象的列表 elif element['class'] == 'mBlock': self.stimuli[ID] = mBlock(self.screen, **element['parm']) self.__update_per_frame_list.append(self.stimuli[ID]) else: pass self.setDaemon(True) #子线程隧主线程退出 self.start()
def __init__(self, layout, gmess, **kwargs): """ layout: dict to define stimulus layout. must contain layout for screen eg, layout = {'screen':{'size':(200,200),'color':(0,0,0),'type':'fullscreen', 'Fps':60,'caption':'this is an example'}, 'cue':{'class':'sinBlock','parm':{'size':(100,100),'position':(100,100), 'frequency':13,'visible':True,'start':True}}} gmess: type of stimMessage which is an object to transmit json string for controlling the GUI's updating kwargs: eg. trigger_mess = xx, if an object with type of stimMessage with the name of trigger_mess, this object will send out trigger event at the precision time when the required stimulus is rendered. """ super(brGui, self).__init__() pygame.init() self.gmess = gmess self.stimuli = {} self.__release_ID_list = [] if kwargs.has_key('trigger_mess'): self.trigger_mess = kwargs['trigger_mess'] self._trigger_enable_ = True else: self._trigger_enable_ = False self.stp = False self.lock = threading.Lock() #初始化screen if layout['screen']['type'].lower() == 'fullscreen': self.screen = pygame.display.set_mode( (0, 0), FULLSCREEN | DOUBLEBUF | HWSURFACE, 32) else: if layout['screen'].has_key('frameless'): self.screen = pygame.display.set_mode(layout['screen']['size'], NOFRAME | DOUBLEBUF, 32) else: self.screen = pygame.display.set_mode(layout['screen']['size'], DOUBLEBUF, 32) _VSYNC_ = False #非全屏情况下无法开启垂直同步 self.screen_color = layout['screen']['color'] self.screen.fill(self.screen_color) pygame.display.set_caption(layout['screen']['caption']) self.Fps = layout['screen']['Fps'] del layout['screen'] self.ask_update_gui = False #线程接收到刷新请求后,通知主进程刷新 self.update_in_this_frame = False #主进程在一帧真实的刷新帧中确定能够进行刷新 self.__update_per_frame_list = [] #接受帧刷新对象 #注册刺激,生成实例 for ID in layout: element = layout[ID] if element['class'] == 'Block': self.stimuli[ID] = Block(self.screen, **element['parm']) elif element['class'] == 'Imagebox': self.stimuli[ID] = Imagebox(self.screen, **element['parm']) elif element['class'] == 'sinBlock': self.stimuli[ID] = sinBlock(self.screen, **element['parm']) self.__update_per_frame_list.append( self.stimuli[ID]) #帧刷新对象的列表 elif element['class'] == 'mBlock': self.stimuli[ID] = mBlock(self.screen, **element['parm']) self.__update_per_frame_list.append(self.stimuli[ID]) else: pass self.setDaemon(True) #子线程隧主线程退出 self.start()