/ adafruit_dotstar.py
adafruit_dotstar.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2016 Damien P. George (original Neopixel object) 4 # Copyright (c) 2017 Ladyada 5 # Copyright (c) 2017 Scott Shawcroft for Adafruit Industries 6 # Copyright (c) 2019 Roy Hooper 7 # 8 # Permission is hereby granted, free of charge, to any person obtaining a copy 9 # of this software and associated documentation files (the "Software"), to deal 10 # in the Software without restriction, including without limitation the rights 11 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 # copies of the Software, and to permit persons to whom the Software is 13 # furnished to do so, subject to the following conditions: 14 # 15 # The above copyright notice and this permission notice shall be included in 16 # all copies or substantial portions of the Software. 17 # 18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 # THE SOFTWARE. 25 26 """ 27 `adafruit_dotstar` - DotStar strip driver (for CircuitPython 5.0+ with _pixelbuf) 28 ================================================================================= 29 30 * Author(s): Damien P. George, Limor Fried, Scott Shawcroft & Roy Hooper 31 """ 32 33 # pylint: disable=ungrouped-imports 34 import sys 35 import busio 36 import digitalio 37 38 if sys.implementation.version[0] < 5: 39 import adafruit_pypixelbuf as _pixelbuf 40 else: 41 try: 42 import _pixelbuf 43 except ImportError: 44 import adafruit_pypixelbuf as _pixelbuf 45 46 __version__ = "0.0.0-auto.0" 47 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_DotStar.git" 48 49 START_HEADER_SIZE = 4 50 51 # Pixel color order constants 52 RBG = "PRBG" 53 """Red Blue Green""" 54 RGB = "PRGB" 55 """Red Green Blue""" 56 GRB = "PGRB" 57 """Green Red Blue""" 58 GBR = "PGBR" 59 """Green Blue Red""" 60 BRG = "PBRG" 61 """Blue Red Green""" 62 BGR = "PBGR" 63 """Blue Green Red""" 64 65 66 class DotStar(_pixelbuf.PixelBuf): 67 """ 68 A sequence of dotstars. 69 70 :param ~microcontroller.Pin clock: The pin to output dotstar clock on. 71 :param ~microcontroller.Pin data: The pin to output dotstar data on. 72 :param int n: The number of dotstars in the chain 73 :param float brightness: Brightness of the pixels between 0.0 and 1.0 74 :param bool auto_write: True if the dotstars should immediately change when 75 set. If False, `show` must be called explicitly. 76 :param str pixel_order: Set the pixel order on the strip - different 77 strips implement this differently. If you send red, and it looks blue 78 or green on the strip, modify this! It should be one of the values above. 79 :param int baudrate: Desired clock rate if using hardware SPI (ignored if 80 using 'soft' SPI). This is only a recommendation; the actual clock 81 rate may be slightly different depending on what the system hardware 82 can provide. 83 84 Example for Gemma M0: 85 86 .. code-block:: python 87 88 import adafruit_dotstar 89 import time 90 from board import * 91 92 RED = 0x100000 93 94 with adafruit_dotstar.DotStar(APA102_SCK, APA102_MOSI, 1) as pixels: 95 pixels[0] = RED 96 time.sleep(2) 97 98 .. py:method:: DotStar.show() 99 100 Shows the new colors on the dotstars themselves if they haven't already 101 been autowritten. 102 103 The colors may or may not be showing after this function returns because 104 it may be done asynchronously. 105 106 .. py:method:: DotStar.fill(color) 107 108 Colors all dotstars the given ***color***. 109 110 .. py:attribute:: brightness 111 112 Overall brightness of all dotstars (0 to 1.0) 113 """ 114 115 def __init__( 116 self, 117 clock, 118 data, 119 n, 120 *, 121 brightness=1.0, 122 auto_write=True, 123 pixel_order=BGR, 124 baudrate=4000000 125 ): 126 self._spi = None 127 try: 128 self._spi = busio.SPI(clock, MOSI=data) 129 while not self._spi.try_lock(): 130 pass 131 self._spi.configure(baudrate=baudrate) 132 133 except (NotImplementedError, ValueError): 134 self.dpin = digitalio.DigitalInOut(data) 135 self.cpin = digitalio.DigitalInOut(clock) 136 self.dpin.direction = digitalio.Direction.OUTPUT 137 self.cpin.direction = digitalio.Direction.OUTPUT 138 self.cpin.value = False 139 140 # Supply one extra clock cycle for each two pixels in the strip. 141 trailer_size = n // 16 142 if n % 16 != 0: 143 trailer_size += 1 144 145 # Four empty bytes for the header. 146 header = bytearray(START_HEADER_SIZE) 147 # 0xff bytes for the trailer. 148 trailer = bytearray(b"\xff") * trailer_size 149 150 super().__init__( 151 n, 152 byteorder=pixel_order, 153 brightness=brightness, 154 auto_write=auto_write, 155 header=header, 156 trailer=trailer, 157 ) 158 159 def deinit(self): 160 """Blank out the DotStars and release the resources.""" 161 self.fill(0) 162 self.show() 163 if self._spi: 164 self._spi.deinit() 165 else: 166 self.dpin.deinit() 167 self.cpin.deinit() 168 169 def __enter__(self): 170 return self 171 172 def __exit__(self, exception_type, exception_value, traceback): 173 self.deinit() 174 175 def __repr__(self): 176 return "[" + ", ".join([str(x) for x in self]) + "]" 177 178 @property 179 def n(self): 180 """ 181 The number of dotstars in the chain (read-only) 182 """ 183 return len(self) 184 185 def _transmit(self, buffer): 186 if self._spi: 187 self._spi.write(buffer) 188 else: 189 self._ds_writebytes(buffer) 190 191 def _ds_writebytes(self, buffer): 192 for b in buffer: 193 for _ in range(8): 194 self.dpin.value = b & 0x80 195 self.cpin.value = True 196 self.cpin.value = False 197 b = b << 1 198 self.cpin.value = False