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)
This commit is contained in:
Laurens Valk
2020-07-07 09:44:16 +02:00
committed by laurensvalk
parent e780a65007
commit 102039385a
+21
View File
@@ -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."""