We were hiding this under a toggle menu to reduce the page size of the hubs module. Now that each hub is on its own page, this is no longer necessary.
In turn, this makes it easier to document the same method for other hubs.
Powered Up devices have many differences compared to EV3 UART devices, so it is better to split them to dedicated classes.
The biggest difference is that PUPDevice class will allow writing to sensors and toggling battery power to the device.
This renames the generic class from LightGrid to LightMatrix. The instance object is simply called display.
This makes user scripts easier to type and read.
This also cleans up most of the draft docstrings for this class and fixes a few argument typos.
The on() method takes a color type, which is an HSV color, so a separate hsv method is not needed.
hub.light.on(Color.GREEN)
hub.light.on(Color(120))
hub.light.on(Color(h=120, s=100, 50))
Now it just displays a Matrix/2D list. We can omit manipulations like clear=False. Composing can be done by simply adding two matrices instead.
We also drop the compressed format. Instead we could have a helper function that gives you a
matrix based on a list of integers in the now-deleted format.
Specify on and off time for consistency with future blink method.
Adding some off time helps reading text with subsequent charachters that are the same. For example, now "Hello" reads H e l l o instead of Helo.
Also drop wait argument for now because it is not currently implemented and may never be.
First step towards making a color type with HSV representation.
Colors such as Color.RED or Color.BLUE continue to exist, but now you
can also define your own colors:
DARK_RED = Color(h=0, s=100, v=50)
This replaces the on-off method in ColorDistanceSensor.remote()
with a PFMotor class that works just like the DCMotor class. This
provides much more functionality without having to invent a new API.
Brown has the same hue as orange, just with a lower value and/or
saturation, so it does not need to have a separate Color instance.
It is also not supported by the color_map methods.
It was originally included in this list because the EV3 Color Sensor
can technically detect it. This is not reliable, however, as it is
also reports this value for orange objects at a slightly larger
distance. The implementation may be updated to return orange
in both cases. If the original values are required, the LUMPDevice
may be used to get the original color index.
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 makes it much easier to have fine grained control
over colors and cycle through them easily (0--360).
This also makes it consistent with sensor APIs. Now you
can measure the color and plug it into this method.
We can make this change because none of
the implementations use this yet.
If we feel we miss out by dropping the RGB, it would be more fitting
to instead expose R, G, and B attributes which point to instances of
the Light class. However, this is using more memory for something that
isn't very useful.
We could also just provide generic color converters between rgb, hsv,
and perhaps hex html color strings, not tied to any particular light or
sensor.
Currently, Pybricks has system patterns for builtin lights.
This adds a user method to set such a pattern.
For example, an indefinite sine pattern could be specified by
def fade(time):
return sin(time/2000/pi)*50+50
hub.light.pattern(fade, 1000)
or a blinking pattern using
def blink(time):
if time >= 1000:
return 100
else:
return 0
hub.light.pattern(blink, 2000)
or briefly:
hub.light.pattern(lambda t: 100 if t >= 1000 else 0, 2000)
This combines the best of simplicity and efficiency: The user doesn't
have to calculate and provide an expensive list of floating points, but
provide a clean and adaptable method instead.
By sampling, we maintain a fixed length pattern which we can preallocate
for the hub lights, and the samples are calculated only once and the
callable may be garbage collected if given as lambda. Also, any
exceptions will be caught immediately during the one-off evaluation.