|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2019 Carter Nelson for Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +""" |
| 23 | +`neopixel_spi` |
| 24 | +================================================================================ |
| 25 | +
|
| 26 | +SPI driven CircuitPython driver for NeoPixels. |
| 27 | +
|
| 28 | +
|
| 29 | +* Author(s): Carter Nelson |
| 30 | +
|
| 31 | +Implementation Notes |
| 32 | +-------------------- |
| 33 | +
|
| 34 | +**Hardware:** |
| 35 | +
|
| 36 | +* Hardware SPI port required on host platform. |
| 37 | +
|
| 38 | +**Software and Dependencies:** |
| 39 | +
|
| 40 | +* Adafruit Blinka: |
| 41 | + https://github.com/adafruit/Adafruit_Blinka |
| 42 | +
|
| 43 | +* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice |
| 44 | +""" |
| 45 | + |
| 46 | +from neopixel import * |
| 47 | + |
| 48 | +__version__ = "0.0.0-auto.0" |
| 49 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel_SPI.git" |
| 50 | + |
| 51 | +class NeoPixel_SPI(NeoPixel): |
| 52 | + """ |
| 53 | + A sequence of neopixels. |
| 54 | +
|
| 55 | + :param ~busio.SPI spi: The SPI bus to output neopixel data on. |
| 56 | + :param int n: The number of neopixels in the chain |
| 57 | + :param int bpp: Bytes per pixel. 3 for RGB and 4 for RGBW pixels. |
| 58 | + :param float brightness: Brightness of the pixels between 0.0 and 1.0 where 1.0 is full |
| 59 | + brightness |
| 60 | + :param bool auto_write: True if the neopixels should immediately change when set. If False, |
| 61 | + `show` must be called explicitly. |
| 62 | + :param tuple pixel_order: Set the pixel color channel order. GRBW is set by default. |
| 63 | +
|
| 64 | + Example: |
| 65 | +
|
| 66 | + .. code-block:: python |
| 67 | +
|
| 68 | + import board |
| 69 | + import neopixel |
| 70 | +
|
| 71 | + pixels = neopixel.NeoPixel_SPI(board.SPI(), 10) |
| 72 | + pixels.fill(0xff0000) |
| 73 | + """ |
| 74 | + #pylint: disable=invalid-name, super-init-not-called |
| 75 | + |
| 76 | + FREQ = 6400000 # 800kHz * 8, actual may be different |
| 77 | + TRST = 80e-6 # Reset code low level time |
| 78 | + |
| 79 | + def __init__(self, spi, n, *, bpp=3, brightness=1.0, auto_write=True, pixel_order=None): |
| 80 | + from adafruit_bus_device.spi_device import SPIDevice |
| 81 | + self._spi = SPIDevice(spi, baudrate=self.FREQ) |
| 82 | + with self._spi as spibus: |
| 83 | + try: |
| 84 | + # get actual SPI frequency |
| 85 | + freq = spibus.frequency |
| 86 | + except AttributeError: |
| 87 | + # use nominal |
| 88 | + freq = self.FREQ |
| 89 | + self.RESET = bytes([0]*round(freq*self.TRST)) |
| 90 | + self.n = n |
| 91 | + if pixel_order is None: |
| 92 | + self.order = GRBW |
| 93 | + self.bpp = bpp |
| 94 | + else: |
| 95 | + self.order = pixel_order |
| 96 | + self.bpp = len(self.order) |
| 97 | + self.buf = bytearray(self.n * self.bpp) |
| 98 | + self.spibuf = bytearray(8*len(self.buf)) |
| 99 | + # Set auto_write to False temporarily so brightness setter does _not_ |
| 100 | + # call show() while in __init__. |
| 101 | + self.auto_write = False |
| 102 | + self.brightness = brightness |
| 103 | + self.auto_write = auto_write |
| 104 | + |
| 105 | + def deinit(self): |
| 106 | + """Blank out the NeoPixels.""" |
| 107 | + for i in range(len(self.buf)): |
| 108 | + self.buf[i] = 0 |
| 109 | + self.show() |
| 110 | + |
| 111 | + def show(self): |
| 112 | + """Shows the new colors on the pixels themselves if they haven't already |
| 113 | + been autowritten.""" |
| 114 | + self._transmogrify() |
| 115 | + with self._spi as spi: |
| 116 | + # write out special byte sequence surrounded by RESET |
| 117 | + # leading RESET needed for cases where MOSI rests HI |
| 118 | + spi.write(self.RESET + self.spibuf + self.RESET) |
| 119 | + |
| 120 | + def _transmogrify(self): |
| 121 | + """Turn every BIT of buf into a special BYTE pattern.""" |
| 122 | + k = 0 |
| 123 | + for byte in self.buf: |
| 124 | + byte = int(byte * self.brightness) |
| 125 | + # MSB first |
| 126 | + for i in range(7, -1, -1): |
| 127 | + if byte >> i & 0x01: |
| 128 | + self.spibuf[k] = 0b11110000 # A NeoPixel 1 bit |
| 129 | + else: |
| 130 | + self.spibuf[k] = 0b11000000 # A NeoPixel 0 bit |
| 131 | + k += 1 |
0 commit comments