import time
import logging
from ledfxcontroller.effects import Effect
from threading import Thread
import voluptuous as vol

_LOGGER = logging.getLogger(__name__)
DEFAULT_RATE = 1.0 / 60.0

@Effect.no_registration
class TemporalEffect(Effect):
    _thread_active = False
    _thread = None

    CONFIG_SCHEMA = vol.Schema({
        vol.Required('speed', default = 1.0): float
    })

    def thread_function(self):

        while self._thread_active:
            startTime = time.time()

            # Treat the return value of the effect loop as a speed modifier
            # such that effects that are nartually faster or slower can have
            # a consistent feel.
            sleepInterval = self.effect_loop()
            if sleepInterval is None:
                sleepInterval = 1.0
            sleepInterval = sleepInterval * DEFAULT_RATE

            # Calculate the time to sleep accounting for potential heavy
            # frame assembly operations
            timeToSleep = (sleepInterval / self._config['speed']) - (time.time() - startTime)
            if timeToSleep > 0:
                time.sleep(timeToSleep)

    def effect_loop(self):
        """
        Triggered periodically based on the effect speed and 
        any additional effect modifiers
        """
        pass

    def activate(self, pixel_count):
        super().activate(pixel_count)

        self._thread_active = True
        self._thread = Thread(target = self.thread_function)
        self._thread.start()

    def deactivate(self):
        if self._thread_active:
            self._thread_active = False
            self._thread.join()
            self._thread = None
        
        super().deactivate()

[((135, 162), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (152, 162), False, 'import logging\n'), ((1434, 1469), 'threading.Thread', 'Thread', ([], {'target': 'self.thread_function'}), '(target=self.thread_function)\n', (1440, 1469), False, 'from threading import Thread\n'), ((332, 366), 'voluptuous.Required', 'vol.Required', (['"""speed"""'], {'default': '(1.0)'}), "('speed', default=1.0)\n", (344, 366), True, 'import voluptuous as vol\n'), ((475, 486), 'time.time', 'time.time', ([], {}), '()\n', (484, 486), False, 'import time\n'), ((1108, 1131), 'time.sleep', 'time.sleep', (['timeToSleep'], {}), '(timeToSleep)\n', (1118, 1131), False, 'import time\n'), ((1035, 1046), 'time.time', 'time.time', ([], {}), '()\n', (1044, 1046), False, 'import time\n')]