diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/argaze/utils/contexts/Random.py | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/argaze/utils/contexts/Random.py b/src/argaze/utils/contexts/Random.py index 192e477..7fc4d91 100644 --- a/src/argaze/utils/contexts/Random.py +++ b/src/argaze/utils/contexts/Random.py @@ -18,6 +18,7 @@ __license__ = "GPLv3" import logging import time +import threading import random from argaze import ArFeatures, DataFeatures, GazeFeatures @@ -49,11 +50,23 @@ class GazePositionGenerator(ArFeatures.ArContext): logging.info('GazePositionGenerator context starts...') + # Create stop event + self.__stop_event = threading.Event() + + # Start gaze position generator thread + self.__gaze_thread = threading.Thread(target=self.__generate_gaze_position) + self.__gaze_thread.start() + + return self + + def __generate_gaze_position(self): + """Generate gaze position.""" + start_time = time.time() self.__x = int(self.range[0] / 2) self.__y = int(self.range[1] / 2) - while True: + while not self.__stop_event.is_set(): # Edit millisecond timestamp timestamp = int((time.time() - start_time) * 1e3) @@ -61,17 +74,20 @@ class GazePositionGenerator(ArFeatures.ArContext): self.__x += random.randint(-10, 10) self.__y += random.randint(-10, 10) - logging.info('> timestamp=%i, x=%i, y=%i', timestamp, self.__x, self.__y) + logging.debug('> timestamp=%i, x=%i, y=%i', timestamp, self.__x, self.__y) # Process timestamped random gaze position self._process_gaze_position(timestamp = timestamp, x = self.__x, y = self.__y) # wait 10ms time.sleep(0.01) - - return self @DataFeatures.PipelineStepExit def __exit__(self, exception_type, exception_value, exception_traceback): - logging.info('GazePositionGenerator context ends...')
\ No newline at end of file + logging.info('GazePositionGenerator context ends...') + + # Stop gaze position generator thread + self.__stop_event.set() + threading.Thread.join(self.__gaze_thread) +
\ No newline at end of file |