From 102039385a9e9750aa2f9b5f90b24c66af05d50b Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Sun, 5 Jul 2020 23:15:22 +0200 Subject: [PATCH] api/_common/ColorLight: add pattern This adds the method equivalent to the recent addition of a single color light. The only difference is that now a color is returned instead of a single brightness. The provided color or HSV tuple will be stored as RGB samples internally, as is already done for pbio light patterns. In the simplest blinking form this could be: hub.light.pattern(lambda t: Color.RED if t >= 1000 else None, 2000) Or a fading, one-color sine: def fade(time): v = sin(time/2000/pi)*50+50 h = 360 s = 100 return (h, s, v) hub.light.pattern(fade, 1000) --- pybricks/_common.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pybricks/_common.py b/pybricks/_common.py index 7185a33..26a69a4 100644 --- a/pybricks/_common.py +++ b/pybricks/_common.py @@ -450,6 +450,27 @@ class ColorLight: """ pass + def pattern(self, pattern, duration, repeat=True): + """Make the light follow a color pattern as a function of time. + + The specified pattern function will be sampled at 64 points between 0 + and the specified duration. The light will be held constant between + samples. + + This function is not blocking. The pattern will be shown as the + user program continues. + + Arguments: + pattern (callable): Function of the + form ``h, s, v = func(t)``` that returns a tuple of ``h``, + ``s``, and ``v`` as a function of time ``t`` in milliseconds. + Instead of an HSV tuple, you may return + a :class:`Button <.parameters.Color>` for simplicity. + time (:ref:`time`): Duration of the pattern. + repeat (bool): Whether to keep repeating the pattern after it + completes. + """ + class LightArray: """Control an array of single-color lights."""