Skip to content

Update the API to support pypixelbuf 2.0 #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 29 additions & 17 deletions neopixel_spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,17 @@
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
"""

# pylint: disable=ungrouped-imports
import sys
from adafruit_bus_device.spi_device import SPIDevice
from adafruit_pypixelbuf import PixelBuf, fill

if sys.implementation.version[0] < 5:
import adafruit_pypixelbuf as _pixelbuf
else:
try:
import _pixelbuf
except ImportError:
import adafruit_pypixelbuf as _pixelbuf

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI.git"
Expand All @@ -60,7 +69,7 @@
"""Green Red Blue White"""


class NeoPixel_SPI(PixelBuf):
class NeoPixel_SPI(_pixelbuf.PixelBuf):
"""
A sequence of neopixels.

Expand Down Expand Up @@ -96,6 +105,9 @@ def __init__(
pixel_order = GRB if bpp == 3 else GRBW
else:
bpp = len(pixel_order)
if isinstance(pixel_order, tuple):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if it's a string instead of a tuple?

Strings are currently used in this library, but I'm not sure what the current desire is for pixel_order? tuple? string? both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I should have left that line outside of the tuple check. That way when pixel_order is supplied we always use its length as bpp. Then we check if it is a tuple, if yes, it gets converted to a string. And if it is already a string it is good to go as is.

This fixes the problem you were seeing for RBGW as well.

order_list = [RGBW[order] for order in pixel_order]
pixel_order = "".join(order_list)

# set up SPI related stuff
self._spi = SPIDevice(spi, baudrate=self.FREQ)
Expand All @@ -111,42 +123,42 @@ def __init__(

# 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,
n, brightness=brightness, byteorder=pixel_order, auto_write=auto_write
)

def deinit(self):
"""Blank out the NeoPixels."""
self.fill(0)
self.show()

def show(self):
def __repr__(self):
return "[" + ", ".join([str(x) for x in self]) + "]"

@property
def n(self):
"""
The number of neopixels in the chain (read-only)
"""
return len(self)

def _transmit(self, buffer):
"""Shows the new colors on the pixels themselves if they haven't already
been autowritten."""
self._transmogrify()
self._transmogrify(buffer)
# pylint: disable=no-member
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)

def _transmogrify(self):
def _transmogrify(self, buffer):
"""Turn every BIT of buf into a special BYTE pattern."""
k = 0
for byte in self.buf:
byte = int(byte * self.brightness)
for byte in buffer:
# MSB first
for i in range(7, -1, -1):
if byte >> i & 0x01:
self.spibuf[k] = 0b11110000 # A NeoPixel 1 bit
else:
self.spibuf[k] = 0b11000000 # A NeoPixel 0 bit
k += 1

def fill(self, color):
"""Colors all pixels the given ***color***."""
fill(self, color)