Skip to content

Commit 452aa2b

Browse files
ntolltannewt
authored andcommitted
Update for friendly tuple support. (#8)
Update readback and __repr__ to use tuples rather than ints for compatibility with other NeoPixel libraries and reflect these changes in the README. Fixes #7
1 parent 1142f1c commit 452aa2b

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

README.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ supercharged version of the original MicroPython driver. Its now more like a
1515
normal Python sequence and features slice support, ``repr`` and ``len`` support.
1616

1717
Colors are now stored as ints by default rather than tuples. However, you can
18-
still use the tuple syntax to set values. For example, ``0x100000`` is equivalent
19-
to ``(0x10, 0, 0)``.
18+
still use the tuple syntax to set values. For example, ``0x100000`` is
19+
equivalent to ``(0x10, 0, 0)``. To make it easier to read and control each
20+
color component, readback values are expressed as tuples.
2021

2122
.. note:: This API represents the brightness of the white pixel when present by
2223
setting the RGB channels to identical values. For example, full white is
2324
0xffffff but is actually (0, 0, 0, 0xff) in the tuple syntax. Setting a pixel
2425
value with an int will use the white pixel if the RGB channels are identical.
25-
For full, independent, control of each color component use the tuple syntax
26-
and ignore the readback value.
26+
For full, independent, control of each color component use the tuple syntax.
2727

2828
Dependencies
2929
=============
@@ -51,8 +51,9 @@ This example demonstrates the library with the single built-in NeoPixel on the
5151
pixels[0] = (10, 0, 0)
5252
5353
This example demonstrates the library with the ten built-in NeoPixels on the
54-
`Circuit Playground Express <https://www.adafruit.com/product/3333>`_. It turns off
55-
``auto_write`` so that all pixels are updated at once.
54+
`Circuit Playground Express <https://www.adafruit.com/product/3333>`_. It turns
55+
off ``auto_write`` so that all pixels are updated at once when the ``show``
56+
method is called.
5657

5758
.. code-block:: python
5859

neopixel.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ def __exit__(self, exception_type, exception_value, traceback):
9191
self.pin.deinit()
9292

9393
def __repr__(self):
94-
return "[" + ", ".join(["0x%06x" % (x,) for x in self]) + "]"
94+
return "[" + ", ".join([str(x) for x in self]) + "]"
9595

9696
def _set_item(self, index, value):
9797
offset = index * self.bpp
@@ -142,14 +142,16 @@ def __getitem__(self, index):
142142
if isinstance(index, slice):
143143
out = []
144144
for in_i in range(*index.indices(len(self.buf) // self.bpp)):
145-
out.append(self[in_i])
145+
out.append(tuple(self.buf[in_i * self.bpp + self.ORDER[i]]
146+
for i in range(self.bpp)))
146147
return out
147148
offset = index * self.bpp
148149
if self.bpp == 4:
149150
w = self.buf[offset + 3]
150151
if w != 0:
151152
return w << 16 | w << 8 | w
152-
return self.buf[offset + 1] << 16 | self.buf[offset] << 8 | self.buf[offset + 2]
153+
return tuple(self.buf[offset + self.ORDER[i]]
154+
for i in range(self.bpp))
153155

154156
def __len__(self):
155157
return len(self.buf) // self.bpp

0 commit comments

Comments
 (0)