Skip to content

RGB Led

A class to control RGB LEDs using the NeoPixel library.

Source code in wsd_esp32\rgb_led.py
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
class RGBLed:
    """
    A class to control RGB LEDs using the NeoPixel library.
    """

    def __init__(self, pin: int, num_leds: int = 1):
        """
        Initializes the RGB LED controller.

        Parameters:
            pin (int): The GPIO pin number to which the LED strip is connected.
            num_leds (int): The number of LEDs in the strip. Default is 1.
        """
        self.pin = Pin(pin, Pin.OUT)
        self.num_leds = num_leds
        self.np = neopixel.NeoPixel(self.pin, self.num_leds)

    def set_color(self, r: int, g: int, b: int):
        """
        Sets the color of all LEDs in the strip.

        Parameters:
            r (int): The red component of the color (0-255).
            g (int): The green component of the color (0-255).
            b (int): The blue component of the color (0-255).
        """
        for i in range(self.num_leds):
            self.np[i] = (r, g, b)
        self.np.write()

    def set_pixel(self, index: int, r: int, g: int, b: int):
        """
        Sets the color of a specific LED in the strip.

        Parameters:
            index (int): The index of the LED to set (0-based).
            r (int): The red component of the color (0-255).
            g (int): The green component of the color (0-255).
            b (int): The blue component of the color (0-255).

        Raises:
            IndexError: If the index is out of range.
        """
        if 0 <= index < self.num_leds:
            self.np[index] = (r, g, b)
            self.np.write()
        else:
            raise IndexError("LED index out of range")

    def set_brightness(self, brightness: float):
        """
        Adjusts the brightness of all LEDs in the strip.

        Parameters:
            brightness (float): The brightness level (0.0 to 1.0).

        Raises:
            ValueError: If the brightness is not between 0.0 and 1.0.
        """
        if 0.0 <= brightness <= 1.0:
            for i in range(self.num_leds):
                r, g, b = self.np[i]
                self.np[i] = (int(r * brightness), int(g * brightness), int(b * brightness))
            self.np.write()
        else:
            raise ValueError("Brightness must be between 0.0 and 1.0")

    def rainbow(self, delay: float):
        """
        Displays a rainbow animation on the LED strip.

        Parameters:
            delay (float): The delay between frames in seconds.
        """
        import time
        for j in range(256):
            for i in range(self.num_leds):
                pixel_index = (i * 256 // self.num_leds) + j
                self.np[i] = self.wheel(pixel_index & 255)
            self.np.write()
            time.sleep(delay)

    def wheel(self, pos: int):
        """
        Generates a color based on a position value.

        Parameters:
            pos (int): The position value (0-255).

        Returns:
            tuple: A tuple representing the RGB color (r, g, b).
        """
        if pos < 85:
            return (pos * 3, 255 - pos * 3, 0)
        elif pos < 170:
            pos -= 85
            return (255 - pos * 3, 0, pos * 3)
        else:
            pos -= 170
            return (0, pos * 3, 255 - pos * 3)

    def clear(self):
        """
        Turns off all LEDs in the strip by setting their color to black.
        """
        for i in range(self.num_leds):
            self.np[i] = (0, 0, 0)
        self.np.write()

__init__(pin, num_leds=1)

Initializes the RGB LED controller.

Parameters:

Name Type Description Default
pin int

The GPIO pin number to which the LED strip is connected.

required
num_leds int

The number of LEDs in the strip. Default is 1.

1
Source code in wsd_esp32\rgb_led.py
 9
10
11
12
13
14
15
16
17
18
19
def __init__(self, pin: int, num_leds: int = 1):
    """
    Initializes the RGB LED controller.

    Parameters:
        pin (int): The GPIO pin number to which the LED strip is connected.
        num_leds (int): The number of LEDs in the strip. Default is 1.
    """
    self.pin = Pin(pin, Pin.OUT)
    self.num_leds = num_leds
    self.np = neopixel.NeoPixel(self.pin, self.num_leds)

set_color(r, g, b)

Sets the color of all LEDs in the strip.

Parameters:

Name Type Description Default
r int

The red component of the color (0-255).

required
g int

The green component of the color (0-255).

required
b int

The blue component of the color (0-255).

required
Source code in wsd_esp32\rgb_led.py
21
22
23
24
25
26
27
28
29
30
31
32
def set_color(self, r: int, g: int, b: int):
    """
    Sets the color of all LEDs in the strip.

    Parameters:
        r (int): The red component of the color (0-255).
        g (int): The green component of the color (0-255).
        b (int): The blue component of the color (0-255).
    """
    for i in range(self.num_leds):
        self.np[i] = (r, g, b)
    self.np.write()

set_pixel(index, r, g, b)

Sets the color of a specific LED in the strip.

Parameters:

Name Type Description Default
index int

The index of the LED to set (0-based).

required
r int

The red component of the color (0-255).

required
g int

The green component of the color (0-255).

required
b int

The blue component of the color (0-255).

required

Raises:

Type Description
IndexError

If the index is out of range.

Source code in wsd_esp32\rgb_led.py
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def set_pixel(self, index: int, r: int, g: int, b: int):
    """
    Sets the color of a specific LED in the strip.

    Parameters:
        index (int): The index of the LED to set (0-based).
        r (int): The red component of the color (0-255).
        g (int): The green component of the color (0-255).
        b (int): The blue component of the color (0-255).

    Raises:
        IndexError: If the index is out of range.
    """
    if 0 <= index < self.num_leds:
        self.np[index] = (r, g, b)
        self.np.write()
    else:
        raise IndexError("LED index out of range")

set_brightness(brightness)

Adjusts the brightness of all LEDs in the strip.

Parameters:

Name Type Description Default
brightness float

The brightness level (0.0 to 1.0).

required

Raises:

Type Description
ValueError

If the brightness is not between 0.0 and 1.0.

Source code in wsd_esp32\rgb_led.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def set_brightness(self, brightness: float):
    """
    Adjusts the brightness of all LEDs in the strip.

    Parameters:
        brightness (float): The brightness level (0.0 to 1.0).

    Raises:
        ValueError: If the brightness is not between 0.0 and 1.0.
    """
    if 0.0 <= brightness <= 1.0:
        for i in range(self.num_leds):
            r, g, b = self.np[i]
            self.np[i] = (int(r * brightness), int(g * brightness), int(b * brightness))
        self.np.write()
    else:
        raise ValueError("Brightness must be between 0.0 and 1.0")

rainbow(delay)

Displays a rainbow animation on the LED strip.

Parameters:

Name Type Description Default
delay float

The delay between frames in seconds.

required
Source code in wsd_esp32\rgb_led.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def rainbow(self, delay: float):
    """
    Displays a rainbow animation on the LED strip.

    Parameters:
        delay (float): The delay between frames in seconds.
    """
    import time
    for j in range(256):
        for i in range(self.num_leds):
            pixel_index = (i * 256 // self.num_leds) + j
            self.np[i] = self.wheel(pixel_index & 255)
        self.np.write()
        time.sleep(delay)

wheel(pos)

Generates a color based on a position value.

Parameters:

Name Type Description Default
pos int

The position value (0-255).

required

Returns:

Name Type Description
tuple

A tuple representing the RGB color (r, g, b).

Source code in wsd_esp32\rgb_led.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
def wheel(self, pos: int):
    """
    Generates a color based on a position value.

    Parameters:
        pos (int): The position value (0-255).

    Returns:
        tuple: A tuple representing the RGB color (r, g, b).
    """
    if pos < 85:
        return (pos * 3, 255 - pos * 3, 0)
    elif pos < 170:
        pos -= 85
        return (255 - pos * 3, 0, pos * 3)
    else:
        pos -= 170
        return (0, pos * 3, 255 - pos * 3)

clear()

Turns off all LEDs in the strip by setting their color to black.

Source code in wsd_esp32\rgb_led.py
105
106
107
108
109
110
111
def clear(self):
    """
    Turns off all LEDs in the strip by setting their color to black.
    """
    for i in range(self.num_leds):
        self.np[i] = (0, 0, 0)
    self.np.write()