Skip to content

rgb_strip

RGB Strip utilities.

Classes:

Name Description
RgbStrip

Wrapper for neopixel strip.

Functions:

Name Description
set_intensity

Set the intensity of a color.

test_rgb_strip

Test for the rgb strip class.

RgbStrip

RgbStrip(pin: int, n: int = LED_COUNT)

Wrapper for neopixel strip.

Parameters:

Name Type Description Default
pin int

The pin the neopixel strip is connected to.

required
n int

Amount of pixels in the strip. Defaults to LED_COUNT.

LED_COUNT

Methods:

Name Description
reset

Reset the strip and turns all leds off.

set_rgb_led

Set one led of the rgb strip.

set_rgb_strip

Set all leds of the rgb strip at once.

Source code in src/aalec/rgb_strip.py
43
44
45
46
47
def __init__(self, pin: int, n: int = constants.LED_COUNT):
    self._n: int = n
    self._np = neopixel.NeoPixel(machine.Pin(pin), n)
    self._np.fill(constants.c_off)
    self._np.write()

reset

reset() -> None

Reset the strip and turns all leds off.

Source code in src/aalec/rgb_strip.py
84
85
86
87
def reset(self) -> None:
    """Reset the strip and turns all leds off."""
    self._np.fill(constants.c_off)
    self._np.write()

set_rgb_led

set_rgb_led(led: int, color: RgbColor) -> None

Set one led of the rgb strip.

Parameters:

Name Type Description Default
led int

Index of the led in the strip (starts with 0).

required
color RgbColor

The color to set

required

Raises:

Type Description
AttributeError

If the led value does not address a led in the strip.

Source code in src/aalec/rgb_strip.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def set_rgb_led(self, led: int, color: constants.RgbColor) -> None:
    """Set one led of the rgb strip.

    Args:
        led (int): Index of the led in the strip (starts with 0).
        color (RgbColor): The color to set

    Raises:
        AttributeError: If the `led` value does not address a led in the strip.
    """
    if not (0 <= led < self._n):
        raise AttributeError(
            f"You chose an invalid led ({led}). Only values from 0 to {self._n - 1} are allowed."
        )
    self._np[led] = color  # type: ignore
    self._np.write()

set_rgb_strip

set_rgb_strip(colors: list[RgbColor]) -> None

Set all leds of the rgb strip at once.

Parameters:

Name Type Description Default
colors list[RgbColor]

A list of colors.

required

Raises:

Type Description
AttributeError

If the length of the list of colors is not exactly the number of leds in the strip.

Source code in src/aalec/rgb_strip.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
def set_rgb_strip(self, colors: list[constants.RgbColor]) -> None:
    """Set all leds of the rgb strip at once.

    Args:
        colors (list[RgbColor]): A list of colors.

    Raises:
        AttributeError: If the length of the list of colors is not
            exactly the number of leds in the strip.
    """
    if len(colors) != self._n:
        raise AttributeError(
            f"You need to provide a list of exact {self._n} colors"
        )
    for i, color in enumerate(colors):
        self._np[i] = color  # type: ignore
    self._np.write()

set_intensity

set_intensity(
    color: RgbColor, intensity: int = MEDIUM
) -> RgbColor

Set the intensity of a color.

Set the sum of the r,g and b values to intensity.

Parameters:

Name Type Description Default
color RgbColor

Color to balance

required
intensity int

Overall intensity of the color (0 - 786). Defaults to MEDIUM.

MEDIUM

Returns:

Name Type Description
RgbColor RgbColor

Balanced color.

Source code in src/aalec/rgb_strip.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
def set_intensity(
    color: constants.RgbColor, intensity: int = constants.MEDIUM
) -> constants.RgbColor:
    """Set the intensity of a color.

    Set the sum of the `r`,`g` and `b` values to `intensity`.

    Args:
        color (RgbColor): Color to balance
        intensity (int, optional): Overall intensity of the color (0 - 786). Defaults to MEDIUM.

    Returns:
        RgbColor: Balanced color.
    """
    color_sum = color.r + color.g + color.b
    if color_sum <= 0 or intensity <= 0:
        return constants.RgbColor(0, 0, 0)

    scale = intensity / color_sum

    return constants.RgbColor(
        int(color.r * scale), int(color.g * scale), int(color.b * scale)
    )

test_rgb_strip

test_rgb_strip()

Test for the rgb strip class.

Source code in src/aalec/rgb_strip.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
def test_rgb_strip():
    """Test for the rgb strip class."""
    from time import sleep

    np = RgbStrip(constants.PIN_RGB_STRIP, constants.LED_COUNT)
    colors = [
        constants.c_off,
        constants.c_red,
        constants.c_green,
        constants.c_blue,
        constants.c_cyan,
        constants.c_purple,
        constants.c_yellow,
        constants.c_white,
    ]
    idx = 0

    try:
        while True:
            for i in range(constants.LED_COUNT):
                np.set_rgb_led(i, colors[(idx + i) % len(colors)])
            idx = (idx + 1) % len(colors)
            sleep(0.5)
    finally:
        np.reset()