use a sine wave and time based positioning

This commit is contained in:
Roy Hooper 2019-12-09 18:37:19 -05:00
parent 072aad6062
commit 17faa952f7

View file

@ -55,7 +55,7 @@ except ImportError:
return int(time.time() * 1000000000) return int(time.time() * 1000000000)
import random import random
from math import ceil, sin from math import ceil, sin, radians
from .color import BLACK, RAINBOW from .color import BLACK, RAINBOW
__version__ = "0.0.0-auto.0" __version__ = "0.0.0-auto.0"
@ -354,24 +354,24 @@ class Pulse(Animation):
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
def __init__(self, pixel_object, speed, color, period=5, max_intensity=1, min_intensity=0): def __init__(self, pixel_object, speed, color, period=5, max_intensity=1, min_intensity=0):
self._intensity = min_intensity
self.max_intensity = max_intensity self.max_intensity = max_intensity
self.min_intensity = min_intensity self.min_intensity = min_intensity
self._direction = 1.0 self._period = period
# TODO Fix this: self._intensity_delta = max_intensity - min_intensity
self._intensity_step = 2 / (period / speed) self._radians_per_second = radians(180 / period)
self._bpp = len(pixel_object[0]) self._bpp = len(pixel_object[0])
self._last_update = monotonic_ns()
self._cycle_position = 0
super(Pulse, self).__init__(pixel_object, speed, color) super(Pulse, self).__init__(pixel_object, speed, color)
def draw(self): def draw(self):
self._intensity += self._intensity_step * self._direction now = monotonic_ns()
if self._direction < 0 and self._intensity <= self.min_intensity: time_since_last_draw = (now - self._last_update) / 1000000000
self._direction = -self._direction self._last_update = now
self._intensity = self.min_intensity self._cycle_position = (self._cycle_position + time_since_last_draw) % self._period
elif self._direction > 0 and self._intensity >= self.max_intensity: intensity = self.min_intensity + (sin(self._radians_per_second * self._cycle_position) * self._intensity_delta)
self._direction = -self._direction
self._intensity = self.max_intensity color = [int(self._color[n] * intensity) for n in range(self._bpp)]
color = [int(self._color[n] * self._intensity) for n in range(self._bpp)]
self.fill(color) self.fill(color)
self.show() self.show()