Simple test

Ensure your device works with this simple test.

examples/tlc59711_simpletest.py
 1
 2
 3
 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
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# simple demo of the TLC59711 16-bit 12 channel LED PWM driver.
# Shows the minimal usage - how to set pixel values in a few ways.
# Author: Tony DiCola

import board
import busio

import adafruit_tlc59711

print("tlc59711_simpletest.py")

# Define SPI bus connected to chip.
# You only need the clock and MOSI (output) line to use this chip.
spi = busio.SPI(board.SCK, MOSI=board.MOSI)
pixels = adafruit_tlc59711.TLC59711(spi, pixel_count=16)

# examples how to set the pixels:
# range:
# 0 - 65535
# or
# 0.0 - 1.0
# every pixel needs a color -
# give it just a list or tuple with 3 integer values: R G B

# set all pixels to a very low level
pixels.set_pixel_all((10, 10, 10))

# every chip has 4 Pixels (=RGB-LEDs = 12 Channel)
pixels[0] = (100, 100, 100)
pixels[1] = (0, 0, 100)
pixels[2] = (0.01, 0.0, 0.01)
pixels[3] = (0.1, 0.01, 0.0)
# if you are ready to show your values you have to call
pixels.show()

# there are a bunch of other ways to set pixel.
# have a look at the other examples.

FancyLED

this is an example for combining the TLC5957 library with FancyLED. Enjoy the colors :-)

examples/tlc59711_fancyled.py
 1
 2
 3
 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# CircuitPython

# SPDX-FileCopyrightText: 2021 s-light
# SPDX-License-Identifier: MIT
# Author Stefan Krüger (s-light)

"""TLC59711 & FancyLED."""

__doc__ = """
TLC59711 & FancyLED.

this is an example for combining the TLC5957 library with FancyLED.
Enjoy the colors :-)
"""

import board

import busio

import adafruit_fancyled.adafruit_fancyled as fancyled
import adafruit_tlc59711

##########################################
print("\n" + (42 * "*") + "\n" + __doc__ + "\n" + (42 * "*") + "\n" + "\n")

##########################################
# print(42 * "*")
# print("initialise digitalio pins for SPI")
# spi_clock = digitalio.DigitalInOut(board.SCK)
# spi_clock.direction = digitalio.Direction.OUTPUT
# spi_mosi = digitalio.DigitalInOut(board.MOSI)
# spi_mosi.direction = digitalio.Direction.OUTPUT
# spi_miso = digitalio.DigitalInOut(board.MISO)
# spi_miso.direction = digitalio.Direction.INPUT

# print((42 * '*') + "\n" + "init busio.SPI")
spi = busio.SPI(board.SCK, MOSI=board.MOSI)

##########################################
print(42 * "*")
print("init TLC5957")
NUM_LEDS = 16
pixels = adafruit_tlc59711.TLC59711(
    spi=spi,
    pixel_count=NUM_LEDS,
)

print("pixel_count", pixels.pixel_count)
print("chip_count", pixels.chip_count)
print("channel_count", pixels.channel_count)


##########################################
# main loop
print(42 * "*")
print("rainbow loop")
hue_offset = 0
while True:
    brightness = 0.8
    color = fancyled.CHSV(hue_offset, 1.0, 1.0)
    color = fancyled.gamma_adjust(color, brightness=brightness)
    pixels.set_pixel_all(color)
    pixels.show()

    # Bigger number = faster spin
    hue_offset += 0.000005
    if hue_offset >= 1:
        hue_offset = 0
        print("heu_offset reset")

Animations Example

You should have a plethora of functions to animate your lights.

examples/tlc59711_dev.py
  1
  2
  3
  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
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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# CircuitPython

# SPDX-FileCopyrightText: 2021 s-light
# SPDX-License-Identifier: MIT
# Author Stefan Krüger (s-light)

"""TLC5971 / TLC59711 Multi Development."""

__doc__ = """
TLC59711 development helper.

this sketch contains a bunch of timing tests and other development things..
Enjoy the colors :-)
"""

import time

import board
import busio

import adafruit_tlc59711


##########################################
PIXEL_COUNT = 16 * 1

spi = busio.SPI(board.SCK, MOSI=board.MOSI)
pixels = adafruit_tlc59711.TLC59711(spi, pixel_count=PIXEL_COUNT)


##########################################
# test function

VALUE_HIGH = 1000


def channelcheck_update_pixel(offset):
    """Channel check pixel."""
    # print("offset", offset)

    # pixels[offset] = (VALUE_HIGH, 0, 0)
    pixels.set_pixel_16bit_value(offset, VALUE_HIGH, 0, 0)
    # clear last pixel
    last = offset - 1
    if last < 0:
        last = PIXEL_COUNT - 1
    # pixels[last] = (0, 0, 1)
    pixels.set_pixel_16bit_value(last, 0, 0, 1)
    # pixels[offset] = (0xAAAA, 0xBBBB, 0xCCCC)
    pixels.show()

    offset += 1
    if offset >= PIXEL_COUNT:
        time.sleep(0.2)
        offset = 0
        print("clear")
        pixels.set_pixel_all((0, 1, 0))
        pixels.show()
    return offset


def channelcheck_update(offset):
    """Channel check."""
    # print("offset", offset)

    pixels.set_channel(offset, VALUE_HIGH)
    # clear last set channel
    last = offset - 1
    if last < 0:
        last = pixels.channel_count - 1
    pixels.set_channel(last, 0)
    pixels.show()

    offset += 1
    if offset >= pixels.channel_count:
        offset = 0
        print("offset overflow. start from 0")
    return offset


##########################################


def timeit_call(message, test_function, loop_count=1000):
    """Measure timing."""
    duration = 0
    start_time = time.monotonic()
    for _index in range(loop_count):
        start_time = time.monotonic()
        test_function()
        end_time = time.monotonic()
        duration += end_time - start_time
    # print(
    #     "duration:\n"
    #     "\t{}s for {} loops\n"
    #     "\t{:.2f}ms per call"
    #     "".format(
    #         duration,
    #         loop_count,
    #         (duration/loop_count)*1000
    #     )
    # )
    # print(
    #     "\t{:.2f}ms per call"
    #     "".format((duration / loop_count) * 1000)
    # )
    # "{:>8.2f}ms".format(3.56)
    print(
        "{call_duration:>10.4f}ms\t{message}"
        "".format(
            call_duration=(duration / loop_count) * 1000,
            message=message,
        )
    )


def timeit_pixels_show():
    """Measure timing."""
    print("*** pixels show:")
    loop_count = 1000

    def _test():
        pixels.show()

    timeit_call("'pixels.show()'", _test, loop_count)


def timeit_pixels_set_single():
    """Measure timing pixels set."""
    print("*** pixels set single:")
    loop_count = 1000

    def _test():
        pixels[3] = (500, 40500, 1000)

    timeit_call("'pixels[3] = (500, 40500, 1000)'", _test, loop_count)

    def _test():
        pixels[3] = (0.1, 0.5, 0.9)

    timeit_call("'pixels[3] = (0.1, 0.5, 0.9)'", _test, loop_count)

    def _test():
        pixels.set_pixel(3, (500, 40500, 1000))

    timeit_call("'pixels.set_pixel(3, (500, 40500, 1000))'", _test, loop_count)

    def _test():
        pixels.set_pixel(3, (0.1, 0.5, 0.9))

    timeit_call("'pixels.set_pixel(3, (0.1, 0.5, 0.9))'", _test, loop_count)


def timeit_pixels_set_loop():
    """Measure timing pixels set."""
    print("*** pixels set loop:")
    loop_count = 10

    def _test():
        for i in range(PIXEL_COUNT):
            pixels[i] = (500, 40500, 1000)

    timeit_call(
        "'pixels[0..{}] = (500, 40500, 1000)'".format(PIXEL_COUNT), _test, loop_count
    )

    def _test():
        for i in range(PIXEL_COUNT):
            pixels[i] = (0.1, 0.5, 0.9)

    timeit_call(
        "'pixels[0..{}] = (0.1, 0.5, 0.9)'".format(PIXEL_COUNT), _test, loop_count
    )

    def _test():
        for i in range(PIXEL_COUNT):
            pixels.set_pixel(i, (500, 40500, 1000))

    timeit_call(
        "'pixels.set_pixel(0..{}, (500, 40500, 1000))'".format(PIXEL_COUNT),
        _test,
        loop_count,
    )

    def _test():
        for i in range(PIXEL_COUNT):
            pixels.set_pixel(i, (0.1, 0.5, 0.9))

    timeit_call(
        "'pixels.set_pixel(0..{}, (0.1, 0.5, 0.9))'".format(PIXEL_COUNT),
        _test,
        loop_count,
    )


def timeit_pixels_set_all():
    """Measure timing pixels set."""
    print("*** pixels set all:")
    loop_count = 10

    def _test():
        pixels.set_pixel_all((500, 40500, 1000))

    timeit_call("'pixels.set_pixel_all((500, 40500, 1000))'", _test, loop_count)

    def _test():
        pixels.set_pixel_all((0.1, 0.5, 0.9))

    timeit_call("'pixels.set_pixel_all((0.1, 0.5, 0.9))'", _test, loop_count)

    def _test():
        pixels.set_pixel_all_16bit_value(500, 40500, 1000)

    timeit_call(
        "'pixels.set_pixel_all_16bit_value(500, 40500, 1000)'", _test, loop_count
    )

    def _test():
        pixels.set_all_black()

    timeit_call("'pixels.set_all_black()'", _test, loop_count)


def timeit_pixels_set_16bit():
    """Measure timing pixels set."""
    print("*** pixels set 16bit:")
    loop_count = 1000

    def _test():
        pixels.set_pixel_16bit_value(3, 500, 40500, 1000)

    timeit_call(
        "'pixels.set_pixel_16bit_value(3, 500, 40500, 1000)'", _test, loop_count
    )

    def _test():
        pixels.set_pixel_16bit_color(3, (500, 40500, 1000))

    timeit_call(
        "'pixels.set_pixel_16bit_color(3, (500, 40500, 1000))'", _test, loop_count
    )

    def _test():
        for i in range(PIXEL_COUNT):
            pixels.set_pixel_16bit_value(i, 500, 40500, 1000)

    timeit_call(
        "'pixels.set_pixel_16bit_value(0..{}, 500, 40500, 1000)'"
        "".format(PIXEL_COUNT),
        _test,
        10,
    )

    def _test():
        for i in range(PIXEL_COUNT):
            pixels.set_pixel_16bit_color(i, (500, 40500, 1000))

    timeit_call(
        "'pixels.set_pixel_16bit_color(0..{}, (500, 40500, 1000))'"
        "".format(PIXEL_COUNT),
        _test,
        10,
    )


def timeit_pixels_set_float():
    """Measure timing pixels set."""
    print("*** pixels set float:")
    loop_count = 1000

    def _test():
        pixels.set_pixel_float_value(3, 0.1, 0.5, 0.9)

    timeit_call("'pixels.set_pixel_float_value(3, 0.1, 0.5, 0.9)'", _test, loop_count)

    def _test():
        pixels.set_pixel_float_color(3, (0.1, 0.5, 0.9))

    timeit_call("'pixels.set_pixel_float_color(3, (0.1, 0.5, 0.9))'", _test, loop_count)

    def _test():
        for i in range(PIXEL_COUNT):
            pixels.set_pixel_float_value(i, 0.1, 0.5, 0.9)

    timeit_call(
        "'pixels.set_pixel_float_value(0..{}, 0.1, 0.5, 0.9)'" "".format(PIXEL_COUNT),
        _test,
        10,
    )

    def _test():
        for i in range(PIXEL_COUNT):
            pixels.set_pixel_float_color(i, (0.1, 0.5, 0.9))

    timeit_call(
        "'pixels.set_pixel_float_color(0..{}, (0.1, 0.5, 0.9))'" "".format(PIXEL_COUNT),
        _test,
        10,
    )

    def _test():
        for i in range(PIXEL_COUNT):
            pixels.set_pixel_16bit_value(
                i, int(0.1 * 65535), int(0.5 * 65535), int(0.9 * 65535)
            )

    timeit_call(
        "'pixels.set_pixel_16bit_value(0..{}, f2i 0.1, f2i 0.5, f2i 0.9)'"
        "".format(PIXEL_COUNT),
        _test,
        10,
    )


def timeit_channel_set():
    """Measure timing channel set."""
    print("*** channel set:")
    loop_count = 1000

    def _test():
        pixels.set_channel(0, 10000)

    timeit_call("'set_channel(0, 10000)'", _test, loop_count)

    def _test():
        pixels.set_channel(0, 10000)
        pixels.set_channel(1, 10000)
        pixels.set_channel(2, 10000)

    timeit_call("'set_channel(0..2, 10000)'", _test, loop_count)

    channel_count = PIXEL_COUNT * 3

    def _test():
        for i in range(channel_count):
            pixels.set_channel(i, 500)

    timeit_call("'set_channel(for 0..{}, 10000)'" "".format(channel_count), _test, 10)


def timeit_channel_set_internal():
    """Measure timing channel set internal."""
    print("*** channel set internal:")
    # loop_count = 1000
    #
    # def _test():
    #     pixels._set_channel_16bit_value(0, 10000)
    # timeit_call(
    #     "'_set_channel_16bit_value(0, 10000)'",
    #     _test,
    #     loop_count
    # )
    #
    # def _test():
    #     pixels._set_channel_16bit_value(0, 10000)
    #     pixels._set_channel_16bit_value(1, 10000)
    #     pixels._set_channel_16bit_value(2, 10000)
    # timeit_call(
    #     "'_set_channel_16bit_value(0..2, 10000)'",
    #     _test,
    #     loop_count
    # )
    #
    # def _test():
    #     for i in range(PIXEL_COUNT * 3):
    #         pixels._set_channel_16bit_value(i, 500)
    # timeit_call(
    #     "'_set_channel_16bit_value(for 0..{}, 10000)'"
    #     "".format(PIXEL_COUNT * 3),
    #     _test,
    #     10
    # )
    print("    must be uncommented in code to work..")


def timeit_pixels_get():
    """Measure timing pixels get."""
    print("*** pixels get:")

    pixels.set_pixel_all((1, 11, 111))

    def _test():
        print("[", end="")
        for i in range(PIXEL_COUNT):
            print("{}:{}, ".format(i, pixels[i]), end="")
        print("]")

    timeit_call("'print('{}:{}, '.format(i, pixels[i]), end='')'", _test, 1)


def time_measurement():
    """Measure timing."""
    print("meassure timing:")
    timeit_pixels_show()
    timeit_pixels_set_single()
    timeit_pixels_set_loop()
    timeit_pixels_set_all()
    timeit_pixels_set_16bit()
    timeit_pixels_set_float()
    timeit_channel_set()
    timeit_channel_set_internal()
    timeit_pixels_get()
    pixels.set_pixel_all((0, 1, 1))


##########################################


def test_bcdata():
    """Test BC-Data setting."""
    print("test BC-Data setting:")
    print("set pixel all to 100, 100, 100")
    pixels.set_pixel_all((100, 100, 100))
    pixels.show()
    time.sleep(2)
    print(
        "bcr: {:>3}\n"
        "bcg: {:>3}\n"
        "bcb: {:>3}\n"
        "".format(
            pixels.bcr,
            pixels.bcg,
            pixels.bcb,
        )
    )
    # calculate bc values
    Ioclmax = adafruit_tlc59711.TLC59711.calculate_Ioclmax(Riref=2.7)
    print("Ioclmax = {}".format(Ioclmax))
    Riref = adafruit_tlc59711.TLC59711.calculate_Riref(Ioclmax=Ioclmax)
    print("Riref = {}".format(Riref))
    BCValues = adafruit_tlc59711.TLC59711.calculate_BCData(
        Ioclmax=Ioclmax,
        IoutR=18,
        IoutG=11,
        IoutB=13,
    )
    # (127, 77, 91)
    print("BCValues = {}".format(BCValues))

    print("set bcX")
    pixels.bcr = BCValues[0]
    pixels.bcg = BCValues[1]
    pixels.bcb = BCValues[2]
    pixels.update_BCData()
    pixels.show()
    print(
        "bcr: {:>3}\n"
        "bcg: {:>3}\n"
        "bcb: {:>3}\n"
        "".format(
            pixels.bcr,
            pixels.bcg,
            pixels.bcb,
        )
    )
    time.sleep(2)


##########################################


def test_main():
    """Test Main."""
    print(42 * "*", end="")
    print(__doc__, end="")
    print(42 * "*")
    # print()
    # time.sleep(0.5)
    # print(42 * '*')

    pixels.set_pixel_all_16bit_value(1, 10, 100)
    pixels.show()
    time.sleep(0.5)

    test_bcdata()
    time.sleep(0.5)
    print(42 * "*")

    time_measurement()
    time.sleep(0.5)
    print(42 * "*")

    offset = 0

    print("loop:")
    while True:
        offset = channelcheck_update(offset)
        time.sleep(0.5)
        print(offset)


##########################################
# main loop

if __name__ == "__main__":

    test_main()

Single Chip Autoshow

This makes it very slow on lots of pixel changes but is convenient for only a handful of pixels.

examples/tlc59711_singlechip_autoshow.py
 1
 2
 3
 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# CircuitPython

# SPDX-FileCopyrightText: 2021 s-light
# SPDX-License-Identifier: MIT
# Author Stefan Krüger (s-light)

"""TLC5971 / TLC59711 Example."""

__doc__ = """
tlc59711_singlechip_autoshow.py - TLC59711AutoShow minimal usage example.

simple demo of the TLC59711 16-bit 12 channel LED PWM driver.
Shows the minimal usage - how to set pixel values.
the TLC59711AutoShow class automatically writes the pixel values on each change.
this makes it very slow on lots of pixel changs but is convenient for only a handfull of pixels..

Author: Tony DiCola, Stefan Krueger

Enjoy the colors :-)
"""

import board
import busio

import adafruit_tlc59711

print(__doc__)

# Define SPI bus connected to chip.
# You only need the clock and MOSI (output) line to use this chip.
spi = busio.SPI(board.SCK, MOSI=board.MOSI)
pixels = adafruit_tlc59711.TLC59711AutoShow(spi)

# Ways to set the values:
# just a list or tuple with 3 integer values: R G B
# each 0 - 65535 or 0.0 - 1.0
# every chip has 4 RGB-LEDs (=12 Channel)
pixels[0] = (100, 100, 10111)
pixels[1] = (0, 0, 100)
pixels[2] = (0.01, 0.0, 0.01)
pixels[3] = (0.1, 0.01, 0.0)

# You can also explicitly control each R0, G0, B0, R1, B1, etc. channel of the first ic
# by getting and setting its 16-bit value directly with properties.
# For example set channel 2 to 1/4 green (i.e. G2):
pixels.g2 = 65535 // 4

# there are a bunch of other advanced ways to set pixel.
# have a look at the other examples.

Brightness Correction Data

Test brightness correction data (BC)

examples/tlc59711_simpletest.py
 1
 2
 3
 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
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# simple demo of the TLC59711 16-bit 12 channel LED PWM driver.
# Shows the minimal usage - how to set pixel values in a few ways.
# Author: Tony DiCola

import board
import busio

import adafruit_tlc59711

print("tlc59711_simpletest.py")

# Define SPI bus connected to chip.
# You only need the clock and MOSI (output) line to use this chip.
spi = busio.SPI(board.SCK, MOSI=board.MOSI)
pixels = adafruit_tlc59711.TLC59711(spi, pixel_count=16)

# examples how to set the pixels:
# range:
# 0 - 65535
# or
# 0.0 - 1.0
# every pixel needs a color -
# give it just a list or tuple with 3 integer values: R G B

# set all pixels to a very low level
pixels.set_pixel_all((10, 10, 10))

# every chip has 4 Pixels (=RGB-LEDs = 12 Channel)
pixels[0] = (100, 100, 100)
pixels[1] = (0, 0, 100)
pixels[2] = (0.01, 0.0, 0.01)
pixels[3] = (0.1, 0.01, 0.0)
# if you are ready to show your values you have to call
pixels.show()

# there are a bunch of other ways to set pixel.
# have a look at the other examples.

Fastset test

Showcases the usage of set_pixel_16bit_value for fastest setting of values.

examples/tlc59711_fastset.py
 1
 2
 3
 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# CircuitPython

# SPDX-FileCopyrightText: 2021 s-light
# SPDX-License-Identifier: MIT
# Author Stefan Krüger (s-light)

"""TLC5971 / TLC59711 Example."""

__doc__ = """
tlc59711_fastset.py - TLC59711 fast set example.

showcases the usage of set_pixel_16bit_value for fastest setting of values.
for speed comparision of all the available set calls
look at the tlc59711_dev.py file.

Enjoy the colors :-)
"""


import time

import board
import busio

import adafruit_tlc59711


##########################################
PIXEL_COUNT = 16

spi = busio.SPI(board.SCK, MOSI=board.MOSI)
pixels = adafruit_tlc59711.TLC59711(spi, pixel_count=PIXEL_COUNT)


##########################################
# test function


def channelcheck_update_pixel(offset):
    """Channel check pixel."""
    # print("offset", offset)

    pixels.set_pixel_16bit_value(offset, 1000, 100, 0)
    # clear last pixel
    last = offset - 1
    if last < 0:
        last = PIXEL_COUNT - 1
    pixels.set_pixel_16bit_value(last, 0, 0, 1)
    pixels.show()

    offset += 1
    if offset >= PIXEL_COUNT:
        time.sleep(0.2)
        offset = 0
        print("clear")
        pixels.set_pixel_all_16bit_value(0, 1, 0)
        pixels.show()
    return offset


def test_main():
    """Test Main."""
    print(42 * "*", end="")
    print(__doc__, end="")
    print(42 * "*")

    bcvalues = adafruit_tlc59711.TLC59711.calculate_BCData(
        Ioclmax=18,
        IoutR=18,
        IoutG=11,
        IoutB=13,
    )
    print("bcvalues = {}".format(bcvalues))
    pixels.bcr = bcvalues[0]
    pixels.bcg = bcvalues[1]
    pixels.bcb = bcvalues[2]
    pixels.update_BCData()
    pixels.show()

    offset = 0

    print("loop:")
    while True:
        offset = channelcheck_update_pixel(offset)
        time.sleep(0.2)


##########################################
# main loop

if __name__ == "__main__":

    test_main()