Skip to content

Commit 7799703

Browse files
authored
Merge pull request #12 from ArthurDent62/negative_indices
Allow negative index with direct channel write
2 parents f983146 + e6e706f commit 7799703

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

adafruit_tlc5947.py

+4
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ def __getitem__(self, key):
245245
"""Retrieve the 12-bit PWM value for the specified channel (0-max).
246246
max depends on the number of boards.
247247
"""
248+
if key < 0: # allow reverse adressing with negative index
249+
key = key + _CHANNELS * self._n
248250
assert 0 <= key < _CHANNELS * self._n
249251
return self._get_gs_value(key)
250252

@@ -255,6 +257,8 @@ def __setitem__(self, key, val):
255257
immediately be updated too, otherwise you must call write to update
256258
the chip with the new PWM state.
257259
"""
260+
if key < 0: # allow reverse adressing with negative index
261+
key = key + _CHANNELS * self._n
258262
assert 0 <= key < _CHANNELS * self._n
259263
assert 0 <= val <= 4095
260264
self._set_gs_value(key, val)

examples/tlc5947_simpletest.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@
5757
# Note if auto_write was disabled you need to call write on the parent to
5858
# make sure the value is written (this is not common, if disabling auto_write
5959
# you probably want to use the direct 12-bit raw access instead shown below).
60-
# tlc5947.write()
60+
# tlc5947.write()
6161

6262
# The other way to read and write channels is directly with each channel 12-bit
6363
# value and an item accessor syntax. Index into the TLC5947 with the channel
6464
# number (0-23) and get or set its 12-bit value (0-4095).
6565
# For example set channel 1 to 50% duty cycle.
66-
# tlc5947[1] = 2048
66+
#tlc5947[1] = 2048
67+
# Or set channel 23 (first channel from the end) to 2/3 duty cycle.
68+
#tlc5947[-1] = 2730
6769
# Again be sure to call write if you disabled auto_write.
6870
#tlc5947.write()

0 commit comments

Comments
 (0)