-
Notifications
You must be signed in to change notification settings - Fork 8
Resolving issue #6 chaining TLC boards. #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9d6d547
Resolving issue #6 chaining TLC boards
ArthurDent62 6fdb5a2
Changed line style within example section.
ArthurDent62 05c6200
Usage examples for chaining drivers.
ArthurDent62 9516917
pylinted
ArthurDent62 bb6d999
harmonizing with other example scripts
ArthurDent62 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# Simple demo of controlling a chain of several TLC5947 12-bit 24-channel PWM controllers. | ||
# Will update channel values to different PWM duty cycles. | ||
# Authors: Tony DiCola, Walter Haschka | ||
|
||
import board | ||
import busio | ||
import digitalio | ||
|
||
import adafruit_tlc5947 | ||
|
||
# Define pins connected to the TLC5947 | ||
SCK = board.SCK | ||
MOSI = board.MOSI | ||
LATCH = digitalio.DigitalInOut(board.D5) | ||
|
||
# Initialize SPI bus. | ||
spi = busio.SPI(clock=SCK, MOSI=MOSI) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to: spi = busio.SPI(board.SCK, board.MOSI) |
||
|
||
# Initialize TLC5947 | ||
DRIVER_COUNT = 2 # change this to the number of drivers you have chained | ||
tlc5947 = adafruit_tlc5947.TLC5947(spi, LATCH, num_drivers=DRIVER_COUNT) | ||
# You can optionally disable auto_write which allows you to control when | ||
# channel state is written to the chip. Normally auto_write is true and | ||
# will automatically write out changes as soon as they happen to a channel, but | ||
# if you need more control or atomic updates of multiple channels then disable | ||
# and manually call write as shown below. | ||
#tlc5947 = adafruit_tlc5947.TLC5947(spi, LATCH, num_drivers=DRIVER_COUNT, auto_write=False) | ||
|
||
# There are two ways to set channel PWM values. The first is by getting | ||
# a PWMOut object that acts like the built-in PWMOut and can be used anywhere | ||
# it is used in your code. Change the duty_cycle property to a 16-bit value | ||
# (note this is NOT the 12-bit value supported by the chip natively) and the | ||
# PWM channel will be updated. | ||
|
||
def first_last(): | ||
"""Cycles the red pin of LED one up, then the other LED; now dims the LEDs | ||
both down. Repeats with green and blue pins. Then starts all over again. | ||
|
||
Hook up one RGB LED to pins 0 (red), 1 (green), and 2 (blue), AND connect | ||
another RGB LED to pins 21, 22 and 23 of the last chained driver, respectively. | ||
""" | ||
redA = tlc5947.create_pwm_out(0) | ||
greenA = tlc5947.create_pwm_out(1) | ||
blueA = tlc5947.create_pwm_out(2) | ||
redZ = tlc5947.create_pwm_out(DRIVER_COUNT*24-3) | ||
greenZ = tlc5947.create_pwm_out(DRIVER_COUNT*24-2) | ||
blueZ = tlc5947.create_pwm_out(DRIVER_COUNT*24-1) | ||
|
||
step = 10 | ||
start_pwm = 0 | ||
end_pwm = 32767 # 50% (32767, or half of the maximum 65535): | ||
|
||
while True: | ||
for (pinA, pinZ) in ((redA, redZ), (greenA, greenZ), (blueA, blueZ)): | ||
# Brighten: | ||
print("LED A up") | ||
for pwm in range(start_pwm, end_pwm, step): | ||
pinA.duty_cycle = pwm | ||
# tlc5947.write() # see NOTE below | ||
|
||
print("LED Z up") | ||
for pwm in range(start_pwm, end_pwm, step): | ||
pinZ.duty_cycle = pwm | ||
# tlc5947.write() # see NOTE below | ||
|
||
# Dim: | ||
print("LED A and LED Z down") | ||
for pwm in range(end_pwm, start_pwm, 0 - step): | ||
pinA.duty_cycle = pwm | ||
pinZ.duty_cycle = pwm | ||
# tlc5947.write() # see NOTE below | ||
|
||
# NOTE: if auto_write was disabled you need to call write on the parent to | ||
# make sure the value is written in each loop (this is not common, if disabling | ||
# auto_write you probably want to use the direct 12-bit raw access instead, | ||
# shown next). | ||
|
||
#---------- | ||
# The other way to read and write channels is directly with each channel 12-bit | ||
# value and an item accessor syntax. Index into the TLC5947 with the channel | ||
# number (0-max) and get or set its 12-bit value (0-4095). | ||
def test_all_channels(step): | ||
"""Loops over all available channels of all connected driver boards, | ||
brightening and dimming all LEDs one after the other. With RGB LEDs, | ||
all each component is cycled. Repeats forever. | ||
|
||
:param step: the PWM increment in each cycle. Higher values makes cycling quicker. | ||
""" | ||
|
||
start_pwm = 0 | ||
end_pwm = 3072 # 75% of the maximum 4095 | ||
|
||
while True: | ||
for pin in range(DRIVER_COUNT*24): | ||
# Brighten: | ||
for pwm in range(start_pwm, end_pwm, step): | ||
tlc5947[pin] = pwm | ||
# Again be sure to call write if you disabled auto_write. | ||
#tlc5947.write() | ||
|
||
# Dim: | ||
for pwm in range(end_pwm, start_pwm, 0 -step): | ||
tlc5947[pin] = pwm | ||
# Again be sure to call write if you disabled auto_write. | ||
#tlc5947.write() | ||
|
||
#---------- | ||
# Choose here which function to try: | ||
#first_last() | ||
test_all_channels(16) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realize the other example is like this, but delete these two lines and just use the
board
pins directly in the SPI constructor (see below).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you see as advantage of the suggested change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just more inline with other SPI examples. At least more current ones. The variables aren't used anywhere else in the code, so no need to create them.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see. Thanks. Though a good compiler would find this and inline by itself, it will most probably ease understanding. I will prepare that.
Do you think that the expression for LATCH just below is to big to do the same? Basically LATCH is also used only once, it only occurs again in code commented-out.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. What you had was totally functional. So this is just cosmetic nitpicking.
I'd leave the LATCH line as is since it's an object creation. That could go in the constructor, but I think the resulting syntax isn't very beginner friendly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR updated; changes done as suggested.