Skip to content

Make things tunable via kwargs #22

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 3 commits into from
Sep 9, 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
42 changes: 30 additions & 12 deletions neopixel_spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,13 @@ class NeoPixel_SPI(_pixelbuf.PixelBuf):
:param float brightness: Brightness of the pixels between 0.0 and 1.0 where 1.0 is full
brightness
:param bool auto_write: True if the neopixels should immediately change when set. If False,
`show` must be called explicitly.
``show`` must be called explicitly.
:param tuple pixel_order: Set the pixel color channel order. GRBW is set by default.
:param int frequency: SPI bus frequency. For 800kHz NeoPixels, use 6400000 (default).
For 400kHz, use 3200000.
:param float reset_time: Reset low level time in seconds. Default is 80e-6.
:param byte bit0: Bit pattern to set timing for a NeoPixel 0 bit.
:param byte bit1: Bit pattern to set timing for a NeoPixel 1 bit.

Example:

Expand All @@ -93,11 +98,19 @@ class NeoPixel_SPI(_pixelbuf.PixelBuf):
pixels.fill(0xff0000)
"""

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
self,
spi,
n,
*,
bpp=3,
brightness=1.0,
auto_write=True,
pixel_order=None,
frequency=6400000,
reset_time=80e-6,
bit0=0b11000000,
bit1=0b11110000
):

# configure bpp and pixel_order
Expand All @@ -109,17 +122,22 @@ def __init__(
order_list = [RGBW[order] for order in pixel_order]
pixel_order = "".join(order_list)

# neopixel stuff
self._bit0 = bit0
self._bit1 = bit1
self._trst = reset_time

# set up SPI related stuff
self._spi = SPIDevice(spi, baudrate=self.FREQ)
self._spi = SPIDevice(spi, baudrate=frequency)
with self._spi as spibus:
try:
# get actual SPI frequency
freq = spibus.frequency
except AttributeError:
# use nominal
freq = self.FREQ
self._reset = bytes([0] * round(freq * self.TRST / 8))
self.spibuf = bytearray(8 * n * bpp)
freq = frequency
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__(
Expand Down Expand Up @@ -149,7 +167,7 @@ def _transmit(self, buffer):
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, buffer):
"""Turn every BIT of buf into a special BYTE pattern."""
Expand All @@ -158,7 +176,7 @@ def _transmogrify(self, buffer):
# MSB first
for i in range(7, -1, -1):
if byte >> i & 0x01:
self.spibuf[k] = 0b11110000 # A NeoPixel 1 bit
self._spibuf[k] = self._bit1 # A NeoPixel 1 bit
else:
self.spibuf[k] = 0b11000000 # A NeoPixel 0 bit
self._spibuf[k] = self._bit0 # A NeoPixel 0 bit
k += 1