diff --git a/README.rst b/README.rst index d6e5095..bc36704 100644 --- a/README.rst +++ b/README.rst @@ -22,6 +22,7 @@ This driver depends on: * `Adafruit CircuitPython `_ * `Bus Device `_ +* `Pypixelbuf `_ Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading diff --git a/neopixel_spi.py b/neopixel_spi.py index 69670da..639813a 100644 --- a/neopixel_spi.py +++ b/neopixel_spi.py @@ -43,17 +43,8 @@ * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice """ -# The following creates a mock neopixel_write module to allow importing -# the CircuitPython NeoPixel module without actually providing neopixel_write. -#pylint: disable=wrong-import-position, exec-used -import sys -from types import ModuleType -MOCK_MODULE = ModuleType('mock_neopixel_write') -exec('def neopixel_write(): pass', MOCK_MODULE.__dict__) -sys.modules['neopixel_write'] = MOCK_MODULE -#pylint: enable=wrong-import-position, exec-used - -from neopixel import NeoPixel +from adafruit_pypixelbuf import PixelBuf, fill +from adafruit_bus_device.spi_device import SPIDevice __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI.git" @@ -68,7 +59,7 @@ GRBW = 'GRBW' """Green Red Blue White""" -class NeoPixel_SPI(NeoPixel): +class NeoPixel_SPI(PixelBuf): """ A sequence of neopixels. @@ -91,46 +82,19 @@ class NeoPixel_SPI(NeoPixel): pixels = neopixel_spi.NeoPixel_SPI(board.SPI(), 10) pixels.fill(0xff0000) """ - #pylint: disable=invalid-name, super-init-not-called, too-many-instance-attributes FREQ = 6400000 # 800kHz * 8, actual may be different TRST = 80e-6 # Reset code low level time def __init__(self, spi, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None): - # We can't call super().__init__() since we don't actually - # have a pin to supply it. So setup is done manually. - # - # neopixel stuff - # - self.bpp = bpp - self.n = n + + # configure bpp and pixel_order if not pixel_order: pixel_order = GRB if bpp == 3 else GRBW else: - self.bpp = bpp = len(pixel_order) - # - # pypixelbuf stuff - # - bpp, byteorder_tuple, has_white, _ = self.parse_byteorder(pixel_order) - self._pixels = n - self._bytes = bpp * n - self._byteorder = byteorder_tuple - self._byteorder_string = pixel_order - self._has_white = has_white - self._bpp = bpp - self._bytearray = bytearray(n * bpp) - self._two_buffers = True - self._rawbytearray = bytearray(n * bpp) - self._offset = 0 - self._dotstar_mode = False - self._pixel_step = bpp - self.auto_write = False - self.brightness = min(1.0, max(0, brightness)) - self.auto_write = auto_write - # - # neopixel_spi stuff - # - from adafruit_bus_device.spi_device import SPIDevice + bpp = len(pixel_order) + + # set up SPI related stuff self._spi = SPIDevice(spi, baudrate=self.FREQ) with self._spi as spibus: try: @@ -139,9 +103,16 @@ def __init__(self, spi, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_orde except AttributeError: # use nominal freq = self.FREQ - self.RESET = bytes([0]*round(freq * self.TRST / 8)) + self._reset = bytes([0]*round(freq * self.TRST / 8)) self.spibuf = bytearray(8 * n * bpp) + # everything else taken care of by base class + super().__init__(n, bytearray(n * bpp), + brightness=brightness, + rawbuf=bytearray(n * bpp), + byteorder=pixel_order, + auto_write=auto_write) + def deinit(self): """Blank out the NeoPixels.""" self.fill(0) @@ -155,7 +126,7 @@ def show(self): with self._spi as spi: # write out special byte sequence surrounded by RESET # leading RESET needed for cases where MOSI rests HI - spi.write(self.RESET + self.spibuf + self.RESET) + spi.write(self._reset + self.spibuf + self._reset) def _transmogrify(self): """Turn every BIT of buf into a special BYTE pattern.""" @@ -169,3 +140,7 @@ def _transmogrify(self): else: self.spibuf[k] = 0b11000000 # A NeoPixel 0 bit k += 1 + + def fill(self, color): + """Colors all pixels the given ***color***.""" + fill(self, color) diff --git a/requirements.txt b/requirements.txt index b5c6a11..3c95591 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ Adafruit-Blinka adafruit-circuitpython-busdevice -adafruit-circuitpython-neopixel +adafruit-circuitpython-pypixelbuf diff --git a/setup.py b/setup.py index 6f3d8d9..ccb2f5c 100644 --- a/setup.py +++ b/setup.py @@ -36,7 +36,7 @@ install_requires=[ 'Adafruit-Blinka', 'adafruit-circuitpython-busdevice', - 'adafruit-circuitpython-neopixel' + 'adafruit-circuitpython-pypixelbuf' ], # Choose your license