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

4
BOTTOM_LEFT = 11
Rahix's avatar
Rahix committed
5
""" ID of the LED in the bottom left corner. """
6
7
BOTTOM_RIGHT = 12
""" ID of the LED in the bottom right corner. """
Rahix's avatar
Rahix committed
8
9
10
11
12
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
    """
    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.

44
45
46
47
48
49
50
    .. warning::

        Because of a small error in the Harmonic Board layout, we could not
        populate the flashlight LEDs in the production run.  You can handsolder
        it, though you have to reverse the direction.

    :param bool on:  Side LED on if ``True``.
fleur's avatar
fleur committed
51
52
53
54
    """
    sys_leds.set_flashlight(on)


55
def set_rocket(led, value):
fleur's avatar
fleur committed
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    |
       +-------+--------+----------+
Rahix's avatar
Rahix committed
70
    :param int value:  Brightness of LED (value between 0 and 31).
fleur's avatar
fleur committed
71
72
73
74
    """
    sys_leds.set_rocket(led, value)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def get_rocket(led):
    """
    Get 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    |
       +-------+--------+----------+
    :rtype: int
    :returns: Brightness of LED (value between 0 and 31).
Rahix's avatar
Rahix committed
92
93

    .. versionadded:: 1.10
94
95
96
    """
    return sys_leds.get_rocket(led)

zenox's avatar
zenox committed
97

zenox's avatar
zenox committed
98
99
100
101
102
103
104
105
106
107
108
109
110
def blink_rocket(led, value, millis):
    """
    Let one of the rockets blink for a certain time without
    blocking the python interpreter.

    :param int led: Choose your rocket!
    :param int value: brightness value (0 < value < 32)
    :param int millis: duration of the rocket being on in milliseconds.

    .. versionadded:: 1.??
    """
    return sys_leds.blink_rocket(led, value, millis)

111

112
113
114
115
def dim_top(value):
    """
    Set global brightness for top RGB LEDs.

Rahix's avatar
Rahix committed
116
    :param int value: Brightness. Default = 1, Range = 1..8
117
118
119
120
121
122
123
124
    """
    sys_leds.dim_top(value)


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

Rahix's avatar
Rahix committed
125
    :param int value: Brightness. Default = 8, Range = 1..8
126
127
128
129
    """
    sys_leds.dim_bottom(value)


fleur's avatar
fleur committed
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
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)


171
172
173
174
175
176
177
def get_rgb(led):
    """
    Get the current RGB value for an RGB LED.

    :param int led: Which LED to set. 0-10 are the LEDs on the top and 11-14
       are the 4 "ambient" LEDs
    :return [r,g,b] color: RGB triplet
Rahix's avatar
Rahix committed
178
179

    .. versionadded:: 1.10
180
181
182
183
    """
    return sys_leds.get_rgb(led)


fleur's avatar
fleur committed
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
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)


250
def set_powersave(eco=True):
fleur's avatar
fleur committed
251
252
253
254
255
256
257
258
259
260
261
262
263
    """
    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)


264
def set_gamma(power=4.0):
fleur's avatar
fleur committed
265
266
267
268
269
270
271
272
273
274
275
    """
    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()


276
def set_gamma_rgb(channel, power=4.0, gain=1.0):
fleur's avatar
fleur committed
277
278
279
280
281
282
283
284
285
286
287
288
289
    """
    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()