Skip to content

Change base class to pypixelbuf #10

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 4 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This driver depends on:

* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
* `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
* `Pypixelbuf <https://github.com/adafruit/Adafruit_CircuitPython_Pypixelbuf>`_

Please ensure all dependencies are available on the CircuitPython filesystem.
This is easily achieved by downloading
Expand Down
67 changes: 21 additions & 46 deletions neopixel_spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -68,7 +59,7 @@
GRBW = 'GRBW'
"""Green Red Blue White"""

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

Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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."""
Expand All @@ -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)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Adafruit-Blinka
adafruit-circuitpython-busdevice
adafruit-circuitpython-neopixel
adafruit-circuitpython-pypixelbuf
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
install_requires=[
'Adafruit-Blinka',
'adafruit-circuitpython-busdevice',
'adafruit-circuitpython-neopixel'
'adafruit-circuitpython-pypixelbuf'
],

# Choose your license
Expand Down