Skip to content

Commit

Permalink
Improve the README and improve index checking. (#9)
Browse files Browse the repository at this point in the history
Handle negative indices correctly and check index bounds. Update the readme.
  • Loading branch information
tannewt authored Sep 6, 2017
1 parent 452aa2b commit 89edb79
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
26 changes: 15 additions & 11 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

Introduction
============
Adafruit CircuitPython NeoPixel
===============================

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

Colors are now stored as ints by default rather than tuples. However, you can
still use the tuple syntax to set values. For example, ``0x100000`` is
equivalent to ``(0x10, 0, 0)``. To make it easier to read and control each
color component, readback values are expressed as tuples.
Colors are stored as tuples by default. However, you can also use int hex syntax
to set values similar to colors on the web. For example, ``0x100000`` (``#100000``
on the web) is equivalent to ``(0x10, 0, 0)``.

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

Dependencies
=============
Expand Down
15 changes: 8 additions & 7 deletions neopixel.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ def __repr__(self):
return "[" + ", ".join([str(x) for x in self]) + "]"

def _set_item(self, index, value):
if index < 0:
index += len(self)
if index >= self.n or index < 0:
raise IndexError
offset = index * self.bpp
r = 0
g = 0
Expand Down Expand Up @@ -145,20 +149,17 @@ def __getitem__(self, index):
out.append(tuple(self.buf[in_i * self.bpp + self.ORDER[i]]
for i in range(self.bpp)))
return out
if index < 0:
index += len(self)
if index >= self.n or index < 0:
raise IndexError
offset = index * self.bpp
if self.bpp == 4:
w = self.buf[offset + 3]
if w != 0:
return w << 16 | w << 8 | w
return tuple(self.buf[offset + self.ORDER[i]]
for i in range(self.bpp))

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

def __len__(self):
return self.n

@property
def brightness(self):
"""Overall brightness of the pixel"""
Expand Down

0 comments on commit 89edb79

Please sign in to comment.