leds.py 5.86 KB
Newer Older
fleur's avatar
fleur committed
1 2 3
import sys_leds
import math

Rahix's avatar
Rahix committed
4 5 6 7 8 9 10 11 12
BOTTOM_RIGHT = 11
""" ID of the LED in the bottom right corner. """
BOTTOM_LEFT = 12
""" ID of the LED in the bottom left corner. """
TOP_RIGHT = 13
""" ID of the LED in the top right corner. """
TOP_LEFT = 14
""" ID of the LED in the top left corner. """

fleur's avatar
fleur committed
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

def update():
    """
    Updates the RGB LEDs.

    This will apply changes that have been set with :func:`leds.prep` or
    :func:`leds.prep_hsv`.  The LEDs can be only updated in bulk, so using this
    approach instead of :func:`leds.set` or :func:`leds.set_hsv` significantly
    reduces the load on the corresponding hardware bus.
    """
    sys_leds.update()


def clear():
    """
    Turns all LEDs off.

    Does **not** reactivate powersave if it has been deactivated, in which case
    ~15mA will be wasted.
    """
    values = [[0, 0, 0] for x in range(15)]
    sys_leds.set_all(values)


37
def set_flashlight(on):
fleur's avatar
fleur committed
38 39 40 41 42 43 44 45 46 47 48
    """
    Turn on the bright side LED.

    This LED can serve as a flashlight if worn on the left wrist or as a rad
    tattoo illuminator if worn on the right wrist.

    :param bool on:  Side LED on if true.
    """
    sys_leds.set_flashlight(on)


49
def set_rocket(led, value):
fleur's avatar
fleur committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    """
    Set brightness of one of the rocket LEDs.

    :param int led: Choose your rocket!

       +-------+--------+----------+
       |   ID  | Color  | Location |
       +=======+========+==========+
       | ``0`` | Blue   | Left     |
       +-------+--------+----------+
       | ``1`` | Yellow | Top      |
       +-------+--------+----------+
       | ``2`` | Green  | Right    |
       +-------+--------+----------+
    :param int value:  Brightness of LED (only two brightness levels are
       supported right now).
    """
    sys_leds.set_rocket(led, value)


70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
def dim_top(value):
    """
    Set global brightness for top RGB LEDs.

    :param int value: Brightness. Default = 1, range = 1...8
    """
    sys_leds.dim_top(value)


def dim_bottom(value):
    """
    Set global brightness for bottom RGB LEDs.

    :param int value: Brightness. Default = 8, range = 1...8
    """
    sys_leds.dim_bottom(value)


fleur's avatar
fleur committed
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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
def prep(led, color):
    """
    Prepare am RGB LED to be set to an RGB value.

    Changes are applied upon calling :func:`leds.update`. This is faster than
    individual :func:`leds.set` or :func:`leds.set_hsv` calls in case of
    multiple changes.

    :param int led: Which LED to prepare. 0-10 are the LEDs on the top and
       11-14 are the 4 "ambient" LEDs
    :param [r,g,b] color: RGB triplet
    """
    sys_leds.prep(led, color)


def prep_hsv(led, color):
    """
    Prepare an RGB LED to be set to an HSV value.

    Changes are applied upon calling :func:`leds.update`. This is faster than
    individual :func:`leds.set` or :func:`leds.set_hsv` calls in case of
    multiple changes.

    :param int led: Which LED to prepare. 0-10 are the LEDs on the top and
       11-14 are the 4 "ambient" LEDs
    :param [h,s,v] color: HSV triplet
    """
    sys_leds.prep_hsv(led, color)


def set(led, color):
    """
    Set an RGB LED to an RGB value.

    :param int led: Which LED to set. 0-10 are the LEDs on the top and 11-14
       are the 4 "ambient" LEDs
    :param [r,g,b] color: RGB triplet
    """
    sys_leds.set(led, color)


def set_hsv(led, color):
    """
    Prepare an RGB LED to be set to an HSV value.

    :param int led: Which LED to set. 0-10 are the LEDs on the top and 11-14
       are the 4 "ambient" LEDs
    :param [h,s,v] color: HSV triplet
    """
    sys_leds.set_hsv(led, color)


def set_all(colors):
    """
    Set multiple RGB LEDs to RGB values.

    Filling starts at LED0 ascending.

    **Example**:

    .. code-block:: python

       import leds, color

       # 1st red, 2nd green & 3rd blue:
       leds.set_all([color.RED, color.GREEN, color.BLUE])

    :param colors: List of RGB triplets
    """
    sys_leds.set_all(colors)


def set_all_hsv(colors):
    """
    Set multiple RGB LEDs to HSV values.

    Filling starts at LED0 ascending.

    :param colors: List of HSV triplets
    """
    sys_leds.set_all_hsv(colors)


def gay(value=0.5):
    """
    Gamma Adjust Yassistant. Prints a rainbow.

    Recommended calibration prodecure:

    .. code-block:: python

       import leds

       leds.gay(1)
       # adjust gain for uniform brightness
       leds.gamma_rgb(channel=..., gain=...)

       leds.gay(0.5)
       # adjust power~4 for uniform brightness
       leds.gamma_rgb(channel=..., power=...)

    :param value: Brightness. Default = 0.5
    """
    values = [[((x * 360.0) / 11), 1.0, value] for x in range(11)]
    sys_leds.set_all_hsv(values)


195
def set_powersave(eco=True):
fleur's avatar
fleur committed
196 197 198 199 200 201 202 203 204 205 206 207 208
    """
    Enable or disable powersave mode.

    Even when set to zero, the RGB LEDs still individually consume ~1mA.
    Powersave intelligently switches the supply power in groups. This
    introduces delays in the magnitude of ~10us, so it can be disabled for high
    speed applications such as POV.

    :param bool eco:  Activates powersave if ``True``, disables it when ``False``.
    """
    sys_leds.set_powersave(eco)


209
def set_gamma(power=4.0):
fleur's avatar
fleur committed
210 211 212 213 214 215 216 217 218 219 220
    """
    Applies same power function gamma correction to all RGB channels.

    :param float power: Exponent of power function.
    """
    table = [int(math.ceil(math.pow((x / 255.0), power) * 255)) for x in range(256)]
    for i in range(3):
        sys_leds.set_gamma(i, table)
    sys_leds.update()


221
def set_gamma_rgb(channel, power=4.0, gain=1.0):
fleur's avatar
fleur committed
222 223 224 225 226 227 228 229 230 231 232 233 234
    """
    Applies power function gamma correction with optional amplification to a single RGB channel.

    :param int channel: RGB channel to be adjusted. 0->Red, 1->Green, 2->Blue.
    :param float power: Exponent of power function.
    :param float gain: Amplification of channel. Values above 1.0 might cause
       overflow.
    """
    table = [
        int(math.ceil(math.pow((x / 255.0), power) * gain * 255)) for x in range(256)
    ]
    sys_leds.set_gamma(channel, table)
    sys_leds.update()