Simple test
Ensure your device works with this simple test.
examples/tlc59711_simpletest.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# simple demo of the TLC59711 16-bit 12 channel LED PWM driver.
5# Shows the minimal usage - how to set pixel values in a few ways.
6# Author: Tony DiCola
7
8import board
9import busio
10
11import adafruit_tlc59711
12
13print("tlc59711_simpletest.py")
14
15# Define SPI bus connected to chip.
16# You only need the clock and MOSI (output) line to use this chip.
17spi = busio.SPI(board.SCK, MOSI=board.MOSI)
18pixels = adafruit_tlc59711.TLC59711(spi, pixel_count=16)
19
20# examples how to set the pixels:
21# range:
22# 0 - 65535
23# or
24# 0.0 - 1.0
25# every pixel needs a color -
26# give it just a list or tuple with 3 integer values: R G B
27
28# set all pixels to a very low level
29pixels.set_pixel_all((10, 10, 10))
30
31# every chip has 4 Pixels (=RGB-LEDs = 12 Channel)
32pixels[0] = (100, 100, 100)
33pixels[1] = (0, 0, 100)
34pixels[2] = (0.01, 0.0, 0.01)
35pixels[3] = (0.1, 0.01, 0.0)
36# if you are ready to show your values you have to call
37pixels.show()
38
39# there are a bunch of other ways to set pixel.
40# 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#!/usr/bin/env python3
2# CircuitPython
3
4# SPDX-FileCopyrightText: 2021 s-light
5# SPDX-License-Identifier: MIT
6# Author Stefan Krüger (s-light)
7
8"""TLC59711 & FancyLED."""
9
10__doc__ = """
11TLC59711 & FancyLED.
12
13this is an example for combining the TLC5957 library with FancyLED.
14Enjoy the colors :-)
15"""
16
17import adafruit_fancyled.adafruit_fancyled as fancyled
18import board
19import busio
20
21import adafruit_tlc59711
22
23##########################################
24print("\n" + (42 * "*") + "\n" + __doc__ + "\n" + (42 * "*") + "\n" + "\n")
25
26##########################################
27# print(42 * "*")
28# print("initialise digitalio pins for SPI")
29# spi_clock = digitalio.DigitalInOut(board.SCK)
30# spi_clock.direction = digitalio.Direction.OUTPUT
31# spi_mosi = digitalio.DigitalInOut(board.MOSI)
32# spi_mosi.direction = digitalio.Direction.OUTPUT
33# spi_miso = digitalio.DigitalInOut(board.MISO)
34# spi_miso.direction = digitalio.Direction.INPUT
35
36# print((42 * '*') + "\n" + "init busio.SPI")
37spi = busio.SPI(board.SCK, MOSI=board.MOSI)
38
39##########################################
40print(42 * "*")
41print("init TLC5957")
42NUM_LEDS = 16
43pixels = adafruit_tlc59711.TLC59711(
44 spi=spi,
45 pixel_count=NUM_LEDS,
46)
47
48print("pixel_count", pixels.pixel_count)
49print("chip_count", pixels.chip_count)
50print("channel_count", pixels.channel_count)
51
52
53##########################################
54# main loop
55print(42 * "*")
56print("rainbow loop")
57hue_offset = 0
58while True:
59 brightness = 0.8
60 color = fancyled.CHSV(hue_offset, 1.0, 1.0)
61 color = fancyled.gamma_adjust(color, brightness=brightness)
62 pixels.set_pixel_all(color)
63 pixels.show()
64
65 # Bigger number = faster spin
66 hue_offset += 0.000005
67 if hue_offset >= 1:
68 hue_offset = 0
69 print("heu_offset reset")
Animations Example
You should have a plethora of functions to animate your lights.
examples/tlc59711_dev.py
1#!/usr/bin/env python3
2# CircuitPython
3
4# SPDX-FileCopyrightText: 2021 s-light
5# SPDX-License-Identifier: MIT
6# Author Stefan Krüger (s-light)
7
8"""TLC5971 / TLC59711 Multi Development."""
9
10__doc__ = """
11TLC59711 development helper.
12
13this sketch contains a bunch of timing tests and other development things..
14Enjoy the colors :-)
15"""
16
17import time
18
19import board
20import busio
21
22import adafruit_tlc59711
23
24##########################################
25PIXEL_COUNT = 16 * 1
26
27spi = busio.SPI(board.SCK, MOSI=board.MOSI)
28pixels = adafruit_tlc59711.TLC59711(spi, pixel_count=PIXEL_COUNT)
29
30
31##########################################
32# test function
33
34VALUE_HIGH = 1000
35
36
37def channelcheck_update_pixel(offset):
38 """Channel check pixel."""
39 # print("offset", offset)
40
41 # pixels[offset] = (VALUE_HIGH, 0, 0)
42 pixels.set_pixel_16bit_value(offset, VALUE_HIGH, 0, 0)
43 # clear last pixel
44 last = offset - 1
45 if last < 0:
46 last = PIXEL_COUNT - 1
47 # pixels[last] = (0, 0, 1)
48 pixels.set_pixel_16bit_value(last, 0, 0, 1)
49 # pixels[offset] = (0xAAAA, 0xBBBB, 0xCCCC)
50 pixels.show()
51
52 offset += 1
53 if offset >= PIXEL_COUNT:
54 time.sleep(0.2)
55 offset = 0
56 print("clear")
57 pixels.set_pixel_all((0, 1, 0))
58 pixels.show()
59 return offset
60
61
62def channelcheck_update(offset):
63 """Channel check."""
64 # print("offset", offset)
65
66 pixels.set_channel(offset, VALUE_HIGH)
67 # clear last set channel
68 last = offset - 1
69 if last < 0:
70 last = pixels.channel_count - 1
71 pixels.set_channel(last, 0)
72 pixels.show()
73
74 offset += 1
75 if offset >= pixels.channel_count:
76 offset = 0
77 print("offset overflow. start from 0")
78 return offset
79
80
81##########################################
82
83
84def timeit_call(message, test_function, loop_count=1000):
85 """Measure timing."""
86 duration = 0
87 start_time = time.monotonic()
88 for _index in range(loop_count):
89 start_time = time.monotonic()
90 test_function()
91 end_time = time.monotonic()
92 duration += end_time - start_time
93 # print(
94 # "duration:\n"
95 # "\t{}s for {} loops\n"
96 # "\t{:.2f}ms per call"
97 # "".format(
98 # duration,
99 # loop_count,
100 # (duration/loop_count)*1000
101 # )
102 # )
103 # print(
104 # "\t{:.2f}ms per call"
105 # "".format((duration / loop_count) * 1000)
106 # )
107 # "{:>8.2f}ms".format(3.56)
108 print(f"{(duration / loop_count) * 1000:>10.4f}ms\t{message}")
109
110
111def timeit_pixels_show():
112 """Measure timing."""
113 print("*** pixels show:")
114 loop_count = 1000
115
116 def _test():
117 pixels.show()
118
119 timeit_call("'pixels.show()'", _test, loop_count)
120
121
122def timeit_pixels_set_single():
123 """Measure timing pixels set."""
124 print("*** pixels set single:")
125 loop_count = 1000
126
127 def _test():
128 pixels[3] = (500, 40500, 1000)
129
130 timeit_call("'pixels[3] = (500, 40500, 1000)'", _test, loop_count)
131
132 def _test():
133 pixels[3] = (0.1, 0.5, 0.9)
134
135 timeit_call("'pixels[3] = (0.1, 0.5, 0.9)'", _test, loop_count)
136
137 def _test():
138 pixels.set_pixel(3, (500, 40500, 1000))
139
140 timeit_call("'pixels.set_pixel(3, (500, 40500, 1000))'", _test, loop_count)
141
142 def _test():
143 pixels.set_pixel(3, (0.1, 0.5, 0.9))
144
145 timeit_call("'pixels.set_pixel(3, (0.1, 0.5, 0.9))'", _test, loop_count)
146
147
148def timeit_pixels_set_loop():
149 """Measure timing pixels set."""
150 print("*** pixels set loop:")
151 loop_count = 10
152
153 def _test():
154 for i in range(PIXEL_COUNT):
155 pixels[i] = (500, 40500, 1000)
156
157 timeit_call(f"'pixels[0..{PIXEL_COUNT}] = (500, 40500, 1000)'", _test, loop_count)
158
159 def _test():
160 for i in range(PIXEL_COUNT):
161 pixels[i] = (0.1, 0.5, 0.9)
162
163 timeit_call(f"'pixels[0..{PIXEL_COUNT}] = (0.1, 0.5, 0.9)'", _test, loop_count)
164
165 def _test():
166 for i in range(PIXEL_COUNT):
167 pixels.set_pixel(i, (500, 40500, 1000))
168
169 timeit_call(
170 f"'pixels.set_pixel(0..{PIXEL_COUNT}, (500, 40500, 1000))'",
171 _test,
172 loop_count,
173 )
174
175 def _test():
176 for i in range(PIXEL_COUNT):
177 pixels.set_pixel(i, (0.1, 0.5, 0.9))
178
179 timeit_call(
180 f"'pixels.set_pixel(0..{PIXEL_COUNT}, (0.1, 0.5, 0.9))'",
181 _test,
182 loop_count,
183 )
184
185
186def timeit_pixels_set_all():
187 """Measure timing pixels set."""
188 print("*** pixels set all:")
189 loop_count = 10
190
191 def _test():
192 pixels.set_pixel_all((500, 40500, 1000))
193
194 timeit_call("'pixels.set_pixel_all((500, 40500, 1000))'", _test, loop_count)
195
196 def _test():
197 pixels.set_pixel_all((0.1, 0.5, 0.9))
198
199 timeit_call("'pixels.set_pixel_all((0.1, 0.5, 0.9))'", _test, loop_count)
200
201 def _test():
202 pixels.set_pixel_all_16bit_value(500, 40500, 1000)
203
204 timeit_call("'pixels.set_pixel_all_16bit_value(500, 40500, 1000)'", _test, loop_count)
205
206 def _test():
207 pixels.set_all_black()
208
209 timeit_call("'pixels.set_all_black()'", _test, loop_count)
210
211
212def timeit_pixels_set_16bit():
213 """Measure timing pixels set."""
214 print("*** pixels set 16bit:")
215 loop_count = 1000
216
217 def _test():
218 pixels.set_pixel_16bit_value(3, 500, 40500, 1000)
219
220 timeit_call("'pixels.set_pixel_16bit_value(3, 500, 40500, 1000)'", _test, loop_count)
221
222 def _test():
223 pixels.set_pixel_16bit_color(3, (500, 40500, 1000))
224
225 timeit_call("'pixels.set_pixel_16bit_color(3, (500, 40500, 1000))'", _test, loop_count)
226
227 def _test():
228 for i in range(PIXEL_COUNT):
229 pixels.set_pixel_16bit_value(i, 500, 40500, 1000)
230
231 timeit_call(
232 f"'pixels.set_pixel_16bit_value(0..{PIXEL_COUNT}, 500, 40500, 1000)'",
233 _test,
234 10,
235 )
236
237 def _test():
238 for i in range(PIXEL_COUNT):
239 pixels.set_pixel_16bit_color(i, (500, 40500, 1000))
240
241 timeit_call(
242 f"'pixels.set_pixel_16bit_color(0..{PIXEL_COUNT}, (500, 40500, 1000))'",
243 _test,
244 10,
245 )
246
247
248def timeit_pixels_set_float():
249 """Measure timing pixels set."""
250 print("*** pixels set float:")
251 loop_count = 1000
252
253 def _test():
254 pixels.set_pixel_float_value(3, 0.1, 0.5, 0.9)
255
256 timeit_call("'pixels.set_pixel_float_value(3, 0.1, 0.5, 0.9)'", _test, loop_count)
257
258 def _test():
259 pixels.set_pixel_float_color(3, (0.1, 0.5, 0.9))
260
261 timeit_call("'pixels.set_pixel_float_color(3, (0.1, 0.5, 0.9))'", _test, loop_count)
262
263 def _test():
264 for i in range(PIXEL_COUNT):
265 pixels.set_pixel_float_value(i, 0.1, 0.5, 0.9)
266
267 timeit_call(
268 f"'pixels.set_pixel_float_value(0..{PIXEL_COUNT}, 0.1, 0.5, 0.9)'",
269 _test,
270 10,
271 )
272
273 def _test():
274 for i in range(PIXEL_COUNT):
275 pixels.set_pixel_float_color(i, (0.1, 0.5, 0.9))
276
277 timeit_call(
278 f"'pixels.set_pixel_float_color(0..{PIXEL_COUNT}, (0.1, 0.5, 0.9))'",
279 _test,
280 10,
281 )
282
283 def _test():
284 for i in range(PIXEL_COUNT):
285 pixels.set_pixel_16bit_value(i, int(0.1 * 65535), int(0.5 * 65535), int(0.9 * 65535))
286
287 timeit_call(
288 f"'pixels.set_pixel_16bit_value(0..{PIXEL_COUNT}, f2i 0.1, f2i 0.5, f2i 0.9)'",
289 _test,
290 10,
291 )
292
293
294def timeit_channel_set():
295 """Measure timing channel set."""
296 print("*** channel set:")
297 loop_count = 1000
298
299 def _test():
300 pixels.set_channel(0, 10000)
301
302 timeit_call("'set_channel(0, 10000)'", _test, loop_count)
303
304 def _test():
305 pixels.set_channel(0, 10000)
306 pixels.set_channel(1, 10000)
307 pixels.set_channel(2, 10000)
308
309 timeit_call("'set_channel(0..2, 10000)'", _test, loop_count)
310
311 channel_count = PIXEL_COUNT * 3
312
313 def _test():
314 for i in range(channel_count):
315 pixels.set_channel(i, 500)
316
317 timeit_call(f"'set_channel(for 0..{channel_count}, 10000)'", _test, 10)
318
319
320def timeit_channel_set_internal():
321 """Measure timing channel set internal."""
322 print("*** channel set internal:")
323 # loop_count = 1000
324 #
325 # def _test():
326 # pixels._set_channel_16bit_value(0, 10000)
327 # timeit_call(
328 # "'_set_channel_16bit_value(0, 10000)'",
329 # _test,
330 # loop_count
331 # )
332 #
333 # def _test():
334 # pixels._set_channel_16bit_value(0, 10000)
335 # pixels._set_channel_16bit_value(1, 10000)
336 # pixels._set_channel_16bit_value(2, 10000)
337 # timeit_call(
338 # "'_set_channel_16bit_value(0..2, 10000)'",
339 # _test,
340 # loop_count
341 # )
342 #
343 # def _test():
344 # for i in range(PIXEL_COUNT * 3):
345 # pixels._set_channel_16bit_value(i, 500)
346 # timeit_call(
347 # "'_set_channel_16bit_value(for 0..{}, 10000)'"
348 # "".format(PIXEL_COUNT * 3),
349 # _test,
350 # 10
351 # )
352 print(" must be uncommented in code to work..")
353
354
355def timeit_pixels_get():
356 """Measure timing pixels get."""
357 print("*** pixels get:")
358
359 pixels.set_pixel_all((1, 11, 111))
360
361 def _test():
362 print("[", end="")
363 for i in range(PIXEL_COUNT):
364 print(f"{i}:{pixels[i]}, ", end="")
365 print("]")
366
367 timeit_call("'print('{}:{}, '.format(i, pixels[i]), end='')'", _test, 1)
368
369
370def time_measurement():
371 """Measure timing."""
372 print("meassure timing:")
373 timeit_pixels_show()
374 timeit_pixels_set_single()
375 timeit_pixels_set_loop()
376 timeit_pixels_set_all()
377 timeit_pixels_set_16bit()
378 timeit_pixels_set_float()
379 timeit_channel_set()
380 timeit_channel_set_internal()
381 timeit_pixels_get()
382 pixels.set_pixel_all((0, 1, 1))
383
384
385##########################################
386
387
388def test_bcdata():
389 """Test BC-Data setting."""
390 print("test BC-Data setting:")
391 print("set pixel all to 100, 100, 100")
392 pixels.set_pixel_all((100, 100, 100))
393 pixels.show()
394 time.sleep(2)
395 print(f"bcr: {pixels.bcr:>3}\nbcg: {pixels.bcg:>3}\nbcb: {pixels.bcb:>3}\n")
396 # calculate bc values
397 Ioclmax = adafruit_tlc59711.TLC59711.calculate_Ioclmax(Riref=2.7)
398 print(f"Ioclmax = {Ioclmax}")
399 Riref = adafruit_tlc59711.TLC59711.calculate_Riref(Ioclmax=Ioclmax)
400 print(f"Riref = {Riref}")
401 BCValues = adafruit_tlc59711.TLC59711.calculate_BCData(
402 Ioclmax=Ioclmax,
403 IoutR=18,
404 IoutG=11,
405 IoutB=13,
406 )
407 # (127, 77, 91)
408 print(f"BCValues = {BCValues}")
409
410 print("set bcX")
411 pixels.bcr = BCValues[0]
412 pixels.bcg = BCValues[1]
413 pixels.bcb = BCValues[2]
414 pixels.update_BCData()
415 pixels.show()
416 print(f"bcr: {pixels.bcr:>3}\nbcg: {pixels.bcg:>3}\nbcb: {pixels.bcb:>3}\n")
417 time.sleep(2)
418
419
420##########################################
421
422
423def test_main():
424 """Test Main."""
425 print(42 * "*", end="")
426 print(__doc__, end="")
427 print(42 * "*")
428 # print()
429 # time.sleep(0.5)
430 # print(42 * '*')
431
432 pixels.set_pixel_all_16bit_value(1, 10, 100)
433 pixels.show()
434 time.sleep(0.5)
435
436 test_bcdata()
437 time.sleep(0.5)
438 print(42 * "*")
439
440 time_measurement()
441 time.sleep(0.5)
442 print(42 * "*")
443
444 offset = 0
445
446 print("loop:")
447 while True:
448 offset = channelcheck_update(offset)
449 time.sleep(0.5)
450 print(offset)
451
452
453##########################################
454# main loop
455
456if __name__ == "__main__":
457 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#!/usr/bin/env python3
2# CircuitPython
3
4# SPDX-FileCopyrightText: 2021 s-light
5# SPDX-License-Identifier: MIT
6# Author Stefan Krüger (s-light)
7
8"""TLC5971 / TLC59711 Example."""
9
10__doc__ = """
11tlc59711_singlechip_autoshow.py - TLC59711AutoShow minimal usage example.
12
13simple demo of the TLC59711 16-bit 12 channel LED PWM driver.
14Shows the minimal usage - how to set pixel values.
15the TLC59711AutoShow class automatically writes the pixel values on each change.
16this makes it very slow on lots of pixel changs but is convenient for only a handfull of pixels..
17
18Author: Tony DiCola, Stefan Krueger
19
20Enjoy the colors :-)
21"""
22
23import board
24import busio
25
26import adafruit_tlc59711
27
28print(__doc__)
29
30# Define SPI bus connected to chip.
31# You only need the clock and MOSI (output) line to use this chip.
32spi = busio.SPI(board.SCK, MOSI=board.MOSI)
33pixels = adafruit_tlc59711.TLC59711AutoShow(spi)
34
35# Ways to set the values:
36# just a list or tuple with 3 integer values: R G B
37# each 0 - 65535 or 0.0 - 1.0
38# every chip has 4 RGB-LEDs (=12 Channel)
39pixels[0] = (100, 100, 10111)
40pixels[1] = (0, 0, 100)
41pixels[2] = (0.01, 0.0, 0.01)
42pixels[3] = (0.1, 0.01, 0.0)
43
44# You can also explicitly control each R0, G0, B0, R1, B1, etc. channel of the first ic
45# by getting and setting its 16-bit value directly with properties.
46# For example set channel 2 to 1/4 green (i.e. G2):
47pixels.g2 = 65535 // 4
48
49# there are a bunch of other advanced ways to set pixel.
50# have a look at the other examples.
Brightness Correction Data
Test brightness correction data (BC)
examples/tlc59711_simpletest.py
1# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
2# SPDX-License-Identifier: MIT
3
4# simple demo of the TLC59711 16-bit 12 channel LED PWM driver.
5# Shows the minimal usage - how to set pixel values in a few ways.
6# Author: Tony DiCola
7
8import board
9import busio
10
11import adafruit_tlc59711
12
13print("tlc59711_simpletest.py")
14
15# Define SPI bus connected to chip.
16# You only need the clock and MOSI (output) line to use this chip.
17spi = busio.SPI(board.SCK, MOSI=board.MOSI)
18pixels = adafruit_tlc59711.TLC59711(spi, pixel_count=16)
19
20# examples how to set the pixels:
21# range:
22# 0 - 65535
23# or
24# 0.0 - 1.0
25# every pixel needs a color -
26# give it just a list or tuple with 3 integer values: R G B
27
28# set all pixels to a very low level
29pixels.set_pixel_all((10, 10, 10))
30
31# every chip has 4 Pixels (=RGB-LEDs = 12 Channel)
32pixels[0] = (100, 100, 100)
33pixels[1] = (0, 0, 100)
34pixels[2] = (0.01, 0.0, 0.01)
35pixels[3] = (0.1, 0.01, 0.0)
36# if you are ready to show your values you have to call
37pixels.show()
38
39# there are a bunch of other ways to set pixel.
40# 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#!/usr/bin/env python3
2# CircuitPython
3
4# SPDX-FileCopyrightText: 2021 s-light
5# SPDX-License-Identifier: MIT
6# Author Stefan Krüger (s-light)
7
8"""TLC5971 / TLC59711 Example."""
9
10__doc__ = """
11tlc59711_fastset.py - TLC59711 fast set example.
12
13showcases the usage of set_pixel_16bit_value for fastest setting of values.
14for speed comparision of all the available set calls
15look at the tlc59711_dev.py file.
16
17Enjoy the colors :-)
18"""
19
20
21import time
22
23import board
24import busio
25
26import adafruit_tlc59711
27
28##########################################
29PIXEL_COUNT = 16
30
31spi = busio.SPI(board.SCK, MOSI=board.MOSI)
32pixels = adafruit_tlc59711.TLC59711(spi, pixel_count=PIXEL_COUNT)
33
34
35##########################################
36# test function
37
38
39def channelcheck_update_pixel(offset):
40 """Channel check pixel."""
41 # print("offset", offset)
42
43 pixels.set_pixel_16bit_value(offset, 1000, 100, 0)
44 # clear last pixel
45 last = offset - 1
46 if last < 0:
47 last = PIXEL_COUNT - 1
48 pixels.set_pixel_16bit_value(last, 0, 0, 1)
49 pixels.show()
50
51 offset += 1
52 if offset >= PIXEL_COUNT:
53 time.sleep(0.2)
54 offset = 0
55 print("clear")
56 pixels.set_pixel_all_16bit_value(0, 1, 0)
57 pixels.show()
58 return offset
59
60
61def test_main():
62 """Test Main."""
63 print(42 * "*", end="")
64 print(__doc__, end="")
65 print(42 * "*")
66
67 bcvalues = adafruit_tlc59711.TLC59711.calculate_BCData(
68 Ioclmax=18,
69 IoutR=18,
70 IoutG=11,
71 IoutB=13,
72 )
73 print(f"bcvalues = {bcvalues}")
74 pixels.bcr = bcvalues[0]
75 pixels.bcg = bcvalues[1]
76 pixels.bcb = bcvalues[2]
77 pixels.update_BCData()
78 pixels.show()
79
80 offset = 0
81
82 print("loop:")
83 while True:
84 offset = channelcheck_update_pixel(offset)
85 time.sleep(0.2)
86
87
88##########################################
89# main loop
90
91if __name__ == "__main__":
92 test_main()