Skip to content

Led Module

This module help you to manage single rgb led (internal or external).


Examples:

>>> from pybox.led import LED
>>> from pybox.color import *
# drive internal led
>>> led = LED()
>>> led.color = BLUE
# turn on the led
>>> led.on()
# turn off the led
>>> led.off()

The user basically should use only the LED class inside it.

Classes

LED

Led class.

Parameters:

  • target (str, default: 'internal' ) –

    led to drive, 'internal', 'external' or a board string (i.e. board.GP18).

  • color (tuple[int], default: RED ) –

    color in (r, g, b) format.

  • brightness (float, default: 0.25 ) –

    value between 0.0 and 1.0.

Examples:

>>> led = LED('external')
>>> led.color = BLUE
>>> led.on()
Source code in pybox/led.py
 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class LED:
    """Led class.

    Args:
        target (str, optional): led to drive, 'internal', 'external' or a board string (i.e. board.GP18).
        color (tuple[int], optional): color in (r, g, b) format.
        brightness (float, optional): value between 0.0 and 1.0.

    Examples:
        >>> led = LED('external')
        >>> led.color = BLUE
        >>> led.on()
    """

    def __init__(self, target: str = 'internal', color: tuple[int] = RED, brightness: float = 0.25, order: str = 'GRB'):
        if target == 'internal':
            pin = board.GP16
            order = 'RGB'
        elif target == 'external':
            pin = board.GP27
            order = 'RGB'
        else:
            pin = target

        self._np = NeoPixel(
            pin, 1, brightness=brightness, auto_write=True, pixel_order=order)

        self._col = color

    def write(self, value: int, color: tuple = None) -> None:
        """Write digital value on led.

        Examples:
            >>> # turn on the led with color RED
            >>> led.write(1, RED)
            >>> # turn off the led
            >>> led.write(0)

        Args:
            value: value to write: 1 to turn on the led, 0 otherwise
            color: color in tuple (r, g, b) format
        """
        if value == 1:
            if color:
                self._col = color

            value = self._col

        else:
            value = OFF

        # print(self._col)
        self._np[0] = value

    def on(self):
        """turn on the led
        """
        self.write(1)

    def off(self):
        """turn off the led
        """
        self.write(0)

    def toggle(self):
        """switch between on and off on the led.

        Examples:
            >>> led.on()
            >>> # turn off the led
            >>> led.toggle()
            >>> # turn on
            >>> led.toggle()
        """

        if self._np[0] == (0, 0, 0):
            self.write(1)
        else:
            self.write(0)

    def deinit(self):
        """deinitialize led.
        """
        self._np.deinit()

    @property
    def color(self):
        """Get or set the color.

        Examples:
            >>> led.color = GREEN
            >>> led.on()

        Returns:
            (tuple): color in tuple format
        """
        return self._col

    @color.setter
    def color(self, col):
        self._col = col

    @property
    def brightness(self) -> float:
        """Get or set the brightness

        Examples:
            >>> # fade in brightness starting from 0 to 1
            >>> import time
            >>> led.on()
            >>> led.brightness = 0
            >>> for i in range(101):
            >>>     led.brightness = i / 100
            >>>     time.sleep(0.01)

        Returns:
            brightness value as a float between 0.0 and 1.0
        """
        return self._np.brightness

    @brightness.setter
    def brightness(self, bright):
        self._np.brightness = bright

Attributes

color property writable

Get or set the color.

Examples:

>>> led.color = GREEN
>>> led.on()

Returns:

  • tuple

    color in tuple format

brightness: float property writable

Get or set the brightness

Examples:

>>> # fade in brightness starting from 0 to 1
>>> import time
>>> led.on()
>>> led.brightness = 0
>>> for i in range(101):
>>>     led.brightness = i / 100
>>>     time.sleep(0.01)

Returns:

  • float

    brightness value as a float between 0.0 and 1.0

Functions

write(value: int, color: tuple = None) -> None

Write digital value on led.

Examples:

>>> # turn on the led with color RED
>>> led.write(1, RED)
>>> # turn off the led
>>> led.write(0)

Parameters:

  • value (int) –

    value to write: 1 to turn on the led, 0 otherwise

  • color (tuple, default: None ) –

    color in tuple (r, g, b) format

Source code in pybox/led.py
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
def write(self, value: int, color: tuple = None) -> None:
    """Write digital value on led.

    Examples:
        >>> # turn on the led with color RED
        >>> led.write(1, RED)
        >>> # turn off the led
        >>> led.write(0)

    Args:
        value: value to write: 1 to turn on the led, 0 otherwise
        color: color in tuple (r, g, b) format
    """
    if value == 1:
        if color:
            self._col = color

        value = self._col

    else:
        value = OFF

    # print(self._col)
    self._np[0] = value
on()

turn on the led

Source code in pybox/led.py
81
82
83
84
def on(self):
    """turn on the led
    """
    self.write(1)
off()

turn off the led

Source code in pybox/led.py
86
87
88
89
def off(self):
    """turn off the led
    """
    self.write(0)
toggle()

switch between on and off on the led.

Examples:

>>> led.on()
>>> # turn off the led
>>> led.toggle()
>>> # turn on
>>> led.toggle()
Source code in pybox/led.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
def toggle(self):
    """switch between on and off on the led.

    Examples:
        >>> led.on()
        >>> # turn off the led
        >>> led.toggle()
        >>> # turn on
        >>> led.toggle()
    """

    if self._np[0] == (0, 0, 0):
        self.write(1)
    else:
        self.write(0)
deinit()

deinitialize led.

Source code in pybox/led.py
107
108
109
110
def deinit(self):
    """deinitialize led.
    """
    self._np.deinit()