Skip to content

Allow negative index with direct channel write #12

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 3 commits into from
Dec 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions adafruit_tlc5947.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ def __getitem__(self, key):
"""Retrieve the 12-bit PWM value for the specified channel (0-max).
max depends on the number of boards.
"""
if key < 0: # allow reverse adressing with negative index
key = key + _CHANNELS * self._n
assert 0 <= key < _CHANNELS * self._n
return self._get_gs_value(key)

Expand All @@ -255,6 +257,8 @@ def __setitem__(self, key, val):
immediately be updated too, otherwise you must call write to update
the chip with the new PWM state.
"""
if key < 0: # allow reverse adressing with negative index
key = key + _CHANNELS * self._n
assert 0 <= key < _CHANNELS * self._n
assert 0 <= val <= 4095
self._set_gs_value(key, val)
6 changes: 4 additions & 2 deletions examples/tlc5947_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@
# Note if auto_write was disabled you need to call write on the parent to
# make sure the value is written (this is not common, if disabling auto_write
# you probably want to use the direct 12-bit raw access instead shown below).
# tlc5947.write()
# tlc5947.write()

# 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-23) and get or set its 12-bit value (0-4095).
# For example set channel 1 to 50% duty cycle.
# tlc5947[1] = 2048
#tlc5947[1] = 2048
# Or set channel 23 (first channel from the end) to 2/3 duty cycle.
#tlc5947[-1] = 2730
# Again be sure to call write if you disabled auto_write.
#tlc5947.write()