From 89edb79a9fbe6a4a9097fea48bad0aefc9c96209 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 6 Sep 2017 12:17:21 -0700 Subject: [PATCH] Improve the README and improve index checking. (#9) Handle negative indices correctly and check index bounds. Update the readme. --- README.rst | 26 +++++++++++++++----------- neopixel.py | 15 ++++++++------- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/README.rst b/README.rst index 7496d4d..8ea05fe 100644 --- a/README.rst +++ b/README.rst @@ -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/ @@ -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 ============= diff --git a/neopixel.py b/neopixel.py index ae663d9..121fe06 100644 --- a/neopixel.py +++ b/neopixel.py @@ -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 @@ -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"""