Created
February 18, 2021 17:21
-
-
Save jepler/fe317509210445bec507f6bc8d0450a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: 2016 Damien P. George | |
# SPDX-FileCopyrightText: 2017 Scott Shawcroft for Adafruit Industries | |
# SPDX-FileCopyrightText: 2019 Carter Nelson | |
# SPDX-FileCopyrightText: 2019 Roy Hooper | |
# | |
# SPDX-License-Identifier: MIT | |
""" | |
`neopixel` - PioPixl8 strip driver | |
==================================================== | |
* Author(s): Damien P. George, Scott Shawcroft, Carter Nelson, Roy Hooper | |
""" | |
# pylint: disable=ungrouped-imports | |
import sys | |
import digitalio | |
import _pixelbuf | |
import _bit_transpose | |
import rp2pio | |
import adafruit_pioasm | |
__version__ = "0.0.0-auto.0" | |
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PioPixl8.git" | |
_program = """ | |
.program piopixl8 | |
.side_set 2 | |
;; out bit 1: '595 "ser" | |
;; side-set bit 1: '595 "srclk" (shift clock) | |
;; side-set bit 2: '595 "rclk" (latch clock) | |
;; 1 iteration = 2 cycles | |
;; 8 iterations + setup = 17 cycles | |
;; 1 loop = 51 cycles + 1 delay cycle = 52 cycles | |
;; 800kHz * 52 = 41.60MHz | |
;; 125MHz / 3 = 41.67MHz | |
.wrap_target | |
set x, 7 side 2 | |
pull | |
bitloop0: | |
set pins, 1 side 0 | |
jmp x--, bitloop0 side 1 | |
set x, 7 side 2 | |
bitloop1: | |
out pins, 1 side 0 | |
jmp x--, bitloop1 side 1 | |
set x, 7 side 2 | |
bitloop2: | |
set pins, 0 side 0 | |
jmp x--, bitloop2 side 1 | |
.wrap | |
""" | |
_assembled = adafruit_pioasm.assemble(_program) | |
# Pixel color order constants | |
RGB = "RGB" | |
"""Red Green Blue""" | |
GRB = "GRB" | |
"""Green Red Blue""" | |
RGBW = "RGBW" | |
"""Red Green Blue White""" | |
GRBW = "GRBW" | |
"""Green Red Blue White""" | |
class PioPixl8(_pixelbuf.PixelBuf): | |
""" | |
A sequence of neopixels. | |
:param ~microcontroller.Pin data: The shift-register's data pin | |
:param ~microcontroller.Pin clock: The shift-register's clock pin. Must directly follow data | |
:param ~microcontroller.Pin strobe: The shift-register's strobe pi. Must directly follow clock | |
:param int n: The number of neopixels in each chain. Must be a multiple of 8 | |
:param int bpp: Bytes per pixel. 3 for RGB and 4 for RGBW pixels. | |
: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. | |
:param str pixel_order: Set the pixel color channel order. GRBW is set by default. | |
Example for Raspberry Pi Pico: | |
.. code-block:: python | |
pixels = piopixl8.PioPixl8(board.GP0, board.GP1, board.GP2, 8*30, auto_write=False) | |
pixels.fill(0xff0000) | |
pixels.show() | |
.. py:method:: PioPixl8.show() | |
Shows the new colors on the pixels themselves if they haven't already | |
been autowritten. | |
The colors may or may not be showing after this function returns because | |
it may be done asynchronously. | |
.. py:method:: PioPixl8.fill(color) | |
Colors all pixels the given ***color***. | |
.. py:attribute:: brightness | |
Overall brightness of the pixel (0 to 1.0) | |
""" | |
def __init__( | |
self, data, clock, strobe, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None | |
): | |
if n % 8: | |
raise ValueError("Length must be a multiple of 8") | |
if not pixel_order: | |
pixel_order = GRB if bpp == 3 else GRBW | |
else: | |
if isinstance(pixel_order, tuple): | |
order_list = [RGBW[order] for order in pixel_order] | |
pixel_order = "".join(order_list) | |
super().__init__( | |
n, brightness=brightness, byteorder=pixel_order, auto_write=auto_write | |
) | |
self.transposed = bytearray(bpp*n) | |
self.sm = rp2pio.StateMachine( | |
_assembled, | |
frequency=800_000 * 52, | |
init=adafruit_pioasm.assemble("set pindirs 7"), | |
first_out_pin=data, | |
out_pin_count=1, | |
first_set_pin=data, | |
set_pin_count=3, | |
first_sideset_pin=clock, | |
sideset_pin_count=2, | |
auto_pull=True, | |
out_shift_right=False, | |
pull_threshold=8, | |
) | |
def deinit(self): | |
"""Blank out the PioPixl8s and release the pin.""" | |
self.fill(0) | |
self.show() | |
self.sm.deinit() | |
def __enter__(self): | |
return self | |
def __exit__(self, exception_type, exception_value, traceback): | |
self.deinit() | |
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): | |
self.sm.write(_bit_transpose.bit_transpose(buffer, output=self.transposed)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment