def client(server_ip): # t : test connect # q : quit client # space : enter control.set_develop_mode(True) control.defaults.audiosystem_autostart = False exp = control.initialize() udp_connection = UDPConnection() print(udp_connection) if not udp_connection.connect_peer(server_ip): print("error connecting to peer") exit() stimuli.TextScreen( "connected to " + udp_connection.peer_ip, "\nSPACE: send text\nT: trigger test\nQ: quit").present() c = Clock() while True: key = exp.keyboard.check() if key == ord("q"): break elif key == misc.constants.K_SPACE: text = io.TextInput().get() stimuli.BlankScreen().present() print("send: {} {}".format(c.time, text)) udp_connection.send(text) elif key == ord("t"): times = [] for cnt in range(20): stimuli.TextLine("ping test " + str(cnt)).present() c.reset_stopwatch() ok, time = udp_connection.ping(timeout=1) print("answer received in {} ms".format(c.stopwatch_time)) times.append(time) c.wait(100) stimuli.BlankScreen().present() print(times) feedback = udp_connection.poll() if feedback is not None: print("received: {} {}".format(c.time, feedback)) udp_connection.unconnect_peer()
class MovingPosition(object): def __init__(self, position, direction, speed, lifetime, extra_age=0, north_up_clockwise=True, is_target=False): """Create a MovingPosition Parameters ---------- position : (int, int) start position direction : int, float (0-360) movement direction in degrees speed : int the moving speed in pixel per second lifetime : int the time the object lives in milliseconds extra_age : int, optional the object can have already an age when creating or resetting (default=0) north_up_clockwise : bool, optional if true (default) all directional information refer to an north up and clockwise system otherwise 0 is right, counterclockwise (default=True) is_target : bool target position """ self._start_position = list(position) self.lifetime = lifetime self.extra_age = extra_age # add extra age for shorter lifetime self.is_target = is_target self._speed = speed self._north_up_clockwise = north_up_clockwise, self._direction = direction self._update_movement_vector() self._clock = Clock() @property def is_dead(self): """Return True is lifetime of the object is over""" return (self.age >= self.lifetime) @property def age(self): """Return the age of a dot""" return self._clock.stopwatch_time + self.extra_age def reset_age(self, randomize_age=False): """Reset the age to zero (born at current time) or if randomize_age=True, age will randomized.""" if randomize_age: self.extra_age = int(random.random() * self.lifetime) self._clock.reset_stopwatch() def is_outside(self, the_range): """Return True the object is outside the range from (0,0)""" pos = self.position return (math.hypot(pos[0], pos[1]) >= the_range) @property def north_up_clockwise(self): """getter for north up and clockwise""" return self._north_up_clockwise @property def position(self): """The current position (depends on time). Note: This property changes continuously over time, since the position is moving. """ return (self._start_position[0] + self._clock.stopwatch_time * self._movement_vector[0], self._start_position[1] + self._clock.stopwatch_time * self._movement_vector[1]) @property def direction(self): """Getter for direction.""" return self._direction @property def speed(self): """Getter for speed.""" return self._speed @direction.setter def direction(self, x): self._direction = x self._update_movement_vector() @speed.setter def speed(self, x): """speed in pix per second""" self._speed = x self._update_movement_vector() def _update_movement_vector(self): if self._north_up_clockwise: direction = 450 - self._direction else: direction = self._direction angle = direction * (math.pi) / 180 speed = self._speed / float(1000) self._movement_vector = (speed * math.cos(angle), speed * math.sin(angle))
##while feedback is None: ## feedback = udp.poll() ##print "<-- ", c.time, feedback while True: key = exp.keyboard.check() if key == ord("q"): break elif key == misc.constants.K_SPACE: text = io.TextInput().get() stimuli.BlankScreen().present() print "--> ", c.time, text udp_connection.send(text) elif key == ord("t"): times = [] for cnt in range(20): stimuli.TextLine("ping test " + str(cnt)).present() c.reset_stopwatch() ok, time = udp_connection.ping() print c.stopwatch_time times.append(time) c.wait(100) stimuli.BlankScreen().present() print times feedback = udp_connection.poll() if feedback is not None: print "<-- ", c.time, feedback udp_connection.unconnect_peer()
class MovingPosition(object): def __init__(self, position, direction, speed, lifetime, extra_age=0, north_up_clockwise=True, is_target=False): """Create a MovingPosition Parameters ---------- position : (int, int) start position direction : int, float (0-360) movement direction in degrees speed : int the moving speed in pixel per second lifetime : int the time the object lives in milliseconds extra_age : int, optional the object can have already an age when creating or resetting (default=0) north_up_clockwise : bool, optional if true (default) all directional information refer to an north up and clockwise system otherwise 0 is right, counterclockwise (default=True) is_target : bool target position """ self._start_position = list(position) self.lifetime = lifetime self.extra_age = extra_age # add extra age for shorter lifetime self.is_target = is_target self._speed = speed self._north_up_clockwise = (north_up_clockwise,) self._direction = direction self._update_movement_vector() self._clock = Clock() @property def is_dead(self): """Return True is lifetime of the object is over""" return self.age >= self.lifetime @property def age(self): """Return the age of a dot""" return self._clock.stopwatch_time + self.extra_age def reset_age(self, randomize_age=False): """Reset the age to zero (born at current time) or if randomize_age=True, age will randomized.""" if randomize_age: self.extra_age = int(random.random() * self.lifetime) self._clock.reset_stopwatch() def is_outside(self, the_range): """Return True the object is outside the range from (0,0)""" pos = self.position return math.hypot(pos[0], pos[1]) >= the_range @property def north_up_clockwise(self): """getter for north up and clockwise""" return self._north_up_clockwise @property def position(self): """The current position (depends on time). Note: This property changes continuously over time, since the position is moving. """ return ( self._start_position[0] + self._clock.stopwatch_time * self._movement_vector[0], self._start_position[1] + self._clock.stopwatch_time * self._movement_vector[1], ) @property def direction(self): """Getter for direction.""" return self._direction @property def speed(self): """Getter for speed.""" return self._speed @direction.setter def direction(self, x): self._direction = x self._update_movement_vector() @speed.setter def speed(self, x): """speed in pix per second""" self._speed = x self._update_movement_vector() def _update_movement_vector(self): if self._north_up_clockwise: direction = 450 - self._direction else: direction = self._direction angle = direction * (math.pi) / 180 speed = self._speed / float(1000) self._movement_vector = (speed * math.cos(angle), speed * math.sin(angle))