def __init__(self, gamepad_id, track_button_events=True, track_motion_events=False): """Create a gamepad/joystick input. Parameters ---------- gamepad_id : int id of the gamepad track_button_events : bool, optional Track button events (default=True) track_motion_events : bool, optional Track motion events (default=False) """ if not expyriment._active_exp.is_initialized: raise RuntimeError( "Cannot create GamePad before expyriment.initialize()!") Input.__init__(self) Output.__init__(self) self.track_button_events = track_button_events self.track_motion_events = track_motion_events self._joystick = pygame.joystick.Joystick(gamepad_id) self._joystick.init()
def __init__(self, suffix, directory, comment_char=None, time_stamp=None): """Create an output file. Filename: {MAINFILE_NAME}_{SUBJECT_ID}_{TIME_STAMP}{suffix} Parameters ---------- suffix : str file suffix/extension (str) directory : str create file in given directory comment_char : str, optional comment character time_stamp : bool, optional using time stamps, based on the experiment start time, not the current time """ Output.__init__(self) self._suffix = suffix self._directory = directory if comment_char is not None: self._comment_char = comment_char else: self._comment_char = defaults.outputfile_comment_char if time_stamp is not None: self._time_stamp = time_stamp else: self._time_stamp = defaults.outputfile_time_stamp self._buffer = [] if not os.path.isdir(directory): os.mkdir(directory) self._filename = self.standard_file_name self._fullpath = directory + "{0}{1}".format(os.path.sep, self._filename) atexit.register(self.save) # Create new file fl = open(self._fullpath, 'w+') fl.close() try: locale_enc = locale.getdefaultlocale()[1] except: locale_enc = "UTF-8" self.write_comment("Expyriment {0}, {1}-file, coding: {2}".format( expyriment.get_version(), self._suffix, locale_enc)) if expyriment._active_exp.is_initialized: self.write_comment("date: {0}".format(time.strftime( "%a %b %d %Y %H:%M:%S", expyriment._active_exp.clock.init_localtime)))
def __init__(self, interface, baseline): """Create a streaming button box input. Parameters ---------- interface : io.SerialPort or io.ParallelPort an interface object baseline : int code that is sent when nothing is pressed (int) """ Input.__init__(self) Output.__init__(self) self._interface = interface if baseline is not None: self._baseline = baseline else: self._baseline = defaults.streamingbuttonbox_baseline
def __init__(self, gamepad_id, track_button_events=True, track_motion_events=False): """Create a gamepad/joystick input. Parameters ---------- gamepad_id : int id of the gamepad track_button_events : bool, optional Track button events (default=True) track_motion_events : bool, optional Track motion events (default=False) """ if not expyriment._active_exp.is_initialized: raise RuntimeError("Cannot create GamePad before expyriment.initialize()!") Input.__init__(self) Output.__init__(self) self.track_button_events = track_button_events self.track_motion_events = track_motion_events self._joystick = pygame.joystick.Joystick(gamepad_id) self._joystick.init()
def __init__(self): """Create a parallel port input and output. """ import types if type(parallel) is not types.ModuleType: message = """ParallelPort can not be initialized. The Python package 'pyParallel' is not installed.""" raise ImportError(message) if float(parallel.VERSION) < 0.2: raise ImportError( "Expyriment {0} ".format(__version__) + "is not compatible with PyParallel {0}.".format(parallel.VERSION) + "\nPlease install PyParallel 0.2 or higher." ) Input.__init__(self) Output.__init__(self) self._parallel = parallel.Parallel() self.input_history = False # dummy
def __init__(self, interface, default_code=None, default_duration=None): """Create a marker output. If a default code is specified, it will automatically be applied when send() is called without a code. If a default duration is specified, a 0 is sent automatically after the specifed duration when send() is called without a duration. Notes ----- EEG/MEG systems: If the system is receiving the markers on a parallel port, the duration between sending a code an the subsequent 0 should be at least 1000/samplerate! Parameters ---------- interface : io.SerialPort or io.ParallelPort interface to use default_code : int, optional default code default_duration : int, optional default duration (in ms) for sending 0 after a code """ Output.__init__(self) self._interface = interface if default_code is not None: self._default_code = default_code else: self._default_code = defaults.markeroutput_default_code if default_duration is not None: self._default_duration = default_duration else: self._default_duration = defaults.markeroutput_default_duration
def __init__(self, colour, open_gl, window_mode, window_size): """Create and set up a screen output. Notes ----- CAUTION: We discourage from creating a screen instance manually! Parameters ---------- colour : (int, int, int) colour of the screen open_gl : int or bool 0/False - No OpenGL (no vsync / no blocking) 1 - OpenGL (vsync / no blocking) 2/True - OpenGL (vsync / blocking) 3 - OpenGL (vsync / alternative blocking) window_mode : bool if screen should be a window window_size : (int, int) size of the window in window_mode, full screen mode if size of window_mode[0]<=0 """ Output.__init__(self) self._colour = colour if open_gl == False: open_gl = 0 elif open_gl == True: open_gl = 2 self._open_gl = open_gl self._fullscreen = not window_mode self._window_size = window_size if ogl is None: warn_message = "PyOpenGL is not installed. \ OpenGL will be deactivated!" print("Warning: " + warn_message) expyriment._active_exp._event_file_warn("Screen,warning," + warn_message) self._open_gl = False pygame.display.init() if expyriment._active_exp.is_initialized: self._monitor_resolution = expyriment._active_exp.screen.monitor_resolution else: self._monitor_resolution = (pygame.display.Info().current_w, pygame.display.Info().current_h) if self._fullscreen: self._window_size = self._monitor_resolution else: self._window_size = window_size if not self._open_gl: if self._fullscreen: self._surface = pygame.display.set_mode(self._window_size, pygame.FULLSCREEN) else: self._surface = pygame.display.set_mode(self._window_size) pygame.display.set_caption("Expyriment") else: try: pygame.display.gl_set_attribute(pygame.GL_SWAP_CONTROL, 1) except: pass if self._fullscreen: self._surface = pygame.display.set_mode( self._window_size, pygame.DOUBLEBUF | pygame.OPENGL | pygame.FULLSCREEN ) else: self._surface = pygame.display.set_mode(self._window_size, pygame.DOUBLEBUF | pygame.OPENGL) pygame.display.set_caption("Expyriment") ogl_version = ogl.glGetString(ogl.GL_VERSION) if float(ogl_version[0:3]) < 2.0: ogl_extensions = ogl.glGetString(ogl.GL_EXTENSIONS) if "ARB_texture_non_power_of_two" not in ogl_extensions: raise RuntimeError( "OpenGL mode is not supported on this \ machine!" ) pygame.mouse.set_visible(False) pygame.event.set_blocked(pygame.MOUSEMOTION) pygame.event.set_blocked(pygame.MOUSEBUTTONDOWN) pygame.event.set_blocked(pygame.MOUSEBUTTONUP)
def __init__(self, port, baudrate=None, bytesize=None, parity=None, stopbits=None, timeout=None, xonxoff=None, rtscts=None, dsrdtr=None, input_history=None, os_buffer_size=None, clock=None): """Create a serial port input and output. The port argument will accept the number of the port (e.g. 0 for COM1) as well as a string describing the full port location ("COM1" or "/dev/ttyS0"). Notes ----- An input_history can be used to overcome the size limitation of the receive buffer of the operating system. An input_history consists of a misc.ByteBuffer instance. In order to not miss any input, the serial port has to be updated regularly (i.e. calling read_input() or clear() before the receive buffer will be full). If the receive buffer size is set correctly, a warning will be given, when the input_history was not updated fast enough. Importantly, the fuller the receive buffer is, the longer clearing and polling will take (this can be more than 1 ms!), since all the bytes have to be transfered to the input_history. Parameters ---------- port : int or str port to use baudrate : int, optional bytesize : int, optional parity : str, optional parity:'E'=even, 'O'=odd, 'N'=none stopbits : int, optional timeout : int, optional the timeout for read(): -1=block xonxoff : int, optional rtscts : int, optional dsrdtr : int, optional input_history : bool, optional True if an input_history should be used os_buffer_size : int, optional the size of the receive input_history provided by the operating system in bytes clock : misc.Clock, optional an experimental clock (optional) """ import types if type(serial) is not types.ModuleType: message = """SerialPort can not be initialized. The Python package 'pySerial' is not installed.""" raise ImportError(message) if float(serial.VERSION) < 2.5: raise ImportError( "Expyriment {0} ".format(__version__) + "is not compatible with PySerial {0}.".format(serial.VERSION) + "\nPlease install PySerial 2.5 or higher.") Input.__init__(self) Output.__init__(self) if baudrate is None: baudrate = defaults.serialport_baudrate if bytesize is None: bytesize = defaults.serialport_bytesize if parity is None: parity = defaults.serialport_parity if stopbits is None: stopbits = defaults.serialport_stopbits if timeout is None: timeout = defaults.serialport_timeout if timeout == -1: timeout = None if xonxoff is None: xonxoff = defaults.serialport_xonxoff if rtscts is None: rtscts = defaults.serialport_rtscts if dsrdtr is None: dsrdtr = defaults.serialport_dsrdtr if clock is not None: self._clock = clock else: if expyriment._active_exp.is_initialized: self._clock = expyriment._active_exp.clock else: self._clock = Clock() if input_history is None: input_history = defaults.serialport_input_history if input_history is True: self._input_history = ByteBuffer( name="SerialPortBuffer (Port {0})".format(repr(port)), clock=self._clock) else: self._input_history = False if os_buffer_size is None: os_buffer_size = defaults.serialport_os_buffer_size self._os_buffer_size = os_buffer_size self._serial = serial.Serial(port, baudrate, bytesize, parity, stopbits, timeout, xonxoff, rtscts, dsrdtr) if not self._serial.isOpen(): raise IOError("Could not open serial port") atexit.register(self.close)
def __init__(self, port, baudrate=None, bytesize=None, parity=None, stopbits=None, timeout=None, xonxoff=None, rtscts=None, dsrdtr=None, input_history=None, os_buffer_size=None, clock=None): """Create a serial port input and output. The port argument will accept the number of the port (e.g. 0 for COM1) as well as a string describing the full port location ("COM1" or "/dev/ttyS0"). Notes ----- An input_history can be used to overcome the size limitation of the receive buffer of the operating system. An input_history consists of a misc.ByteBuffer instance. In order to not miss any input, the serial port has to be updated regularly (i.e. calling read_input() or clear() before the receive buffer will be full). If the receive buffer size is set correctly, a warning will be given, when the input_history was not updated fast enough. Importantly, the fuller the receive buffer is, the longer clearing and polling will take (this can be more than 1 ms!), since all the bytes have to be transfered to the input_history. Parameters ---------- port : int or str port to use baudrate : int, optional bytesize : int, optional parity : str, optional parity:'E'=even, 'O'=odd, 'N'=none stopbits : int, optional timeout : int, optional the timeout for read(): -1=block xonxoff : int, optional rtscts : int, optional dsrdtr : int, optional input_history : bool, optional True if an input_history should be used os_buffer_size : int, optional the size of the receive input_history provided by the operating system in bytes clock : misc.Clock, optional an experimental clock (optional) """ import types if type(serial) is not types.ModuleType: message = """SerialPort can not be initialized. The Python package 'pySerial' is not installed.""" raise ImportError(message) if float(serial.VERSION) < 2.5: raise ImportError("Expyriment {0} ".format(__version__) + "is not compatible with PySerial {0}.".format( serial.VERSION) + "\nPlease install PySerial 2.5 or higher.") Input.__init__(self) Output.__init__(self) if baudrate is None: baudrate = defaults.serialport_baudrate if bytesize is None: bytesize = defaults.serialport_bytesize if parity is None: parity = defaults.serialport_parity if stopbits is None: stopbits = defaults.serialport_stopbits if timeout is None: timeout = defaults.serialport_timeout if timeout == -1: timeout = None if xonxoff is None: xonxoff = defaults.serialport_xonxoff if rtscts is None: rtscts = defaults.serialport_rtscts if dsrdtr is None: dsrdtr = defaults.serialport_dsrdtr if clock is not None: self._clock = clock else: if expyriment._active_exp.is_initialized: self._clock = expyriment._active_exp.clock else: self._clock = Clock() if input_history is None: input_history = defaults.serialport_input_history if input_history is True: self._input_history = ByteBuffer( name="SerialPortBuffer (Port {0})".format(repr(port)), clock=self._clock) else: self._input_history = False if os_buffer_size is None: os_buffer_size = defaults.serialport_os_buffer_size self._os_buffer_size = os_buffer_size self._serial = serial.Serial(port, baudrate, bytesize, parity, stopbits, timeout, xonxoff, rtscts, dsrdtr) if not self._serial.isOpen(): raise IOError("Could not open serial port") atexit.register(self.close)
def __init__(self, colour, open_gl, window_mode, window_size): """Create and set up a screen output. Notes ----- CAUTION: We discourage from creating a screen instance manually! Parameters ---------- colour : (int, int, int) colour of the screen open_gl : int or bool 0/False - No OpenGL (no vsync / no blocking) 1 - OpenGL (vsync / no blocking) 2/True - OpenGL (vsync / blocking) 3 - OpenGL (vsync / alternative blocking) window_mode : bool if screen should be a window window_size : (int, int) size of the window in window_mode, full screen mode if size of window_mode[0]<=0 """ Output.__init__(self) self._colour = colour if open_gl == False: open_gl = 0 elif open_gl == True: open_gl = 2 self._open_gl = open_gl self._fullscreen = not window_mode self._window_size = window_size if ogl is None: warn_message = "PyOpenGL is not installed. \ OpenGL will be deactivated!" print("Warning: " + warn_message) expyriment._active_exp._event_file_warn("Screen,warning," + warn_message) self._open_gl = False pygame.display.init() if expyriment._active_exp.is_initialized: self._monitor_resolution = \ expyriment._active_exp.screen.monitor_resolution else: self._monitor_resolution = (pygame.display.Info().current_w, pygame.display.Info().current_h) if self._fullscreen: self._window_size = self._monitor_resolution else: self._window_size = window_size if not self._open_gl: if self._fullscreen: self._surface = pygame.display.set_mode( self._window_size, pygame.FULLSCREEN) else: self._surface = pygame.display.set_mode(self._window_size) pygame.display.set_caption('Expyriment') else: try: pygame.display.gl_set_attribute(pygame.GL_SWAP_CONTROL, 1) except: pass if self._fullscreen: self._surface = pygame.display.set_mode( self._window_size, pygame.DOUBLEBUF | pygame.OPENGL | pygame.FULLSCREEN) else: self._surface = pygame.display.set_mode( self._window_size, pygame.DOUBLEBUF | pygame.OPENGL) pygame.display.set_caption('Expyriment') ogl_version = ogl.glGetString(ogl.GL_VERSION) if float(ogl_version[0:3]) < 2.0: ogl_extensions = ogl.glGetString(ogl.GL_EXTENSIONS) if "ARB_texture_non_power_of_two" not in ogl_extensions: raise RuntimeError("OpenGL mode is not supported on this \ machine!") pygame.mouse.set_visible(False) pygame.event.set_blocked(pygame.MOUSEMOTION) pygame.event.set_blocked(pygame.MOUSEBUTTONDOWN) pygame.event.set_blocked(pygame.MOUSEBUTTONUP)
def __init__(self, colour, open_gl, window_mode, window_size): """Create and set up a screen output. Notes ----- CAUTION: We discourage from creating a screen instance manually! Parameters ---------- colour : (int, int, int) colour of the screen open_gl : bool if OpenGL should be used window_mode : bool if screen should be a window window_size : (int, int) size of the window in window_mode, full screen mode if size of window_mode[0]<=0 """ Output.__init__(self) self._colour = colour self._open_gl = open_gl self._fullscreen = not window_mode self._window_size = window_size if ogl is None: warn_message = "PyOpenGL is not installed. \ OpenGL will be deactivated!" print("Warning: " + warn_message) expyriment._active_exp._event_file_warn("Screen,warning," + warn_message) self._open_gl = False if self._open_gl and not self._fullscreen: warn_message = "OpenGL does not support window mode. \ OpenGL will be deactivated!" print("Warning: " + warn_message) expyriment._active_exp._event_file_warn("Screen,warning," + warn_message) self._open_gl = False pygame.display.init() if self._fullscreen: self._window_size = (pygame.display.Info().current_w, pygame.display.Info().current_h) else: self._window_size = window_size if not self._open_gl: if self._fullscreen: self._surface = pygame.display.set_mode(self._window_size, pygame.FULLSCREEN) else: self._surface = pygame.display.set_mode(self._window_size) else: self._surface = pygame.display.set_mode( self._window_size, pygame.DOUBLEBUF | pygame.OPENGL | pygame.FULLSCREEN) ogl_version = ogl.glGetString(ogl.GL_VERSION) if float(ogl_version[0:3]) < 2.0: ogl_extensions = ogl.glGetString(ogl.GL_EXTENSIONS) if "ARB_texture_non_power_of_two" not in ogl_extensions: raise RuntimeError("OpenGL mode is not supported on this \ machine!") pygame.mouse.set_visible(False) pygame.event.set_blocked(pygame.MOUSEMOTION) pygame.event.set_blocked(pygame.MOUSEBUTTONDOWN) pygame.event.set_blocked(pygame.MOUSEBUTTONUP)