|
| 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 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in |
| 15 | +# all copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | +# THE SOFTWARE. |
| 24 | + |
| 25 | +""" |
| 26 | +`adafruit_dotstar` - DotStar strip driver |
| 27 | +==================================================== |
| 28 | +
|
| 29 | +* Author(s): Damien P. George, Limor Fried & Scott Shawcroft |
| 30 | +""" |
| 31 | +import busio |
| 32 | +import digitalio |
| 33 | +import time |
| 34 | + |
| 35 | +class DotStar: |
| 36 | + """ |
| 37 | + A sequence of dotstars. |
| 38 | +
|
| 39 | + :param ~microcontroller.Pin clock: The pin to output dotstar clock on. |
| 40 | + :param ~microcontroller.Pin data: The pin to output dotstar data on. |
| 41 | + :param int n: The number of dotstars in the chain |
| 42 | + :param float brightness: Brightness of the pixels between 0.0 and 1.0 |
| 43 | + :param bool auto_write: True if the neopixels should immediately change when set. If False, `show` must be called explicitly. |
| 44 | +
|
| 45 | +
|
| 46 | + Example for Gemma M0: |
| 47 | +
|
| 48 | + .. code-block:: python |
| 49 | +
|
| 50 | + import adafruit_dotstar |
| 51 | + import time |
| 52 | + from board import * |
| 53 | +
|
| 54 | + RED = 0x100000 |
| 55 | +
|
| 56 | + with adafruit_dotstar.DotStar(APA102_SCK, APA102_MOSI, 1) as pixels: |
| 57 | + pixels[0] = RED |
| 58 | + time.sleep(2) |
| 59 | + """ |
| 60 | + |
| 61 | + def __init__(self, clock, data, n, brightness=1.0, auto_write=True): |
| 62 | + self.spi = None |
| 63 | + try: |
| 64 | + self.spi = busio.SPI(clock, MOSI=data) |
| 65 | + while not self.spi.try_lock(): |
| 66 | + pass |
| 67 | + self.spi.configure(baudrate=4000000) |
| 68 | + except ValueError: |
| 69 | + self.dpin = digitalio.DigitalInOut(data) |
| 70 | + self.cpin = digitalio.DigitalInOut(clock) |
| 71 | + self.dpin.direction = digitalio.Direction.OUTPUT |
| 72 | + self.cpin.direction = digitalio.Direction.OUTPUT |
| 73 | + self.cpin.value = False |
| 74 | + self.n = n |
| 75 | + self.start_header = 4 |
| 76 | + # Supply one extra clock cycle for each two pixels in the strip. |
| 77 | + self.end_header = n // 16 |
| 78 | + if n % 16 != 0: |
| 79 | + n += 1 |
| 80 | + self.buf = bytearray(n * 4 + self.start_header + self.end_header) |
| 81 | + # Four empty bytes to start. |
| 82 | + for i in range(self.start_header): |
| 83 | + self.buf[i] = 0x00 |
| 84 | + # Mark the beginnings of each pixel. |
| 85 | + for i in range(n): |
| 86 | + self.buf[self.start_header + 4 * i] = 0xff |
| 87 | + # 0xff bytes at the end. |
| 88 | + for i in range(self.end_header): |
| 89 | + self.buf[len(self.buf) - 1 - i] = 0xff |
| 90 | + self.brightness = brightness |
| 91 | + self.auto_write = auto_write |
| 92 | + |
| 93 | + def deinit(self): |
| 94 | + """Blank out the DotStars and release the resources.""" |
| 95 | + self.auto_write = False |
| 96 | + for i in range(self.start_header, len(self.buf) - self.end_header): |
| 97 | + # Preserve the pixel markers. |
| 98 | + if i % 4 == 0: |
| 99 | + continue |
| 100 | + self.buf[i] = 0 |
| 101 | + self.show() |
| 102 | + if self.spi: |
| 103 | + self.spi.deinit() |
| 104 | + else: |
| 105 | + self.dpin.deinit() |
| 106 | + self.cpin.deinit() |
| 107 | + |
| 108 | + def __enter__(self): |
| 109 | + return self |
| 110 | + |
| 111 | + def __exit__(self, exception_type, exception_value, traceback): |
| 112 | + self.deinit() |
| 113 | + |
| 114 | + def __repr__(self): |
| 115 | + return "[" + ", ".join([str(x) for x in self]) + "]" |
| 116 | + |
| 117 | + def _set_item(self, index, value): |
| 118 | + offset = index * 4 + self.start_header |
| 119 | + r = 0 |
| 120 | + g = 0 |
| 121 | + b = 0 |
| 122 | + if type(value) == int: |
| 123 | + r = value >> 16 |
| 124 | + g = (value >> 8) & 0xff |
| 125 | + b = value & 0xff |
| 126 | + else: |
| 127 | + r, g, b = value |
| 128 | + # Each pixel starts with 0xFF, then red/green/blue. Although the data |
| 129 | + # sheet suggests using a global brightness in the first byte, we don't |
| 130 | + # do that because it causes further issues with persistence of vision |
| 131 | + # projects. |
| 132 | + self.buf[offset] = 0xff |
| 133 | + self.buf[offset + 1] = b |
| 134 | + self.buf[offset + 2] = g |
| 135 | + self.buf[offset + 3] = r |
| 136 | + |
| 137 | + def __setitem__(self, index, val): |
| 138 | + if isinstance(index, slice): |
| 139 | + start, stop, step = index.indices(len(self)) |
| 140 | + length = stop - start |
| 141 | + if step != 0: |
| 142 | + length = math.ceil(length / step) |
| 143 | + if len(val) != length: |
| 144 | + raise ValueError("Slice and input sequence size do not match.") |
| 145 | + for val_i, in_i in enumerate(range(start, stop, step)): |
| 146 | + self._set_item(in_i, val[val_i]) |
| 147 | + else: |
| 148 | + self._set_item(index, val) |
| 149 | + |
| 150 | + if self.auto_write: |
| 151 | + self.show() |
| 152 | + |
| 153 | + def __getitem__(self, index): |
| 154 | + if isinstance(index, slice): |
| 155 | + out = [] |
| 156 | + for in_i in range(*index.indices(len(self.buf) // self.bpp)): |
| 157 | + out.append(tuple(self.buf[in_i * 4 + (3 - i) + self.start_header] |
| 158 | + for i in range(3))) |
| 159 | + return out |
| 160 | + if index < 0: |
| 161 | + index += len(self) |
| 162 | + if index >= self.n or index < 0: |
| 163 | + raise IndexError |
| 164 | + offset = index * 4 |
| 165 | + return tuple(self.buf[offset + (3 - i) + self.start_header] |
| 166 | + for i in range(3)) |
| 167 | + |
| 168 | + def __len__(self): |
| 169 | + return self.n |
| 170 | + |
| 171 | + @property |
| 172 | + def brightness(self): |
| 173 | + """Overall brightness of the pixel""" |
| 174 | + return self._brightness |
| 175 | + |
| 176 | + @brightness.setter |
| 177 | + def brightness(self, brightness): |
| 178 | + self._brightness = min(max(brightness, 0.0), 1.0) |
| 179 | + |
| 180 | + def fill(self, color): |
| 181 | + """Colors all pixels the given ***color***.""" |
| 182 | + auto_write = self.auto_write |
| 183 | + self.auto_write = False |
| 184 | + for i in range(len(self)): |
| 185 | + self[i] = color |
| 186 | + if auto_write: |
| 187 | + self.show() |
| 188 | + self.auto_write = auto_write |
| 189 | + |
| 190 | + def ds_writebytes(self, buf): |
| 191 | + for b in buf: |
| 192 | + for i in range(8): |
| 193 | + self.cpin.value = True |
| 194 | + self.dpin.value = (b & 0x80) |
| 195 | + self.cpin.value = False |
| 196 | + b = b << 1 |
| 197 | + |
| 198 | + def show(self): |
| 199 | + """Shows the new colors on the pixels themselves if they haven't already |
| 200 | + been autowritten. |
| 201 | +
|
| 202 | + The colors may or may not be showing after this function returns because |
| 203 | + it may be done asynchronously.""" |
| 204 | + # Create a second output buffer if we need to compute brightness |
| 205 | + buf = self.buf |
| 206 | + if self.brightness < 0.99: |
| 207 | + buf = bytearray(n * bpp + 8) |
| 208 | + # Four empty bytes to start. |
| 209 | + for i in range(4): |
| 210 | + buf[i] = 0x00 |
| 211 | + for i in range(4, len(self.buf) - 4): |
| 212 | + if i % 4 == 0: |
| 213 | + buf[i] = self.buf |
| 214 | + continue |
| 215 | + buf[i] = self.buf[i] * self._brightness |
| 216 | + # Four 0xff bytes at the end. |
| 217 | + for i in range(4): |
| 218 | + buf[len(self.buf) - 4 + i] = 0xff |
| 219 | + |
| 220 | + if self.spi: |
| 221 | + self.spi.write(buf) |
| 222 | + else: |
| 223 | + self.ds_writebytes(buf) |
| 224 | + self.cpin.value = False |
0 commit comments