Skip to content

Commit 89edb79

Browse files
authored
Improve the README and improve index checking. (#9)
Handle negative indices correctly and check index bounds. Update the readme.
1 parent 452aa2b commit 89edb79

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

README.rst

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
Introduction
3-
============
2+
Adafruit CircuitPython NeoPixel
3+
===============================
44

55
.. image:: https://readthedocs.org/projects/adafruit-circuitpython-neopixel/badge/?version=latest
66
:target: https://circuitpython.readthedocs.io/projects/neopixel/en/latest/
@@ -10,20 +10,24 @@ Introduction
1010
:target: https://gitter.im/adafruit/circuitpython?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
1111
:alt: Gitter
1212
13+
.. image :: https://img.shields.io/discord/327254708534116352.svg
14+
:target: https://adafru.it/discord
15+
:alt: Discord
16+
1317
Higher level NeoPixel driver that presents the strip as a sequence. This is a
1418
supercharged version of the original MicroPython driver. Its now more like a
1519
normal Python sequence and features slice support, ``repr`` and ``len`` support.
1620

17-
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
19-
equivalent to ``(0x10, 0, 0)``. To make it easier to read and control each
20-
color component, readback values are expressed as tuples.
21+
Colors are stored as tuples by default. However, you can also use int hex syntax
22+
to set values similar to colors on the web. For example, ``0x100000`` (``#100000``
23+
on the web) is equivalent to ``(0x10, 0, 0)``.
2124

22-
.. note:: This API represents the brightness of the white pixel when present by
23-
setting the RGB channels to identical values. For example, full white is
24-
0xffffff but is actually (0, 0, 0, 0xff) in the tuple syntax. Setting a pixel
25-
value with an int will use the white pixel if the RGB channels are identical.
26-
For full, independent, control of each color component use the tuple syntax.
25+
.. note:: The int hex API represents the brightness of the white pixel when
26+
present by setting the RGB channels to identical values. For example, full
27+
white is 0xffffff but is actually (0, 0, 0, 0xff) in the tuple syntax. Setting
28+
a pixel value with an int will use the white pixel if the RGB channels are
29+
identical. For full, independent, control of each color component use the
30+
tuple syntax.
2731

2832
Dependencies
2933
=============

neopixel.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ def __repr__(self):
9494
return "[" + ", ".join([str(x) for x in self]) + "]"
9595

9696
def _set_item(self, index, value):
97+
if index < 0:
98+
index += len(self)
99+
if index >= self.n or index < 0:
100+
raise IndexError
97101
offset = index * self.bpp
98102
r = 0
99103
g = 0
@@ -145,20 +149,17 @@ def __getitem__(self, index):
145149
out.append(tuple(self.buf[in_i * self.bpp + self.ORDER[i]]
146150
for i in range(self.bpp)))
147151
return out
152+
if index < 0:
153+
index += len(self)
154+
if index >= self.n or index < 0:
155+
raise IndexError
148156
offset = index * self.bpp
149-
if self.bpp == 4:
150-
w = self.buf[offset + 3]
151-
if w != 0:
152-
return w << 16 | w << 8 | w
153157
return tuple(self.buf[offset + self.ORDER[i]]
154158
for i in range(self.bpp))
155159

156160
def __len__(self):
157161
return len(self.buf) // self.bpp
158162

159-
def __len__(self):
160-
return self.n
161-
162163
@property
163164
def brightness(self):
164165
"""Overall brightness of the pixel"""

0 commit comments

Comments
 (0)