Example #1
0
    def wait(self, length=None, package_size=None, duration=None,
             callback_function=None, process_control_events=True):
        """Wait for data.

        Parameters
        ----------
        length : int, optional
            The length of the data to be waited for in bytes.
            If not set, a single package will be waited for.
        package_size : int, optional
            The size of the package to be waited for.
            If not set, the default package size will be used.
            If length < package_size, package_size = length.
        duration: int, optional
            The duration to wait in milliseconds.
        callback_function : function, optional
            function to repeatedly execute during waiting loop
        process_control_events : bool, optional
            process ``io.Keyboard.process_control_keys()`` and
            ``io.Mouse.process_quit_event()`` (default = True)

        Returns:
        --------
        data : str
            The received data.
        rt : int
            The time it took to receive the data in milliseconds.

        Notes
        -----
        This will also by default process control events (quit and pause).
        Thus, keyboard events will be cleared from the cue and cannot be
        received by a Keyboard().check() anymore!

        See Also
        --------
        design.experiment.register_wait_callback_function

        """

        if _internals.skip_wait_methods:
            return None, None

        start = get_time()
        data = None
        rt = None

        if package_size is None:
            package_size = self._default_package_size
        if length is None:
            length = package_size
        elif length < package_size:
            package_size = length
        while True:
            try:
                if data is None:
                    data = self._socket.recv(package_size)
                while len(data) < length:
                    if length - len(data) >= package_size:
                        data = data + self._socket.recv(package_size)
                    else:
                        data = data + self._socket.recv(length - len(data))
                    if duration:
                        if int((get_time() - start) * 1000) >= duration:
                            data = None
                            rt = None
                            break
                rt = int((get_time() - start) * 1000)
                break
            except socket.error as e:
                err = e.args[0]
                if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
                    if isinstance(callback_function, FunctionType):
                        callback_function()
                    if _internals.active_exp is not None and \
                    _internals.active_exp.is_initialized:
                        rtn_callback = _internals.active_exp._execute_wait_callback()
                        if isinstance(rtn_callback, CallbackQuitEvent):
                            data = rtn_callback
                            rt = int((get_time() - start) * 1000)
                            break
                        if process_control_events:
                            if _internals.active_exp.mouse.process_quit_event() or \
                            _internals.active_exp.keyboard.process_control_keys():
                                break
                        else:
                            _internals.pump_pygame_events()

            if duration:
                if int((get_time() - start) * 1000) >= duration:
                    data = None
                    rt = None
                    break

        if self._logging:
            _internals.active_exp._event_file_log(
                            "TcpClient,received,{0},wait".format(data))
        return data, rt
    def wait(self, codes=None, duration=None, no_clear_buffer=False,
             callback_function=None, process_control_events=True):
        """Wait for responses defined as codes.

        The functions returns the found key code and the reaction time, that
        is, the time relative to the called of wait. By default the buffer
        will be cleared() before waiting.

        Parameters
        ----------
        codes : int or list, optional
            codes to wait for
        duration : int, optional
            maximal time to wait in ms
        no_clear_buffer : bool, optional
            do not clear the buffer.  In this case RT could be negative,
            if the event is already in the buffer (default = False)
        callback_function : function, optional
            function to repeatedly execute during waiting loop
        process_control_events : bool, optional
            process ``io.Keyboard.process_control_keys()`` and
            ``io.Mouse.process_quit_event()`` (default = True)

        Returns
        -------
        key : int
            code of pressed key
        rt : int
            reaction time in ms

        Notes
        -----
        This will also by default process control events (quit and pause).
        Thus, keyboard events will be cleared from the cue and cannot be
        received by a Keyboard().check() anymore!

        See Also
        --------
        design.experiment.register_wait_callback_function

        """

        if _internals.skip_wait_methods:
            return None, None
        start = self._buffer.clock.time
        if not no_clear_buffer:
            self.clear()
        while True:
            if isinstance(callback_function, FunctionType):
                callback_function()
            if _internals.active_exp is not None and \
               _internals.active_exp.is_initialized:
                rtn_callback = _internals.active_exp._execute_wait_callback()
                if isinstance(rtn_callback, _internals.CallbackQuitEvent):
                    return rtn_callback
                if process_control_events:
                    if _internals.active_exp.mouse.process_quit_event() or \
                       _internals.active_exp.keyboard.process_control_keys():
                        break
                else:
                    _internals.pump_pygame_events()
            if duration is not None:
                if int(self._buffer.clock.time - start) > duration:
                    return (None, None)
            found = self.check(codes)
            if found is not None:
                found = (found[0], found[1] - start)
                break

        if self._logging:
            _internals.active_exp._event_file_log(
                "CedrusResponseDevice,received,{0},wait".format(found))

        return found
Example #3
0
    def wait(self, events, duration=None, callback_function=None,
             process_control_events=True):
        """Wait for (a) certain event(s).

        Events to wait for are in the form of a list with 4 elements and do
        not include a timestamp: [status, data1, data2, data3]

        Parameters
        ----------
        events : int or list
            event(s) to wait for
        duration : int, optional
            maximal time to wait in ms
        callback_function : function, optional
            function to repeatedly execute during waiting loop
        process_control_events : bool, optional
            process ``io.Keyboard.process_control_keys()`` and
            ``io.Mouse.process_quit_event()`` (default = True)

        Returns
        -------
        evt : int
            found event
        rt : int
            reaction time in ms

        Notes
        -----
        This will also by default process control events (quit and pause).
        Thus, keyboard events will be cleared from the cue and cannot be
        received by a Keyboard().check() anymore!

        See Also
        --------
        design.experiment.register_wait_callback_function

        """

        if _internals.skip_wait_methods:
            return None, None
        start = get_time()
        rt = None
        _event = None
        self.clear()
        if isinstance(events, (list, tuple)) and \
           len(events) == 4 and \
           isinstance(events[0], int) and \
           isinstance(events[1], int) and \
           isinstance(events[2], int) and \
           isinstance(events[3], int):
            events = [events]
        done = False
        while not done:
            if isinstance(callback_function, FunctionType):
                callback_function()
            if _internals.active_exp is not None and \
               _internals.active_exp.is_initialized:
                rtn_callback = _internals.active_exp._execute_wait_callback()
                if isinstance(rtn_callback, CallbackQuitEvent):
                    _event = rtn_callback
                    rt = int((get_time() - start) * 1000)
                    break
                if process_control_events:
                    if _internals.active_exp.mouse.process_quit_event() or \
                       _internals.active_exp.keyboard.process_control_keys():
                        break
                else:
                    _internals.pump_pygame_events()
            event = self.read(1)
            if event is not None and event[0][0] in events:
                rt = int((get_time() - start) * 1000)
                _event = event[0][0]
                done = True
                break
            if duration:
                if int((get_time() - start) * 1000) >= duration:
                    done = True
                    break

            time.sleep(0.0005)

        if self._logging:
            _internals.active_exp._event_file_log(
                "MIDI In ({0}),received,{1},wait".format(self.id, _event), 2)
        return _event, rt
Example #4
0
    def wait(self,
             length,
             package_size=None,
             duration=None,
             callback_function=None,
             process_control_events=True):
        """Wait for data.

        Parameters
        ----------
        length : int
            The length of the data to be waited for in bytes.
            If not set, a single package will be waited for.
        package_size : int, optional
            The size of the package to be waited for, optional.
            If not set, the default package size will be used.
            If length < package_size, package_size = length.
        duration: int, optional
            The duration to wait in milliseconds.
        callback_function : function, optional
            function to repeatedly execute during waiting loop
        process_control_events : bool, optional
            process ``io.Keyboard.process_control_keys()`` and
            ``io.Mouse.process_quit_event()`` (default = True)

        Returns
        -------
        data : str
            The received data.
        rt : int
            The time it took to receive the data in milliseconds.

        Notes
        -----
        This will also by default process control events (quit and pause).
        Thus, keyboard events will be cleared from the cue and cannot be
        received by a Keyboard().check() anymore!

        """

        if _internals.skip_wait_methods:
            return None, None

        start = get_time()
        data = None
        rt = None

        if package_size is None:
            package_size = self._default_package_size
        if length is None:
            length = package_size
        elif length < package_size:
            package_size = length
        while True:
            try:
                if data is None:
                    data = self._client[0].recv(package_size)
                while len(data) < length:
                    if length - len(data) >= package_size:
                        data = data + self._client[0].recv(package_size)
                    else:
                        data = data + self._client[0].recv(length - len(data))
                    if duration:
                        if int((get_time() - start) * 1000) >= duration:
                            data = None
                            rt = None
                            break
                rt = int((get_time() - start) * 1000)
                break
            except socket.error as e:
                err = e.args[0]
                if err == errno.EAGAIN or err == errno.EWOULDBLOCK:
                    if isinstance(callback_function, FunctionType):
                        callback_function()
                    if _internals.active_exp is not None and \
                    _internals.active_exp.is_initialized:
                        rtn_callback = _internals.active_exp._execute_wait_callback(
                        )
                        if isinstance(rtn_callback, CallbackQuitEvent):
                            data = rtn_callback
                            rt = int((get_time() - start) * 1000)
                            break
                        if process_control_events:
                            if _internals.active_exp.mouse.process_quit_event() or \
                            _internals.active_exp.keyboard.process_control_keys():
                                break
                        else:
                            _internals.pump_pygame_events()

            if duration:
                if int((get_time() - start) * 1000) >= duration:
                    data = None
                    rt = None
                    break

        if self._logging:
            _internals.active_exp._event_file_log(
                "TcpServer,received,{0},wait".format(data))

        return data, rt
Example #5
0
    def wait(self,
             codes=None,
             duration=None,
             no_clear_buffer=False,
             callback_function=None,
             process_control_events=True):
        """Wait for responses defined as codes.

        The functions returns the found key code and the reaction time, that
        is, the time relative to the called of wait. By default the buffer
        will be cleared() before waiting.

        Parameters
        ----------
        codes : int or list, optional
            codes to wait for
        duration : int, optional
            maximal time to wait in ms
        no_clear_buffer : bool, optional
            do not clear the buffer.  In this case RT could be negative,
            if the event is already in the buffer (default = False)
        callback_function : function, optional
            function to repeatedly execute during waiting loop
        process_control_events : bool, optional
            process ``io.Keyboard.process_control_keys()`` and
            ``io.Mouse.process_quit_event()`` (default = True)

        Returns
        -------
        key : int
            code of pressed key
        rt : int
            reaction time in ms

        Notes
        -----
        This will also by default process control events (quit and pause).
        Thus, keyboard events will be cleared from the cue and cannot be
        received by a Keyboard().check() anymore!

        See Also
        --------
        design.experiment.register_wait_callback_function

        """

        if _internals.skip_wait_methods:
            return None, None
        start = self._buffer.clock.time
        if not no_clear_buffer:
            self.clear()
        while True:
            if isinstance(callback_function, FunctionType):
                callback_function()
            if _internals.active_exp is not None and \
               _internals.active_exp.is_initialized:
                rtn_callback = _internals.active_exp._execute_wait_callback()
                if isinstance(rtn_callback, _internals.CallbackQuitEvent):
                    return rtn_callback
                if process_control_events:
                    if _internals.active_exp.mouse.process_quit_event() or \
                       _internals.active_exp.keyboard.process_control_keys():
                        break
                else:
                    _internals.pump_pygame_events()
            if duration is not None:
                if int(self._buffer.clock.time - start) > duration:
                    return (None, None)
            found = self.check(codes)
            if found is not None:
                found = (found[0], found[1] - start)
                break

        if self._logging:
            _internals.active_exp._event_file_log(
                "CedrusResponseDevice,received,{0},wait".format(found))

        return found