Skip to content

Add support for multiple chained boards #7

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

Closed
wants to merge 2 commits into from

Conversation

caternuson
Copy link
Contributor

PRing on behalf of forum poster:
https://forums.adafruit.com/viewtopic.php?f=60&t=140130&p=709433#p709428
Their code simply copy-pasted here.

@caternuson caternuson requested a review from a team November 15, 2018 22:39
Copy link
Contributor

@brennen brennen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested on hardware, but code looks good and it's well-documented.

@ladyada
Copy link
Member

ladyada commented Nov 16, 2018

hmm i would require a hardware test - even if its just 2 boards - since there's a bit of math going on here and its non-trivial changes... @brennen @kattni wanna check it next week sometime?

@brennen
Copy link
Contributor

brennen commented Nov 16, 2018

Yep, will do. Pretty sure I've got two boards here.

Copy link
Contributor

@brennen brennen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something's off here, at least on CPython / the Pi. Doesn't seem to control pins 21-23 on the second board.

@@ -50,6 +50,8 @@
# access is by design for the internal class.
#pylint: disable=protected-access

_CHANNELS = 24
_STOREBYTES = _CHANNELS + _CHANNELS/2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
_STOREBYTES = _CHANNELS + _CHANNELS/2
_STOREBYTES = int(_CHANNELS + _CHANNELS/2)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...otherwise we wind up with a float here, at least on CPython.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for finding this one! I hoped to avoid this pitfall by rephrasing _STOREBYTES = _CHANNELS * 1.5 but forgot about Python 3 division rules. So here is my fix:

Suggested change
_STOREBYTES = _CHANNELS + _CHANNELS/2
_STOREBYTES = _CHANNELS + _CHANNELS//2

which I find less bulky, but also a bit less clear.

Copy link
Contributor

@ArthurDent62 ArthurDent62 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had time to test my own code now. Sincere apologies for being so boastful to think I could do without.

Improvements to the library to allow for Python's negative index feature on the board. Changed example file because the last LED was on after each cycle.

"""
assert 0 <= key <= 23
assert 0 <= key < _CHANNELS * self._n
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To allow for Python's negative indices (adressing elements from the rear end of an array), the suggested change is needed. Otherwise the negative index adressing near the end of tlc5947_chain.py would fail.

Suggested change
assert 0 <= key < _CHANNELS * self._n
if key < 0: # allow reverse adressing with negative index
key = key + _CHANNELS * self._n
assert 0 <= key < _CHANNELS * self._n

If auto_write is enabled (the default) then the chip PWM state will
immediately be updated too, otherwise you must call write to update
the chip with the new PWM state.
"""
assert 0 <= key <= 23
assert 0 <= key < _CHANNELS * self._n
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here - enable Python's negative array index feature:

Suggested change
assert 0 <= key < _CHANNELS * self._n
if key < 0: # allow reverse adressing with negative index
key = key + _CHANNELS * self._n
assert 0 <= key < _CHANNELS * self._n

# (note this is NOT the 12-bit value supported by the chip natively) and the
# PWM channel will be updated.

# With one RGB LED hooked up to pins 0, 1, and 2, AND
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Simplify comment.

Suggested change
# With one RGB LED hooked up to pins 0, 1, and 2, AND
# Hook up one RGB LED to pins 0, 1, and 2, AND connect another RGB LED
# to pins 21, 22 and 23 of the last chained driver.

# PWM channel will be updated.

# With one RGB LED hooked up to pins 0, 1, and 2, AND
# with another RGB LED hooked up to pins 21, 22 and 23 of the last chained driver,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove.

Suggested change
# with another RGB LED hooked up to pins 21, 22 and 23 of the last chained driver,


# With one RGB LED hooked up to pins 0, 1, and 2, AND
# with another RGB LED hooked up to pins 21, 22 and 23 of the last chained driver,
# cycle the red, green, and blue pins of LED one up and down, and the other vice versa:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to below, and change to reflect code changes.

Suggested change
# cycle the red, green, and blue pins of LED one up and down, and the other vice versa:

while True:
for (pinA, pinZ) in ((redA, redZ), (greenA, greenZ), (blueA, blueZ)):
# Brighten:
print("LED A up, LED Z down")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With current code the second LED will stay lit after the cycle. Proposed change will brighten the LEDs one after the other, then dim them again, repeating for each channel:

Suggested change
print("LED A up, LED Z down")
print("LED A up")

print("LED A up, LED Z down")
for pwm in range(start_pwm, end_pwm, step):
pinA.duty_cycle = pwm
pinZ.duty_cycle = end_pwm - pwm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove.

Suggested change
pinZ.duty_cycle = end_pwm - pwm

pinA.duty_cycle = pwm
pinZ.duty_cycle = end_pwm - pwm
# tlc5947.write() # see NOTE below

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now brighten the LED on the last pins of the last driver board:

Suggested change
print("LED Z up")
for pwm in range(start_pwm, end_pwm, step):
pinZ.duty_cycle = pwm
# tlc5947.write() # see NOTE below

# tlc5947.write() # see NOTE below

# Dim:
print("LED A down, LED Z up")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dimming both LEDs now:

Suggested change
print("LED A down, LED Z up")
print("LED A and LED Z down")

print("LED A down, LED Z up")
for pwm in range(end_pwm, start_pwm, 0 - step):
pinA.duty_cycle = pwm
pinZ.duty_cycle = end_pwm - pwm
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just do the same with both LEDs:

Suggested change
pinZ.duty_cycle = end_pwm - pwm
pinZ.duty_cycle = pwm

@caternuson
Copy link
Contributor Author

This is where me in the middle kind of falls apart.

@ArthurDent62 Do you feel like you could PR this yourself at this point?

@ArthurDent62
Copy link
Contributor

@caternuson Well, see me convinced. The multiple edit sure was over the top. I have opened a new PR with all the related changes included. I've also extended the chain example to test all the available channels one by one. So you could delete this PR when you find mine. Thanks!

@kattni
Copy link
Contributor

kattni commented Nov 25, 2018

Closing in favor of #8

@kattni kattni closed this Nov 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants