pybricks.pupdevices: Avoid class-specific LightArray.

Let's minimize complicated hacks if we can help it.
This commit is contained in:
Laurens Valk
2022-09-30 16:04:33 +02:00
parent bfa0b69e93
commit abc00df943
2 changed files with 28 additions and 54 deletions
+26 -16
View File
@@ -605,28 +605,19 @@ class ColorLight:
"""
class LightArray:
"""Control an array of single-color lights."""
class LightArray3:
"""Control an array of three single-color lights."""
def __init__(self, n: int):
"""LightArray(n)
Initializes the light array.
Arguments:
n (int): Number of lights
"""
def on(self, brightness: Union[Number, Collection[Number]]) -> None:
def on(self, brightness: Union[Number, Tuple[Number, Number, Number]]) -> None:
"""on(brightness)
Turns on the lights at the specified brightness.
Arguments:
brightness (Number or tuple):
Brightness (0--100) of each light, in the order shown above.
If you give just one brightness value, all lights get that
brightness.
brightness (Number or tuple, %):
Use a single value to set the brightness of all lights at the
same time. Use a tuple of three values to set the brightness
of each light individually.
"""
def off(self) -> None:
@@ -635,6 +626,25 @@ class LightArray:
Turns off all the lights."""
class LightArray4(LightArray3):
"""Control an array of four single-color lights."""
def on(
self, brightness: Union[Number, Tuple[Number, Number, Number, Number]]
) -> None:
"""on(brightness)
Turns on the lights at the specified brightness.
Arguments:
brightness (Number or tuple, %):
Use a single value to set the brightness of all lights at the
same time. Use a tuple of four values to set the brightness
of each light individually. The order of the lights is shown
in the image above.
"""
class LightMatrix:
"""Control a rectangular grid of single-color lights."""
+2 -38
View File
@@ -203,24 +203,7 @@ class PFMotor(DCMotor):
class ColorSensor(_common.AmbientColorSensor):
"""LEGO® SPIKE Color Sensor."""
class _LightArray(_common.LightArray):
def __init__(self):
super().__init__(3)
def on(self, brightness: Union[Number, Tuple[Number, Number, Number]]) -> None:
"""on(brightness)
Turns on the lights at the specified brightness.
Arguments:
brightness (Number or tuple, %):
A single value will set the brightness of all three lights
to the same value. A tuple of 3 values will set the
brightness of each LED individually.
"""
return super().on(brightness)
lights = _LightArray()
lights = _common.LightArray3()
# HACK: jedi can't find inherited __init__ so docs have to be duplicated
def __init__(self, port: Port):
@@ -234,26 +217,7 @@ class ColorSensor(_common.AmbientColorSensor):
class UltrasonicSensor:
"""LEGO® SPIKE Color Sensor."""
class _LightArray(_common.LightArray):
def __init__(self):
super().__init__(4)
def on(
self, brightness: Union[Number, Tuple[Number, Number, Number, Number]]
) -> None:
"""on(brightness)
Turns on the lights at the specified brightness.
Arguments:
brightness (Number or tuple, %):
A single value will set the brightness of all four lights
to the same value. A tuple of 4 values will set the
brightness of each LED individually.
"""
return super().on(brightness)
lights = _LightArray()
lights = _common.LightArray4()
def __init__(self, port: Port):
"""UltrasonicSensor(port)